From c29da86cdca4e27ade19f85011fa0b3e5d2e7a22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20K=C4=99ska?= <372403+keskad@users.noreply.github.com> Date: Wed, 22 May 2024 10:49:08 +0200 Subject: [PATCH] feat: Parametrize labels and annotations for testing and easier forking --- pkgs/contract/k8s.go | 45 +++++++++++++++++++++++++++++++---------- pkgs/k8s/annotations.go | 20 +++++++++--------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/pkgs/contract/k8s.go b/pkgs/contract/k8s.go index e0fd5e0..07cd8e1 100644 --- a/pkgs/contract/k8s.go +++ b/pkgs/contract/k8s.go @@ -5,17 +5,40 @@ import ( "strings" ) -const ( - LabelFeedbackEnabled = "pipelinesfeedback.keskad.pl/enabled" - AnnotationPrId = "pipelinesfeedback.keskad.pl/pr-id" - AnnotationCommitHash = "pipelinesfeedback.keskad.pl/commit" - AnnotationHttpsRepo = "pipelinesfeedback.keskad.pl/https-repo-url" - AnnotationReference = "pipelinesfeedback.keskad.pl/ref" - AnnotationTechnicalJob = "pipelinesfeedback.keskad.pl/technical-job" -) +// GetPrIdAnnotation returns by default "pipelinesfeedback.keskad.pl/pr-id". Parametrized with 'ANNOTATION_FEEDBACK_BASE' env variable +func GetPrIdAnnotation() string { + return getAnnotationBase() + "/pr-id" +} + +// GetCommmitAnnotation returns by default "pipelinesfeedback.keskad.pl/commit". Parametrized with 'ANNOTATION_FEEDBACK_BASE' env variable +func GetCommmitAnnotation() string { + return getAnnotationBase() + "/commit" +} + +// GetHttpsRepoUrlAnnotation returns by default "pipelinesfeedback.keskad.pl/https-repo-url". Parametrized with 'ANNOTATION_FEEDBACK_BASE' env variable +func GetHttpsRepoUrlAnnotation() string { + return getAnnotationBase() + "/https-repo-url" +} + +// GetRefAnnotation returns by default "pipelinesfeedback.keskad.pl/ref". Parametrized with 'ANNOTATION_FEEDBACK_BASE' env variable +func GetRefAnnotation() string { + return getAnnotationBase() + "/ref" +} + +// GetTechnicalJobAnnotation returns by default "pipelinesfeedback.keskad.pl/technical-job". Parametrized with 'ANNOTATION_FEEDBACK_BASE' env variable +func GetTechnicalJobAnnotation() string { + return getAnnotationBase() + "/technical-job" +} + +func getAnnotationBase() string { + if val := os.Getenv("ANNOTATION_FEEDBACK_BASE"); val != "" { + return os.Getenv("ANNOTATION_FEEDBACK_BASE") + } + return "pipelinesfeedback.keskad.pl" +} -func GetFeedbackLabel() (string, string) { - labelName := LabelFeedbackEnabled +func getFeedbackLabel() (string, string) { + labelName := "pipelinesfeedback.keskad.pl/enabled" labelValue := "true" if val := os.Getenv("LABEL_FEEDBACK_ENABLED_NAME"); val != "" { @@ -29,7 +52,7 @@ func GetFeedbackLabel() (string, string) { // IsJobHavingRequiredLabel decides if a controller should take the resource func IsJobHavingRequiredLabel(labels map[string]string) bool { - requiredLabelName, requiredLabelValue := GetFeedbackLabel() + requiredLabelName, requiredLabelValue := getFeedbackLabel() // there is a label present if val, ok := labels[requiredLabelName]; ok { // label has required value set diff --git a/pkgs/k8s/annotations.go b/pkgs/k8s/annotations.go index 8501ea8..58043cc 100644 --- a/pkgs/k8s/annotations.go +++ b/pkgs/k8s/annotations.go @@ -20,15 +20,15 @@ func HasUsableAnnotations(meta metav1.ObjectMeta) (bool, error) { func CreateJobContextFromKubernetesAnnotations(meta metav1.ObjectMeta) (contract.JobContext, error) { isTechnicalJob := false techJob := "" - if val, exists := meta.Annotations[contract.AnnotationTechnicalJob]; exists { - logrus.Debugf("Has '%s'", contract.AnnotationTechnicalJob) + if val, exists := meta.Annotations[contract.GetTechnicalJobAnnotation()]; exists { + logrus.Debugf("Has '%s'", contract.GetTechnicalJobAnnotation()) isTechnicalJob = true techJob = val } repoHttpsUrl := "" - if val, exists := meta.Annotations[contract.AnnotationHttpsRepo]; exists { - logrus.Debugf("Has '%s'", contract.AnnotationHttpsRepo) + if val, exists := meta.Annotations[contract.GetHttpsRepoUrlAnnotation()]; exists { + logrus.Debugf("Has '%s'", contract.GetHttpsRepoUrlAnnotation()) repoHttpsUrl = val } @@ -39,16 +39,16 @@ func CreateJobContextFromKubernetesAnnotations(meta metav1.ObjectMeta) (contract scm.TechnicalJob = techJob - if val, exists := meta.Annotations[contract.AnnotationPrId]; exists { - logrus.Debugf("Has '%s'", contract.AnnotationPrId) + if val, exists := meta.Annotations[contract.GetPrIdAnnotation()]; exists { + logrus.Debugf("Has '%s'", contract.GetPrIdAnnotation()) scm.PrId = val } - if val, exists := meta.Annotations[contract.AnnotationReference]; exists { - logrus.Debugf("Has '%s'", contract.AnnotationReference) + if val, exists := meta.Annotations[contract.GetRefAnnotation()]; exists { + logrus.Debugf("Has '%s'", contract.GetRefAnnotation()) scm.Reference = val } - if val, exists := meta.Annotations[contract.AnnotationCommitHash]; exists { - logrus.Debugf("Has '%s'", contract.AnnotationCommitHash) + if val, exists := meta.Annotations[contract.GetCommmitAnnotation()]; exists { + logrus.Debugf("Has '%s'", contract.GetCommmitAnnotation()) scm.Commit = val } return scm, nil