Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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/2449.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
`kubernetes/schema_container.go`: add support of `restart_policy` option for sidecar init containers
```
13 changes: 13 additions & 0 deletions kubernetes/schema_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,19 @@ func containerFields(isUpdatable bool) map[string]*schema.Schema {
Schema: resourcesFieldV1(isUpdatable),
},
},
"restart_policy": {
Copy link
Collaborator

Choose a reason for hiding this comment

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

From first glance be sure to include a test case for this new field. You also want to make sure to add a changelog-entry, this is mentioned in CHANGELOG_GUIDE

The changelog entry and new test should be added into the description of the PR also.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Although you added the changelog you are still missing a testcase that adds this. You would be adding this inside

func TestAccKubernetesDeploymentV1_initContainerForceNew(t *testing.T) {
var conf1, conf2 appsv1.Deployment
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
namespace := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
resourceName := "kubernetes_deployment_v1.test"
imageName := busyboxImage
imageName1 := agnhostImage
initCommand := "until nslookup " + name + "-init-service." + namespace + ".svc.cluster.local; do echo waiting for init-service; sleep 2; done"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshIgnore: []string{"metadata.0.resource_version"},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesDeploymentV1Destroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesConfig_ignoreAnnotations() +
testAccKubernetesDeploymentV1Config_initContainer(
namespace, name, imageName, imageName1, "64Mi", "testvar",
"initcontainer2", initCommand, "IfNotPresent"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDeploymentV1Exists(resourceName, &conf1),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.0.image", imageName),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.0.resources.0.requests.memory", "64Mi"),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.0.env.2.value", "testvar"),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.1.name", "initcontainer2"),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.1.image", imageName1),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.1.command.2", initCommand),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.1.image_pull_policy", "IfNotPresent"),
),
},
{ // Test for non-empty plans. No modification.
Config: testAccKubernetesConfig_ignoreAnnotations() +
testAccKubernetesDeploymentV1Config_initContainer(
namespace, name, imageName, imageName1, "64Mi", "testvar",
"initcontainer2", initCommand, "IfNotPresent"),
PlanOnly: true,
},
{ // Modify resources.limits.memory.
Config: testAccKubernetesConfig_ignoreAnnotations() +
testAccKubernetesDeploymentV1Config_initContainer(
namespace, name, imageName, imageName1, "80Mi", "testvar",
"initcontainer2", initCommand, "IfNotPresent"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDeploymentV1Exists(resourceName, &conf2),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.0.resources.0.requests.memory", "80Mi"),
testAccCheckKubernetesDeploymentForceNew(&conf1, &conf2, false),
),
},
{ // Modify name of environment variable.
Config: testAccKubernetesConfig_ignoreAnnotations() +
testAccKubernetesDeploymentV1Config_initContainer(
namespace, name, imageName, imageName1, "64Mi", "testvar",
"initcontainer2", initCommand, "IfNotPresent"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDeploymentV1Exists(resourceName, &conf2),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.0.env.2.value", "testvar"),
testAccCheckKubernetesDeploymentForceNew(&conf1, &conf2, false),
),
},
{ // Modify init_container's command.
Config: testAccKubernetesConfig_ignoreAnnotations() +
testAccKubernetesDeploymentV1Config_initContainer(
namespace, name, imageName, imageName1, "64Mi", "testvar",
"initcontainer2", "echo done", "IfNotPresent"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDeploymentV1Exists(resourceName, &conf2),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.1.command.2", "echo done"),
testAccCheckKubernetesDeploymentForceNew(&conf1, &conf2, false),
),
},
{ // Modify init_container's image_pull_policy.
Config: testAccKubernetesConfig_ignoreAnnotations() +
testAccKubernetesDeploymentV1Config_initContainer(
namespace, name, imageName, imageName1, "64Mi", "testvar",
"initcontainer2", "echo done", "Never"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDeploymentV1Exists(resourceName, &conf2),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.1.image_pull_policy", "Never"),
testAccCheckKubernetesDeploymentForceNew(&conf1, &conf2, false),
),
},
{ // Modify init_container's image
Config: testAccKubernetesConfig_ignoreAnnotations() +
testAccKubernetesDeploymentV1Config_initContainer(
namespace, name, imageName, imageName, "64Mi", "testvar",
"initcontainer2", "echo done", "Never"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDeploymentV1Exists(resourceName, &conf2),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.1.image", imageName),
testAccCheckKubernetesDeploymentForceNew(&conf1, &conf2, false),
),
},
{ // Modify init_container's name.
Config: testAccKubernetesConfig_ignoreAnnotations() +
testAccKubernetesDeploymentV1Config_initContainer(
namespace, name, imageName, imageName, "64Mi", "testvar",
"initcontainertwo", "echo done", "Never"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDeploymentV1Exists(resourceName, &conf2),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.init_container.1.name", "initcontainertwo"),
testAccCheckKubernetesDeploymentForceNew(&conf1, &conf2, false),
),
},
},
})
}

as well as

func TestAccKubernetesPodV1_initContainer_updateForcesNew(t *testing.T) {
var conf1, conf2 api.Pod
podName := acctest.RandomWithPrefix("tf-acc-test")
image := busyboxImage
image1 := agnhostImage
resourceName := "kubernetes_pod_v1.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesPodV1Destroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesConfig_ignoreAnnotations() +
testAccKubernetesPodV1ConfigWithInitContainer(podName, image),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesPodV1Exists(resourceName, &conf1),
resource.TestCheckResourceAttr(resourceName, "metadata.0.name", podName),
resource.TestCheckResourceAttr(resourceName, "spec.0.container.0.name", "container"),
resource.TestCheckResourceAttr(resourceName, "spec.0.init_container.0.name", "initcontainer"),
resource.TestCheckResourceAttr(resourceName, "spec.0.init_container.0.image", image),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"metadata.0.resource_version"},
},
{
Config: testAccKubernetesConfig_ignoreAnnotations() +
testAccKubernetesPodV1ConfigWithInitContainer(podName, image1),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesPodV1Exists(resourceName, &conf2),
resource.TestCheckResourceAttr(resourceName, "metadata.0.name", podName),
resource.TestCheckResourceAttr(resourceName, "spec.0.container.0.name", "container"),
resource.TestCheckResourceAttr(resourceName, "spec.0.init_container.0.name", "initcontainer"),
resource.TestCheckResourceAttr(resourceName, "spec.0.init_container.0.image", image1),
testAccCheckKubernetesPodForceNew(&conf1, &conf2, true),
),
},
},
})
}

Refer to the contribution guideline to learn how to run tests locally on your machine

Type: schema.TypeString,
Optional: true,
Computed: isComputed,
ForceNew: !isUpdatable,
Default: conditionalDefault(!isComputed, string(corev1.RestartPolicyAlways)),
Description: "Restart policy for init container. One of Always, OnFailure, Never. More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#restart-policy.",
ValidateFunc: validation.StringInSlice([]string{
string(corev1.RestartPolicyAlways),
string(corev1.RestartPolicyOnFailure),
string(corev1.RestartPolicyNever),
}, false),
},
"security_context": {
Type: schema.TypeList,
Optional: true,
Expand Down