Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make migrator job from KServices to pure k8s resources #8621

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
128 changes: 128 additions & 0 deletions resources/serverless/templates/_helper_knative_migration.txt
@@ -0,0 +1,128 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

readonly SERVING_API_VERSION="serving.knative.dev/v1"

readonly FUNCTIONS_RESOURCE_TYPE="functions.serverless.kyma-project.io"
readonly KSERVICE_RESOURCE_TYPE="services.serving.knative.dev"
readonly TRIGGERS_RESOURCE_TYPE="triggers.eventing.knative.dev"

readonly MANAGED_BY_LABEL_KEY="serverless.kyma-project.io/managed-by"
readonly MANAGED_BY_LABEL_VALUE="function-controller"

# patchResources patches Functions/Triggers
#
patchResources() {
local -r functions=$(kubectl get ${FUNCTIONS_RESOURCE_TYPE} --all-namespaces -ojson | jq -c ".items[]")
if [[ -z ${functions} ]]; then
echo "There are not any Functions. Skipping... :("
return 0
fi

IFS=$'\n'
for function in ${functions}
do
patchSingleFunction "${function}"
done

local -r triggers=$(kubectl get ${TRIGGERS_RESOURCE_TYPE} --all-namespaces -ojson | jq -c ".items[] | select( .spec.subscriber.ref.apiVersion == \"${SERVING_API_VERSION}\" )")
if [[ -z ${triggers} ]]; then
echo "There are not any Triggers. Skipping... :("
return 0
fi

IFS=$'\n'
for trigger in ${triggers}
do
local subscriberName="$(echo ${trigger} | jq -r '.spec.subscriber.ref.name')"
local subscriberNamespace="$(echo ${trigger} | jq -r '.spec.subscriber.ref.namespace')"

IFS=$'\n'
for function in ${functions}
do
local functionName="$(echo ${function} | jq -r '.metadata.name')"
local functionNamespace="$(echo ${function} | jq -r '.metadata.namespace')"

if [ "${subscriberName}" == "${functionName}" ] && [ "${subscriberNamespace}" == "${functionNamespace}" ]; then
patchSingleTrigger "${trigger}"
fi
done
done
}

# patchSingleTrigger patches single Trigger to new subscriber
#
# Arguments:
# $1 - Trigger resource
patchSingleTrigger() {
local -r trigger="${1}"

local -r triggerName="$(echo ${trigger} | jq -r '.metadata.name')"
local -r triggerNamespace="$(echo ${trigger} | jq -r '.metadata.namespace')"

local -r subscriberName="$(echo ${trigger} | jq -r '.spec.subscriber.ref.name')"
local -r subscriberNamespace="$(echo ${trigger} | jq -r '.spec.subscriber.ref.namespace')"

kubectl patch triggers -n "${triggerNamespace}" "${triggerName}" \
--patch "{\"spec\": {\"subscriber\": {\"ref\": {\"apiVersion\": \"v1\", \"kind\": \"Service\", \"name\": \"${subscriberName}\", \"namespace\": \"${subscriberNamespace}\"}}}}" \
--type=merge
}

# patchSingleFunction patches single Function with default fields
#
# Arguments:
# $1 - Function resource
patchSingleFunction() {
local -r function="${1}"

local -r functionName="$(echo ${function} | jq -r '.metadata.name')"
local -r functionNamespace="$(echo ${function} | jq -r '.metadata.namespace')"

local minReplicas="$(echo ${function} | jq -r '.spec.minReplicas')"
if [[ -z ${minReplicas} || "${minReplicas}" == "null" || "${minReplicas}" == "0" ]]; then
minReplicas=1
fi
local maxReplicas="$(echo ${function} | jq -r '.spec.maxReplicas')"
if [[ -z ${maxReplicas} || "${maxReplicas}" == "null" || "${maxReplicas}" == "0" ]]; then
maxReplicas=1
fi

local requestCpu="$(echo ${function} | jq -r '.spec.resources.requests.cpu')"
if [[ -z ${requestCpu} || "${requestCpu}" == "null" ]]; then
requestCpu="50m"
fi
local requestMemory="$(echo ${function} | jq -r '.spec.resources.requests.memory')"
if [[ -z ${requestMemory} || "${requestMemory}" == "null" ]]; then
requestMemory="64Mi"
fi
local limitsCpu="$(echo ${function} | jq -r '.spec.resources.limits.cpu')"
if [[ -z ${limitsCpu} || "${limitsCpu}" == "null" ]]; then
limitsCpu="100m"
fi
local limitsMemory="$(echo ${function} | jq -r '.spec.resources.limits.memory')"
if [[ -z ${limitsMemory} || "${limitsMemory}" == "null" ]]; then
limitsMemory="128Mi"
fi

kubectl patch functions -n "${functionNamespace}" "${functionName}" \
--patch "{\"spec\": {\"minReplicas\": ${minReplicas}, \"maxReplicas\": ${maxReplicas}, \"resources\": {\"requests\": {\"cpu\": \"${requestCpu}\", \"memory\": \"${requestMemory}\"}, \"limits\": {\"cpu\": \"${limitsCpu}\", \"memory\": \"${limitsMemory}\"}}}}" \
--type=merge
}

# deleteKServices deletes all KServices managed/created by function-controller
#
deleteKServices() {
kubectl delete ${KSERVICE_RESOURCE_TYPE} \
--all-namespaces \
-l "${MANAGED_BY_LABEL_KEY}"="${MANAGED_BY_LABEL_VALUE}" \
--ignore-not-found
}

main() {
patchResources
deleteKServices || 0
}
main
110 changes: 110 additions & 0 deletions resources/serverless/templates/knative-migrator-job-post.yaml
@@ -0,0 +1,110 @@
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "fullname" . }}-knative-migration-job
namespace: {{ .Release.Namespace }}
labels:
{{- include "tplValue" ( dict "value" .Values.global.commonLabels "context" . ) | nindent 4 }}
annotations:
helm.sh/hook: post-upgrade
helm.sh/hook-weight: "0"
helm.sh/hook-delete-policy: hook-succeeded
data:
knative-migration: |-
{{ include (print $.Template.BasePath "/_helper_knative_migration.txt") . | indent 4 }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ template "fullname" . }}-knative-migration-job
namespace: {{ .Release.Namespace }}
labels:
{{- include "tplValue" ( dict "value" .Values.global.commonLabels "context" . ) | nindent 4 }}
annotations:
helm.sh/hook: post-upgrade
helm.sh/hook-delete-policy: hook-succeeded
helm.sh/hook-weight: "0"
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: {{ template "fullname" . }}-knative-migration-job
labels:
{{- include "tplValue" ( dict "value" .Values.global.commonLabels "context" . ) | nindent 4 }}
annotations:
helm.sh/hook: post-upgrade
helm.sh/hook-delete-policy: hook-succeeded
helm.sh/hook-weight: "0"
rules:
- apiGroups: ["serving.knative.dev"]
resources: ["services"]
verbs: ["*"]
- apiGroups: ["eventing.knative.dev"]
resources: ["triggers"]
verbs: ["*"]
- apiGroups: ["serverless.kyma-project.io"]
resources: ["functions"]
verbs: ["*"]
- apiGroups: [""]
resources: ["services"]
verbs: ["*"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: {{ template "fullname" . }}-knative-migration-job
labels:
{{- include "tplValue" ( dict "value" .Values.global.commonLabels "context" . ) | nindent 4 }}
annotations:
helm.sh/hook: post-upgrade
helm.sh/hook-delete-policy: hook-succeeded
helm.sh/hook-weight: "0"
subjects:
- kind: ServiceAccount
name: {{ template "fullname" . }}-knative-migration-job
namespace: {{ .Release.Namespace }}
roleRef:
kind: ClusterRole
name: {{ template "fullname" . }}-knative-migration-job
apiGroup: rbac.authorization.k8s.io
---
apiVersion: batch/v1
kind: Job
metadata:
name: {{ template "fullname" . }}-knative-migration-job
namespace: {{ .Release.Namespace }}
labels:
{{- include "tplValue" ( dict "value" .Values.global.commonLabels "context" . ) | nindent 4 }}
annotations:
helm.sh/hook: post-upgrade
helm.sh/hook-weight: "1"
helm.sh/hook-delete-policy: hook-succeeded
spec:
template:
metadata:
labels:
{{- include "tplValue" ( dict "value" .Values.global.commonLabels "context" . ) | nindent 8 }}
annotations:
sidecar.istio.io/inject: "false"
spec:
serviceAccountName: {{ template "fullname" . }}-knative-migration-job
restartPolicy: OnFailure
volumes:
- name: migration-script
projected:
sources:
- configMap:
name: {{ template "fullname" . }}-knative-migration-job
- name: export
emptyDir: {}
containers:
- name: knative-migration-job
image: "{{ .Values.knativeMigration.image.repository }}:{{ .Values.knativeMigration.image.tag }}"
imagePullPolicy: "{{ .Values.knativeMigration.image.pullPolicy }}"
command:
- "/bin/bash"
- "/config/knative-migration"
volumeMounts:
- name: migration-script
mountPath: /config
6 changes: 6 additions & 0 deletions resources/serverless/values.yaml
Expand Up @@ -16,6 +16,12 @@ injectCerts:
tag: 'v20200507-070ff576'
pullPolicy: IfNotPresent

knativeMigration:
image:
repository: 'eu.gcr.io/kyma-project/test-infra/alpine-kubectl'
tag: 'v20200310-5f52f407'
pullPolicy: IfNotPresent

tests:
enabled: true
image:
Expand Down