77 "context"
88 "fmt"
99 "log"
10+ "strconv"
1011 "time"
1112
1213 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -16,6 +17,7 @@ import (
1617 batchv1 "k8s.io/api/batch/v1"
1718 corev1 "k8s.io/api/core/v1"
1819 "k8s.io/apimachinery/pkg/api/errors"
20+ apierrors "k8s.io/apimachinery/pkg/api/errors"
1921 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2022 pkgApi "k8s.io/apimachinery/pkg/types"
2123 "k8s.io/client-go/kubernetes"
@@ -55,6 +57,27 @@ func resourceKubernetesJobV1CustomizeDiff(ctx context.Context, d *schema.Resourc
5557 return nil
5658 }
5759
60+ // Retrieve old and new TTL values as strings
61+ oldTTLRaw , newTTLRaw := d .GetChange ("spec.0.ttl_seconds_after_finished" )
62+
63+ var oldTTLStr , newTTLStr string
64+
65+ if oldTTLRaw != nil {
66+ oldTTLStr , _ = oldTTLRaw .(string )
67+ }
68+ if newTTLRaw != nil {
69+ newTTLStr , _ = newTTLRaw .(string )
70+ }
71+
72+ oldTTLInt , err := strconv .Atoi (oldTTLStr )
73+ if err != nil {
74+ oldTTLInt = 0
75+ }
76+ newTTLInt , err := strconv .Atoi (newTTLStr )
77+ if err != nil {
78+ newTTLInt = 0
79+ }
80+
5881 conn , err := meta .(KubeClientsets ).MainClientset ()
5982 if err != nil {
6083 return err
@@ -65,23 +88,40 @@ func resourceKubernetesJobV1CustomizeDiff(ctx context.Context, d *schema.Resourc
6588 return err
6689 }
6790
68- ttlAttr := d .Get ("spec.0.ttl_seconds_after_finished" )
69- ttlSeconds , ok := ttlAttr .(int )
70- if ! ok || ttlSeconds != 0 {
71- return nil
72- }
73-
74- // getting the job
91+ // Check if the Job exists
7592 _ , err = conn .BatchV1 ().Jobs (namespace ).Get (ctx , name , metav1.GetOptions {})
7693 if err != nil {
77- if errors .IsNotFound (err ) {
78- // Suppress diff
79- d .Clear ("spec" )
80- d .Clear ("metadata" )
94+ if apierrors .IsNotFound (err ) {
95+ // 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" )
102+ return nil
103+ } 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" )
107+ return nil
108+ }
109+ } else {
110+ return nil
111+ }
112+ } else {
113+ return err
114+ }
115+ } 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 )
120+ d .ForceNew ("spec.0.ttl_seconds_after_finished" )
81121 return nil
82122 }
83- return err
84123 }
124+
85125 return nil
86126}
87127
@@ -219,6 +259,30 @@ func resourceKubernetesJobV1Update(ctx context.Context, d *schema.ResourceData,
219259 return diag .FromErr (err )
220260 }
221261
262+ // Attempt to get the Job
263+ _ , err = conn .BatchV1 ().Jobs (namespace ).Get (ctx , name , metav1.GetOptions {})
264+ if err != nil {
265+ if apierrors .IsNotFound (err ) {
266+ // Job is missing; cannot update
267+ ttlAttr := d .Get ("spec.0.ttl_seconds_after_finished" )
268+ ttlStr , _ := ttlAttr .(string )
269+ ttlInt , err := strconv .Atoi (ttlStr )
270+ if err != nil {
271+ ttlInt = 0
272+ }
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 ())
276+ 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 ())
280+ }
281+ }
282+ return diag .Errorf ("Error retrieving Job: %s" , err )
283+ }
284+
285+ // Proceed with the update as usual
222286 ops := patchMetadata ("metadata.0." , "/metadata/" , d )
223287
224288 if d .HasChange ("spec" ) {
@@ -250,7 +314,6 @@ func resourceKubernetesJobV1Update(ctx context.Context, d *schema.ResourceData,
250314 }
251315 return resourceKubernetesJobV1Read (ctx , d , meta )
252316}
253-
254317func resourceKubernetesJobV1Delete (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
255318 conn , err := meta .(KubeClientsets ).MainClientset ()
256319 if err != nil {
0 commit comments