Skip to content

Commit 01f6171

Browse files
authored
Fix restart_policy validation in Deployment (#2595)
Fix validation of restart_policy in Deployment's pod template
1 parent b4cf5b6 commit 01f6171

File tree

5 files changed

+80
-3
lines changed

5 files changed

+80
-3
lines changed

.changelog/2595.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/kubernetes_deployment_v1: Fix validation of `restart_policy` values
3+
```

.github/workflows/documentation-check.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ jobs:
3737
3838
- name: Undocumented changes
3939
run: |
40-
echo "Documentation is not up to date. Please refer to the `Making Changes` in the Contribution Guide on how to properly update documentation."
40+
echo 'Documentation is not up to date. Please refer to the `Making Changes` in the Contribution Guide on how to properly update documentation.'
4141
exit 1
4242
if: failure()

docs/resources/deployment_v1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Optional:
111111
- `os` (Block List, Max: 1) Specifies the OS of the containers in the pod. (see [below for nested schema](#nestedblock--spec--template--spec--os))
112112
- `priority_class_name` (String) If specified, indicates the pod's priority. "system-node-critical" and "system-cluster-critical" are two special keywords which indicate the highest priorities with the former being the highest priority. Any other name must be defined by creating a PriorityClass object with that name. If not specified, the pod priority will be default or zero if there is no default.
113113
- `readiness_gate` (Block List) If specified, all readiness gates will be evaluated for pod readiness. A pod is ready when all its containers are ready AND all conditions specified in the readiness gates have status equal to "True" More info: https://git.k8s.io/enhancements/keps/sig-network/0007-pod-ready%2B%2B.md (see [below for nested schema](#nestedblock--spec--template--spec--readiness_gate))
114-
- `restart_policy` (String) Restart policy for all containers within the pod. One of Always, OnFailure, Never. More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy.
114+
- `restart_policy` (String) Restart policy for all containers within the pod. Defaults to Always as the only option. More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy.
115115
- `runtime_class_name` (String) RuntimeClassName is a feature for selecting the container runtime configuration. The container runtime configuration is used to run a Pod's containers. More info: https://kubernetes.io/docs/concepts/containers/runtime-class
116116
- `scheduler_name` (String) If specified, the pod will be dispatched by specified scheduler. If not specified, the pod will be dispatched by default scheduler.
117117
- `security_context` (Block List, Max: 1) SecurityContext holds pod-level security attributes and common container settings. Optional: Defaults to empty (see [below for nested schema](#nestedblock--spec--template--spec--security_context))

kubernetes/resource_kubernetes_deployment_v1.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1717

1818
appsv1 "k8s.io/api/apps/v1"
19+
corev1 "k8s.io/api/core/v1"
1920
"k8s.io/apimachinery/pkg/api/errors"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2122
"k8s.io/apimachinery/pkg/types"
@@ -199,7 +200,7 @@ func resourceKubernetesDeploymentSchemaV1() map[string]*schema.Schema {
199200
Required: true,
200201
MaxItems: 1,
201202
Elem: &schema.Resource{
202-
Schema: podSpecFields(true, false),
203+
Schema: deploymentPodTemplateSpecFields(),
203204
},
204205
},
205206
},
@@ -217,6 +218,16 @@ func resourceKubernetesDeploymentSchemaV1() map[string]*schema.Schema {
217218
}
218219
}
219220

221+
func deploymentPodTemplateSpecFields() map[string]*schema.Schema {
222+
psf := podSpecFields(true, false)
223+
rp := psf["restart_policy"]
224+
rp.ValidateFunc = validation.StringInSlice([]string{
225+
string(corev1.RestartPolicyAlways),
226+
}, false)
227+
rp.Description = "Restart policy for all containers within the pod. Defaults to Always as the only option. More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy."
228+
return psf
229+
}
230+
220231
func resourceKubernetesDeploymentV1Create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
221232
conn, err := meta.(KubeClientsets).MainClientset()
222233
if err != nil {

kubernetes/resource_kubernetes_deployment_v1_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,36 @@ func TestAccKubernetesDeploymentV1_config_with_automount_service_account_token(t
12251225
})
12261226
}
12271227

1228+
func TestAccKubernetesDeploymentV1_with_restart_policy(t *testing.T) {
1229+
var conf appsv1.Deployment
1230+
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
1231+
resourceName := "kubernetes_deployment_v1.test"
1232+
imageName := busyboxImage
1233+
1234+
resource.ParallelTest(t, resource.TestCase{
1235+
PreCheck: func() { testAccPreCheck(t) },
1236+
IDRefreshName: resourceName,
1237+
IDRefreshIgnore: []string{"metadata.0.resource_version"},
1238+
ProviderFactories: testAccProviderFactories,
1239+
CheckDestroy: testAccCheckKubernetesDeploymentV1Destroy,
1240+
Steps: []resource.TestStep{
1241+
{
1242+
Config: testAccKubernetesDeploymentV1Config_with_restart_policy(name, imageName, "Never"),
1243+
ExpectError: regexp.MustCompile("expected spec\\.0\\.template\\.0\\.spec\\.0\\.restart_policy to be one of \\[\"Always\"\\], got Never"),
1244+
},
1245+
{
1246+
Config: testAccKubernetesDeploymentV1Config_with_restart_policy(name, imageName, "Always"),
1247+
Check: resource.ComposeAggregateTestCheckFunc(
1248+
testAccCheckKubernetesDeploymentV1Exists(resourceName, &conf),
1249+
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.generation"),
1250+
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.resource_version"),
1251+
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.uid"),
1252+
),
1253+
},
1254+
},
1255+
})
1256+
}
1257+
12281258
func testAccCheckKubernetesDeploymentForceNew(old, new *appsv1.Deployment, wantNew bool) resource.TestCheckFunc {
12291259
return func(s *terraform.State) error {
12301260
if wantNew {
@@ -1409,6 +1439,39 @@ func testAccKubernetesDeploymentV1Config_basic(name, imageName string) string {
14091439
`, name, imageName)
14101440
}
14111441

1442+
func testAccKubernetesDeploymentV1Config_with_restart_policy(name, imageName, restartPolicy string) string {
1443+
return fmt.Sprintf(`resource "kubernetes_deployment_v1" "test" {
1444+
metadata {
1445+
name = "%s"
1446+
}
1447+
spec {
1448+
replicas = 2
1449+
selector {
1450+
match_labels = {
1451+
TestLabelOne = "one"
1452+
}
1453+
}
1454+
template {
1455+
metadata {
1456+
labels = {
1457+
TestLabelOne = "one"
1458+
}
1459+
}
1460+
spec {
1461+
container {
1462+
image = "%s"
1463+
name = "tf-acc-test"
1464+
command = ["sleep", "300"]
1465+
}
1466+
restart_policy = "%s"
1467+
termination_grace_period_seconds = 1
1468+
}
1469+
}
1470+
}
1471+
}
1472+
`, name, imageName, restartPolicy)
1473+
}
1474+
14121475
func testAccKubernetesDeploymentV1Config_initContainer(namespace, name, imageName, imageName1, memory, envName, initName, initCommand, pullPolicy string) string {
14131476
return fmt.Sprintf(`resource "kubernetes_namespace_v1" "test" {
14141477
metadata {

0 commit comments

Comments
 (0)