Skip to content

Commit

Permalink
Ch21 exercise updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sixeyed committed Jul 31, 2020
1 parent 744b658 commit d59db96
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.box
*.tgz
.serverless/
.vagrant/
packer_cache/
builds/
Expand Down
9 changes: 9 additions & 0 deletions ch21/add-todo-to-hosts.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

$controller='ingress-nginx-controller'
$ns='kiamol-ingress-nginx'
$ip=$(kubectl get svc $controller -o jsonpath='{.status.loadBalancer.ingress[0].*}' -n $ns)
if ($ip -eq 'localhost') {
$ip='127.0.0.1'
}

Add-Content -Value "$ip todo.kiamol.local$([Environment]::NewLine)$ip api.todo.kiamol.local" -Path C:/windows/system32/drivers/etc/hosts
10 changes: 10 additions & 0 deletions ch21/add-todo-to-hosts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

CONTROLLER='ingress-nginx-controller'
NS='kiamol-ingress-nginx'
IP=$(kubectl get svc $CONTROLLER -o jsonpath='{.status.loadBalancer.ingress[0].*}' -n $NS)
if [ "$IP" = "localhost" ]; then
IP='127.0.0.1'
fi

echo "\n$IP todo.kiamol.local\n$IP api.todo.kiamol.local" | sudo tee -a /etc/hosts
7 changes: 6 additions & 1 deletion ch21/docker-images/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
version: "3.7"

services:
services:
ch21-kubeless-cli:
image: kiamol/ch21-kubeless-cli
build:
context: ./kubeless-cli

ch21-serverless-cli:
image: kiamol/ch21-serverless-cli
build:
Expand Down
20 changes: 20 additions & 0 deletions ch21/docker-images/kubeless-cli/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#using node to match base image for serverless cli
FROM node:12

ENV KUBELESS_VERSION="v1.0.7" \
KUBERNETES_VERSION="1.18.5"

RUN curl -LO https://github.com/kubeless/kubeless/releases/download/${KUBELESS_VERSION}/kubeless_linux-amd64.zip && \
unzip kubeless_linux-amd64.zip && \
rm -f kubeless_linux-amd64.zip && \
chmod +x bundles/kubeless_linux-amd64/kubeless && \
mv bundles/kubeless_linux-amd64/kubeless /usr/local/bin/

RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/v${KUBERNETES_VERSION}/bin/linux/amd64/kubectl && \
chmod +x kubectl && \
mv kubectl /usr/local/bin/

COPY start.sh /
RUN chmod +x /start.sh
CMD /start.sh
WORKDIR /kiamol
13 changes: 13 additions & 0 deletions ch21/docker-images/kubeless-cli/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

# set up access to Kube API
kubectl config set-cluster default --server=https://kubernetes.default --certificate-authority=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
kubectl config set-context default --cluster=default
kubectl config set-credentials user --token=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
kubectl config set-context default --user=user
kubectl config use-context default

cd /
git clone https://github.com/sixeyed/kiamol

while true; do sleep 1000; done
2 changes: 1 addition & 1 deletion ch21/functions/hello-kiamol/hello-kiamol.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch21.net.kiamol;
package io.kubeless;

import io.kubeless.Event;
import io.kubeless.Context;
Expand Down
7 changes: 6 additions & 1 deletion ch21/functions/todo-api/ingress/ingress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ spec:
- path: /todos
backend:
serviceName: todo-api
servicePort: 8080
servicePort: 8080

# this spec is equivalent to running
# kubeless trigger http create todo-api --function-name todo-api --path todos --hostname api.todo.kiamol.local
# but that fails with Kubeless 1.0.7 running on Kubernetes 1.18 - see
# https://github.com/kubeless/kubeless/issues/1130
6 changes: 2 additions & 4 deletions ch21/functions/todo-audit/audit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

def handler(event, context):
new_item = json.loads(event.data)
print(f"AUDIT @ {new_item['Item']['DateAdded']}: {new_item['Item']['Item']}")
def handler(event, context):
print(f"AUDIT @ {event['data']['Item']['DateAdded']}: {event['data']['Item']['Item']}")
15 changes: 15 additions & 0 deletions ch21/functions/todo-mutating-handler/cronjob-trigger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: kubeless.io/v1beta1
kind: CronJobTrigger
metadata:
finalizers:
- kubeless.io/cronjobtrigger
labels:
created-by: kubeless
name: todo-mutating-handler
namespace: default
spec:
function-name: todo-mutating-handler
schedule: '*/1 * * * *'

# finalizers help with clean-up tasks - see
# https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#finalizers
2 changes: 1 addition & 1 deletion ch21/ingress-nginx/nginx-ingress-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: Namespace
metadata:
name: kiamol-ingress-nginx
labels:
kiamol: ch15
kiamol: ch21
---
apiVersion: v1
kind: ConfigMap
Expand Down
33 changes: 33 additions & 0 deletions ch21/kubeless-cli.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: kubeless-cli
labels:
kiamol: ch21
---
apiVersion: v1
kind: Pod
metadata:
name: kubeless-cli
labels:
kiamol: ch21
spec:
serviceAccountName: kubeless-cli
containers:
- name: kubeless-cli
image: kiamol/ch21-kubeless-cli
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kubeless-cli
labels:
kiamol: ch21
subjects:
- kind: ServiceAccount
name: kubeless-cli
namespace: default
roleRef:
kind: ClusterRole
name: cluster-admin # tsk
apiGroup: rbac.authorization.k8s.io
6 changes: 6 additions & 0 deletions ch21/kubeless/kubeless-v1.0.7.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
apiVersion: v1
kind: Namespace
metadata:
name: kubeless
labels:
kiamol: ch21
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
Expand Down
6 changes: 2 additions & 4 deletions ch21/serverless/todo-audit/audit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

def handler(event, context):
new_item = json.loads(event.data)
print(f"AUDIT @ {new_item['Item']['DateAdded']}: {new_item['Item']['Item']}")
def handler(event, context):
print(f"AUDIT @ {event['data']['Item']['DateAdded']}: {event['data']['Item']['Item']}")
2 changes: 2 additions & 0 deletions ch21/todo-list/db/todo-db-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ spec:
env:
- name: POSTGRES_PASSWORD_FILE
value: /secrets/postgres_password
- name: PGDATA
value: /var/lib/postgresql/data/pgdata
volumeMounts:
- name: secret
mountPath: "/secrets"
Expand Down
3 changes: 1 addition & 2 deletions ch21/todo-list/db/todo-db-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ spec:
- port: 5432
targetPort: 5432
selector:
app: todo-db
type: LoadBalancer
app: todo-db

0 comments on commit d59db96

Please sign in to comment.