@@ -25,20 +25,21 @@ import (
25
25
"context"
26
26
"encoding/json"
27
27
"fmt"
28
+ "os"
29
+ "strings"
30
+
28
31
"github.com/prometheus/client_golang/prometheus"
29
32
corev1 "k8s.io/api/core/v1"
30
33
apierrors "k8s.io/apimachinery/pkg/api/errors"
31
34
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32
35
"k8s.io/apimachinery/pkg/runtime"
33
36
"k8s.io/apimachinery/pkg/types"
34
37
"k8s.io/client-go/tools/record"
35
- "os"
36
38
ctrl "sigs.k8s.io/controller-runtime"
37
39
"sigs.k8s.io/controller-runtime/pkg/client"
38
40
logf "sigs.k8s.io/controller-runtime/pkg/log"
39
41
"sigs.k8s.io/controller-runtime/pkg/metrics"
40
42
"sigs.k8s.io/controller-runtime/pkg/webhook"
41
- "strings"
42
43
)
43
44
44
45
// nolint:unused
@@ -102,14 +103,27 @@ type PodCustomDefaulter struct {
102
103
103
104
var _ webhook.CustomDefaulter = & PodCustomDefaulter {}
104
105
106
+ // potentialPodName returns the pod name if available, otherwise a descriptive string
107
+ // This mirrors Istio's approach to handling generateName in admission webhooks
108
+ func potentialPodName (metadata metav1.ObjectMeta ) string {
109
+ if metadata .Name != "" {
110
+ return metadata .Name
111
+ }
112
+ if metadata .GenerateName != "" {
113
+ return metadata .GenerateName + "***** (actual name not yet known)"
114
+ }
115
+ return ""
116
+ }
117
+
105
118
// Default implements webhook.CustomDefaulter so a webhook will be registered for the Kind Pod.
106
119
func (d * PodCustomDefaulter ) Default (ctx context.Context , obj runtime.Object ) error {
107
120
pod , ok := obj .(* corev1.Pod )
108
121
if ! ok {
109
122
return fmt .Errorf ("expected an Pod object but got %T" , obj )
110
123
}
111
124
112
- podlog .Info ("Defaulting for Pod" , "name" , pod .GetName ())
125
+ podName := potentialPodName (pod .ObjectMeta )
126
+ podlog .Info ("Processing pod for WIF injection" , "pod" , podName , "namespace" , pod .Namespace )
113
127
114
128
// Skip if pod already has WIF volumes/env vars
115
129
if hasWorkloadIdentityConfig (pod ) {
@@ -160,7 +174,7 @@ func (d *PodCustomDefaulter) Default(ctx context.Context, obj runtime.Object) er
160
174
// Inject WIF configuration
161
175
injectWorkloadIdentityConfig (d , pod , wifConfig )
162
176
163
- podlog .Info ("Injected WIF configuration" , "pod" , pod .GetName () , "serviceAccount" , saName )
177
+ podlog .Info ("Injected WIF configuration" , "pod" , podName , "namespace" , pod .Namespace , "serviceAccount" , saName )
164
178
return nil
165
179
}
166
180
@@ -365,7 +379,7 @@ func injectWorkloadIdentityConfig(d *PodCustomDefaulter, pod *corev1.Pod, config
365
379
injectionOperations .WithLabelValues ("volume" , "injected" , "success" ).Inc ()
366
380
} else {
367
381
podlog .Info ("Skipped WIF injection due to existing volume" ,
368
- "pod" , pod .GetName ( ),
382
+ "pod" , potentialPodName ( pod .ObjectMeta ),
369
383
"namespace" , pod .Namespace ,
370
384
"component" , "volume" ,
371
385
"volume" , "token" ,
@@ -394,7 +408,7 @@ func injectWorkloadIdentityConfig(d *PodCustomDefaulter, pod *corev1.Pod, config
394
408
injectionOperations .WithLabelValues ("volume" , "injected" , "success" ).Inc ()
395
409
} else {
396
410
podlog .Info ("Skipped WIF injection due to existing volume" ,
397
- "pod" , pod .GetName ( ),
411
+ "pod" , potentialPodName ( pod .ObjectMeta ),
398
412
"namespace" , pod .Namespace ,
399
413
"component" , "volume" ,
400
414
"volume" , credentialsVolumeName ,
@@ -424,7 +438,7 @@ func injectWorkloadIdentityConfig(d *PodCustomDefaulter, pod *corev1.Pod, config
424
438
} else {
425
439
if volumeMountExists (container , "token" ) {
426
440
podlog .Info ("Skipped WIF injection due to existing volume mount" ,
427
- "pod" , pod .GetName ( ),
441
+ "pod" , potentialPodName ( pod .ObjectMeta ),
428
442
"namespace" , pod .Namespace ,
429
443
"container" , container .Name ,
430
444
"component" , "mount" ,
@@ -434,7 +448,7 @@ func injectWorkloadIdentityConfig(d *PodCustomDefaulter, pod *corev1.Pod, config
434
448
}
435
449
if mountPathExists (container , tokenMountPath ) {
436
450
podlog .Info ("Skipped WIF injection due to mount path conflict" ,
437
- "pod" , pod .GetName ( ),
451
+ "pod" , potentialPodName ( pod .ObjectMeta ),
438
452
"namespace" , pod .Namespace ,
439
453
"container" , container .Name ,
440
454
"component" , "mount" ,
@@ -457,7 +471,7 @@ func injectWorkloadIdentityConfig(d *PodCustomDefaulter, pod *corev1.Pod, config
457
471
} else {
458
472
if volumeMountExists (container , credentialsVolumeName ) {
459
473
podlog .Info ("Skipped WIF injection due to existing volume mount" ,
460
- "pod" , pod .GetName ( ),
474
+ "pod" , potentialPodName ( pod .ObjectMeta ),
461
475
"namespace" , pod .Namespace ,
462
476
"container" , container .Name ,
463
477
"component" , "mount" ,
@@ -467,7 +481,7 @@ func injectWorkloadIdentityConfig(d *PodCustomDefaulter, pod *corev1.Pod, config
467
481
}
468
482
if mountPathExists (container , credentialsMountPath ) {
469
483
podlog .Info ("Skipped WIF injection due to mount path conflict" ,
470
- "pod" , pod .GetName ( ),
484
+ "pod" , potentialPodName ( pod .ObjectMeta ),
471
485
"namespace" , pod .Namespace ,
472
486
"container" , container .Name ,
473
487
"component" , "mount" ,
@@ -486,7 +500,7 @@ func injectWorkloadIdentityConfig(d *PodCustomDefaulter, pod *corev1.Pod, config
486
500
injectionOperations .WithLabelValues ("env" , "injected" , "success" ).Inc ()
487
501
} else {
488
502
podlog .Info ("Skipped WIF injection due to existing environment variable" ,
489
- "pod" , pod .GetName ( ),
503
+ "pod" , potentialPodName ( pod .ObjectMeta ),
490
504
"namespace" , pod .Namespace ,
491
505
"container" , container .Name ,
492
506
"component" , "env" ,
0 commit comments