Skip to content

Commit bc239a2

Browse files
Handle TTL value changes for Job recreation in Kubernetes
1 parent ff6c89b commit bc239a2

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

kubernetes/resource_kubernetes_job_v1.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,30 +93,28 @@ func resourceKubernetesJobV1CustomizeDiff(ctx context.Context, d *schema.Resourc
9393
if err != nil {
9494
if apierrors.IsNotFound(err) {
9595
// Job is missing
96-
if oldTTLInt == 0 {
97-
if newTTLInt == 0 {
98-
// TTL remains 0; suppress diff to prevent recreation
99-
log.Printf("[DEBUG] Job %s not found and ttl_seconds_after_finished remains 0; suppressing diff", d.Id())
100-
d.Clear("spec")
101-
d.Clear("metadata")
96+
if oldTTLInt >= 0 {
97+
if oldTTLInt != newTTLInt {
98+
// TTL value changed; force recreation
99+
log.Printf("[DEBUG] Job %s not found and ttl_seconds_after_finished changed from %d to %d; forcing recreation", d.Id(), oldTTLInt, newTTLInt)
100+
d.ForceNew("spec.0.ttl_seconds_after_finished")
102101
return nil
103102
} else {
104-
// TTL changed from 0 to non-zero; force recreation
105-
log.Printf("[DEBUG] Job %s not found and ttl_seconds_after_finished changed from 0 to %d; forcing recreation", d.Id(), newTTLInt)
106-
d.ForceNew("spec.0.ttl_seconds_after_finished")
103+
// TTL remains the same; suppress diff
104+
log.Printf("[DEBUG] Job %s not found and ttl_seconds_after_finished remains %d; suppressing diff", d.Id(), oldTTLInt)
105+
d.Clear("spec")
106+
d.Clear("metadata")
107107
return nil
108108
}
109-
} else {
110-
return nil
111109
}
112110
} else {
113111
return err
114112
}
115113
} else {
116-
// Job exists
117-
if oldTTLInt == 0 && newTTLInt != 0 {
118-
// TTL changing from 0 to non-zero; force recreation
119-
log.Printf("[DEBUG] Job %s exists and ttl_seconds_after_finished changed from 0 to %d; forcing recreation", d.Id(), newTTLInt)
114+
// Job exists, check if TTL changed
115+
if oldTTLInt != newTTLInt {
116+
// TTL changed; force recreation
117+
log.Printf("[DEBUG] Job %s exists and ttl_seconds_after_finished changed from %d to %d; forcing recreation", d.Id(), oldTTLInt, newTTLInt)
120118
d.ForceNew("spec.0.ttl_seconds_after_finished")
121119
return nil
122120
}
@@ -263,21 +261,22 @@ func resourceKubernetesJobV1Update(ctx context.Context, d *schema.ResourceData,
263261
_, err = conn.BatchV1().Jobs(namespace).Get(ctx, name, metav1.GetOptions{})
264262
if err != nil {
265263
if apierrors.IsNotFound(err) {
266-
// Job is missing; cannot update
264+
// Job is missing; check TTL
267265
ttlAttr := d.Get("spec.0.ttl_seconds_after_finished")
268266
ttlStr, _ := ttlAttr.(string)
269267
ttlInt, err := strconv.Atoi(ttlStr)
270268
if err != nil {
271269
ttlInt = 0
272270
}
273-
if ttlInt == 0 {
274-
// Job was deleted due to TTL = 0; nothing to update
275-
log.Printf("[INFO] Job %s not found but ttl_seconds_after_finished = 0; nothing to update", d.Id())
271+
272+
if ttlInt >= 0 {
273+
// Job was deleted due to TTL nothing to update
274+
log.Printf("[INFO] Job %s not found but ttl_seconds_after_finished = %v; nothing to update", d.Id(), ttlInt)
276275
return nil
277-
} else {
278-
// Job was deleted unexpectedly; return an error
279-
return diag.Errorf("Job %s not found; cannot update because it has been deleted", d.Id())
280276
}
277+
278+
// Job was deleted unexpectedly; return an error
279+
return diag.Errorf("Job %s not found; cannot update because it has been deleted", d.Id())
281280
}
282281
return diag.Errorf("Error retrieving Job: %s", err)
283282
}

0 commit comments

Comments
 (0)