Skip to content

Commit

Permalink
Finishing touches
Browse files Browse the repository at this point in the history
  • Loading branch information
JaylonmcShan03 committed Oct 23, 2024
1 parent 49e0e4e commit a48323b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 106 deletions.
73 changes: 0 additions & 73 deletions kubernetes/resource_kubernetes_job_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"fmt"
"log"
"strconv"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -17,7 +16,6 @@ import (
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
pkgApi "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
Expand All @@ -30,7 +28,6 @@ func resourceKubernetesJobV1() *schema.Resource {
ReadContext: resourceKubernetesJobV1Read,
UpdateContext: resourceKubernetesJobV1Update,
DeleteContext: resourceKubernetesJobV1Delete,
CustomizeDiff: resourceKubernetesJobV1CustomizeDiff,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand All @@ -51,51 +48,6 @@ func resourceKubernetesJobV1() *schema.Resource {
}
}

func resourceKubernetesJobV1CustomizeDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
if d.Id() == "" {
log.Printf("[DEBUG] Resource ID is empty, resource not created yet.")
return nil
}

// Retrieve old and new TTL values
oldTTLRaw, newTTLRaw := d.GetChange("spec.0.ttl_seconds_after_finished")

// If both TTL values are not set or are empty, skip TTL diff
if (oldTTLRaw == nil && newTTLRaw == nil) || (oldTTLRaw == "" && newTTLRaw == "") {
log.Printf("[DEBUG] No ttl_seconds_after_finished provided or both values are empty; skipping TTL diff")
return nil
}

// Only check TTL if present
var oldTTLStr, newTTLStr string
if oldTTLRaw != nil {
oldTTLStr = oldTTLRaw.(string)
}
if newTTLRaw != nil {
newTTLStr = newTTLRaw.(string)
}

oldTTLInt, err := strconv.Atoi(oldTTLStr)
if err != nil {
log.Printf("[DEBUG] Invalid old TTL value: %s, skipping diff", oldTTLStr)
return nil
}
newTTLInt, err := strconv.Atoi(newTTLStr)
if err != nil {
log.Printf("[DEBUG] Invalid new TTL value: %s, skipping diff", newTTLStr)
return nil
}

// Suppress the diff if the old and new TTL values are the same
if oldTTLInt == newTTLInt {
log.Printf("[DEBUG] ttl_seconds_after_finished has not changed; suppressing diff")
d.Clear("spec")
d.Clear("metadata")
}

return nil
}

func resourceKubernetesJobV1Schema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"metadata": jobMetadataSchema(),
Expand Down Expand Up @@ -230,31 +182,6 @@ func resourceKubernetesJobV1Update(ctx context.Context, d *schema.ResourceData,
return diag.FromErr(err)
}

// Attempt to get the Job
_, err = conn.BatchV1().Jobs(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
// Job is missing; check TTL
ttlAttr := d.Get("spec.0.ttl_seconds_after_finished")
ttlStr, _ := ttlAttr.(string)
ttlInt, err := strconv.Atoi(ttlStr)
if err != nil {
ttlInt = 0
}

if ttlInt >= 0 {
// Job was deleted due to TTL nothing to update
log.Printf("[INFO] Job %s not found but ttl_seconds_after_finished = %v; nothing to update", d.Id(), ttlInt)
return nil
}

// Job was deleted unexpectedly; return an error
return diag.Errorf("Job %s not found; cannot update because it has been deleted", d.Id())
}
return diag.Errorf("Error retrieving Job: %s", err)
}

// Proceed with the update as usual
ops := patchMetadata("metadata.0.", "/metadata/", d)

if d.HasChange("spec") {
Expand Down
41 changes: 8 additions & 33 deletions kubernetes/resource_kubernetes_job_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func TestAccKubernetesJobV1_customizeDiff_ttlZero(t *testing.T) {
Steps: []resource.TestStep{
// Step 1: Create the Job
{
Config: testAccKubernetesJobV1Config_customizeDiff_ttlZero(name, imageName),
Config: testAccKubernetesJobV1Config_Diff(name, imageName, 0),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesJobV1Exists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "spec.0.ttl_seconds_after_finished", "0"),
Expand All @@ -263,7 +263,7 @@ func TestAccKubernetesJobV1_customizeDiff_ttlZero(t *testing.T) {
PreConfig: func() {
time.Sleep(70 * time.Second)
},
Config: testAccKubernetesJobV1Config_customizeDiff_ttlZero(name, imageName),
Config: testAccKubernetesJobV1Config_Diff(name, imageName, 0),
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
Expand All @@ -286,7 +286,7 @@ func TestAccKubernetesJobV1_updateTTLFromZero(t *testing.T) {
Steps: []resource.TestStep{
// Step 1: Create the Job with ttl_seconds_after_finished = 0
{
Config: testAccKubernetesJobV1Config_customizeDiff_ttlZero(name, imageName),
Config: testAccKubernetesJobV1Config_Diff(name, imageName, 0),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesJobV1Exists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "spec.0.ttl_seconds_after_finished", "0"),
Expand All @@ -297,13 +297,13 @@ func TestAccKubernetesJobV1_updateTTLFromZero(t *testing.T) {
PreConfig: func() {
time.Sleep(120 * time.Second)
},
Config: testAccKubernetesJobV1Config_customizeDiff_ttlZero(name, imageName),
Config: testAccKubernetesJobV1Config_Diff(name, imageName, 0),
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
// Step 3: Update the Job to ttl_seconds_after_finished = 5
{
Config: testAccKubernetesJobV1Config_customizeDiff_ttlFive(name, imageName),
Config: testAccKubernetesJobV1Config_Diff(name, imageName, 5),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesJobV1Exists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "spec.0.ttl_seconds_after_finished", "5"),
Expand Down Expand Up @@ -593,14 +593,14 @@ func testAccKubernetesJobV1Config_modified(name, imageName string) string {
}`, name, imageName)
}

func testAccKubernetesJobV1Config_customizeDiff_ttlZero(name, imageName string) string {
func testAccKubernetesJobV1Config_Diff(name, imageName string, ttl int) string {
return fmt.Sprintf(`
resource "kubernetes_job_v1" "test" {
metadata {
name = "%s"
}
spec {
ttl_seconds_after_finished = 0
ttl_seconds_after_finished = %d
template {
metadata {}
spec {
Expand All @@ -615,30 +615,5 @@ resource "kubernetes_job_v1" "test" {
}
wait_for_completion = false
}
`, name, imageName)
}

func testAccKubernetesJobV1Config_customizeDiff_ttlFive(name, imageName string) string {
return fmt.Sprintf(`
resource "kubernetes_job_v1" "test" {
metadata {
name = "%s"
}
spec {
ttl_seconds_after_finished = 5
template {
metadata {}
spec {
container {
name = "wait-test"
image = "%s"
command = ["sleep", "60"]
}
restart_policy = "Never"
}
}
}
wait_for_completion = false
}
`, name, imageName)
`, name, ttl, imageName)
}

0 comments on commit a48323b

Please sign in to comment.