Skip to content

Commit 384d2da

Browse files
authored
Add support for max_surge (#2630)
1 parent f4ed7af commit 384d2da

File tree

6 files changed

+150
-3
lines changed

6 files changed

+150
-3
lines changed

.changelog/2630.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:feature
2+
`resources_kubernetes_daemon_set_v1` : Added `max_surge` argument for to `rolling_update` block.
3+
```

docs/resources/daemon_set_v1.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,10 @@ Optional:
22572257

22582258
- `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.
22592259

2260+
- `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.
2261+
2262+
- `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.
2263+
22602264

22612265

22622266

docs/resources/daemonset.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,9 @@ Optional:
22572257

22582258
- `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.
22592259

2260+
- `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.
2261+
2262+
- `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.
22602263

22612264

22622265

kubernetes/resource_kubernetes_daemon_set_v1.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,19 @@ func resourceKubernetesDaemonSetSchemaV1() map[string]*schema.Schema {
104104
MaxItems: 1,
105105
Elem: &schema.Resource{
106106
Schema: map[string]*schema.Schema{
107+
"max_surge": {
108+
Type: schema.TypeString,
109+
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.",
110+
Optional: true,
111+
Default: 0,
112+
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^(0|[1-9][0-9]*|[1-9][0-9]%|100%)$`), ""),
113+
},
107114
"max_unavailable": {
108115
Type: schema.TypeString,
109-
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.",
116+
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.",
110117
Optional: true,
111118
Default: 1,
112-
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^([1-9][0-9]*|[1-9][0-9]%|[1-9]%|100%)$`), ""),
119+
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^(0|[1-9][0-9]*|[1-9][0-9]%|100%)$`), ""),
113120
},
114121
},
115122
},

kubernetes/resource_kubernetes_daemon_set_v1_test.go

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ func TestAccKubernetesDaemonSetV1_basic(t *testing.T) {
7373
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.image", imageName),
7474
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.name", "tf-acc-test"),
7575
resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.type", "RollingUpdate"),
76-
resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.rolling_update.0.max_unavailable", "1"),
7776
resource.TestCheckResourceAttr(resourceName, "wait_for_rollout", "true"),
7877
),
7978
},
@@ -433,6 +432,44 @@ func TestAccKubernetesDaemonSetV1_minimalWithTemplateNamespace(t *testing.T) {
433432
})
434433
}
435434

435+
func TestAccKubernetesDaemonSetV1_MaxSurge(t *testing.T) {
436+
var conf appsv1.DaemonSet
437+
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
438+
resourceName := "kubernetes_daemon_set_v1.test"
439+
imageName := busyboxImage
440+
441+
resource.ParallelTest(t, resource.TestCase{
442+
PreCheck: func() { testAccPreCheck(t) },
443+
IDRefreshName: resourceName,
444+
IDRefreshIgnore: []string{"metadata.0.resource_version"},
445+
ProviderFactories: testAccProviderFactories,
446+
CheckDestroy: testAccCheckKubernetesDaemonSetV1Destroy,
447+
Steps: []resource.TestStep{
448+
{
449+
Config: testAccKubernetesDaemonSetV1ConfigWithMaxSurge(name, imageName, "0"),
450+
Check: resource.ComposeAggregateTestCheckFunc(
451+
testAccCheckKubernetesDaemonSetV1Exists(resourceName, &conf),
452+
resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.rolling_update.0.max_surge", "0"),
453+
),
454+
},
455+
{
456+
Config: testAccKubernetesDaemonSetV1ConfigWithMaxSurge(name, imageName, "2"),
457+
Check: resource.ComposeAggregateTestCheckFunc(
458+
testAccCheckKubernetesDaemonSetV1Exists(resourceName, &conf),
459+
resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.rolling_update.0.max_surge", "2"),
460+
),
461+
},
462+
{
463+
Config: testAccKubernetesDaemonSetV1ConfigWithMaxSurge(name, imageName, "10%"),
464+
Check: resource.ComposeAggregateTestCheckFunc(
465+
testAccCheckKubernetesDaemonSetV1Exists(resourceName, &conf),
466+
resource.TestCheckResourceAttr(resourceName, "spec.0.strategy.0.rolling_update.0.max_surge", "10%"),
467+
),
468+
},
469+
},
470+
})
471+
}
472+
436473
func testAccCheckKubernetesDaemonSetV1Destroy(s *terraform.State) error {
437474
conn, err := testAccProvider.Meta().(KubeClientsets).MainClientset()
438475

@@ -504,6 +541,90 @@ func testAccCheckKubernetesDaemonSetV1ForceNew(old, new *appsv1.DaemonSet, wantN
504541
}
505542
}
506543

544+
func testAccKubernetesDaemonSetV1ConfigWithMaxSurge(name, imageName, maxSurge string) string {
545+
// If maxSurge is set to 0, maxUnavailable = 1
546+
if maxSurge == "0" {
547+
return fmt.Sprintf(`resource "kubernetes_daemon_set_v1" "test" {
548+
metadata {
549+
name = "%s"
550+
}
551+
552+
spec {
553+
selector {
554+
match_labels = {
555+
foo = "bar"
556+
}
557+
}
558+
559+
template {
560+
metadata {
561+
labels = {
562+
foo = "bar"
563+
}
564+
}
565+
566+
spec {
567+
container {
568+
image = "%s"
569+
name = "tf-acc-test"
570+
command = ["sleep", "300"]
571+
}
572+
termination_grace_period_seconds = 1
573+
}
574+
}
575+
576+
strategy {
577+
rolling_update {
578+
max_surge = "%s"
579+
max_unavailable = "1" # Set maxUnavailable to 1 if maxSurge is 0
580+
}
581+
}
582+
}
583+
}
584+
`, name, imageName, maxSurge)
585+
}
586+
587+
// If maxSurge is != 0
588+
return fmt.Sprintf(`resource "kubernetes_daemon_set_v1" "test" {
589+
metadata {
590+
name = "%s"
591+
}
592+
593+
spec {
594+
selector {
595+
match_labels = {
596+
foo = "bar"
597+
}
598+
}
599+
600+
template {
601+
metadata {
602+
labels = {
603+
foo = "bar"
604+
}
605+
}
606+
607+
spec {
608+
container {
609+
image = "%s"
610+
name = "tf-acc-test"
611+
command = ["sleep", "300"]
612+
}
613+
termination_grace_period_seconds = 1
614+
}
615+
}
616+
617+
strategy {
618+
rolling_update {
619+
max_surge = "%s"
620+
max_unavailable = "0" # Set maxUnavailable to 0 if maxSurge is set
621+
}
622+
}
623+
}
624+
}
625+
`, name, imageName, maxSurge)
626+
}
627+
507628
func testAccKubernetesDaemonSetV1Config_minimal(name, imageName string) string {
508629
return fmt.Sprintf(`resource "kubernetes_daemon_set_v1" "test" {
509630
metadata {

kubernetes/structures_daemonset.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ func flattenDaemonSetStrategy(in appsv1.DaemonSetUpdateStrategy) []interface{} {
4949

5050
func flattenDaemonSetStrategyRollingUpdate(in *appsv1.RollingUpdateDaemonSet) []interface{} {
5151
att := make(map[string]interface{})
52+
if in.MaxSurge != nil {
53+
att["max_surge"] = in.MaxSurge.String()
54+
}
55+
5256
if in.MaxUnavailable != nil {
5357
att["max_unavailable"] = in.MaxUnavailable.String()
5458
}
@@ -109,6 +113,11 @@ func expandRollingUpdateDaemonSet(p []interface{}) *appsv1.RollingUpdateDaemonSe
109113
}
110114
in := p[0].(map[string]interface{})
111115

116+
if v, ok := in["max_surge"].(string); ok {
117+
val := intstr.Parse(v)
118+
obj.MaxSurge = &val
119+
}
120+
112121
if v, ok := in["max_unavailable"].(string); ok {
113122
val := intstr.Parse(v)
114123
obj.MaxUnavailable = &val

0 commit comments

Comments
 (0)