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

[TK-1957] Update container.resources schema for Pod, DaemonSet and Deployment #1889

Merged
merged 5 commits into from
Nov 18, 2022
Merged
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
15 changes: 15 additions & 0 deletions .changelog/1889.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```release-note:bug
Fix an issue when empty values of `spec.container.resources.limits` or `spec.container.resources.requests` produce continuous diff output during `plan` although no real changes were made. Affected resources: `kubernetes_pod`, `kubernetes_pod_v1`, `kubernetes_daemonset`, `kubernetes_daemon_set_v1`, `kubernetes_deployment`, `kubernetes_deployment_v1`.
```

```release-note:bug
Fix an issue when changing values of `spec.container.resources.limits` or `spec.container.resources.requests` does not update appropriate Kubernetes resources. Affected resources: `kubernetes_pod`, `kubernetes_pod_v1`.
```

```release-note:enhancement
`r/kubernetes_pod`: changing values of `spec.container.resources.limits` or `spec.container.resources.requests` will force resource recreation.
```

```release-note:enhancement
`r/kubernetes_pod_v1`: changing values of `spec.container.resources.limits` or `spec.container.resources.requests` will force resource recreation.
```
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ tests-lint: tools
@echo "==> Checking acceptance test terraform blocks code with terrafmt..."
@terrafmt diff -f ./kubernetes --check --pattern '*_test.go' --quiet || (echo; \
echo "Unexpected differences in acceptance test HCL formatting."; \
echo "To see the full differences, run: terrafmt diff ./kubnernetes --pattern '*_test.go'"; \
echo "To see the full differences, run: terrafmt diff ./kubernetes --pattern '*_test.go'"; \
echo "To automatically fix the formatting, run 'make tests-lint-fix' and commit the changes."; \
exit 1)

Expand Down
240 changes: 240 additions & 0 deletions kubernetes/resource_kubernetes_daemonset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,69 @@ func TestAccKubernetesDaemonSet_with_container_security_context_seccomp_profile(
})
}

func TestAccKubernetesDaemonSet_with_resource_requirements(t *testing.T) {
var conf appsv1.DaemonSet

daemonSetName := acctest.RandomWithPrefix("tf-acc-test")
imageName := nginxImageVersion
resourceName := "kubernetes_daemon_set_v1.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesDaemonSetDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesDaemonSetConfigWithResourceRequirements(daemonSetName, imageName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDaemonSetExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.image", imageName),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.resources.0.requests.memory", "50Mi"),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.resources.0.requests.cpu", "250m"),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.resources.0.limits.memory", "512Mi"),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.resources.0.limits.cpu", "500m"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"metadata.0.resource_version",
"wait_for_rollout",
},
},
{
Config: testAccKubernetesDaemonSetConfigWithEmptyResourceRequirements(daemonSetName, imageName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDaemonSetExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.image", imageName),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.resources.0.requests.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.resources.0.limits.#", "0"),
),
},
{
Config: testAccKubernetesDaemonSetConfigWithResourceRequirementsLimitsOnly(daemonSetName, imageName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDaemonSetExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.image", imageName),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.resources.0.limits.memory", "512Mi"),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.resources.0.limits.cpu", "500m"),
),
},
{
Config: testAccKubernetesDaemonSetConfigWithResourceRequirementsRequestsOnly(daemonSetName, imageName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesDaemonSetExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.image", imageName),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.resources.0.requests.memory", "512Mi"),
resource.TestCheckResourceAttr(resourceName, "spec.0.template.0.spec.0.container.0.resources.0.requests.cpu", "500m"),
),
},
},
})
}

func testAccCheckKubernetesDaemonsetForceNew(old, new *appsv1.DaemonSet, wantNew bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
if wantNew {
Expand Down Expand Up @@ -824,3 +887,180 @@ func testAccKubernetesDaemonSetConfigWithContainerSecurityContextSeccompProfileL
}
`, deploymentName, imageName)
}

func testAccKubernetesDaemonSetConfigWithResourceRequirements(deploymentName, imageName string) string {
return fmt.Sprintf(`resource "kubernetes_daemon_set_v1" "test" {
metadata {
name = "%s"

labels = {
Test = "TfAcceptanceTest"
}
}

spec {
selector {
match_labels = {
Test = "TfAcceptanceTest"
}
}

template {
metadata {
labels = {
Test = "TfAcceptanceTest"
}
}

spec {
container {
image = "%s"
name = "containername"

resources {
limits = {
cpu = "0.5"
memory = "512Mi"
"nvidia/gpu" = "1"
}

requests = {
cpu = "250m"
memory = "50Mi"
"nvidia/gpu" = "1"
}
}
}
}
}
}
}
`, deploymentName, imageName)
}

func testAccKubernetesDaemonSetConfigWithEmptyResourceRequirements(deploymentName, imageName string) string {
return fmt.Sprintf(`resource "kubernetes_daemon_set_v1" "test" {
metadata {
name = "%s"

labels = {
Test = "TfAcceptanceTest"
}
}

spec {
selector {
match_labels = {
Test = "TfAcceptanceTest"
}
}

template {
metadata {
labels = {
Test = "TfAcceptanceTest"
}
}

spec {
container {
image = "%s"
name = "containername"

resources {
limits = {}
requests = {}
}
}
}
}
}
}
`, deploymentName, imageName)
}

func testAccKubernetesDaemonSetConfigWithResourceRequirementsLimitsOnly(deploymentName, imageName string) string {
return fmt.Sprintf(`resource "kubernetes_daemon_set_v1" "test" {
metadata {
name = "%s"

labels = {
Test = "TfAcceptanceTest"
}
}

spec {
selector {
match_labels = {
Test = "TfAcceptanceTest"
}
}

template {
metadata {
labels = {
Test = "TfAcceptanceTest"
}
}

spec {
container {
image = "%s"
name = "containername"

resources {
limits = {
cpu = "500m"
memory = "512Mi"
}
}
}
}
}
}
}
`, deploymentName, imageName)
}

func testAccKubernetesDaemonSetConfigWithResourceRequirementsRequestsOnly(deploymentName, imageName string) string {
return fmt.Sprintf(`resource "kubernetes_daemon_set_v1" "test" {
metadata {
name = "%s"

labels = {
Test = "TfAcceptanceTest"
}
}

spec {
selector {
match_labels = {
Test = "TfAcceptanceTest"
}
}

template {
metadata {
labels = {
Test = "TfAcceptanceTest"
}
}

spec {
container {
image = "%s"
name = "containername"

resources {
requests = {
cpu = "500m"
memory = "512Mi"
}
}
}
}
}
}
}
`, deploymentName, imageName)
}
Loading