@@ -30,6 +30,7 @@ func NewPodInterceptor(c client.Client, decoder admission.Decoder) webhook.Admis
30
30
31
31
// +kubebuilder:rbac:groups=ml.sigstore.dev,resources=modelvalidations,verbs=get;list;watch
32
32
// +kubebuilder:rbac:groups=ml.sigstore.dev,resources=modelvalidations/status,verbs=get;update;patch
33
+ // +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch
33
34
34
35
// podInterceptor extends pods with Model Validation Init-Container if annotation is specified.
35
36
type podInterceptor struct {
@@ -54,36 +55,31 @@ func (p *podInterceptor) Handle(ctx context.Context, req admission.Request) admi
54
55
logger .Error (err , "failed to get namespace" )
55
56
return admission .Errored (http .StatusInternalServerError , err )
56
57
}
57
- if ns .Labels ["validation.ml.sigstore.dev/ignore" ] == "true" {
58
+ if ns .Labels [constants . IgnoreNamespaceLabel ] == "true" {
58
59
logger .Info ("Namespace has ignore label, skipping" , "namespace" , req .Namespace )
59
60
return admission .Allowed ("namespace ignored" )
60
61
}
61
62
62
63
logger .Info ("Checking pod labels" , "labels" , pod .Labels )
63
- if v := pod .Labels ["validation.ml.sigstore.dev/ml" ]; v != "true" {
64
- logger .Info ("Validation label not found or not true" , "value" , v )
65
- return admission .Allowed ("no annotation found, no action needed" )
64
+ modelValidationName , ok := pod .Labels [constants .ModelValidationLabel ]
65
+ if ! ok || modelValidationName == "" {
66
+ logger .Info ("ModelValidation label not found or empty, skipping injection" )
67
+ return admission .Allowed ("no ModelValidation label found, no action needed" )
66
68
}
67
- logger .Info ("Validation label found, proceeding with injection" )
69
+ logger .Info ("ModelValidation label found, proceeding with injection" , "modelValidationName" , modelValidationName )
68
70
69
- logger .Info ("Search associated Model Validation CR" , "pod" , pod .Name , "namespace" , pod .Namespace )
70
- rhmvList := & v1alpha1.ModelValidationList {}
71
- if err := p .client .List (ctx , rhmvList ); err != nil {
72
- msg := "failed to get the ModelValidation Spec, skipping injection"
71
+ logger .Info ("Search associated Model Validation CR" , "pod" , pod .Name , "namespace" , pod .Namespace ,
72
+ "modelValidationName" , modelValidationName )
73
+ rhmv := & v1alpha1.ModelValidation {}
74
+ err := p .client .Get (ctx , client.ObjectKey {Name : modelValidationName , Namespace : pod .Namespace }, rhmv )
75
+ if err != nil {
76
+ msg := fmt .Sprintf ("failed to get the ModelValidation CR %s/%s" , pod .Namespace , modelValidationName )
73
77
logger .Error (err , msg )
74
- return admission .Errored (http .StatusNotFound , err )
78
+ return admission .Errored (http .StatusBadRequest , err ) // Fail deployment if CR not found
75
79
}
76
-
77
- got := len (rhmvList .Items )
78
- if got != 1 {
79
- err := fmt .Errorf ("got no or to many specs, expect: 1, got: %d" , got )
80
- logger .Error (err , "skip injection" )
81
- return admission .Errored (http .StatusBadRequest , err )
82
- }
83
- rhmv := rhmvList .Items [0 ]
84
80
// NOTE: check if validation sidecar is already injected. Then no action needed.
85
81
for _ , c := range pod .Spec .InitContainers {
86
- if c .Name == modelValidationInitContainerName {
82
+ if c .Name == constants . ModelValidationInitContainerName {
87
83
return admission .Allowed ("validation exists, no action needed" )
88
84
}
89
85
}
@@ -98,7 +94,7 @@ func (p *podInterceptor) Handle(ctx context.Context, req admission.Request) admi
98
94
vm = append (vm , c .VolumeMounts ... )
99
95
}
100
96
pp .Spec .InitContainers = append (pp .Spec .InitContainers , corev1.Container {
101
- Name : modelValidationInitContainerName ,
97
+ Name : constants . ModelValidationInitContainerName ,
102
98
ImagePullPolicy : corev1 .PullAlways ,
103
99
Image : constants .ModelTransparencyCliImage ,
104
100
Command : []string {"/usr/local/bin/model_signing" },
@@ -149,5 +145,3 @@ func validationConfigToArgs(logger logr.Logger, cfg v1alpha1.ValidationConfig, s
149
145
logger .Info ("missing validation config" )
150
146
return []string {}
151
147
}
152
-
153
- const modelValidationInitContainerName = "model-validation"
0 commit comments