Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for max_surge #2630

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2630.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
`resources_kubernetes_daemon_set_v1` : Added `max_surge` argument for to `rolling_update` block.
```
4 changes: 4 additions & 0 deletions docs/resources/daemon_set_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2257,6 +2257,10 @@ Optional:

- `max_unavailable` (String) The maximum number of DaemonSet pods that can be unavailable during the update. Value can be an absolute number (ex: 5) or a percentage of total number of DaemonSet pods at the start of the update (ex: 10%). Absolute number is calculated from percentage by rounding up. This cannot be 0. Default value is 1. Example: when this is set to 30%, at most 30% of the total number of nodes that should be running the daemon pod (i.e. status.desiredNumberScheduled) can have their pods stopped for an update at any given time. The update starts by stopping at most 30% of those DaemonSet pods and then brings up new DaemonSet pods in their place. Once the new pods are available, it then proceeds onto other DaemonSet pods, thus ensuring that at least 70% of original number of DaemonSet pods are available at all times during the update.

- `max_surge` (String) The maximum number of nodes with an existing available DaemonSet pod that can have an updated DaemonSet pod during during an update. Value can be an absolute number (ex: 5) or a percentage of desired pods (ex: 10%). This can not be 0 if MaxUnavailable is 0. Absolute number is calculated from percentage by rounding up to a minimum of 1. Default value is 0. Example: when this is set to 30%, at most 30% of the total number of nodes that should be running the daemon pod (i.e. status.desiredNumberScheduled) can have their a new pod created before the old pod is marked as deleted. The update starts by launching new pods on 30% of nodes. Once an updated pod is available (Ready for at least minReadySeconds) the old DaemonSet pod on that node is marked deleted. If the old pod becomes unavailable for any reason Ready transitions to false, is evicted, or is drained) an updated pod is immediatedly created on that node without considering surge limits. Allowing surge implies the possibility that the resources consumed by the daemonset on any given node can double if the readiness check fails, and so resource intensive daemonsets should take into account that they may cause evictionsduring disruption.

- `max_unavailable` (String) The maximum number of DaemonSet pods that can be unavailable during the update. Value can be an absolute number (ex: 5) or a percentage of total number of DaemonSet pods at the start of the update (ex: 10%). Absolute number is calculated from percentage by rounding up. This cannot be 0 if MaxSurge is 0 Default value is 1. Example: when this is set to 30%, at most 30% of the total number of nodes that should be running the daemon pod (i.e. status.desiredNumberScheduled) can have their pods stopped for an update at any given time. The update starts by stopping at most 30% of those DaemonSet pods and then brings up new DaemonSet pods in their place. Once the new pods are available, it then proceeds onto other DaemonSet pods, thus ensuring that at least 70% of original number of DaemonSet pods are available at all times during the update.




Expand Down
3 changes: 3 additions & 0 deletions docs/resources/daemonset.md
Original file line number Diff line number Diff line change
Expand Up @@ -2257,6 +2257,9 @@ Optional:

- `max_unavailable` (String) The maximum number of DaemonSet pods that can be unavailable during the update. Value can be an absolute number (ex: 5) or a percentage of total number of DaemonSet pods at the start of the update (ex: 10%). Absolute number is calculated from percentage by rounding up. This cannot be 0. Default value is 1. Example: when this is set to 30%, at most 30% of the total number of nodes that should be running the daemon pod (i.e. status.desiredNumberScheduled) can have their pods stopped for an update at any given time. The update starts by stopping at most 30% of those DaemonSet pods and then brings up new DaemonSet pods in their place. Once the new pods are available, it then proceeds onto other DaemonSet pods, thus ensuring that at least 70% of original number of DaemonSet pods are available at all times during the update.

- `max_surge` (String) The maximum number of nodes with an existing available DaemonSet pod that can have an updated DaemonSet pod during during an update. Value can be an absolute number (ex: 5) or a percentage of desired pods (ex: 10%). This can not be 0 if MaxUnavailable is 0. Absolute number is calculated from percentage by rounding up to a minimum of 1. Default value is 0. Example: when this is set to 30%, at most 30% of the total number of nodes that should be running the daemon pod (i.e. status.desiredNumberScheduled) can have their a new pod created before the old pod is marked as deleted. The update starts by launching new pods on 30% of nodes. Once an updated pod is available (Ready for at least minReadySeconds) the old DaemonSet pod on that node is marked deleted. If the old pod becomes unavailable for any reason Ready transitions to false, is evicted, or is drained) an updated pod is immediatedly created on that node without considering surge limits. Allowing surge implies the possibility that the resources consumed by the daemonset on any given node can double if the readiness check fails, and so resource intensive daemonsets should take into account that they may cause evictionsduring disruption.

- `max_unavailable` (String) The maximum number of DaemonSet pods that can be unavailable during the update. Value can be an absolute number (ex: 5) or a percentage of total number of DaemonSet pods at the start of the update (ex: 10%). Absolute number is calculated from percentage by rounding up. This cannot be 0 if MaxSurge is 0 Default value is 1. Example: when this is set to 30%, at most 30% of the total number of nodes that should be running the daemon pod (i.e. status.desiredNumberScheduled) can have their pods stopped for an update at any given time. The update starts by stopping at most 30% of those DaemonSet pods and then brings up new DaemonSet pods in their place. Once the new pods are available, it then proceeds onto other DaemonSet pods, thus ensuring that at least 70% of original number of DaemonSet pods are available at all times during the update.



Expand Down
11 changes: 9 additions & 2 deletions kubernetes/resource_kubernetes_daemon_set_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,19 @@ func resourceKubernetesDaemonSetSchemaV1() map[string]*schema.Schema {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"max_surge": {
Type: schema.TypeString,
Description: "The maximum number of nodes with an existing available DaemonSet pod that can have an updated DaemonSet pod during during an update. Value can be an absolute number (ex: 5) or a percentage of desired pods (ex: 10%). This can not be 0 if MaxUnavailable is 0. Absolute number is calculated from percentage by rounding up to a minimum of 1. Default value is 0. Example: when this is set to 30%, at most 30% of the total number of nodes that should be running the daemon pod (i.e. status.desiredNumberScheduled) can have their a new pod created before the old pod is marked as deleted. The update starts by launching new pods on 30% of nodes. Once an updated pod is available (Ready for at least minReadySeconds) the old DaemonSet pod on that node is marked deleted. If the old pod becomes unavailable for any reason Ready transitions to false, is evicted, or is drained) an updated pod is immediatedly created on that node without considering surge limits. Allowing surge implies the possibility that the resources consumed by the daemonset on any given node can double if the readiness check fails, and so resource intensive daemonsets should take into account that they may cause evictionsduring disruption.",
Optional: true,
Default: 0,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^(0|[1-9][0-9]*|[1-9][0-9]%|100%)$`), ""),
},
"max_unavailable": {
Type: schema.TypeString,
Description: "The maximum number of DaemonSet pods that can be unavailable during the update. Value can be an absolute number (ex: 5) or a percentage of total number of DaemonSet pods at the start of the update (ex: 10%). Absolute number is calculated from percentage by rounding up. This cannot be 0. Default value is 1. Example: when this is set to 30%, at most 30% of the total number of nodes that should be running the daemon pod (i.e. status.desiredNumberScheduled) can have their pods stopped for an update at any given time. The update starts by stopping at most 30% of those DaemonSet pods and then brings up new DaemonSet pods in their place. Once the new pods are available, it then proceeds onto other DaemonSet pods, thus ensuring that at least 70% of original number of DaemonSet pods are available at all times during the update.",
Description: "The maximum number of DaemonSet pods that can be unavailable during the update. Value can be an absolute number (ex: 5) or a percentage of total number of DaemonSet pods at the start of the update (ex: 10%). Absolute number is calculated from percentage by rounding up. This cannot be 0 if MaxSurge is 0 Default value is 1. Example: when this is set to 30%, at most 30% of the total number of nodes that should be running the daemon pod (i.e. status.desiredNumberScheduled) can have their pods stopped for an update at any given time. The update starts by stopping at most 30% of those DaemonSet pods and then brings up new DaemonSet pods in their place. Once the new pods are available, it then proceeds onto other DaemonSet pods, thus ensuring that at least 70% of original number of DaemonSet pods are available at all times during the update.",
Optional: true,
Default: 1,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^([1-9][0-9]*|[1-9][0-9]%|[1-9]%|100%)$`), ""),
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^(0|[1-9][0-9]*|[1-9][0-9]%|100%)$`), ""),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this crashes and burns if you set anything from "1%" to "9%". At least it fails on 1% for us:

│ Error: expected value of spec.0.strategy.0.rolling_update.0.max_unavailable to match regular expression "^(0|[1-9][0-9]*|[1-9][0-9]%|100%)$", got 1%

},
},
},
Expand Down
123 changes: 122 additions & 1 deletion kubernetes/resource_kubernetes_daemon_set_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func TestAccKubernetesDaemonSetV1_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.image", imageName),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.name", "tf-acc-test"),
resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.type", "RollingUpdate"),
resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.rolling_update.0.max_unavailable", "1"),
resource.TestCheckResourceAttr(resourceName, "wait_for_rollout", "true"),
),
},
Expand Down Expand Up @@ -433,6 +432,44 @@ func TestAccKubernetesDaemonSetV1_minimalWithTemplateNamespace(t *testing.T) {
})
}

func TestAccKubernetesDaemonSetV1_MaxSurge(t *testing.T) {
var conf appsv1.DaemonSet
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
resourceName := "kubernetes_daemon_set_v1.test"
imageName := busyboxImage

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: resourceName,
IDRefreshIgnore: []string{"metadata.0.resource_version"},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesDaemonSetV1Destroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesDaemonSetV1ConfigWithMaxSurge(name, imageName, "0"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDaemonSetV1Exists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.rolling_update.0.max_surge", "0"),
),
},
{
Config: testAccKubernetesDaemonSetV1ConfigWithMaxSurge(name, imageName, "2"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDaemonSetV1Exists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.rolling_update.0.max_surge", "2"),
),
},
{
Config: testAccKubernetesDaemonSetV1ConfigWithMaxSurge(name, imageName, "10%"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDaemonSetV1Exists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.rolling_update.0.max_surge", "10%"),
),
},
},
})
}

func testAccCheckKubernetesDaemonSetV1Destroy(s *terraform.State) error {
conn, err := testAccProvider.Meta().(KubeClientsets).MainClientset()

Expand Down Expand Up @@ -504,6 +541,90 @@ func testAccCheckKubernetesDaemonSetV1ForceNew(old, new *appsv1.DaemonSet, wantN
}
}

func testAccKubernetesDaemonSetV1ConfigWithMaxSurge(name, imageName, maxSurge string) string {
// If maxSurge is set to 0, maxUnavailable = 1
if maxSurge == "0" {
return fmt.Sprintf(`resource "kubernetes_daemon_set_v1" "test" {
metadata {
name = "%s"
}

spec {
selector {
match_labels = {
foo = "bar"
}
}

template {
metadata {
labels = {
foo = "bar"
}
}

spec {
container {
image = "%s"
name = "tf-acc-test"
command = ["sleep", "300"]
}
termination_grace_period_seconds = 1
}
}

strategy {
rolling_update {
max_surge = "%s"
max_unavailable = "1" # Set maxUnavailable to 1 if maxSurge is 0
}
}
}
}
`, name, imageName, maxSurge)
}

// If maxSurge is != 0
return fmt.Sprintf(`resource "kubernetes_daemon_set_v1" "test" {
metadata {
name = "%s"
}

spec {
selector {
match_labels = {
foo = "bar"
}
}

template {
metadata {
labels = {
foo = "bar"
}
}

spec {
container {
image = "%s"
name = "tf-acc-test"
command = ["sleep", "300"]
}
termination_grace_period_seconds = 1
}
}

strategy {
rolling_update {
max_surge = "%s"
max_unavailable = "0" # Set maxUnavailable to 0 if maxSurge is set
}
}
}
}
`, name, imageName, maxSurge)
}

func testAccKubernetesDaemonSetV1Config_minimal(name, imageName string) string {
return fmt.Sprintf(`resource "kubernetes_daemon_set_v1" "test" {
metadata {
Expand Down
9 changes: 9 additions & 0 deletions kubernetes/structures_daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func flattenDaemonSetStrategy(in appsv1.DaemonSetUpdateStrategy) []interface{} {

func flattenDaemonSetStrategyRollingUpdate(in *appsv1.RollingUpdateDaemonSet) []interface{} {
att := make(map[string]interface{})
if in.MaxSurge != nil {
att["max_surge"] = in.MaxSurge.String()
}

if in.MaxUnavailable != nil {
att["max_unavailable"] = in.MaxUnavailable.String()
}
Expand Down Expand Up @@ -109,6 +113,11 @@ func expandRollingUpdateDaemonSet(p []interface{}) *appsv1.RollingUpdateDaemonSe
}
in := p[0].(map[string]interface{})

if v, ok := in["max_surge"].(string); ok {
val := intstr.Parse(v)
sheneska marked this conversation as resolved.
Show resolved Hide resolved
obj.MaxSurge = &val
}

if v, ok := in["max_unavailable"].(string); ok {
val := intstr.Parse(v)
obj.MaxUnavailable = &val
Expand Down
Loading