Skip to content

Commit 3781502

Browse files
authored
Merge pull request #8 from zswanson/specify-labels-migrations-pod
allow optional labels on generated Job template
2 parents c46482f + 2f02331 commit 3781502

File tree

7 files changed

+40
-9
lines changed

7 files changed

+40
-9
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

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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
})

config/crd/bases/migrations.coderanger.net_migrators.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ spec:
4848
type: string
4949
image:
5050
type: string
51+
labels:
52+
additionalProperties:
53+
type: string
54+
type: object
5155
selector:
5256
description: A label selector is a label query over a set of resources.
5357
The result of matchLabels and matchExpressions are ANDed. An empty

config/rbac/role.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ rules:
99
- apiGroups:
1010
- ""
1111
resources:
12-
- pods
12+
- events
1313
verbs:
14-
- get
15-
- list
16-
- watch
14+
- create
15+
- patch
1716
- apiGroups:
1817
- ""
1918
resources:
20-
- events
19+
- pods
2120
verbs:
22-
- create
23-
- patch
21+
- get
22+
- list
23+
- watch
2424
- apiGroups:
2525
- apps
2626
resources:

0 commit comments

Comments
 (0)