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 5 commits into
base: main
Choose a base branch
from

Conversation

arthurgur
Copy link

Description

Resolve #2446

Acceptance tests

  • Have you added an acceptance test for the functionality being added?
  • Have you run the acceptance tests on this branch?

Output from acceptance testing:

$ make testacc TESTARGS='-run=TestAccXXX'

...

Release Note

Release note for CHANGELOG:

...

References

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

@hashicorp-cla
Copy link

hashicorp-cla commented Mar 19, 2024

CLA assistant check
All committers have signed the CLA.

@arthurgur arthurgur marked this pull request as ready for review March 19, 2024 18:30
@arthurgur arthurgur requested a review from a team as a code owner March 19, 2024 18:30
@@ -587,6 +587,19 @@ func containerFields(isUpdatable bool) map[string]*schema.Schema {
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

@alemairebe
Copy link

@arthurgur do you need some help with the implementation of tests ?

@BBBmau
Copy link
Contributor

BBBmau commented Apr 24, 2024

@arthurgur any blockers that you're running into?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Init container doesn't support restart_policy option (to transform it in a sidecar container)
4 participants