Skip to content

Commit cc098ea

Browse files
committed
adapted condition in webhook
Signed-off-by: odubajDT <[email protected]>
1 parent 46f8744 commit cc098ea

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

webhooks/common.go

+5
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ func shouldUseSidecar(annotations map[string]string) bool {
104104
return ok
105105
}
106106

107+
func shouldUseInProcess(annotations map[string]string) bool {
108+
_, ok := annotations[fmt.Sprintf("%s/%s", common.OpenFeatureAnnotationPrefix, common.InProcessConfigurationAnnotation)]
109+
return ok
110+
}
111+
107112
func (m *PodMutator) getFeatureFlagSource(ctx context.Context, namespace string, name string) (*api.FeatureFlagSource, error) {
108113
fcConfig := &api.FeatureFlagSource{}
109114
if err := m.Client.Get(ctx, client.ObjectKey{Name: name, Namespace: namespace}, fcConfig); err != nil {

webhooks/pod_webhook.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,12 @@ func (m *PodMutator) Handle(ctx context.Context, req admission.Request) admissio
8484
return admission.Errored(code, err)
8585
}
8686
}
87-
} else { // use in-process evaluation
87+
} else if shouldUseInProcess(annotations) { // use in-process evaluation
8888
if code, err := m.handleInProcessConfiguration(ctx, req, annotations, pod); err != nil {
8989
return admission.Errored(code, err)
9090
}
91+
} else {
92+
return admission.Denied("cannot mutate pods without 'featureflagsource' or 'inprocessconfiguration' annotation")
9193
}
9294

9395
marshaledPod, err := json.Marshal(pod)

webhooks/pod_webhook_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,21 @@ func TestPodMutator_Handle(t *testing.T) {
275275
goodInProcessAnnotatedPod, err := json.Marshal(inProcessPod)
276276
require.Nil(t, err)
277277

278+
missingAnnotationPod := corev1.Pod{
279+
ObjectMeta: metav1.ObjectMeta{
280+
Name: "myNotAnnotatedPod",
281+
Namespace: mutatePodNamespace,
282+
Annotations: map[string]string{
283+
fmt.Sprintf("%s/%s", common.OpenFeatureAnnotationPrefix, common.EnabledAnnotation): "true",
284+
},
285+
OwnerReferences: []metav1.OwnerReference{{UID: "123"}},
286+
},
287+
Spec: corev1.PodSpec{ServiceAccountName: defaultPodServiceAccountName},
288+
}
289+
290+
missingPod, err := json.Marshal(missingAnnotationPod)
291+
require.Nil(t, err)
292+
278293
tests := []struct {
279294
name string
280295
mutator *PodMutator
@@ -523,6 +538,47 @@ func TestPodMutator_Handle(t *testing.T) {
523538
},
524539
allow: true,
525540
},
541+
{
542+
name: "ofo enabled but annotation missing",
543+
mutator: &PodMutator{
544+
Client: NewClient(true,
545+
&inProcessPod,
546+
&corev1.ServiceAccount{
547+
ObjectMeta: metav1.ObjectMeta{
548+
Name: defaultPodServiceAccountName,
549+
Namespace: mutatePodNamespace,
550+
},
551+
},
552+
&rbac.ClusterRoleBinding{
553+
ObjectMeta: metav1.ObjectMeta{Name: common.ClusterRoleBindingName},
554+
Subjects: nil,
555+
RoleRef: rbac.RoleRef{},
556+
},
557+
),
558+
decoder: decoder,
559+
Log: testr.New(t),
560+
},
561+
req: admission.Request{
562+
AdmissionRequest: admissionv1.AdmissionRequest{
563+
UID: "123",
564+
Object: runtime.RawExtension{
565+
Raw: missingPod,
566+
Object: &missingAnnotationPod,
567+
},
568+
},
569+
},
570+
setup: func(mockInjector *flagdinjectorfake.MockFlagdContainerInjector) {
571+
mockInjector.EXPECT().
572+
InjectFlagd(
573+
gomock.Any(),
574+
gomock.Any(),
575+
gomock.Any(),
576+
gomock.Any(),
577+
).Return(nil).Times(0)
578+
},
579+
wantCode: http.StatusForbidden,
580+
allow: false,
581+
},
526582
{
527583
name: "wrong request",
528584
mutator: &PodMutator{

0 commit comments

Comments
 (0)