From 4241fe7cfab91aa6d38309eacf5712436a6e8327 Mon Sep 17 00:00:00 2001 From: Thomas Schuetz <38893055+thschue@users.noreply.github.com> Date: Tue, 18 Oct 2022 09:13:27 +0200 Subject: [PATCH] feat(operator): Allow pre- and post-deployment tasks as labels or annotations (#181) --- examples/podtatohead-deployment/manifest.yaml | 3 +- operator/webhooks/pod_mutating_webhook.go | 32 ++++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/examples/podtatohead-deployment/manifest.yaml b/examples/podtatohead-deployment/manifest.yaml index 1c390f8f20..cf6c15a2d2 100644 --- a/examples/podtatohead-deployment/manifest.yaml +++ b/examples/podtatohead-deployment/manifest.yaml @@ -1,4 +1,3 @@ ---- apiVersion: v1 kind: Namespace metadata: @@ -76,11 +75,11 @@ spec: metadata: labels: component: podtato-head-hat + keptn.sh/pre-deployment-tasks: check-entry-service annotations: keptn.sh/app: podtato-head keptn.sh/workload: podtato-head-hat keptn.sh/version: 0.1.0 - keptn.sh/pre-deployment-tasks: check-entry-service spec: terminationGracePeriodSeconds: 5 containers: diff --git a/operator/webhooks/pod_mutating_webhook.go b/operator/webhooks/pod_mutating_webhook.go index 0b18a728b6..5ef884c8c9 100644 --- a/operator/webhooks/pod_mutating_webhook.go +++ b/operator/webhooks/pod_mutating_webhook.go @@ -302,20 +302,20 @@ func (a *PodMutatingWebhook) generateWorkload(ctx context.Context, pod *corev1.P var preDeploymentAnalysis []string var postDeploymentAnalysis []string - if pod.Annotations[common.PreDeploymentTaskAnnotation] != "" { - preDeploymentTasks = strings.Split(pod.Annotations[common.PreDeploymentTaskAnnotation], ",") + if annotations, found := getLabelOrAnnotation(pod, common.PreDeploymentTaskAnnotation, ""); found { + preDeploymentTasks = strings.Split(annotations, ",") } - if pod.Annotations[common.PostDeploymentTaskAnnotation] != "" { - postDeploymentTasks = strings.Split(pod.Annotations[common.PostDeploymentTaskAnnotation], ",") + if annotations, found := getLabelOrAnnotation(pod, common.PostDeploymentTaskAnnotation, ""); found { + postDeploymentTasks = strings.Split(annotations, ",") } - if pod.Annotations[common.PreDeploymentAnalysisAnnotation] != "" { - preDeploymentAnalysis = strings.Split(pod.Annotations[common.PreDeploymentAnalysisAnnotation], ",") + if annotations, found := getLabelOrAnnotation(pod, common.PreDeploymentAnalysisAnnotation, ""); found { + preDeploymentAnalysis = strings.Split(annotations, ",") } - if pod.Annotations[common.PostDeploymentAnalysisAnnotation] != "" { - postDeploymentAnalysis = strings.Split(pod.Annotations[common.PostDeploymentAnalysisAnnotation], ",") + if annotations, found := getLabelOrAnnotation(pod, common.PostDeploymentAnalysisAnnotation, ""); found { + postDeploymentAnalysis = strings.Split(annotations, ",") } // create TraceContext @@ -402,11 +402,21 @@ func (a *PodMutatingWebhook) getResourceReference(pod *corev1.Pod) klcv1alpha1.R func getLabelOrAnnotation(pod *corev1.Pod, primaryAnnotation string, secondaryAnnotation string) (string, bool) { if pod.Annotations[primaryAnnotation] != "" { return pod.Annotations[primaryAnnotation], true - } else if pod.Labels[primaryAnnotation] != "" { + } + + if pod.Labels[primaryAnnotation] != "" { return pod.Labels[primaryAnnotation], true - } else if pod.Annotations[secondaryAnnotation] != "" { + } + + if secondaryAnnotation == "" { + return "", false + } + + if pod.Annotations[secondaryAnnotation] != "" { return pod.Annotations[secondaryAnnotation], true - } else if pod.Labels[secondaryAnnotation] != "" { + } + + if pod.Labels[secondaryAnnotation] != "" { return pod.Labels[secondaryAnnotation], true } return "", false