This commit is contained in:
2024-02-20 17:15:27 +08:00
committed by huty
parent 6706e1a633
commit 34158042ad
1529 changed files with 177765 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
version: "3.7"
services:
ch21-kubeless-cli:
image: kiamol/ch21-kubeless-cli
build:
context: ./kubeless-cli
ch21-serverless-cli:
image: kiamol/ch21-serverless-cli
build:
context: ./serverless-cli
ch21-todo-api:
image: kiamol/ch21-todo-api
build:
context: ./todo-api

View File

@@ -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

View File

@@ -0,0 +1,13 @@
#!/bin/sh
# set up access to Kube API
kubectl config set-cluster default --server=https://kubernetes.default.svc.cluster.local --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

View File

@@ -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

View File

@@ -0,0 +1,13 @@
#!/bin/sh
# set up access to Kube API
kubectl config set-cluster default --server=https://kubernetes.default.svc.cluster.local --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

View File

@@ -0,0 +1,15 @@
FROM node:12.17 AS builder
WORKDIR /src
COPY package.json .
RUN npm install
FROM node:12.17-alpine3.11
CMD ["node", "/app/server.js"]
ENV PORT=80
EXPOSE 80
WORKDIR /app
COPY --from=builder /src/node_modules/ /app/node_modules/
COPY *.js ./

View File

@@ -0,0 +1,24 @@
const NATS = require('nats')
const nc = NATS.connect({url: 'nats://message-queue:4222', json: true})
const { v4: uuidv4 } = require('uuid');
function post(req, res, next) {
console.log('** todo-api handler called');
var newItemEvent = {
Subject: "events.todo.newitem",
Item: {
Item: Object.keys(req.body)[0],
DateAdded: new Date().toISOString()
},
CorrelationId: uuidv4()
}
nc.publish('events.todo.newitem', newItemEvent)
console.log(`** New item published, event ID: ${newItemEvent.CorrelationId}`);
res.send(201);
next();
}
module.exports = { post }

View File

@@ -0,0 +1,12 @@
{
"name": "todo-api",
"version": "0.1.0",
"main": "server.js",
"author": "kiamol",
"dependencies": {
"nats" : "1.4.9",
"restify" : "8.5.1",
"uuid" : "8.2.0"
}
}

View File

@@ -0,0 +1,10 @@
const restify = require("restify");
const handler = require('./handler');
var server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.post("/todos", handler.post);
server.listen(process.env.PORT, function() {
console.log(`${server.name} listening at ${server.url}`);
});