Skip to content

Commit

Permalink
feat: Parametrize labels and annotations for testing and easier forking
Browse files Browse the repository at this point in the history
  • Loading branch information
keskad committed May 22, 2024
1 parent 35218f3 commit c29da86
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
45 changes: 34 additions & 11 deletions pkgs/contract/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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
Expand Down
20 changes: 10 additions & 10 deletions pkgs/k8s/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down

0 comments on commit c29da86

Please sign in to comment.