Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate labels and annotations from plans to jobs #286

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 0 additions & 73 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,77 +88,6 @@ volumes:
host:
path: /var/run/docker.sock

---
kind: pipeline
name: s390x

platform:
os: linux
arch: amd64

node:
arch: s390x

steps:
- name: build
image: rancher/dapper:v0.6.0
commands:
- dapper ci
volumes:
- name: docker
path: /var/run/docker.sock

- name: upload-artifacts
image: rancher/drone-images:github-release-s390x
settings:
api_key:
from_secret: github_token
prerelease: true
checksum:
- sha256
checksum_file: CHECKSUMsum-s390x.txt
checksum_flatten: true
files:
- "dist/artifacts/*"
when:
instance:
- drone-publish.rancher.io
ref:
- refs/head/master
- refs/tags/*
event:
- tag

- name: push-controller
image: rancher/drone-images:docker-s390x
volumes:
- name: docker
path: /var/run/docker.sock
settings:
dockerfile: package/Dockerfile
build_args:
- ARCH=s390x
- TAG=${DRONE_TAG}-s390x
password:
from_secret: docker_password
repo: "rancher/system-upgrade-controller"
tag: "${DRONE_TAG}-s390x"
username:
from_secret: docker_username
when:
instance:
- drone-publish.rancher.io
ref:
- refs/head/master
- refs/tags/*
event:
- tag

volumes:
- name: docker
host:
path: /var/run/docker.sock

---
kind: pipeline
name: arm64
Expand Down Expand Up @@ -266,7 +195,6 @@ steps:
platforms:
- linux/amd64
- linux/arm64
- linux/s390x
target: "rancher/system-upgrade-controller:${DRONE_TAG}"
template: "rancher/system-upgrade-controller:${DRONE_TAG}-ARCH"
when:
Expand Down Expand Up @@ -301,7 +229,6 @@ steps:

depends_on:
- amd64
- s390x
- arm64

---
Expand Down
55 changes: 34 additions & 21 deletions pkg/upgrade/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,35 +124,48 @@ func New(plan *upgradeapiv1.Plan, node *corev1.Node, controllerName string) *bat
labelPlanName := upgradeapi.LabelPlanName(plan.Name)
nodeHostname := upgradenode.Hostname(node)
shortNodeName := strings.SplitN(node.Name, ".", 2)[0]

jobAnnotations := labels.Set{
upgradeapi.AnnotationTTLSecondsAfterFinished: strconv.FormatInt(int64(TTLSecondsAfterFinished), 10),
}
podAnnotations := labels.Set{}

for key, value := range plan.Annotations {
if !strings.Contains(key, "cattle.io/") {
jobAnnotations[key] = value
podAnnotations[key] = value
}
}

jobLabels := labels.Set{
upgradeapi.LabelController: controllerName,
upgradeapi.LabelExclusive: exclusiveString,
upgradeapi.LabelNode: node.Name,
upgradeapi.LabelPlan: plan.Name,
upgradeapi.LabelVersion: plan.Status.LatestVersion,
labelPlanName: plan.Status.LatestHash,
}

for key, value := range plan.Labels {
if !strings.Contains(key, "cattle.io/") {
jobLabels[key] = value
}
}

job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: name.SafeConcatName("apply", plan.Name, "on", shortNodeName, "with", plan.Status.LatestHash),
Namespace: plan.Namespace,
Annotations: labels.Set{
upgradeapi.AnnotationTTLSecondsAfterFinished: strconv.FormatInt(int64(TTLSecondsAfterFinished), 10),
},
Labels: labels.Set{
upgradeapi.LabelController: controllerName,
upgradeapi.LabelExclusive: exclusiveString,
upgradeapi.LabelNode: node.Name,
upgradeapi.LabelPlan: plan.Name,
upgradeapi.LabelVersion: plan.Status.LatestVersion,
labelPlanName: plan.Status.LatestHash,
},
Name: name.SafeConcatName("apply", plan.Name, "on", shortNodeName, "with", plan.Status.LatestHash),
Namespace: plan.Namespace,
Annotations: jobAnnotations,
Labels: jobLabels,
},
Spec: batchv1.JobSpec{
BackoffLimit: &BackoffLimit,
TTLSecondsAfterFinished: &TTLSecondsAfterFinished,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels.Set{
upgradeapi.LabelController: controllerName,
upgradeapi.LabelExclusive: exclusiveString,
upgradeapi.LabelNode: node.Name,
upgradeapi.LabelPlan: plan.Name,
upgradeapi.LabelVersion: plan.Status.LatestVersion,
labelPlanName: plan.Status.LatestHash,
},
Annotations: podAnnotations,
Labels: jobLabels,
},
Spec: corev1.PodSpec{
HostIPC: true,
Expand Down
28 changes: 28 additions & 0 deletions pkg/upgrade/job/job_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,33 @@ var _ = Describe("Jobs", func() {
Expect(*job.Spec.ActiveDeadlineSeconds).To(Equal(int64(300)))
})
})

Context("When the Plan has annotations and labels", func() {
It("Copies the non-cattle.io metadata to the Job and Pod", func() {
plan.Annotations = make(map[string]string)
plan.Annotations["cattle.io/some-annotation"] = "foo"
plan.Annotations["plan.cattle.io/some-annotation"] = "bar"
plan.Annotations["some.other/annotation"] = "baz"
plan.Labels = make(map[string]string)
plan.Labels["cattle.io/some-label"] = "biz"
plan.Labels["plan.cattle.io/some-label"] = "buz"
plan.Labels["some.other/label"] = "bla"

job := sucjob.New(plan, node, "foobar")
Expect(job.Annotations).To(Not(HaveKey("cattle.io/some-annotation")))
Expect(job.Annotations).To(Not(HaveKey("plan.cattle.io/some-annotation")))
Expect(job.Annotations).To(HaveKeyWithValue("some.other/annotation", "baz"))
Expect(job.Labels).To(Not(HaveKey("cattle.io/some-label")))
Expect(job.Labels).To(Not(HaveKey("plan.cattle.io/some-label")))
Expect(job.Labels).To(HaveKeyWithValue("some.other/label", "bla"))

Expect(job.Spec.Template.Annotations).To(Not(HaveKey("cattle.io/some-annotation")))
Expect(job.Spec.Template.Annotations).To(Not(HaveKey("plan.cattle.io/some-annotation")))
Expect(job.Spec.Template.Annotations).To(HaveKeyWithValue("some.other/annotation", "baz"))
Expect(job.Spec.Template.Labels).To(Not(HaveKey("cattle.io/some-label")))
Expect(job.Spec.Template.Labels).To(Not(HaveKey("plan.cattle.io/some-label")))
Expect(job.Spec.Template.Labels).To(HaveKeyWithValue("some.other/label", "bla"))
})
})
})
})