Skip to content

Commit

Permalink
feat(operator): Allow pre- and post-deployment tasks as labels or ann…
Browse files Browse the repository at this point in the history
…otations (#181)
  • Loading branch information
thschue committed Oct 18, 2022
1 parent fa8b875 commit 4241fe7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
3 changes: 1 addition & 2 deletions examples/podtatohead-deployment/manifest.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
apiVersion: v1
kind: Namespace
metadata:
Expand Down Expand Up @@ -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:
Expand Down
32 changes: 21 additions & 11 deletions operator/webhooks/pod_mutating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4241fe7

Please sign in to comment.