Skip to content

Commit 418a9a1

Browse files
Merge pull request #12779 from sharifelgamal/gcp-auth-test2
Fix disabling gcp-auth addon and test
2 parents 0e8ae11 + 67f3149 commit 418a9a1

File tree

5 files changed

+46
-63
lines changed

5 files changed

+46
-63
lines changed

deploy/addons/gcp-auth/gcp-auth-webhook.yaml.tmpl

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2017 The Kubernetes Authors.
1+
# Copyright 2021 The Kubernetes Authors.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
---
1615
apiVersion: v1
1716
kind: ServiceAccount
1817
metadata:

pkg/addons/addons.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func addonSpecificChecks(cc *config.ClusterConfig, name string, enable bool, run
267267
}
268268

269269
// If the gcp-auth credentials haven't been mounted in, don't start the pods
270-
if name == "gcp-auth" {
270+
if name == "gcp-auth" && enable {
271271
rr, err := runner.RunCmd(exec.Command("cat", credentialsPath))
272272
if err != nil || rr.Stdout.String() == "" {
273273
return true, nil

pkg/addons/kubectl.go

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ func kubectlCommand(cc *config.ClusterConfig, files []string, enable bool) *exec
4141
}
4242

4343
args := []string{fmt.Sprintf("KUBECONFIG=%s", path.Join(vmpath.GuestPersistentDir, "kubeconfig")), kubectlBinary, kubectlAction}
44+
if !enable {
45+
// --ignore-not-found just ignores when we try to delete a resource that is already gone,
46+
// like a completed job with a ttlSecondsAfterFinished
47+
args = append(args, "--ignore-not-found")
48+
}
4449
for _, f := range files {
4550
args = append(args, []string{"-f", f}...)
4651
}

pkg/addons/kubectl_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestKubectlCommand(t *testing.T) {
3939
description: "disable an addon",
4040
files: []string{"a", "b"},
4141
enable: false,
42-
expected: "sudo KUBECONFIG=/var/lib/minikube/kubeconfig /var/lib/minikube/binaries/v1.17.0/kubectl delete -f a -f b",
42+
expected: "sudo KUBECONFIG=/var/lib/minikube/kubeconfig /var/lib/minikube/binaries/v1.17.0/kubectl delete --ignore-not-found -f a -f b",
4343
},
4444
}
4545

test/integration/addons_test.go

+38-59
Original file line numberDiff line numberDiff line change
@@ -47,62 +47,36 @@ func TestAddons(t *testing.T) {
4747
defer Cleanup(t, profile, cancel)
4848

4949
setupSucceeded := t.Run("Setup", func(t *testing.T) {
50-
// We don't need a dummy file is we're on GCE
51-
if !detect.IsOnGCE() || detect.IsCloudShell() {
52-
// Set an env var to point to our dummy credentials file
53-
err := os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", filepath.Join(*testdataDir, "gcp-creds.json"))
54-
t.Cleanup(func() {
55-
os.Unsetenv("GOOGLE_APPLICATION_CREDENTIALS")
56-
})
57-
if err != nil {
58-
t.Fatalf("Failed setting GOOGLE_APPLICATION_CREDENTIALS env var: %v", err)
59-
}
50+
// Set an env var to point to our dummy credentials file
51+
// don't use t.Setenv because we sometimes manually unset the env var later manually
52+
err := os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", filepath.Join(*testdataDir, "gcp-creds.json"))
53+
t.Cleanup(func() {
54+
os.Unsetenv("GOOGLE_APPLICATION_CREDENTIALS")
55+
})
56+
if err != nil {
57+
t.Fatalf("Failed setting GOOGLE_APPLICATION_CREDENTIALS env var: %v", err)
58+
}
6059

61-
err = os.Setenv("GOOGLE_CLOUD_PROJECT", "this_is_fake")
62-
t.Cleanup(func() {
63-
os.Unsetenv("GOOGLE_CLOUD_PROJECT")
64-
})
65-
if err != nil {
66-
t.Fatalf("Failed setting GOOGLE_CLOUD_PROJECT env var: %v", err)
67-
}
60+
err = os.Setenv("GOOGLE_CLOUD_PROJECT", "this_is_fake")
61+
t.Cleanup(func() {
62+
os.Unsetenv("GOOGLE_CLOUD_PROJECT")
63+
})
64+
if err != nil {
65+
t.Fatalf("Failed setting GOOGLE_CLOUD_PROJECT env var: %v", err)
6866
}
6967

70-
args := append([]string{"start", "-p", profile, "--wait=true", "--memory=4000", "--alsologtostderr", "--addons=registry", "--addons=metrics-server", "--addons=olm", "--addons=volumesnapshots", "--addons=csi-hostpath-driver"}, StartArgs()...)
68+
args := append([]string{"start", "-p", profile, "--wait=true", "--memory=4000", "--alsologtostderr", "--addons=registry", "--addons=metrics-server", "--addons=olm", "--addons=volumesnapshots", "--addons=csi-hostpath-driver", "--addons=gcp-auth"}, StartArgs()...)
7169
if !NoneDriver() { // none driver does not support ingress
7270
args = append(args, "--addons=ingress", "--addons=ingress-dns")
7371
}
7472
if !arm64Platform() {
7573
args = append(args, "--addons=helm-tiller")
7674
}
77-
if !detect.IsOnGCE() {
78-
args = append(args, "--addons=gcp-auth")
79-
}
8075
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
8176
if err != nil {
8277
t.Fatalf("%s failed: %v", rr.Command(), err)
8378
}
8479

85-
// If we're running the integration tests on GCE, which is frequently the case, first check to make sure we exit out properly,
86-
// then use force to actually test using creds.
87-
if detect.IsOnGCE() {
88-
args = []string{"-p", profile, "addons", "enable", "gcp-auth"}
89-
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
90-
if err != nil {
91-
t.Errorf("%s failed: %v", rr.Command(), err)
92-
} else {
93-
if !detect.IsCloudShell() && !strings.Contains(rr.Output(), "It seems that you are running in GCE") {
94-
t.Errorf("Unexpected error message: %v", rr.Output())
95-
} else {
96-
// ok, use force here since we are in GCE
97-
// do not use --force unless absolutely necessary
98-
args = append(args, "--force")
99-
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
100-
if err != nil {
101-
t.Errorf("%s failed: %v", rr.Command(), err)
102-
}
103-
}
104-
}
105-
}
10680
})
10781

10882
if !setupSucceeded {
@@ -669,15 +643,34 @@ func validateGCPAuthAddon(ctx context.Context, t *testing.T, profile string) {
669643

670644
got = strings.TrimSpace(rr.Stdout.String())
671645
expected = "this_is_fake"
672-
if detect.IsOnGCE() && !detect.IsCloudShell() {
673-
expected = "k8s-minikube"
674-
}
646+
675647
if got != expected {
676648
t.Errorf("'printenv GOOGLE_CLOUD_PROJECT' returned %s, expected %s", got, expected)
677649
}
678650

651+
disableGCPAuth := func() error {
652+
_, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "addons", "disable", "gcp-auth", "--alsologtostderr", "-v=1"))
653+
if err != nil {
654+
return err
655+
}
656+
return nil
657+
}
658+
659+
if err := retry.Expo(disableGCPAuth, Minutes(2), Minutes(10), 5); err != nil {
660+
t.Errorf("failed to disable GCP auth addon: %v", err)
661+
}
662+
679663
// If we're on GCE, we have proper credentials and can test the registry secrets with an artifact registry image
680664
if detect.IsOnGCE() && !detect.IsCloudShell() {
665+
os.Unsetenv("GOOGLE_APPLICATION_CREDENTIALS")
666+
os.Unsetenv("GOOGLE_CLOUD_PROJECT")
667+
args := []string{"-p", profile, "addons", "enable", "gcp-auth"}
668+
rr, err := Run(t, exec.CommandContext(ctx, Target(), args...))
669+
if err != nil {
670+
t.Errorf("%s failed: %v", rr.Command(), err)
671+
} else if !strings.Contains(rr.Output(), "It seems that you are running in GCE") {
672+
t.Errorf("Unexpected error message: %v", rr.Output())
673+
}
681674
_, err = Run(t, exec.CommandContext(ctx, "kubectl", "--context", profile, "apply", "-f", filepath.Join(*testdataDir, "private-image.yaml")))
682675
if err != nil {
683676
t.Fatalf("print env project: %v", err)
@@ -696,23 +689,9 @@ func validateGCPAuthAddon(ctx context.Context, t *testing.T, profile string) {
696689
t.Fatalf("print env project: %v", err)
697690
}
698691

699-
// Make sure the pod is up and running, which means we successfully pulled the private image down
700-
// 8 minutes, because 4 is not enough for images to pull in all cases.
701692
_, err = PodWait(ctx, t, profile, "default", "integration-test=private-image-eu", Minutes(8))
702693
if err != nil {
703694
t.Fatalf("wait for private image: %v", err)
704695
}
705696
}
706-
707-
disableGCPAuth := func() error {
708-
_, err = Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "addons", "disable", "gcp-auth", "--alsologtostderr", "-v=1"))
709-
if err != nil {
710-
return err
711-
}
712-
return nil
713-
}
714-
715-
if err := retry.Expo(disableGCPAuth, Minutes(2), Minutes(10), 5); err != nil {
716-
t.Errorf("failed to disable GCP auth addon: %v", err)
717-
}
718697
}

0 commit comments

Comments
 (0)