From ea1ace2125e98eee899b44c9cb61446b8d2c2352 Mon Sep 17 00:00:00 2001 From: Jaylon McShan Date: Tue, 22 Oct 2024 08:34:06 -0500 Subject: [PATCH] Introducing ForceNew field instead of handling the logic in CustomizeDiff --- kubernetes/resource_kubernetes_job_v1.go | 45 ++++++------------------ kubernetes/schema_job_spec.go | 2 +- 2 files changed, 12 insertions(+), 35 deletions(-) diff --git a/kubernetes/resource_kubernetes_job_v1.go b/kubernetes/resource_kubernetes_job_v1.go index abccf49816..b441a6d849 100644 --- a/kubernetes/resource_kubernetes_job_v1.go +++ b/kubernetes/resource_kubernetes_job_v1.go @@ -57,25 +57,19 @@ func resourceKubernetesJobV1CustomizeDiff(ctx context.Context, d *schema.Resourc return nil } - // Retrieve old and new TTL values as strings + // Retrieve old and new TTL schema values as oldTTLRaw, newTTLRaw := d.GetChange("spec.0.ttl_seconds_after_finished") - - var oldTTLStr, newTTLStr string - - if oldTTLRaw != nil { - oldTTLStr, _ = oldTTLRaw.(string) - } - if newTTLRaw != nil { - newTTLStr, _ = newTTLRaw.(string) - } + oldTTLStr, _ := oldTTLRaw.(string) + newTTLStr, _ := newTTLRaw.(string) oldTTLInt, err := strconv.Atoi(oldTTLStr) if err != nil { - oldTTLInt = 0 + return fmt.Errorf("invalid old TTL value: %v", err) } + newTTLInt, err := strconv.Atoi(newTTLStr) if err != nil { - newTTLInt = 0 + return fmt.Errorf("invalid new TTL value: %v", err) } conn, err := meta.(KubeClientsets).MainClientset() @@ -92,32 +86,15 @@ func resourceKubernetesJobV1CustomizeDiff(ctx context.Context, d *schema.Resourc _, err = conn.BatchV1().Jobs(namespace).Get(ctx, name, metav1.GetOptions{}) if err != nil { if apierrors.IsNotFound(err) { - // Job is missing - if oldTTLInt >= 0 { - if oldTTLInt != newTTLInt { - // TTL value changed; force recreation - log.Printf("[DEBUG] Job %s not found and ttl_seconds_after_finished changed from %d to %d; forcing recreation", d.Id(), oldTTLInt, newTTLInt) - d.ForceNew("spec.0.ttl_seconds_after_finished") - return nil - } else { - // TTL remains the same; suppress diff - log.Printf("[DEBUG] Job %s not found and ttl_seconds_after_finished remains %d; suppressing diff", d.Id(), oldTTLInt) - d.Clear("spec") - d.Clear("metadata") - return nil - } + // Job is missing, suppress diff if the TTL is the same + if oldTTLInt == newTTLInt { + log.Printf("[DEBUG] Job %s not found and ttl_seconds_after_finished remains unchanged; suppressing diff", d.Id()) + d.Clear("spec") + d.Clear("metadata") } } else { return err } - } else { - // Job exists, check if TTL changed - if oldTTLInt != newTTLInt { - // TTL changed; force recreation - log.Printf("[DEBUG] Job %s exists and ttl_seconds_after_finished changed from %d to %d; forcing recreation", d.Id(), oldTTLInt, newTTLInt) - d.ForceNew("spec.0.ttl_seconds_after_finished") - return nil - } } return nil diff --git a/kubernetes/schema_job_spec.go b/kubernetes/schema_job_spec.go index 1e7157bab3..243d8aae13 100644 --- a/kubernetes/schema_job_spec.go +++ b/kubernetes/schema_job_spec.go @@ -235,7 +235,7 @@ func jobSpecFields(specUpdatable bool) map[string]*schema.Schema { "ttl_seconds_after_finished": { Type: schema.TypeString, Optional: true, - ForceNew: false, + ForceNew: true, ValidateFunc: func(value interface{}, key string) ([]string, []error) { v, err := strconv.Atoi(value.(string)) if err != nil {