Skip to content

Commit

Permalink
min_ready_seconds parameter added to statefulset
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Subramaniam <aayushsubramaniam@gmail.com>
  • Loading branch information
aayushsss1 committed May 16, 2024
1 parent 33b3ec0 commit 8ce72ae
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
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: "This is an optional field that specifies the minimum number of seconds for which a newly created Pod should be running and ready without any of its containers crashing, for it to be considered available. This field defaults to 0 (the 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 8ce72ae

Please sign in to comment.