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 restart_policy option for init containers #2449

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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/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 @@
Schema: resourcesFieldV1(isUpdatable),
},
},
"restart_policy": {
Copy link
Contributor

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
Contributor

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,

Check failure on line 593 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.3.9)

undefined: isComputed

Check failure on line 593 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.6.0)

undefined: isComputed

Check failure on line 593 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.4.0)

undefined: isComputed

Check failure on line 593 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / unit_test

undefined: isComputed

Check failure on line 593 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.8.0)

undefined: isComputed

Check failure on line 593 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / checkers-and-linters

undefined: isComputed

Check failure on line 593 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / golang_linter

undefined: isComputed

Check failure on line 593 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / golang_linter

undefined: isComputed

Check failure on line 593 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / acceptance_tests_kind

undefined: isComputed

Check failure on line 593 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / acceptance_tests

undefined: isComputed
ForceNew: !isUpdatable,
Default: conditionalDefault(!isComputed, string(corev1.RestartPolicyAlways)),

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.3.9)

undefined: isComputed

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.3.9)

undefined: corev1

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.6.0)

undefined: isComputed

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.6.0)

undefined: corev1

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.4.0)

undefined: isComputed

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.4.0)

undefined: corev1

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / unit_test

undefined: isComputed

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / unit_test

undefined: corev1

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.8.0)

undefined: isComputed

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.8.0)

undefined: corev1

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / checkers-and-linters

undefined: isComputed

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / checkers-and-linters

undefined: corev1

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / golang_linter

undefined: isComputed

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / golang_linter

undefined: corev1

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / golang_linter

undefined: isComputed

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / acceptance_tests_kind

undefined: isComputed

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / acceptance_tests_kind

undefined: corev1

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / acceptance_tests

undefined: isComputed

Check failure on line 595 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / acceptance_tests

undefined: corev1
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),

Check failure on line 598 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.3.9)

undefined: corev1

Check failure on line 598 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.6.0)

undefined: corev1

Check failure on line 598 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.4.0)

undefined: corev1

Check failure on line 598 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / unit_test

undefined: corev1

Check failure on line 598 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.8.0)

undefined: corev1

Check failure on line 598 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / checkers-and-linters

undefined: corev1

Check failure on line 598 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / golang_linter

undefined: corev1

Check failure on line 598 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / acceptance_tests_kind

undefined: corev1

Check failure on line 598 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / acceptance_tests

undefined: corev1
string(corev1.RestartPolicyOnFailure),

Check failure on line 599 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.3.9)

undefined: corev1

Check failure on line 599 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.6.0)

undefined: corev1

Check failure on line 599 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.4.0)

undefined: corev1

Check failure on line 599 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / unit_test

undefined: corev1

Check failure on line 599 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.8.0)

undefined: corev1

Check failure on line 599 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / checkers-and-linters

undefined: corev1

Check failure on line 599 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / golang_linter

undefined: corev1

Check failure on line 599 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / acceptance_tests_kind

undefined: corev1

Check failure on line 599 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / acceptance_tests

undefined: corev1
string(corev1.RestartPolicyNever),

Check failure on line 600 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.3.9)

undefined: corev1

Check failure on line 600 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.6.0)

undefined: corev1

Check failure on line 600 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.4.0)

undefined: corev1

Check failure on line 600 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / unit_test

undefined: corev1

Check failure on line 600 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / check (1.8.0)

undefined: corev1

Check failure on line 600 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / checkers-and-linters

undefined: corev1

Check failure on line 600 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / golang_linter

undefined: corev1) (typecheck)

Check failure on line 600 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / acceptance_tests_kind

undefined: corev1

Check failure on line 600 in kubernetes/schema_container.go

View workflow job for this annotation

GitHub Actions / acceptance_tests

undefined: corev1
}, false),
},
"security_context": {
Type: schema.TypeList,
Optional: true,
Expand Down
Loading