Skip to content

Commit 9f4cabc

Browse files
aayushsss1BBBmau
andauthored
min_ready_seconds parameter support for statefulset (#2493)
* min_ready_seconds parameter added to statefulset Signed-off-by: Aayush Subramaniam <[email protected]> * Documentation Updated Signed-off-by: Aayush Subramaniam <[email protected]> * Added changelog and fixed lint in test * documentation updated * update docs and description to match k8s docs --------- Signed-off-by: Aayush Subramaniam <[email protected]> Co-authored-by: Mauricio Alvarez Leon <[email protected]> Co-authored-by: BBBmau <[email protected]>
1 parent c7aaa23 commit 9f4cabc

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed

.changelog/2493.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
resource/resource_kubernetes_stateful_set_v1: Add support for `min_ready_seconds`
3+
```

docs/resources/stateful_set_v1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Optional:
6161
- `revision_history_limit` (Number) The maximum number of revisions that will be maintained in the StatefulSet's revision history. The default value is 10.
6262
- `update_strategy` (Block List) The strategy that the StatefulSet controller will use to perform updates. (see [below for nested schema](#nestedblock--spec--update_strategy))
6363
- `volume_claim_template` (Block List) A list of claims that pods are allowed to reference. Every claim in this list must have at least one matching (by name) volumeMount in one container in the template. (see [below for nested schema](#nestedblock--spec--volume_claim_template))
64+
- `min_ready_seconds` - (Optional) - Minimum number of seconds for which a newly created pod should be ready without any of its container crashing for it to be considered available. Defaults to 0. (pod will be considered available as soon as it is ready)
6465

6566
<a id="nestedblock--spec--selector"></a>
6667
### Nested Schema for `spec.selector`
@@ -2361,6 +2362,7 @@ resource "kubernetes_stateful_set_v1" "prometheus" {
23612362
}
23622363
23632364
spec {
2365+
min_ready_seconds = 10
23642366
pod_management_policy = "Parallel"
23652367
replicas = 1
23662368
revision_history_limit = 5

kubernetes/resource_kubernetes_stateful_set_v1_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func TestAccKubernetesStatefulSetV1_basic(t *testing.T) {
7676
resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name),
7777
resource.TestCheckResourceAttr(resourceName, "spec.#", "1"),
7878
resource.TestCheckResourceAttr(resourceName, "spec.0.replicas", "1"),
79+
resource.TestCheckResourceAttr(resourceName, "spec.0.min_ready_seconds", "10"),
7980
resource.TestCheckResourceAttr(resourceName, "spec.0.revision_history_limit", "11"),
8081
resource.TestCheckResourceAttr(resourceName, "spec.0.service_name", "ss-test-service"),
8182
resource.TestCheckResourceAttr(resourceName, "spec.0.persistent_volume_claim_retention_policy.0.when_deleted", "Delete"),
@@ -222,6 +223,22 @@ func TestAccKubernetesStatefulSetV1_Update(t *testing.T) {
222223
resource.TestCheckResourceAttr(resourceName, "spec.0.replicas", "0"),
223224
),
224225
},
226+
{
227+
Config: testAccKubernetesStatefulSetV1ConfigUpdateMinReadySeconds(name, imageName, 10),
228+
Check: resource.ComposeAggregateTestCheckFunc(
229+
testAccCheckKubernetesStatefulSetV1Exists(resourceName, &conf),
230+
resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name),
231+
resource.TestCheckResourceAttr(resourceName, "spec.0.min_ready_seconds", "10"),
232+
),
233+
},
234+
{
235+
Config: testAccKubernetesStatefulSetV1ConfigUpdateMinReadySeconds(name, imageName, 0),
236+
Check: resource.ComposeAggregateTestCheckFunc(
237+
testAccCheckKubernetesStatefulSetV1Exists(resourceName, &conf),
238+
resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name),
239+
resource.TestCheckResourceAttr(resourceName, "spec.0.min_ready_seconds", "0"),
240+
),
241+
},
225242
{
226243
Config: testAccKubernetesStatefulSetV1ConfigRollingUpdatePartition(name, imageName),
227244
Check: resource.ComposeAggregateTestCheckFunc(
@@ -522,6 +539,7 @@ func testAccKubernetesStatefulSetV1ConfigBasic(name, imageName string) string {
522539
}
523540
524541
spec {
542+
min_ready_seconds = 10
525543
pod_management_policy = "OrderedReady"
526544
replicas = 1
527545
revision_history_limit = 11
@@ -856,6 +874,92 @@ func testAccKubernetesStatefulSetV1ConfigUpdateReplicas(name, imageName, replica
856874
`, name, replicas, imageName)
857875
}
858876

877+
func testAccKubernetesStatefulSetV1ConfigUpdateMinReadySeconds(name string, imageName string, minReadySeconds int) string {
878+
return fmt.Sprintf(`resource "kubernetes_stateful_set_v1" "test" {
879+
metadata {
880+
annotations = {
881+
TestAnnotationOne = "one"
882+
TestAnnotationTwo = "two"
883+
}
884+
885+
labels = {
886+
TestLabelOne = "one"
887+
TestLabelTwo = "two"
888+
TestLabelThree = "three"
889+
}
890+
891+
name = "%s"
892+
}
893+
894+
spec {
895+
min_ready_seconds = %d
896+
pod_management_policy = "OrderedReady"
897+
replicas = 1
898+
revision_history_limit = 11
899+
900+
selector {
901+
match_labels = {
902+
app = "ss-test"
903+
}
904+
}
905+
906+
service_name = "ss-test-service"
907+
908+
template {
909+
metadata {
910+
labels = {
911+
app = "ss-test"
912+
}
913+
}
914+
915+
spec {
916+
container {
917+
name = "ss-test"
918+
image = %q
919+
args = ["pause"]
920+
921+
port {
922+
container_port = "80"
923+
name = "web"
924+
}
925+
926+
volume_mount {
927+
name = "ss-test"
928+
mount_path = "/work-dir"
929+
}
930+
}
931+
termination_grace_period_seconds = 1
932+
}
933+
}
934+
935+
update_strategy {
936+
type = "RollingUpdate"
937+
938+
rolling_update {
939+
partition = 1
940+
}
941+
}
942+
943+
volume_claim_template {
944+
metadata {
945+
name = "ss-test"
946+
}
947+
948+
spec {
949+
access_modes = ["ReadWriteOnce"]
950+
951+
resources {
952+
requests = {
953+
storage = "1Gi"
954+
}
955+
}
956+
}
957+
}
958+
}
959+
}
960+
`, name, minReadySeconds, imageName)
961+
}
962+
859963
func testAccKubernetesStatefulSetV1ConfigUpdateTemplate(name, imageName string) string {
860964
return fmt.Sprintf(`resource "kubernetes_stateful_set_v1" "test" {
861965
metadata {

kubernetes/schema_stateful_set_spec.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ func statefulSetSpecFields() map[string]*schema.Schema {
134134
},
135135
},
136136
},
137+
"min_ready_seconds": {
138+
Type: schema.TypeInt,
139+
Description: "Minimum number of seconds for which a newly created pod should be ready without any of its container crashing for it to be considered available. Defaults to 0. (pod will be considered available as soon as it is ready)",
140+
Optional: true,
141+
Default: 0,
142+
ValidateFunc: validateNonNegativeInteger,
143+
},
137144
}
138145
return s
139146
}

kubernetes/structures_stateful_set.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ func expandStatefulSetSpec(s []interface{}) (*v1.StatefulSetSpec, error) {
8383
obj.VolumeClaimTemplates = append(obj.VolumeClaimTemplates, *p)
8484
}
8585
}
86+
87+
if v, ok := in["min_ready_seconds"].(int); ok {
88+
obj.MinReadySeconds = int32(v)
89+
}
8690
return obj, nil
8791
}
8892
func expandStatefulSetSpecUpdateStrategy(s []interface{}) (*v1.StatefulSetUpdateStrategy, error) {
@@ -181,6 +185,7 @@ func flattenStatefulSetSpec(spec v1.StatefulSetSpec, d *schema.ResourceData, met
181185
att["persistent_volume_claim_retention_policy"] = flattenStatefulSetSpecPersistentVolumeClaimRetentionPolicy(*spec.PersistentVolumeClaimRetentionPolicy)
182186
}
183187

188+
att["min_ready_seconds"] = spec.MinReadySeconds
184189
return []interface{}{att}, nil
185190
}
186191

@@ -290,6 +295,17 @@ func patchStatefulSetSpec(d *schema.ResourceData) (PatchOperations, error) {
290295
})
291296
}
292297
}
298+
299+
if d.HasChange("spec.0.min_ready_seconds") {
300+
log.Printf("[TRACE] StatefulSet.Spec.MinReadySeconds has changes")
301+
if v, ok := d.Get("spec.0.min_ready_seconds").(int); ok {
302+
vv := int32(v)
303+
ops = append(ops, &ReplaceOperation{
304+
Path: "/spec/minReadySeconds",
305+
Value: vv,
306+
})
307+
}
308+
}
293309
return ops, nil
294310
}
295311

0 commit comments

Comments
 (0)