@@ -17,86 +17,93 @@ import (
1717 crclient "sigs.k8s.io/controller-runtime/pkg/client"
1818)
1919
20- func EnsureAzureWorkloadIdentityWebhookMutation (t * testing.T , ctx context.Context , guestClient crclient.Client ) {
21- t .Run ("EnsureAzureWorkloadIdentityWebhookMutation" , func (t * testing.T ) {
22- AtLeast (t , Version422 )
23- g := NewWithT (t )
20+ func ValidateAzureWorkloadIdentityWebhookMutation (t testing.TB , ctx context.Context , guestClient crclient.Client ) {
21+ g := NewWithT (t )
2422
25- nsName := fmt .Sprintf ("azure-wi-e2e-%d" , time .Now ().UnixNano ())
26- testNamespace := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : nsName }}
27- g .Expect (guestClient .Create (ctx , testNamespace )).To (Succeed (), "failed to create test namespace" )
23+ nsName := fmt .Sprintf ("azure-wi-e2e-%d" , time .Now ().UnixNano ())
24+ testNamespace := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : nsName }}
25+ g .Expect (guestClient .Create (ctx , testNamespace )).To (Succeed (), "failed to create test namespace" )
26+ defer func () {
27+ _ = guestClient .Delete (context .Background (), testNamespace )
28+ }()
2829
29- serviceAccount := & corev1.ServiceAccount {
30- ObjectMeta : metav1.ObjectMeta {
31- Name : "azure-wi-test-sa" ,
32- Namespace : nsName ,
33- Annotations : map [string ]string {
34- "azure.workload.identity/client-id" : "00000000-0000-0000-0000-000000000000" ,
35- },
30+ serviceAccount := & corev1.ServiceAccount {
31+ ObjectMeta : metav1.ObjectMeta {
32+ Name : "azure-wi-test-sa" ,
33+ Namespace : nsName ,
34+ Annotations : map [string ]string {
35+ "azure.workload.identity/client-id" : "00000000-0000-0000-0000-000000000000" ,
3636 },
37- }
38- g .Expect (guestClient .Create (ctx , serviceAccount )).To (Succeed (), "failed to create test service account" )
37+ },
38+ }
39+ g .Expect (guestClient .Create (ctx , serviceAccount )).To (Succeed (), "failed to create test service account" )
3940
40- pod := & corev1.Pod {
41- ObjectMeta : metav1.ObjectMeta {
42- Name : "azure-wi-webhook-test-pod" ,
43- Namespace : nsName ,
44- Labels : map [string ]string {
45- "azure.workload.identity/use" : "true" ,
46- },
41+ pod := & corev1.Pod {
42+ ObjectMeta : metav1.ObjectMeta {
43+ Name : "azure-wi-webhook-test-pod" ,
44+ Namespace : nsName ,
45+ Labels : map [string ]string {
46+ "azure.workload.identity/use" : "true" ,
4747 },
48- Spec : corev1. PodSpec {
49- ServiceAccountName : serviceAccount . Name ,
50- SecurityContext : & corev1. PodSecurityContext {
51- RunAsNonRoot : ptr . To ( true ),
52- SeccompProfile : & corev1. SeccompProfile {
53- Type : corev1 .SeccompProfileTypeRuntimeDefault ,
54- } ,
48+ },
49+ Spec : corev1. PodSpec {
50+ ServiceAccountName : serviceAccount . Name ,
51+ SecurityContext : & corev1. PodSecurityContext {
52+ RunAsNonRoot : ptr . To ( true ),
53+ SeccompProfile : & corev1.SeccompProfile {
54+ Type : corev1 . SeccompProfileTypeRuntimeDefault ,
5555 },
56- Containers : []corev1. Container {
57- {
58- Name : "app" ,
59- Image : "registry.k8s.io/pause:3.10 " ,
60- Command : [] string { " /pause" } ,
61- SecurityContext : & corev1. SecurityContext {
62- AllowPrivilegeEscalation : ptr . To ( false ),
63- Capabilities : & corev1. Capabilities {
64- Drop : [] corev1.Capability { "ALL" },
65- },
56+ },
57+ Containers : []corev1. Container {
58+ {
59+ Name : "app " ,
60+ Image : "registry.k8s.io /pause:3.10" ,
61+ Command : [] string { "/pause" },
62+ SecurityContext : & corev1. SecurityContext {
63+ AllowPrivilegeEscalation : ptr . To ( false ),
64+ Capabilities : & corev1.Capabilities {
65+ Drop : []corev1. Capability { "ALL" },
6666 },
6767 },
6868 },
69- RestartPolicy : corev1 .RestartPolicyNever ,
7069 },
71- }
72- g .Expect (guestClient .Create (ctx , pod )).To (Succeed (), "failed to create pod for webhook mutation test" )
70+ RestartPolicy : corev1 .RestartPolicyNever ,
71+ },
72+ }
73+ g .Expect (guestClient .Create (ctx , pod )).To (Succeed (), "failed to create pod for webhook mutation test" )
7374
74- EventuallyObject (
75- t ,
76- ctx ,
77- "Azure workload identity webhook to mutate test pod" ,
78- func (ctx context.Context ) (* corev1.Pod , error ) {
79- mutatedPod := & corev1.Pod {}
80- err := guestClient .Get (ctx , types.NamespacedName {Name : pod .Name , Namespace : pod .Namespace }, mutatedPod )
81- return mutatedPod , err
75+ EventuallyObject (
76+ t ,
77+ ctx ,
78+ "Azure workload identity webhook to mutate test pod" ,
79+ func (ctx context.Context ) (* corev1.Pod , error ) {
80+ mutatedPod := & corev1.Pod {}
81+ err := guestClient .Get (ctx , types.NamespacedName {Name : pod .Name , Namespace : pod .Namespace }, mutatedPod )
82+ return mutatedPod , err
83+ },
84+ []Predicate [* corev1.Pod ]{
85+ func (mutatedPod * corev1.Pod ) (bool , string , error ) {
86+ if hasProjectedTokenVolume (mutatedPod .Spec .Volumes ) {
87+ return true , "" , nil
88+ }
89+ return false , "expected projected service account token volume to be injected" , nil
8290 },
83- []Predicate [* corev1.Pod ]{
84- func (mutatedPod * corev1.Pod ) (bool , string , error ) {
85- if hasProjectedTokenVolume (mutatedPod .Spec .Volumes ) {
86- return true , "" , nil
87- }
88- return false , "expected projected service account token volume to be injected" , nil
89- },
90- func (mutatedPod * corev1.Pod ) (bool , string , error ) {
91- if hasAzureFederatedTokenEnv (mutatedPod .Spec .Containers ) {
92- return true , "" , nil
93- }
94- return false , "expected AZURE_FEDERATED_TOKEN_FILE env var in pod containers" , nil
95- },
91+ func (mutatedPod * corev1.Pod ) (bool , string , error ) {
92+ if hasAzureFederatedTokenEnv (mutatedPod .Spec .Containers ) {
93+ return true , "" , nil
94+ }
95+ return false , "expected AZURE_FEDERATED_TOKEN_FILE env var in pod containers" , nil
9696 },
97- WithTimeout (3 * time .Minute ),
98- WithInterval (5 * time .Second ),
99- )
97+ },
98+ WithTimeout (3 * time .Minute ),
99+ WithInterval (5 * time .Second ),
100+ )
101+ }
102+
103+ func EnsureAzureWorkloadIdentityWebhookMutation (t * testing.T , ctx context.Context , guestClient crclient.Client ) {
104+ t .Run ("EnsureAzureWorkloadIdentityWebhookMutation" , func (t * testing.T ) {
105+ AtLeast (t , Version422 )
106+ ValidateAzureWorkloadIdentityWebhookMutation (t , ctx , guestClient )
100107 })
101108}
102109
0 commit comments