Skip to content

Commit

Permalink
Introducing ForceNew field instead of handling the logic in Customize…
Browse files Browse the repository at this point in the history
…Diff
  • Loading branch information
JaylonmcShan03 committed Oct 22, 2024
1 parent bc239a2 commit ea1ace2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 35 deletions.
45 changes: 11 additions & 34 deletions kubernetes/resource_kubernetes_job_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/schema_job_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ea1ace2

Please sign in to comment.