Skip to content

Commit c28572a

Browse files
author
Zach Swanson
committed
add optional labels to apply to Job pod template
1 parent 0ba76c7 commit c28572a

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ with these fields:
4343
- args: optional string array to be used as the upgrade Job's `args`.
4444
- image: optional image to use for the upgrade Job.
4545
- container: optional name of a container from the selected template Pod. The selected container will be used to run the upgrader.
46+
- labels: optional map of labels to set on the Job's pod template,
4647

4748
The migrator Job will contain only the single template container, initContainers will be included but sidecars will not. Any livenessProbes and readinessProbes in the template will be ignored.
4849

api/v1beta1/migrator_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type MigratorSpec struct {
2929
Image string `json:"image,omitempty"`
3030
Args *[]string `json:"args,omitempty"`
3131
Container string `json:"container,omitempty"`
32+
Labels map[string]string `json:"labels,omitempty"`
3233
}
3334

3435
// MigratorStatus defines the observed state of Migrator

components/migrations.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ func (comp *migrationsComponent) Reconcile(ctx *cu.Context) (cu.Result, error) {
194194
}
195195
migrationPodSpec.InitContainers = initContainers
196196

197+
// add labels to the job's pod template
198+
jobTemplateLabels := map[string]string{"migrations": obj.Name}
199+
if obj.Spec.Labels != nil {
200+
for k, v := range obj.Spec.Labels {
201+
jobTemplateLabels[k] = v
202+
}
203+
}
204+
197205
migrationJob := &batchv1.Job{
198206
ObjectMeta: metav1.ObjectMeta{
199207
Name: obj.Name + "-migrations",
@@ -204,7 +212,7 @@ func (comp *migrationsComponent) Reconcile(ctx *cu.Context) (cu.Result, error) {
204212
Spec: batchv1.JobSpec{
205213
Template: corev1.PodTemplateSpec{
206214
ObjectMeta: metav1.ObjectMeta{
207-
Labels: map[string]string{"migrations": obj.Name},
215+
Labels: jobTemplateLabels,
208216
Annotations: map[string]string{webhook.NOWAIT_MIGRATOR_ANNOTATION: "true"},
209217
},
210218
Spec: *migrationPodSpec,
@@ -220,7 +228,7 @@ func (comp *migrationsComponent) Reconcile(ctx *cu.Context) (cu.Result, error) {
220228
uncachedObj := &migrationsv1beta1.Migrator{}
221229
err = ctx.UncachedClient.Get(ctx, types.NamespacedName{Name: obj.Name, Namespace: obj.Namespace}, uncachedObj)
222230
if err != nil {
223-
return cu.Result{}, errors.Wrap(err, "erro getting latest migrator for status")
231+
return cu.Result{}, errors.Wrap(err, "error getting latest migrator for status")
224232
}
225233
if uncachedObj.Status.LastSuccessfulMigration == migrationContainer.Image {
226234
ctx.Conditions.SetfTrue(comp.GetReadyCondition(), "MigrationsUpToDate", "Migration %s already run", migrationContainer.Image)

components/migrations_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,13 @@ var _ = Describe("Migrations component", func() {
249249
helper.TestClient.GetName("testing-migrations", job)
250250
Expect(job.Spec.Template.Spec.Containers[0].Image).To(Equal("myapp:v1"))
251251
})
252+
253+
It("applies specified labels to the migration pod", func() {
254+
obj.Spec.Labels = map[string]string{"key1": "value1"}
255+
helper.TestClient.Create(pod)
256+
helper.MustReconcile()
257+
helper.TestClient.GetName("testing-migrations", job)
258+
Expect(job.Spec.Template.ObjectMeta.Labels).To(HaveKeyWithValue("key1", "value1"))
259+
Expect(job.Spec.Template.ObjectMeta.Labels).To(HaveKeyWithValue("migrations", "testing"))
260+
})
252261
})

0 commit comments

Comments
 (0)