@@ -4,12 +4,14 @@ import (
4
4
"context"
5
5
"fmt"
6
6
"os"
7
+ "reflect"
7
8
8
9
batchv1 "k8s.io/api/batch/v1"
9
10
corev1 "k8s.io/api/core/v1"
10
11
kcorev1 "k8s.io/api/core/v1"
11
12
"k8s.io/apimachinery/pkg/api/errors"
12
13
"k8s.io/apimachinery/pkg/api/resource"
14
+ metaapi "k8s.io/apimachinery/pkg/apis/meta/v1"
13
15
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
16
"k8s.io/apimachinery/pkg/runtime"
15
17
batchset "k8s.io/client-go/kubernetes/typed/batch/v1"
@@ -269,11 +271,39 @@ func (gapfj *generatorAzurePathFixJob) Create() (runtime.Object, error) {
269
271
}
270
272
271
273
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
277
307
}
278
308
279
309
func (gapfj * generatorAzurePathFixJob ) Delete (opts metav1.DeleteOptions ) error {
0 commit comments