Skip to content

Commit 3be3051

Browse files
committed
Adding test case for the webhook behavior change
Authored-by: Jordan Liggitt <[email protected]> Signed-off-by: Davanum Srinivas <[email protected]>
1 parent cf82fd7 commit 3be3051

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/testing/testcase.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"k8s.io/client-go/informers"
3939
"k8s.io/client-go/kubernetes"
4040
fakeclientset "k8s.io/client-go/kubernetes/fake"
41+
"k8s.io/utils/ptr"
4142
)
4243

4344
var matchEverythingRules = []registrationv1.RuleWithOperations{{
@@ -897,6 +898,40 @@ func NewMutatingTestCases(url *url.URL, configurationName string) []MutatingTest
897898
"mutation.webhook.admission.k8s.io/round_0_index_0": mutationAnnotationValue(configurationName, "invalidMutation", false),
898899
},
899900
},
901+
{
902+
Name: "match & invalid patch",
903+
Webhooks: []registrationv1.MutatingWebhook{{
904+
Name: "invalidPatch",
905+
ClientConfig: ccfgSVC("invalidPatch"),
906+
Rules: matchEverythingRules,
907+
NamespaceSelector: &metav1.LabelSelector{},
908+
ObjectSelector: &metav1.LabelSelector{},
909+
AdmissionReviewVersions: []string{"v1beta1"},
910+
}},
911+
ExpectStatusCode: http.StatusInternalServerError,
912+
ErrorContains: "unexpected end of JSON input",
913+
ExpectAnnotations: map[string]string{
914+
"mutation.webhook.admission.k8s.io/round_0_index_0": mutationAnnotationValue(configurationName, "invalidPatch", false),
915+
},
916+
},
917+
{
918+
Name: "match & invalid patch fail open",
919+
Webhooks: []registrationv1.MutatingWebhook{{
920+
Name: "invalidPatch",
921+
ClientConfig: ccfgSVC("invalidPatch"),
922+
Rules: matchEverythingRules,
923+
NamespaceSelector: &metav1.LabelSelector{},
924+
ObjectSelector: &metav1.LabelSelector{},
925+
AdmissionReviewVersions: []string{"v1beta1"},
926+
FailurePolicy: ptr.To(registrationv1.Ignore),
927+
}},
928+
ExpectAllow: true,
929+
ExpectStatusCode: http.StatusOK,
930+
ExpectAnnotations: map[string]string{
931+
"failed-open.mutation.webhook.admission.k8s.io/round_0_index_0": "invalidPatch",
932+
"mutation.webhook.admission.k8s.io/round_0_index_0": mutationAnnotationValue(configurationName, "invalidPatch", false),
933+
},
934+
},
900935
{
901936
Name: "match & remove label dry run unsupported",
902937
Webhooks: []registrationv1.MutatingWebhook{{

staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/testing/webhook_server.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
137137
Patch: []byte(`[{"op": "add", "path": "/metadata/labels/added", "value": "test"}]`),
138138
},
139139
})
140+
case "/invalidPatch":
141+
w.Header().Set("Content-Type", "application/json")
142+
pt := v1beta1.PatchTypeJSONPatch
143+
json.NewEncoder(w).Encode(&v1beta1.AdmissionReview{
144+
Response: &v1beta1.AdmissionResponse{
145+
Allowed: true,
146+
PatchType: &pt,
147+
Patch: []byte(`[{`),
148+
},
149+
})
140150
case "/invalidMutation":
141151
w.Header().Set("Content-Type", "application/json")
142152
pt := v1beta1.PatchTypeJSONPatch

0 commit comments

Comments
 (0)