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

Fix ForceNew and non-empty plans caused by PodSpec defaults #1074

Merged
merged 1 commit into from
Dec 21, 2020
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
17 changes: 13 additions & 4 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ PKG_NAME := kubernetes
OS_ARCH := $(shell go env GOOS)_$(shell go env GOARCH)
TF_PROV_DOCS := $(PWD)/kubernetes/test-infra/tfproviderdocs
EXT_PROV_DIR := $(PWD)/kubernetes/test-infra/external-providers
EXT_PROV_BIN := /tmp/.terraform.d/localhost/test/kubernetes/9.9.9/$(OS_ARCH)/terraform-provider-kubernetes_9.9.9_$(OS_ARCH)

ifneq ($(PWD),$(PROVIDER_DIR))
$(error "Makefile must be run from the provider directory")
endif

default: build

all: build depscheck fmtcheck test testacc test-compile tests-lint tests-lint-fix tools vet website-lint website-lint-fix
all: build depscheck fmtcheck test test-update testacc test-compile tests-lint tests-lint-fix tools vet website-lint website-lint-fix

build: fmtcheck
go install
Expand Down Expand Up @@ -43,12 +44,11 @@ test: fmtcheck
echo $(TEST) | \
xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4

testacc: fmtcheck
testacc: fmtcheck vet
rm -rf $(EXT_PROV_DIR)/.terraform $(EXT_PROV_DIR)/.terraform.lock.hcl || true
mkdir $(EXT_PROV_DIR)/.terraform
mkdir -p /tmp/.terraform.d/localhost/test/kubernetes/9.9.9/$(OS_ARCH) || true
go clean -cache
go build -o /tmp/.terraform.d/localhost/test/kubernetes/9.9.9/$(OS_ARCH)/terraform-provider-kubernetes_9.9.9_$(OS_ARCH)
ls $(EXT_PROV_BIN) || go build -o $(EXT_PROV_BIN)
cd $(EXT_PROV_DIR) && TF_CLI_CONFIG_FILE=$(EXT_PROV_DIR)/.terraformrc TF_PLUGIN_CACHE_DIR=$(EXT_PROV_DIR)/.terraform terraform init -upgrade
TF_CLI_CONFIG_FILE=$(EXT_PROV_DIR)/.terraformrc TF_PLUGIN_CACHE_DIR=$(EXT_PROV_DIR)/.terraform TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m

Expand All @@ -73,6 +73,15 @@ tests-lint-fix: tools
@find ./kubernetes -name "*_test.go" -exec sed -i ':a;N;$$!ba;s/fmt.Sprintf(`\n/fmt.Sprintf(`/g' '{}' \; # remove newlines for terrafmt
@terrafmt fmt -f ./kubernetes --pattern '*_test.go'

test-update: fmtcheck vet
rm -rf $(EXT_PROV_DIR)/.terraform $(EXT_PROV_DIR)/.terraform.lock.hcl || true
mkdir $(EXT_PROV_DIR)/.terraform
mkdir -p /tmp/.terraform.d/localhost/test/kubernetes/9.9.9/$(OS_ARCH) || true
go clean -cache
go build -o /tmp/.terraform.d/localhost/test/kubernetes/9.9.9/$(OS_ARCH)/terraform-provider-kubernetes_9.9.9_$(OS_ARCH)
cd $(EXT_PROV_DIR) && TF_CLI_CONFIG_FILE=$(EXT_PROV_DIR)/.terraformrc TF_PLUGIN_CACHE_DIR=$(EXT_PROV_DIR)/.terraform terraform init -upgrade
TF_CLI_CONFIG_FILE=$(EXT_PROV_DIR)/.terraformrc TF_PLUGIN_CACHE_DIR=$(EXT_PROV_DIR)/.terraform TF_ACC=1 go test $(TEST) -v -run 'regression' $(TESTARGS)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change makes the testacc command faster. If you need to run update-related tests (the ones with _regression in the name, now you can run make test-update.

It also works with non-update related tests. Sometimes I like to do this if I've made schema changes that I want to be sure are in the test run:

TESTARGS="-run 'TestAccKubernetesJob'" make test-update

tools:
go install github.com/bflad/tfproviderdocs
go install github.com/client9/misspell/cmd/misspell
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ Please note: We take Terraform's security and our users' trust very seriously. I
## Requirements

- [Terraform](https://www.terraform.io/downloads.html) 0.12.x
- Note that version 0.11.x currently works, but is [deprecated](https://www.hashicorp.com/blog/deprecating-terraform-0-11-support-in-terraform-providers/)
- [Go](https://golang.org/doc/install) 1.14.x (to build the provider plugin)
- [Go](https://golang.org/doc/install) 1.15.x (to build the provider plugin)


## Kubernetes Alpha Provider
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/data_source_kubernetes_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func dataSourceKubernetesPod() *schema.Resource {
podSpecFields := podSpecFields(false, false, false)
podSpecFields := podSpecFields(false, false)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the isDeprecated field from the podSpecFields function because we're about to release a major update. The replication controller resource was the one using isDeprecrated, since originally the provider was created with a replication controller spec that didn't match the API. The issue was fixed in January 2019 (commit d5ff12c) and the affected resources have had deprecation warnings since then.

// Setting this default to false prevents a perpetual diff caused by volume_mounts
// being mutated on the server side as Kubernetes automatically adds a mount
// for the service account token
Expand Down
13 changes: 9 additions & 4 deletions kubernetes/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var testAccProviders map[string]*schema.Provider
// Global constants for testing images (reduces the number of docker pulls).
const (
nginxImageVersion = "nginx:1.19.4"
nginxImageVersion1 = "nginx:1.19.3"
busyboxImageVersion = "busybox:1.32.0"
busyboxImageVersion1 = "busybox:1.31"
alpineImageVersion = "alpine:3.12.1"
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added these after Docker Hub started rate-limiting pulls. I was hitting the limit with my local testing. (100 pulls per 6 hours). This is a much more polite way to use their service anyway 😄

var testAccProvider *schema.Provider
var testAccExternalProviders map[string]resource.ExternalProvider
var testAccProviderFactories = map[string]func() (*schema.Provider, error){
Expand All @@ -27,9 +35,6 @@ var testAccProviderFactories = map[string]func() (*schema.Provider, error){

func init() {
testAccProvider = Provider()
testAccProviders = map[string]*schema.Provider{
"kubernetes": testAccProvider,
}
testAccProviderFactories = map[string]func() (*schema.Provider, error){
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed since we're using ProviderFactories now instead.

"kubernetes": func() (*schema.Provider, error) {
return Provider(), nil
Expand Down
12 changes: 6 additions & 6 deletions kubernetes/resource_kubernetes_affinity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestAccKubernetesPod_with_node_affinity_with_required_during_scheduling_ignored_during_execution(t *testing.T) {
var conf api.Pod
podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
imageName := "nginx:1.7.9"
imageName := nginxImageVersion
keyName := "spec.0.affinity.0.node_affinity.0.required_during_scheduling_ignored_during_execution"

resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -45,7 +45,7 @@ func TestAccKubernetesPod_with_node_affinity_with_required_during_scheduling_ign
func TestAccKubernetesPod_with_node_affinity_with_preferred_during_scheduling_ignored_during_execution(t *testing.T) {
var conf api.Pod
podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
imageName := "nginx:1.7.9"
imageName := nginxImageVersion
keyName := "spec.0.affinity.0.node_affinity.0.preferred_during_scheduling_ignored_during_execution"

resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestAccKubernetesPod_with_node_affinity_with_preferred_during_scheduling_ig
func TestAccKubernetesPod_with_pod_affinity_with_required_during_scheduling_ignored_during_execution(t *testing.T) {
var conf api.Pod
podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
imageName := "nginx:1.7.9"
imageName := nginxImageVersion
keyName := "spec.0.affinity.0.pod_affinity.0.required_during_scheduling_ignored_during_execution"

resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestAccKubernetesPod_with_pod_affinity_with_required_during_scheduling_igno
func TestAccKubernetesPod_with_pod_affinity_with_preferred_during_scheduling_ignored_during_execution(t *testing.T) {
var conf api.Pod
podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
imageName := "nginx:1.7.9"
imageName := nginxImageVersion
keyName := "spec.0.affinity.0.pod_affinity.0.preferred_during_scheduling_ignored_during_execution"

resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestAccKubernetesPod_with_pod_affinity_with_preferred_during_scheduling_ign
func TestAccKubernetesPod_with_pod_anti_affinity_with_required_during_scheduling_ignored_during_execution(t *testing.T) {
var conf api.Pod
podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
imageName := "nginx:1.7.9"
imageName := nginxImageVersion
keyName := "spec.0.affinity.0.pod_anti_affinity.0.required_during_scheduling_ignored_during_execution"

resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestAccKubernetesPod_with_pod_anti_affinity_with_required_during_scheduling
func TestAccKubernetesPod_with_pod_anti_affinity_with_preferred_during_scheduling_ignored_during_execution(t *testing.T) {
var conf api.Pod
podName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
imageName := "nginx:1.7.9"
imageName := nginxImageVersion
keyName := "spec.0.affinity.0.pod_anti_affinity.0.preferred_during_scheduling_ignored_during_execution"

resource.Test(t, resource.TestCase{
Expand Down
36 changes: 19 additions & 17 deletions kubernetes/resource_kubernetes_cron_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
func TestAccKubernetesCronJob_basic(t *testing.T) {
var conf v1beta1.CronJob
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
imageName := alpineImageVersion

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -23,7 +24,7 @@ func TestAccKubernetesCronJob_basic(t *testing.T) {
CheckDestroy: testAccCheckKubernetesCronJobDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesCronJobConfig_basic(name),
Config: testAccKubernetesCronJobConfig_basic(name, imageName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesCronJobExists("kubernetes_cron_job.test", &conf),
resource.TestCheckResourceAttr("kubernetes_cron_job.test", "metadata.0.name", name),
Expand All @@ -42,11 +43,11 @@ func TestAccKubernetesCronJob_basic(t *testing.T) {
resource.TestCheckResourceAttr("kubernetes_cron_job.test", "spec.0.job_template.0.spec.0.parallelism", "1"),
resource.TestCheckResourceAttr("kubernetes_cron_job.test", "spec.0.job_template.0.spec.0.backoff_limit", "2"),
resource.TestCheckResourceAttr("kubernetes_cron_job.test", "spec.0.job_template.0.spec.0.template.0.spec.0.container.0.name", "hello"),
resource.TestCheckResourceAttr("kubernetes_cron_job.test", "spec.0.job_template.0.spec.0.template.0.spec.0.container.0.image", "alpine"),
resource.TestCheckResourceAttr("kubernetes_cron_job.test", "spec.0.job_template.0.spec.0.template.0.spec.0.container.0.image", imageName),
),
},
{
Config: testAccKubernetesCronJobConfig_modified(name),
Config: testAccKubernetesCronJobConfig_modified(name, imageName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesCronJobExists("kubernetes_cron_job.test", &conf),
resource.TestCheckResourceAttr("kubernetes_cron_job.test", "metadata.0.name", name),
Expand Down Expand Up @@ -75,6 +76,7 @@ func TestAccKubernetesCronJob_basic(t *testing.T) {
func TestAccKubernetesCronJob_extra(t *testing.T) {
var conf v1beta1.CronJob
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
imageName := alpineImageVersion

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -83,7 +85,7 @@ func TestAccKubernetesCronJob_extra(t *testing.T) {
CheckDestroy: testAccCheckKubernetesCronJobDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesCronJobConfig_extra(name),
Config: testAccKubernetesCronJobConfig_extra(name, imageName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesCronJobExists("kubernetes_cron_job.test", &conf),
resource.TestCheckResourceAttr("kubernetes_cron_job.test", "metadata.0.name", name),
Expand All @@ -96,7 +98,7 @@ func TestAccKubernetesCronJob_extra(t *testing.T) {
),
},
{
Config: testAccKubernetesCronJobConfig_extraModified(name),
Config: testAccKubernetesCronJobConfig_extraModified(name, imageName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesCronJobExists("kubernetes_cron_job.test", &conf),
resource.TestCheckResourceAttr("kubernetes_cron_job.test", "metadata.0.name", name),
Expand Down Expand Up @@ -169,7 +171,7 @@ func testAccCheckKubernetesCronJobExists(n string, obj *v1beta1.CronJob) resourc
}
}

func testAccKubernetesCronJobConfig_basic(name string) string {
func testAccKubernetesCronJobConfig_basic(name, imageName string) string {
return fmt.Sprintf(`resource "kubernetes_cron_job" "test" {
metadata {
name = "%s"
Expand All @@ -190,18 +192,18 @@ func testAccKubernetesCronJobConfig_basic(name string) string {
spec {
container {
name = "hello"
image = "alpine"
image = "%s"
command = ["echo", "'hello'"]
}
}
}
}
}
}
}`, name)
}`, name, imageName)
}

func testAccKubernetesCronJobConfig_modified(name string) string {
func testAccKubernetesCronJobConfig_modified(name, imageName string) string {
return fmt.Sprintf(`resource "kubernetes_cron_job" "test" {
metadata {
name = "%s"
Expand All @@ -221,18 +223,18 @@ func testAccKubernetesCronJobConfig_modified(name string) string {
spec {
container {
name = "hello"
image = "alpine"
image = "%s"
command = ["echo", "'hello'"]
}
}
}
}
}
}
}`, name)
}`, name, imageName)
}

func testAccKubernetesCronJobConfig_extra(name string) string {
func testAccKubernetesCronJobConfig_extra(name, imageName string) string {
return fmt.Sprintf(`resource "kubernetes_cron_job" "test" {
metadata {
name = "%s"
Expand All @@ -252,18 +254,18 @@ func testAccKubernetesCronJobConfig_extra(name string) string {
spec {
container {
name = "hello"
image = "alpine"
image = "%s"
command = ["echo", "'hello'"]
}
}
}
}
}
}
}`, name)
}`, name, imageName)
}

func testAccKubernetesCronJobConfig_extraModified(name string) string {
func testAccKubernetesCronJobConfig_extraModified(name, imageName string) string {
return fmt.Sprintf(`resource "kubernetes_cron_job" "test" {
metadata {
name = "%s"
Expand All @@ -283,13 +285,13 @@ func testAccKubernetesCronJobConfig_extraModified(name string) string {
spec {
container {
name = "hello"
image = "alpine"
image = "%s"
command = ["echo", "'hello'"]
}
}
}
}
}
}
}`, name)
}`, name, imageName)
}
Loading