Skip to content

Commit

Permalink
min_ready_seconds parameter support for statefulset (#2493)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Aug 12, 2024
1 parent c7aaa23 commit 9f4cabc
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/2493.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
resource/resource_kubernetes_stateful_set_v1: Add support for `min_ready_seconds`
```
2 changes: 2 additions & 0 deletions docs/resources/stateful_set_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Optional:
- `revision_history_limit` (Number) The maximum number of revisions that will be maintained in the StatefulSet's revision history. The default value is 10.
- `update_strategy` (Block List) The strategy that the StatefulSet controller will use to perform updates. (see [below for nested schema](#nestedblock--spec--update_strategy))
- `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))
- `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)

<a id="nestedblock--spec--selector"></a>
### Nested Schema for `spec.selector`
Expand Down Expand Up @@ -2361,6 +2362,7 @@ resource "kubernetes_stateful_set_v1" "prometheus" {
}
spec {
min_ready_seconds = 10
pod_management_policy = "Parallel"
replicas = 1
revision_history_limit = 5
Expand Down
104 changes: 104 additions & 0 deletions kubernetes/resource_kubernetes_stateful_set_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func TestAccKubernetesStatefulSetV1_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name),
resource.TestCheckResourceAttr(resourceName, "spec.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.replicas", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.min_ready_seconds", "10"),
resource.TestCheckResourceAttr(resourceName, "spec.0.revision_history_limit", "11"),
resource.TestCheckResourceAttr(resourceName, "spec.0.service_name", "ss-test-service"),
resource.TestCheckResourceAttr(resourceName, "spec.0.persistent_volume_claim_retention_policy.0.when_deleted", "Delete"),
Expand Down Expand Up @@ -222,6 +223,22 @@ func TestAccKubernetesStatefulSetV1_Update(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "spec.0.replicas", "0"),
),
},
{
Config: testAccKubernetesStatefulSetV1ConfigUpdateMinReadySeconds(name, imageName, 10),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesStatefulSetV1Exists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name),
resource.TestCheckResourceAttr(resourceName, "spec.0.min_ready_seconds", "10"),
),
},
{
Config: testAccKubernetesStatefulSetV1ConfigUpdateMinReadySeconds(name, imageName, 0),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesStatefulSetV1Exists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name),
resource.TestCheckResourceAttr(resourceName, "spec.0.min_ready_seconds", "0"),
),
},
{
Config: testAccKubernetesStatefulSetV1ConfigRollingUpdatePartition(name, imageName),
Check: resource.ComposeAggregateTestCheckFunc(
Expand Down Expand Up @@ -522,6 +539,7 @@ func testAccKubernetesStatefulSetV1ConfigBasic(name, imageName string) string {
}
spec {
min_ready_seconds = 10
pod_management_policy = "OrderedReady"
replicas = 1
revision_history_limit = 11
Expand Down Expand Up @@ -856,6 +874,92 @@ func testAccKubernetesStatefulSetV1ConfigUpdateReplicas(name, imageName, replica
`, name, replicas, imageName)
}

func testAccKubernetesStatefulSetV1ConfigUpdateMinReadySeconds(name string, imageName string, minReadySeconds int) string {
return fmt.Sprintf(`resource "kubernetes_stateful_set_v1" "test" {
metadata {
annotations = {
TestAnnotationOne = "one"
TestAnnotationTwo = "two"
}
labels = {
TestLabelOne = "one"
TestLabelTwo = "two"
TestLabelThree = "three"
}
name = "%s"
}
spec {
min_ready_seconds = %d
pod_management_policy = "OrderedReady"
replicas = 1
revision_history_limit = 11
selector {
match_labels = {
app = "ss-test"
}
}
service_name = "ss-test-service"
template {
metadata {
labels = {
app = "ss-test"
}
}
spec {
container {
name = "ss-test"
image = %q
args = ["pause"]
port {
container_port = "80"
name = "web"
}
volume_mount {
name = "ss-test"
mount_path = "/work-dir"
}
}
termination_grace_period_seconds = 1
}
}
update_strategy {
type = "RollingUpdate"
rolling_update {
partition = 1
}
}
volume_claim_template {
metadata {
name = "ss-test"
}
spec {
access_modes = ["ReadWriteOnce"]
resources {
requests = {
storage = "1Gi"
}
}
}
}
}
}
`, name, minReadySeconds, imageName)
}

func testAccKubernetesStatefulSetV1ConfigUpdateTemplate(name, imageName string) string {
return fmt.Sprintf(`resource "kubernetes_stateful_set_v1" "test" {
metadata {
Expand Down
7 changes: 7 additions & 0 deletions kubernetes/schema_stateful_set_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ func statefulSetSpecFields() map[string]*schema.Schema {
},
},
},
"min_ready_seconds": {
Type: schema.TypeInt,
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)",
Optional: true,
Default: 0,
ValidateFunc: validateNonNegativeInteger,
},
}
return s
}
16 changes: 16 additions & 0 deletions kubernetes/structures_stateful_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func expandStatefulSetSpec(s []interface{}) (*v1.StatefulSetSpec, error) {
obj.VolumeClaimTemplates = append(obj.VolumeClaimTemplates, *p)
}
}

if v, ok := in["min_ready_seconds"].(int); ok {
obj.MinReadySeconds = int32(v)
}
return obj, nil
}
func expandStatefulSetSpecUpdateStrategy(s []interface{}) (*v1.StatefulSetUpdateStrategy, error) {
Expand Down Expand Up @@ -181,6 +185,7 @@ func flattenStatefulSetSpec(spec v1.StatefulSetSpec, d *schema.ResourceData, met
att["persistent_volume_claim_retention_policy"] = flattenStatefulSetSpecPersistentVolumeClaimRetentionPolicy(*spec.PersistentVolumeClaimRetentionPolicy)
}

att["min_ready_seconds"] = spec.MinReadySeconds
return []interface{}{att}, nil
}

Expand Down Expand Up @@ -290,6 +295,17 @@ func patchStatefulSetSpec(d *schema.ResourceData) (PatchOperations, error) {
})
}
}

if d.HasChange("spec.0.min_ready_seconds") {
log.Printf("[TRACE] StatefulSet.Spec.MinReadySeconds has changes")
if v, ok := d.Get("spec.0.min_ready_seconds").(int); ok {
vv := int32(v)
ops = append(ops, &ReplaceOperation{
Path: "/spec/minReadySeconds",
Value: vv,
})
}
}
return ops, nil
}

Expand Down

0 comments on commit 9f4cabc

Please sign in to comment.