Skip to content

Commit 69a6932

Browse files
authored
Merge pull request #745 from terraform-providers/add-ttl-after
Add ttlSecondsAfterFinished to kubernetes_job and kubernetes_cron_job
2 parents 6951f86 + edb8840 commit 69a6932

File tree

6 files changed

+59
-14
lines changed

6 files changed

+59
-14
lines changed

kubernetes/provider_test.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
gversion "github.com/hashicorp/go-version"
11+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1112
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
1213
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1314
"github.com/terraform-providers/terraform-provider-aws/aws"
@@ -218,20 +219,8 @@ func getClusterVersion() (*gversion.Version, error) {
218219
}
219220

220221
func skipIfClusterVersionLessThan(t *testing.T, vs string) {
221-
cv, err := getClusterVersion()
222-
223-
if err != nil {
224-
t.Fatal(err)
225-
}
226-
227-
v, err := gversion.NewVersion(vs)
228-
229-
if err != nil {
230-
t.Fatal(err)
231-
}
232-
233-
if cv.LessThan(v) {
234-
t.Skip(fmt.Sprintf("This test will only run on cluster versions %v and above", v))
222+
if clusterVersionLessThan(vs) {
223+
t.Skip(fmt.Sprintf("This test will only run on cluster versions %v and above", vs))
235224
}
236225
}
237226

@@ -325,6 +314,32 @@ func getFirstNode() (api.Node, error) {
325314
return resp.Items[0], nil
326315
}
327316

317+
func clusterVersionLessThan(vs string) bool {
318+
cv, err := getClusterVersion()
319+
320+
if err != nil {
321+
return false
322+
}
323+
324+
v, err := gversion.NewVersion(vs)
325+
326+
if err != nil {
327+
return false
328+
}
329+
330+
return cv.LessThan(v)
331+
}
332+
333+
func skipCheckIf(skip func() (bool, string), check resource.TestCheckFunc) resource.TestCheckFunc {
334+
return func(s *terraform.State) error {
335+
if s, reason := skip(); s {
336+
fmt.Println("Skipping check:", reason)
337+
return nil
338+
}
339+
return check(s)
340+
}
341+
}
342+
328343
type currentEnv struct {
329344
Config string
330345
Ctx string

kubernetes/resource_kubernetes_job_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package kubernetes
22

33
import (
44
"fmt"
5+
"os"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
@@ -11,6 +12,10 @@ import (
1112
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1213
)
1314

15+
func ttlAfterDisabled() (bool, string) {
16+
return os.Getenv("FEATURE_GATE_TTL_AFTER_FINISHED") != "enabled", "TTLAfterFinished is not enabled"
17+
}
18+
1419
func TestAccKubernetesJob_basic(t *testing.T) {
1520
var conf api.Job
1621
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
@@ -37,6 +42,9 @@ func TestAccKubernetesJob_basic(t *testing.T) {
3742
resource.TestCheckResourceAttr("kubernetes_job.test", "spec.0.parallelism", "2"),
3843
resource.TestCheckResourceAttr("kubernetes_job.test", "spec.0.template.0.spec.0.container.0.name", "hello"),
3944
resource.TestCheckResourceAttr("kubernetes_job.test", "spec.0.template.0.spec.0.container.0.image", "alpine"),
45+
46+
skipCheckIf(ttlAfterDisabled,
47+
resource.TestCheckResourceAttr("kubernetes_job.test", "spec.0.ttl_seconds_after_finished", "10")),
4048
),
4149
},
4250
{
@@ -58,6 +66,9 @@ func TestAccKubernetesJob_basic(t *testing.T) {
5866
resource.TestCheckResourceAttr("kubernetes_job.test", "spec.0.manual_selector", "true"),
5967
resource.TestCheckResourceAttr("kubernetes_job.test", "spec.0.template.0.spec.0.container.0.name", "hello"),
6068
resource.TestCheckResourceAttr("kubernetes_job.test", "spec.0.template.0.spec.0.container.0.image", "alpine"),
69+
70+
skipCheckIf(ttlAfterDisabled,
71+
resource.TestCheckResourceAttr("kubernetes_job.test", "spec.0.ttl_seconds_after_finished", "0")),
6172
),
6273
},
6374
},
@@ -126,6 +137,7 @@ resource "kubernetes_job" "test" {
126137
}
127138
spec {
128139
active_deadline_seconds = 120
140+
ttl_seconds_after_finished = 10
129141
backoff_limit = 10
130142
completions = 10
131143
parallelism = 2

kubernetes/schema_job_spec.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ func jobSpecFields() map[string]*schema.Schema {
127127
Schema: podTemplateFields,
128128
},
129129
},
130+
"ttl_seconds_after_finished": {
131+
Type: schema.TypeInt,
132+
Optional: true,
133+
ForceNew: true,
134+
ValidateFunc: validatePositiveInteger,
135+
Description: "ttlSecondsAfterFinished limits the lifetime of a Job that has finished execution (either Complete or Failed). If this field is set, ttlSecondsAfterFinished after the Job finishes, it is eligible to be automatically deleted. When the Job is being deleted, its lifecycle guarantees (e.g. finalizers) will be honored. If this field is unset, the Job won't be automatically deleted. If this field is set to zero, the Job becomes eligible to be deleted immediately after it finishes.",
136+
},
130137
}
131138

132139
return s

kubernetes/structure_job.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func flattenJobSpec(in batchv1.JobSpec, d *schema.ResourceData, prefix ...string
4848
}
4949
att["template"] = podSpec
5050

51+
if in.TTLSecondsAfterFinished != nil {
52+
att["ttl_seconds_after_finished"] = *in.TTLSecondsAfterFinished
53+
}
54+
5155
return []interface{}{att}, nil
5256
}
5357

@@ -90,6 +94,10 @@ func expandJobSpec(j []interface{}) (batchv1.JobSpec, error) {
9094
}
9195
obj.Template = *template
9296

97+
if v, ok := in["ttl_seconds_after_finished"].(int); ok && v > 0 {
98+
obj.TTLSecondsAfterFinished = ptrToInt32(int32(v))
99+
}
100+
93101
return obj, nil
94102
}
95103

website/docs/r/cron_job.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ resource "kubernetes_cron_job" "demo" {
2929
starting_deadline_seconds = 10
3030
successful_jobs_history_limit = 10
3131
suspend = true
32+
ttl_seconds_after_finished = 10
3233
job_template {
3334
metadata {}
3435
spec {
@@ -110,6 +111,7 @@ More info: http://kubernetes.io/docs/user-guide/labels
110111
* `parallelism` - (Optional) Specifies the maximum desired number of pods the job should run at any given time. The actual number of pods running in steady state will be less than this number when `((.spec.completions - .status.successful) < .spec.parallelism)`, i.e. when the work left to do is less than max parallelism. More info: https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/
111112
* `selector` - (Optional) A label query over pods that should match the pod count. Normally, the system sets this field for you. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
112113
* `template` - (Optional) Describes the pod that will be created when executing a job. More info: https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/
114+
* `ttl_seconds_after_finished` - (Optional) ttlSecondsAfterFinished limits the lifetime of a Job that has finished execution (either Complete or Failed). If this field is set, ttlSecondsAfterFinished after the Job finishes, it is eligible to be automatically deleted. When the Job is being deleted, its lifecycle guarantees (e.g. finalizers) will be honored. If this field is unset, the Job won't be automatically deleted. If this field is set to zero, the Job becomes eligible to be deleted immediately after it finishes.
113115

114116
### `selector`
115117

website/docs/r/job.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ More info: http://kubernetes.io/docs/user-guide/labels
7979
* `parallelism` - (Optional) Specifies the maximum desired number of pods the job should run at any given time. The actual number of pods running in steady state will be less than this number when `((.spec.completions - .status.successful) < .spec.parallelism)`, i.e. when the work left to do is less than max parallelism. More info: https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/
8080
* `selector` - (Optional) A label query over pods that should match the pod count. Normally, the system sets this field for you. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
8181
* `template` - (Optional) Describes the pod that will be created when executing a job. More info: https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/
82+
* `ttl_seconds_after_finished` - (Optional) ttlSecondsAfterFinished limits the lifetime of a Job that has finished execution (either Complete or Failed). If this field is set, ttlSecondsAfterFinished after the Job finishes, it is eligible to be automatically deleted. When the Job is being deleted, its lifecycle guarantees (e.g. finalizers) will be honored. If this field is unset, the Job won't be automatically deleted. If this field is set to zero, the Job becomes eligible to be deleted immediately after it finishes.
8283

8384
### `selector`
8485

0 commit comments

Comments
 (0)