|
| 1 | +package webhooks |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/google/go-cmp/cmp" |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + "k8s.io/apimachinery/pkg/api/resource" |
| 9 | + "k8s.io/utils/ptr" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGcloudSetupContainer(t *testing.T) { |
| 13 | + const ( |
| 14 | + workloadIdProvider = "projects/12345/locations/global/workloadIdentityPools/on-prem-kubernetes/providers/this-cluster" |
| 15 | + |
| 16 | + project = "project" |
| 17 | + gcloudImage = "google/cloud-sdk:slim" |
| 18 | + ) |
| 19 | + |
| 20 | + expectedTemplate := corev1.Container{ |
| 21 | + Name: "gcloud-setup", |
| 22 | + Image: gcloudImage, |
| 23 | + Command: []string{ |
| 24 | + "sh", "-c", |
| 25 | + `gcloud iam workload-identity-pools create-cred-config \ |
| 26 | + $(GCP_WORKLOAD_IDENTITY_PROVIDER) \ |
| 27 | + --service-account=$(GCP_SERVICE_ACCOUNT) \ |
| 28 | + --output-file=$(CLOUDSDK_CONFIG)/federation.json \ |
| 29 | + --credential-source-file=/var/run/secrets/sts.googleapis.com/serviceaccount/token |
| 30 | +gcloud auth login --cred-file=$(CLOUDSDK_CONFIG)/federation.json |
| 31 | +`, |
| 32 | + }, |
| 33 | + VolumeMounts: []corev1.VolumeMount{ |
| 34 | + { |
| 35 | + Name: "gcp-iam-token", |
| 36 | + MountPath: "/var/run/secrets/sts.googleapis.com/serviceaccount", |
| 37 | + ReadOnly: true, |
| 38 | + }, |
| 39 | + { |
| 40 | + Name: "gcloud-config", |
| 41 | + MountPath: "/var/run/secrets/gcloud/config", |
| 42 | + }, |
| 43 | + }, |
| 44 | + Env: []corev1.EnvVar{ |
| 45 | + { |
| 46 | + Name: "GCP_WORKLOAD_IDENTITY_PROVIDER", |
| 47 | + Value: workloadIdProvider, |
| 48 | + }, |
| 49 | + { |
| 50 | + Name: "GCP_SERVICE_ACCOUNT", |
| 51 | + Value: saEmail, |
| 52 | + }, |
| 53 | + { |
| 54 | + Name: "CLOUDSDK_CONFIG", |
| 55 | + Value: "/var/run/secrets/gcloud/config", |
| 56 | + }, |
| 57 | + { |
| 58 | + Name: "CLOUDSDK_CORE_PROJECT", |
| 59 | + Value: project, |
| 60 | + }, |
| 61 | + }, |
| 62 | + SecurityContext: &corev1.SecurityContext{ |
| 63 | + AllowPrivilegeEscalation: ptr.To(false), |
| 64 | + Capabilities: &corev1.Capabilities{ |
| 65 | + Drop: []corev1.Capability{ |
| 66 | + "ALL", |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + t.Run("Without runAsUser and resources", func(t *testing.T) { |
| 73 | + actual := gcloudSetupContainer(workloadIdProvider, saEmail, project, gcloudImage, nil, nil) |
| 74 | + expected := *expectedTemplate.DeepCopy() |
| 75 | + if diff := cmp.Diff(actual, expected); diff != "" { |
| 76 | + t.Errorf("gcloudSetupContainer() mismatch (-want +got):\n%s", diff) |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + t.Run("With runAsUser", func(t *testing.T) { |
| 81 | + user := int64(1000) |
| 82 | + actual := gcloudSetupContainer(workloadIdProvider, saEmail, project, gcloudImage, ptr.To(user), nil) |
| 83 | + |
| 84 | + expected := *expectedTemplate.DeepCopy() |
| 85 | + expected.SecurityContext.RunAsUser = ptr.To(user) |
| 86 | + |
| 87 | + if diff := cmp.Diff(actual, expected); diff != "" { |
| 88 | + t.Errorf("gcloudSetupContainer() mismatch (-want +got):\n%s", diff) |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + t.Run("With resources", func(t *testing.T) { |
| 93 | + resources := corev1.ResourceRequirements{ |
| 94 | + Requests: corev1.ResourceList{ |
| 95 | + corev1.ResourceCPU: resource.MustParse("100m"), |
| 96 | + }, |
| 97 | + } |
| 98 | + actual := gcloudSetupContainer(workloadIdProvider, saEmail, project, gcloudImage, nil, &resources) |
| 99 | + |
| 100 | + expected := *expectedTemplate.DeepCopy() |
| 101 | + expected.Resources = resources |
| 102 | + |
| 103 | + if diff := cmp.Diff(actual, expected); diff != "" { |
| 104 | + t.Errorf("gcloudSetupContainer() mismatch (-want +got):\n%s", diff) |
| 105 | + } |
| 106 | + }) |
| 107 | +} |
0 commit comments