Skip to content

Commit

Permalink
Ch21 exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
sixeyed committed Jul 28, 2020
1 parent d7fb5c8 commit 744b658
Show file tree
Hide file tree
Showing 38 changed files with 3,874 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ch21.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Ch21 Image Builds

on:
push:
paths:
- ".github/workflows/ch21.yaml"
- "ch21/docker-images/**"
schedule:
- cron: "0 0-7 * * *"

jobs:
ch21:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: azure/docker-login@v1
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: ch21
working-directory: ./ch21/docker-images
run: |
docker-compose build
docker-compose push
7 changes: 7 additions & 0 deletions ch21/docker-images/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: "3.7"

services:
ch21-serverless-cli:
image: kiamol/ch21-serverless-cli
build:
context: ./serverless-cli
15 changes: 15 additions & 0 deletions ch21/docker-images/serverless-cli/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM node:12

ENV SERVERLESS_VERSION="1.77.1" \
KUBERNETES_VERSION="1.18.5"

RUN npm install -g serverless@${SERVERLESS_VERSION}

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/serverless-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
10 changes: 10 additions & 0 deletions ch21/functions/hello-kiamol/hello-kiamol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ch21.net.kiamol;

import io.kubeless.Event;
import io.kubeless.Context;

public class Kiamol {
public String hello(io.kubeless.Event event, io.kubeless.Context context) {
return "Hello from chapter 21!";
}
}
15 changes: 15 additions & 0 deletions ch21/functions/todo-api/ingress/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: todo-api
labels:
kiamol: ch21
spec:
rules:
- host: api.todo.kiamol.local
http:
paths:
- path: /todos
backend:
serviceName: todo-api
servicePort: 8080
11 changes: 11 additions & 0 deletions ch21/functions/todo-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "todo-api",
"version": "0.1.0",
"main": "server.js",
"author": "kiamol",
"dependencies": {
"nats" : "1.4.9",
"uuid" : "8.2.0"
}
}

21 changes: 21 additions & 0 deletions ch21/functions/todo-api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const NATS = require('nats')
const nc = NATS.connect({url: 'nats://message-queue:4222', json: true})
const { v4: uuidv4 } = require('uuid');

function handler(event, context) {
console.log('** todo-api handler called');

var newItemEvent = {
Subject: "events.todo.newitem",
Item: {
Item: event.data,
DateAdded: new Date().toISOString()
},
CorrelationId: uuidv4()
}

nc.publish('events.todo.newitem', newItemEvent)
console.log(`** New item published, event ID: ${newItemEvent.CorrelationId}`);
}

module.exports = { handler }
5 changes: 5 additions & 0 deletions ch21/functions/todo-audit/audit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import json

def handler(event, context):
new_item = json.loads(event.data)
print(f"AUDIT @ {new_item['Item']['DateAdded']}: {new_item['Item']['Item']}")
28 changes: 28 additions & 0 deletions ch21/functions/todo-mutating-handler/mutating-handler.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apiVersion: kubeless.io/v1beta1
kind: Function
metadata:
name: todo-mutating-handler
namespace: default
label:
created-by: kubeless
spec:
runtime: php7.3
timeout: "10"
handler: todo-mutating-handler.mutate
deps: ""
function-content-type: text
function: |
<?php
function mutate($event, $context) {
fwrite(STDOUT, '* Mutating handler called.');
$conn = pg_connect('host=todo-db dbname=todo user=postgres password=kiamol-2*2*');
if (!$conn) {
echo 'Connection failed';
exit;
}
$sql = 'UPDATE "public"."ToDos" SET "Item"=\'Leave a nice review for KIAMOL :)\'';
$result = pg_query($conn, $sql);
fwrite(STDOUT, '* Mutation complete.');
return "* Mutated...";
}
Loading

0 comments on commit 744b658

Please sign in to comment.