Skip to content

Commit

Permalink
Extract out helper method for building CronJob
Browse files Browse the repository at this point in the history
  • Loading branch information
dudo committed May 13, 2024
1 parent 9d23e51 commit 46805c4
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 88 deletions.
107 changes: 19 additions & 88 deletions pkg/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pkg

import (
"encoding/json"
"fmt"
"os"
"strconv"
"testing"
Expand Down Expand Up @@ -83,46 +82,12 @@ func TestGetCronitorID(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
annotations := fmt.Sprintf(`"%s": "%s"`, "k8s.cronitor.io/id-inference", tc.annotationIDInference)
annotations += fmt.Sprintf(`, "%s": "%s"`, "k8s.cronitor.io/cronitor-id", tc.annotationCronitorID)

jsonBlob := fmt.Sprintf(`{
"apiVersion": "batch/v1beta1",
"kind": "CronJob",
"metadata": {
"name": "id-test-cronjob",
"namespace": "default",
"annotations": {%s}
},
"spec": {
"concurrencyPolicy": "Forbid",
"jobTemplate": {
"spec": {
"backoffLimit": 3,
"template": {
"spec": {
"containers": [
{
"args": [
"/bin/sh",
"-c",
"date ; sleep 5 ; echo Hello from k8s"
],
"image": "busybox",
"name": "hello"
}
],
"restartPolicy": "OnFailure"
}
}
}
},
"schedule": "*/1 * * * *"
}
}`, annotations)

var cronJob v1.CronJob
err := json.Unmarshal([]byte(jsonBlob), &cronJob)
annotations := []Annotation{
{Key: "k8s.cronitor.io/id-inference", Value: tc.annotationIDInference},
{Key: "k8s.cronitor.io/cronitor-id", Value: tc.annotationCronitorID},
}

cronJob, err := CronJobFromAnnotations(annotations)
if err != nil {
t.Fatalf("unexpected error unmarshalling json: %v", err)
}
Expand All @@ -138,85 +103,51 @@ func TestGetCronitorID(t *testing.T) {
func TestGetCronitorName(t *testing.T) {
tests := []struct {
name string
AnnotationNamePrefix string
annotationNamePrefix string
annotationCronitorName string
expectedName string
}{
{
name: "default behavior",
AnnotationNamePrefix: "",
annotationNamePrefix: "",
annotationCronitorName: "",
expectedName: "default/name-test-cronjob",
},
{
name: "no prefix for name",
AnnotationNamePrefix: "none",
annotationNamePrefix: "none",
annotationCronitorName: "",
expectedName: "name-test-cronjob",
},
{
name: "explicit prefix of namespace",
AnnotationNamePrefix: "namespace",
annotationNamePrefix: "namespace",
annotationCronitorName: "",
expectedName: "default/name-test-cronjob",
},
{
name: "specific cronitor name",
AnnotationNamePrefix: "",
annotationNamePrefix: "",
annotationCronitorName: "foo",
expectedName: "foo",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
annotations := fmt.Sprintf(`"%s": "%s"`, "k8s.cronitor.io/name-prefix", tc.AnnotationNamePrefix)
annotations += fmt.Sprintf(`, "%s": "%s"`, "k8s.cronitor.io/cronitor-name", tc.annotationCronitorName)

jsonBlob := fmt.Sprintf(`{
"apiVersion": "batch/v1beta1",
"kind": "CronJob",
"metadata": {
"name": "name-test-cronjob",
"namespace": "default",
"annotations": {%s}
},
"spec": {
"concurrencyPolicy": "Forbid",
"jobTemplate": {
"spec": {
"backoffLimit": 3,
"template": {
"spec": {
"containers": [
{
"args": [
"/bin/sh",
"-c",
"date ; sleep 5 ; echo Hello from k8s"
],
"image": "busybox",
"name": "hello"
}
],
"restartPolicy": "OnFailure"
}
}
}
},
"schedule": "*/1 * * * *"
}
}`, annotations)

var cronJob v1.CronJob
err := json.Unmarshal([]byte(jsonBlob), &cronJob)
annotations := []Annotation{
{Key: "k8s.cronitor.io/name-prefix", Value: tc.annotationNamePrefix},
{Key: "k8s.cronitor.io/cronitor-name", Value: tc.annotationCronitorName},
}

cronJob, err := CronJobFromAnnotations(annotations)
if err != nil {
t.Fatalf("unexpected error unmarshalling json: %v", err)
}

parser := NewCronitorConfigParser(&cronJob)
if id := parser.GetCronitorName(); id != tc.expectedName {
t.Errorf("expected ID %s, got %s", tc.expectedName, id)
if name := parser.GetCronitorName(); name != tc.expectedName {
t.Errorf("expected Name %s, got %s", tc.expectedName, name)
}
})
}
Expand Down
62 changes: 62 additions & 0 deletions pkg/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package pkg

import (
"encoding/json"
"fmt"
"strings"

v1 "k8s.io/api/batch/v1"
)

type Annotation struct {
Key string
Value string
}

func CronJobFromAnnotations(annotations []Annotation) (v1.CronJob, error) {
var annotationParts []string
for _, a := range annotations {
annotationParts = append(annotationParts, fmt.Sprintf("\"%s\": \"%s\"", a.Key, a.Value))
}
annotationsStr := strings.Join(annotationParts, ", ")

jsonBlob := fmt.Sprintf(`{
"apiVersion": "batch/v1beta1",
"kind": "CronJob",
"metadata": {
"name": "name-test-cronjob",
"namespace": "default",
"annotations": {%s}
},
"spec": {
"concurrencyPolicy": "Forbid",
"jobTemplate": {
"spec": {
"backoffLimit": 3,
"template": {
"spec": {
"containers": [
{
"args": [
"/bin/sh",
"-c",
"date ; sleep 5 ; echo Hello from k8s"
],
"image": "busybox",
"name": "hello"
}
],
"restartPolicy": "OnFailure"
}
}
}
},
"schedule": "*/1 * * * *"
}
}`, annotationsStr)

var cronJob v1.CronJob
err := json.Unmarshal([]byte(jsonBlob), &cronJob)

return cronJob, err
}

0 comments on commit 46805c4

Please sign in to comment.