Skip to content

Commit a48323b

Browse files
Finishing touches
1 parent 49e0e4e commit a48323b

File tree

2 files changed

+8
-106
lines changed

2 files changed

+8
-106
lines changed

kubernetes/resource_kubernetes_job_v1.go

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"fmt"
99
"log"
10-
"strconv"
1110
"time"
1211

1312
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -17,7 +16,6 @@ import (
1716
batchv1 "k8s.io/api/batch/v1"
1817
corev1 "k8s.io/api/core/v1"
1918
"k8s.io/apimachinery/pkg/api/errors"
20-
apierrors "k8s.io/apimachinery/pkg/api/errors"
2119
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2220
pkgApi "k8s.io/apimachinery/pkg/types"
2321
"k8s.io/client-go/kubernetes"
@@ -30,7 +28,6 @@ func resourceKubernetesJobV1() *schema.Resource {
3028
ReadContext: resourceKubernetesJobV1Read,
3129
UpdateContext: resourceKubernetesJobV1Update,
3230
DeleteContext: resourceKubernetesJobV1Delete,
33-
CustomizeDiff: resourceKubernetesJobV1CustomizeDiff,
3431
Importer: &schema.ResourceImporter{
3532
StateContext: schema.ImportStatePassthroughContext,
3633
},
@@ -51,51 +48,6 @@ func resourceKubernetesJobV1() *schema.Resource {
5148
}
5249
}
5350

54-
func resourceKubernetesJobV1CustomizeDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
55-
if d.Id() == "" {
56-
log.Printf("[DEBUG] Resource ID is empty, resource not created yet.")
57-
return nil
58-
}
59-
60-
// Retrieve old and new TTL values
61-
oldTTLRaw, newTTLRaw := d.GetChange("spec.0.ttl_seconds_after_finished")
62-
63-
// If both TTL values are not set or are empty, skip TTL diff
64-
if (oldTTLRaw == nil && newTTLRaw == nil) || (oldTTLRaw == "" && newTTLRaw == "") {
65-
log.Printf("[DEBUG] No ttl_seconds_after_finished provided or both values are empty; skipping TTL diff")
66-
return nil
67-
}
68-
69-
// Only check TTL if present
70-
var oldTTLStr, newTTLStr string
71-
if oldTTLRaw != nil {
72-
oldTTLStr = oldTTLRaw.(string)
73-
}
74-
if newTTLRaw != nil {
75-
newTTLStr = newTTLRaw.(string)
76-
}
77-
78-
oldTTLInt, err := strconv.Atoi(oldTTLStr)
79-
if err != nil {
80-
log.Printf("[DEBUG] Invalid old TTL value: %s, skipping diff", oldTTLStr)
81-
return nil
82-
}
83-
newTTLInt, err := strconv.Atoi(newTTLStr)
84-
if err != nil {
85-
log.Printf("[DEBUG] Invalid new TTL value: %s, skipping diff", newTTLStr)
86-
return nil
87-
}
88-
89-
// Suppress the diff if the old and new TTL values are the same
90-
if oldTTLInt == newTTLInt {
91-
log.Printf("[DEBUG] ttl_seconds_after_finished has not changed; suppressing diff")
92-
d.Clear("spec")
93-
d.Clear("metadata")
94-
}
95-
96-
return nil
97-
}
98-
9951
func resourceKubernetesJobV1Schema() map[string]*schema.Schema {
10052
return map[string]*schema.Schema{
10153
"metadata": jobMetadataSchema(),
@@ -230,31 +182,6 @@ func resourceKubernetesJobV1Update(ctx context.Context, d *schema.ResourceData,
230182
return diag.FromErr(err)
231183
}
232184

233-
// Attempt to get the Job
234-
_, err = conn.BatchV1().Jobs(namespace).Get(ctx, name, metav1.GetOptions{})
235-
if err != nil {
236-
if apierrors.IsNotFound(err) {
237-
// Job is missing; check TTL
238-
ttlAttr := d.Get("spec.0.ttl_seconds_after_finished")
239-
ttlStr, _ := ttlAttr.(string)
240-
ttlInt, err := strconv.Atoi(ttlStr)
241-
if err != nil {
242-
ttlInt = 0
243-
}
244-
245-
if ttlInt >= 0 {
246-
// Job was deleted due to TTL nothing to update
247-
log.Printf("[INFO] Job %s not found but ttl_seconds_after_finished = %v; nothing to update", d.Id(), ttlInt)
248-
return nil
249-
}
250-
251-
// Job was deleted unexpectedly; return an error
252-
return diag.Errorf("Job %s not found; cannot update because it has been deleted", d.Id())
253-
}
254-
return diag.Errorf("Error retrieving Job: %s", err)
255-
}
256-
257-
// Proceed with the update as usual
258185
ops := patchMetadata("metadata.0.", "/metadata/", d)
259186

260187
if d.HasChange("spec") {

kubernetes/resource_kubernetes_job_v1_test.go

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func TestAccKubernetesJobV1_customizeDiff_ttlZero(t *testing.T) {
252252
Steps: []resource.TestStep{
253253
// Step 1: Create the Job
254254
{
255-
Config: testAccKubernetesJobV1Config_customizeDiff_ttlZero(name, imageName),
255+
Config: testAccKubernetesJobV1Config_Diff(name, imageName, 0),
256256
Check: resource.ComposeAggregateTestCheckFunc(
257257
testAccCheckKubernetesJobV1Exists(resourceName, &conf),
258258
resource.TestCheckResourceAttr(resourceName, "spec.0.ttl_seconds_after_finished", "0"),
@@ -263,7 +263,7 @@ func TestAccKubernetesJobV1_customizeDiff_ttlZero(t *testing.T) {
263263
PreConfig: func() {
264264
time.Sleep(70 * time.Second)
265265
},
266-
Config: testAccKubernetesJobV1Config_customizeDiff_ttlZero(name, imageName),
266+
Config: testAccKubernetesJobV1Config_Diff(name, imageName, 0),
267267
PlanOnly: true,
268268
ExpectNonEmptyPlan: false,
269269
},
@@ -286,7 +286,7 @@ func TestAccKubernetesJobV1_updateTTLFromZero(t *testing.T) {
286286
Steps: []resource.TestStep{
287287
// Step 1: Create the Job with ttl_seconds_after_finished = 0
288288
{
289-
Config: testAccKubernetesJobV1Config_customizeDiff_ttlZero(name, imageName),
289+
Config: testAccKubernetesJobV1Config_Diff(name, imageName, 0),
290290
Check: resource.ComposeAggregateTestCheckFunc(
291291
testAccCheckKubernetesJobV1Exists(resourceName, &conf),
292292
resource.TestCheckResourceAttr(resourceName, "spec.0.ttl_seconds_after_finished", "0"),
@@ -297,13 +297,13 @@ func TestAccKubernetesJobV1_updateTTLFromZero(t *testing.T) {
297297
PreConfig: func() {
298298
time.Sleep(120 * time.Second)
299299
},
300-
Config: testAccKubernetesJobV1Config_customizeDiff_ttlZero(name, imageName),
300+
Config: testAccKubernetesJobV1Config_Diff(name, imageName, 0),
301301
PlanOnly: true,
302302
ExpectNonEmptyPlan: false,
303303
},
304304
// Step 3: Update the Job to ttl_seconds_after_finished = 5
305305
{
306-
Config: testAccKubernetesJobV1Config_customizeDiff_ttlFive(name, imageName),
306+
Config: testAccKubernetesJobV1Config_Diff(name, imageName, 5),
307307
Check: resource.ComposeAggregateTestCheckFunc(
308308
testAccCheckKubernetesJobV1Exists(resourceName, &conf),
309309
resource.TestCheckResourceAttr(resourceName, "spec.0.ttl_seconds_after_finished", "5"),
@@ -593,14 +593,14 @@ func testAccKubernetesJobV1Config_modified(name, imageName string) string {
593593
}`, name, imageName)
594594
}
595595

596-
func testAccKubernetesJobV1Config_customizeDiff_ttlZero(name, imageName string) string {
596+
func testAccKubernetesJobV1Config_Diff(name, imageName string, ttl int) string {
597597
return fmt.Sprintf(`
598598
resource "kubernetes_job_v1" "test" {
599599
metadata {
600600
name = "%s"
601601
}
602602
spec {
603-
ttl_seconds_after_finished = 0
603+
ttl_seconds_after_finished = %d
604604
template {
605605
metadata {}
606606
spec {
@@ -615,30 +615,5 @@ resource "kubernetes_job_v1" "test" {
615615
}
616616
wait_for_completion = false
617617
}
618-
`, name, imageName)
619-
}
620-
621-
func testAccKubernetesJobV1Config_customizeDiff_ttlFive(name, imageName string) string {
622-
return fmt.Sprintf(`
623-
resource "kubernetes_job_v1" "test" {
624-
metadata {
625-
name = "%s"
626-
}
627-
spec {
628-
ttl_seconds_after_finished = 5
629-
template {
630-
metadata {}
631-
spec {
632-
container {
633-
name = "wait-test"
634-
image = "%s"
635-
command = ["sleep", "60"]
636-
}
637-
restart_policy = "Never"
638-
}
639-
}
640-
}
641-
wait_for_completion = false
642-
}
643-
`, name, imageName)
618+
`, name, ttl, imageName)
644619
}

0 commit comments

Comments
 (0)