fixed language issues...;)

This commit is contained in:
Olaf Bohlen
2023-02-15 11:46:43 +01:00
parent e281d67b08
commit 267308dc0c
8 changed files with 60 additions and 60 deletions

View File

@@ -1,11 +1,11 @@
FROM docker.eenfach.de/olbohlen/kshbase:latest FROM docker.eenfach.de/olbohlen/kshbase:latest
LABEL author="Olaf Bohlen <olbohlen@eenfach.de>" LABEL author="Olaf Bohlen <olbohlen@eenfach.de>"
COPY receipt-processor.ksh /usr/local/bin/receipt-processor.ksh COPY recipe-processor.ksh /usr/local/bin/recipe-processor.ksh
RUN chmod 755 /usr/local/bin/receipt-processor.ksh && \ RUN chmod 755 /usr/local/bin/recipe-processor.ksh && \
useradd cookie && \ useradd cookie && \
dnf install -y jq dnf install -y jq
USER cookie USER cookie
ENTRYPOINT ["/usr/local/bin/receipt-processor.ksh"] ENTRYPOINT ["/usr/local/bin/recipe-processor.ksh"]

View File

@@ -1,13 +1,13 @@
apiVersion: apiextensions.k8s.io/v1 apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition kind: CustomResourceDefinition
metadata: metadata:
name: cookiereceipts.de.eenfach.olbohlen name: cookierecipes.de.eenfach.olbohlen
spec: spec:
group: de.eenfach.olbohlen group: de.eenfach.olbohlen
names: names:
kind: CookieReceipt kind: CookieRecipe
plural: cookiereceipts plural: cookierecipes
singular: cookiereceipt singular: cookierecipe
scope: Namespaced scope: Namespaced
versions: versions:
- name: v1 - name: v1
@@ -15,7 +15,7 @@ spec:
storage: true storage: true
schema: schema:
openAPIV3Schema: openAPIV3Schema:
description: "The CookieReceipt CRD is a k8s demo for enhancing functionality, it will not (unfortunately) provide you real cookies in the end..." description: "The CookieRecipe CRD is a k8s demo for enhancing functionality, it will not (unfortunately) provide you real cookies in the end..."
type: object type: object
properties: properties:
apiVersion: apiVersion:
@@ -40,7 +40,7 @@ spec:
type: string type: string
description: where does this come from? (grandma, mum, the internet...) description: where does this come from? (grandma, mum, the internet...)
ingredients: ingredients:
description: this list provides required ingredients for this receipt description: this list provides required ingredients for this recipe
type: array type: array
x-kubernetes-list-type: atomic x-kubernetes-list-type: atomic
items: items:

View File

@@ -3,19 +3,19 @@ kind: Deployment
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels: labels:
app: receipt-processor app: recipe-processor
name: receipt-processor name: recipe-processor
spec: spec:
replicas: 1 replicas: 1
selector: selector:
matchLabels: matchLabels:
app: receipt-processor app: recipe-processor
strategy: {} strategy: {}
template: template:
metadata: metadata:
creationTimestamp: null creationTimestamp: null
labels: labels:
app: receipt-processor app: recipe-processor
spec: spec:
serviceAccountName: cookieprocessor serviceAccountName: cookieprocessor
containers: containers:

View File

@@ -2,12 +2,12 @@
apiVersion: rbac.authorization.k8s.io/v1 apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole kind: ClusterRole
metadata: metadata:
name: cookiereceipt-edit name: cookierecipe-edit
rules: rules:
- apiGroups: - apiGroups:
- de.eenfach.olbohlen - de.eenfach.olbohlen
resources: resources:
- cookiereceipts - cookierecipes
verbs: verbs:
- create - create
- get - get
@@ -19,12 +19,12 @@ rules:
apiVersion: rbac.authorization.k8s.io/v1 apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole kind: ClusterRole
metadata: metadata:
name: cookiereceipt-view name: cookierecipe-view
rules: rules:
- apiGroups: - apiGroups:
- de.eenfach.olbohlen - de.eenfach.olbohlen
resources: resources:
- cookiereceipts - cookierecipes
verbs: verbs:
- get - get
- list - list
@@ -32,11 +32,11 @@ rules:
apiVersion: rbac.authorization.k8s.io/v1 apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding kind: ClusterRoleBinding
metadata: metadata:
name: cookiereceipt-edit name: cookierecipe-edit
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
kind: ClusterRole kind: ClusterRole
name: cookiereceipt-edit name: cookierecipe-edit
subjects: subjects:
- apiGroup: rbac.authorization.k8s.io - apiGroup: rbac.authorization.k8s.io
kind: Group kind: Group

View File

@@ -8,11 +8,11 @@ metadata:
apiVersion: rbac.authorization.k8s.io/v1 apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding kind: RoleBinding
metadata: metadata:
name: cookiereceipt-view name: cookierecipe-view
roleRef: roleRef:
apiGroup: rbac.authorization.k8s.io apiGroup: rbac.authorization.k8s.io
kind: ClusterRole kind: ClusterRole
name: cookiereceipt-view name: cookierecipe-view
subjects: subjects:
- kind: ServiceAccount - kind: ServiceAccount
name: cookieprocessor name: cookieprocessor

View File

@@ -1,5 +1,5 @@
apiVersion: de.eenfach.olbohlen/v1 apiVersion: de.eenfach.olbohlen/v1
kind: CookieReceipt kind: CookieRecipe
metadata: metadata:
name: oaty-hazelnut name: oaty-hazelnut
spec: spec:

View File

@@ -8,86 +8,86 @@ TOKEN=$(</run/secrets/kubernetes.io/serviceaccount/token)
CACRT="/run/secrets/kubernetes.io/serviceaccount/ca.crt" CACRT="/run/secrets/kubernetes.io/serviceaccount/ca.crt"
# some associate arrays we need # some associate arrays we need
typeset -A receiptname typeset -A recipename
typeset -A receiptversion typeset -A recipeversion
CURLOPTS="-s --cacert ${CACRT} -H 'Accept: application/json' -H 'User-Agent: ksh93 receipt processor 0.1' -H 'Authorization: Bearer ${TOKEN}'" CURLOPTS="-s --cacert ${CACRT} -H 'Accept: application/json' -H 'User-Agent: ksh93 recipe processor 0.1' -H 'Authorization: Bearer ${TOKEN}'"
while sleep 5; do while sleep 5; do
# MAIN loop # MAIN loop
# we will later store here modified receipt uids, so we clear it for every run # we will later store here modified recipe uids, so we clear it for every run
updatedreceipts="" updatedrecipes=""
# get a CookieReceiptList item from the API and store the json in a variable # get a CookieRecipeList item from the API and store the json in a variable
respjson=$(eval curl -XGET ${CURLOPTS} "${APIURL}/apis/de.eenfach.olbohlen/v1/namespaces/${NAMESPACE}/cookiereceipts?limit=500") respjson=$(eval curl -XGET ${CURLOPTS} "${APIURL}/apis/de.eenfach.olbohlen/v1/namespaces/${NAMESPACE}/cookierecipes?limit=500")
# check if we got the expected resource: # check if we got the expected resource:
kind=$(echo "${respjson}" | jq .kind) kind=$(echo "${respjson}" | jq .kind)
if [ x${kind} != 'x"CookieReceiptList"' ]; then if [ x${kind} != 'x"CookieRecipeList"' ]; then
printf "Error: unexpected Result from API\n" printf "Error: unexpected Result from API\n"
break ## jump out of this loop iteration break ## jump out of this loop iteration
fi fi
# how many receipts are there? # how many recipes are there?
numreceipts=$(echo "${respjson}" | jq .items\[\].metadata.name | wc -l) numrecipes=$(echo "${respjson}" | jq .items\[\].metadata.name | wc -l)
# populate the in memory arrays with json metadata, we need # populate the in memory arrays with json metadata, we need
# - the name # - the name
# - the uid # - the uid
# - the resourceVersion # - the resourceVersion
# for every receipt to figure out if we need to process them # for every recipe to figure out if we need to process them
i=0 i=0
while [ ${i} -lt ${numreceipts} ]; do while [ ${i} -lt ${numrecipes} ]; do
receiptuid=$(echo "${respjson}" | jq -r .items[${i}].metadata.uid) recipeuid=$(echo "${respjson}" | jq -r .items[${i}].metadata.uid)
receiptname[${receiptuid}]=$(echo "${respjson}" | jq -r .items[${i}].metadata.name) recipename[${recipeuid}]=$(echo "${respjson}" | jq -r .items[${i}].metadata.name)
# check if we already have processed this version of the receipt, if not # check if we already have processed this version of the recipe, if not
# store the uid of that receipt in the updatedreceipts var # store the uid of that recipe in the updatedrecipes var
newversion=$(echo "${respjson}" | jq -r .items[${i}].metadata.resourceVersion) newversion=$(echo "${respjson}" | jq -r .items[${i}].metadata.resourceVersion)
if [ "x${newversion}" != "x${receiptversion[${receiptuid}]}" ]; then if [ "x${newversion}" != "x${recipeversion[${recipeuid}]}" ]; then
# we have an update! # we have an update!
updatedreceipts="${updatedreceipts} ${receiptuid}" updatedrecipes="${updatedrecipes} ${recipeuid}"
receiptversion[${receiptuid}]="${newversion}" recipeversion[${recipeuid}]="${newversion}"
fi fi
i=$(( ${i} + 1 )) i=$(( ${i} + 1 ))
done done
# now that we have a list of updated receipts, we are going to process them # now that we have a list of updated recipes, we are going to process them
for r in ${updatedreceipts}; do for r in ${updatedrecipes}; do
printf "\n\nNew receipt found: %s\n" "${receiptname[${r}]}" printf "\n\nNew recipe found: %s\n" "${recipename[${r}]}"
printf "--------------------------------------------------------------------------\n\n" printf "--------------------------------------------------------------------------\n\n"
# get the name for the UID and fetch only that object # get the name for the UID and fetch only that object
receipt=$(eval curl -XGET ${CURLOPTS} '${APIURL}/apis/de.eenfach.olbohlen/v1/namespaces/${NAMESPACE}/cookiereceipts/${receiptname[${r}]}' ) recipe=$(eval curl -XGET ${CURLOPTS} '${APIURL}/apis/de.eenfach.olbohlen/v1/namespaces/${NAMESPACE}/cookierecipes/${recipename[${r}]}' )
# receipt contains now the json for one receipt, now we parse that # recipe contains now the json for one recipe, now we parse that
# we set scale to Fahrenheit if sanescale is false, else scale will use Celsius # we set scale to Fahrenheit if sanescale is false, else scale will use Celsius
scale_b=$( echo "${receipt}" | jq -r .spec.sanescale ) scale_b=$( echo "${recipe}" | jq -r .spec.sanescale )
if [ "x${sanescale}" == "xfalse" ]; then if [ "x${sanescale}" == "xfalse" ]; then
scale=Fahrenheit scale=Fahrenheit
fi fi
temperature=$( echo "${receipt}" | jq -r .spec.temperature ) temperature=$( echo "${recipe}" | jq -r .spec.temperature )
preheat_b=$( echo "${receipt}" | jq -r .spec.preheat ) preheat_b=$( echo "${recipe}" | jq -r .spec.preheat )
if [ "x${preheat_b}" == "xtrue" ]; then if [ "x${preheat_b}" == "xtrue" ]; then
printf "Pre: we heat up the oven to %s degrees %s\n\n" "${temperature}" "${scale:-Celsius}" printf "Pre: we heat up the oven to %s degrees %s\n\n" "${temperature}" "${scale:-Celsius}"
fi fi
# how many ingredients do we have? # how many ingredients do we have?
num_in=$(echo "${receipt}" | jq .spec.ingredients\[\].name | wc -l) num_in=$(echo "${recipe}" | jq .spec.ingredients\[\].name | wc -l)
# list up that we fetch needed ingredients # list up that we fetch needed ingredients
printf "Fetching ingredients from receipt:\n" printf "Fetching ingredients from recipe:\n"
printf "----------------------------------\n" printf "----------------------------------\n"
i=0 i=0
while [ ${i} -lt ${num_in} ]; do while [ ${i} -lt ${num_in} ]; do
in_name=$( echo "${receipt}" | jq -r .spec.ingredients[${i}].name ) in_name=$( echo "${recipe}" | jq -r .spec.ingredients[${i}].name )
in_amount=$( echo "${receipt}" | jq -r .spec.ingredients[${i}].amount ) in_amount=$( echo "${recipe}" | jq -r .spec.ingredients[${i}].amount )
in_unit=$( echo "${receipt}" | jq -r .spec.ingredients[${i}].unit ) in_unit=$( echo "${recipe}" | jq -r .spec.ingredients[${i}].unit )
in_remarks=$( echo "${receipt}" | jq -r .spec.ingredients[${i}].remarks ) in_remarks=$( echo "${recipe}" | jq -r .spec.ingredients[${i}].remarks )
printf "Fetching %s%s of %s" "${in_amount}" "${in_unit}" "${in_name}" printf "Fetching %s%s of %s" "${in_amount}" "${in_unit}" "${in_name}"
if [ "x${in_remarks}" != "xnull" ]; then if [ "x${in_remarks}" != "xnull" ]; then
@@ -104,14 +104,14 @@ while sleep 5; do
# an "order" attribute in each list item, and we select on that. # an "order" attribute in each list item, and we select on that.
# first again, we need the amount of instructions... # first again, we need the amount of instructions...
num_steps=$(echo "${receipt}" | jq .spec.steps\[\].order | wc -l) num_steps=$(echo "${recipe}" | jq .spec.steps\[\].order | wc -l)
# now let's iterate over that # now let's iterate over that
printf "\n\nProcessing the instructions:\n" printf "\n\nProcessing the instructions:\n"
printf "----------------------------\n" printf "----------------------------\n"
i=1 i=1
while [ ${i} -le ${num_steps} ]; do while [ ${i} -le ${num_steps} ]; do
instruction=$( echo "${receipt}" | jq '.spec.steps[] | select(.order == '${i}') | { instruction } | join (" ")' ) instruction=$( echo "${recipe}" | jq '.spec.steps[] | select(.order == '${i}') | { instruction } | join (" ")' )
printf "Step %i/%i: %s..." ${i} ${num_steps} "${instruction}" printf "Step %i/%i: %s..." ${i} ${num_steps} "${instruction}"
sleep $(( ${RANDOM} % 6 + 1 )) sleep $(( ${RANDOM} % 6 + 1 ))
@@ -119,7 +119,7 @@ while sleep 5; do
sleep 1 sleep 1
i=$(( ${i} + 1 )) i=$(( ${i} + 1 ))
done done
printf "\nDone with this receipt." printf "\nDone with this recipe."
printf "\n=======================\n\n" printf "\n=======================\n\n"
done done
done done

View File

@@ -1,5 +1,5 @@
apiVersion: de.eenfach.olbohlen/v1 apiVersion: de.eenfach.olbohlen/v1
kind: CookieReceipt kind: CookieRecipe
metadata: metadata:
name: vintage-chocolate-chip name: vintage-chocolate-chip
spec: spec:
@@ -62,7 +62,7 @@ spec:
instruction: Leave on the tray for a couple of mins to set and then lift onto a cooling rack. instruction: Leave on the tray for a couple of mins to set and then lift onto a cooling rack.
--- ---
apiVersion: de.eenfach.olbohlen/v1 apiVersion: de.eenfach.olbohlen/v1
kind: CookieReceipt kind: CookieRecipe
metadata: metadata:
name: double-dipped-shortbread name: double-dipped-shortbread
spec: spec: