Skip to content

Commit 353a0cb

Browse files
flavianmissiopenshift-cherrypick-robot
authored andcommitted
azurepathfixjob: recreate job when detect changes to container env vars
the storage controller sometimes recreates the storage account after the job has been created. to account for that, we compare the container envs between created and desired values, and when they differ we recreate the job.
1 parent ac9b6e1 commit 353a0cb

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

pkg/resource/azurepathfixjob.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"reflect"
78

89
batchv1 "k8s.io/api/batch/v1"
910
corev1 "k8s.io/api/core/v1"
1011
kcorev1 "k8s.io/api/core/v1"
1112
"k8s.io/apimachinery/pkg/api/errors"
1213
"k8s.io/apimachinery/pkg/api/resource"
14+
metaapi "k8s.io/apimachinery/pkg/apis/meta/v1"
1315
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1416
"k8s.io/apimachinery/pkg/runtime"
1517
batchset "k8s.io/client-go/kubernetes/typed/batch/v1"
@@ -269,11 +271,39 @@ func (gapfj *generatorAzurePathFixJob) Create() (runtime.Object, error) {
269271
}
270272

271273
func (gapfj *generatorAzurePathFixJob) Update(o runtime.Object) (runtime.Object, bool, error) {
272-
return commonUpdate(gapfj, o, func(obj runtime.Object) (runtime.Object, error) {
273-
return gapfj.client.Jobs(gapfj.GetNamespace()).Update(
274-
context.TODO(), obj.(*batchv1.Job), metav1.UpdateOptions{},
275-
)
276-
})
274+
// updating jobs doesn't work like other objects - we get validation errors
275+
// if we try. in our case, we only care about the job container's env vars,
276+
// so we check if the existing job's container env vars match the expected,
277+
// and if they don't we recreate the job.
278+
exp, err := gapfj.expected()
279+
if err != nil {
280+
return nil, false, err
281+
}
282+
expectedJob := exp.(*batchv1.Job)
283+
job := o.(*batchv1.Job)
284+
expectedEnvs := expectedJob.Spec.Template.Spec.Containers[0].Env
285+
actualEnvs := job.Spec.Template.Spec.Containers[0].Env
286+
287+
if reflect.DeepEqual(expectedEnvs, actualEnvs) {
288+
return o, false, nil
289+
}
290+
291+
// if we are here it means the expected container envs differed from
292+
// the actual container envs, so we recreate the job.
293+
gracePeriod := int64(0)
294+
propagationPolicy := metaapi.DeletePropagationForeground
295+
opts := metaapi.DeleteOptions{
296+
GracePeriodSeconds: &gracePeriod,
297+
PropagationPolicy: &propagationPolicy,
298+
}
299+
if err := gapfj.Delete(opts); err != nil {
300+
return nil, false, err
301+
}
302+
createdObj, err := gapfj.Create()
303+
if err != nil {
304+
return nil, false, err
305+
}
306+
return createdObj, true, nil
277307
}
278308

279309
func (gapfj *generatorAzurePathFixJob) Delete(opts metav1.DeleteOptions) error {

0 commit comments

Comments
 (0)