|
| 1 | +/* |
| 2 | +Copyright 2026 The KubeFleet Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package pdb |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "errors" |
| 23 | + "fmt" |
| 24 | + "net/http" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/google/go-cmp/cmp" |
| 28 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 29 | + admissionv1 "k8s.io/api/admission/v1" |
| 30 | + authenticationv1 "k8s.io/api/authentication/v1" |
| 31 | + policyv1 "k8s.io/api/policy/v1" |
| 32 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 33 | + "k8s.io/apimachinery/pkg/runtime" |
| 34 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 35 | +) |
| 36 | + |
| 37 | +func TestHandle(t *testing.T) { |
| 38 | + scheme := runtime.NewScheme() |
| 39 | + if err := policyv1.AddToScheme(scheme); err != nil { |
| 40 | + t.Fatalf("policyv1.AddToScheme() = %v, want nil", err) |
| 41 | + } |
| 42 | + decoder := admission.NewDecoder(scheme) |
| 43 | + |
| 44 | + pdbInDefaultNS := &policyv1.PodDisruptionBudget{ |
| 45 | + ObjectMeta: metav1.ObjectMeta{ |
| 46 | + Name: "test-pdb", |
| 47 | + Namespace: "default", |
| 48 | + }, |
| 49 | + } |
| 50 | + pdbInFleetNS := &policyv1.PodDisruptionBudget{ |
| 51 | + ObjectMeta: metav1.ObjectMeta{ |
| 52 | + Name: "test-pdb", |
| 53 | + Namespace: "fleet-system", |
| 54 | + }, |
| 55 | + } |
| 56 | + pdbInKubeNS := &policyv1.PodDisruptionBudget{ |
| 57 | + ObjectMeta: metav1.ObjectMeta{ |
| 58 | + Name: "test-pdb", |
| 59 | + Namespace: "kube-system", |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + pdbInDefaultNSBytes, err := json.Marshal(pdbInDefaultNS) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("json.Marshal() = %v, want nil", err) |
| 66 | + } |
| 67 | + pdbInFleetNSBytes, err := json.Marshal(pdbInFleetNS) |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("json.Marshal() = %v, want nil", err) |
| 70 | + } |
| 71 | + pdbInKubeNSBytes, err := json.Marshal(pdbInKubeNS) |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("json.Marshal() = %v, want nil", err) |
| 74 | + } |
| 75 | + |
| 76 | + userInfo := authenticationv1.UserInfo{ |
| 77 | + Username: "test-user", |
| 78 | + Groups: []string{"system:authenticated"}, |
| 79 | + } |
| 80 | + |
| 81 | + testCases := map[string]struct { |
| 82 | + req admission.Request |
| 83 | + wantResponse admission.Response |
| 84 | + }{ |
| 85 | + "deny CREATE in non-reserved namespace": { |
| 86 | + req: admission.Request{ |
| 87 | + AdmissionRequest: admissionv1.AdmissionRequest{ |
| 88 | + Name: "test-pdb", |
| 89 | + Namespace: "default", |
| 90 | + Operation: admissionv1.Create, |
| 91 | + Object: runtime.RawExtension{ |
| 92 | + Raw: pdbInDefaultNSBytes, |
| 93 | + Object: pdbInDefaultNS, |
| 94 | + }, |
| 95 | + UserInfo: userInfo, |
| 96 | + }, |
| 97 | + }, |
| 98 | + wantResponse: admission.Denied(fmt.Sprintf(usrFriendlyDenialErrMsgFmt, "default", "test-pdb")), |
| 99 | + }, |
| 100 | + "allow CREATE in fleet- reserved namespace": { |
| 101 | + req: admission.Request{ |
| 102 | + AdmissionRequest: admissionv1.AdmissionRequest{ |
| 103 | + Name: "test-pdb", |
| 104 | + Namespace: "fleet-system", |
| 105 | + Operation: admissionv1.Create, |
| 106 | + Object: runtime.RawExtension{ |
| 107 | + Raw: pdbInFleetNSBytes, |
| 108 | + Object: pdbInFleetNS, |
| 109 | + }, |
| 110 | + UserInfo: userInfo, |
| 111 | + }, |
| 112 | + }, |
| 113 | + wantResponse: admission.Allowed(""), |
| 114 | + }, |
| 115 | + "allow CREATE in kube- reserved namespace": { |
| 116 | + req: admission.Request{ |
| 117 | + AdmissionRequest: admissionv1.AdmissionRequest{ |
| 118 | + Name: "test-pdb", |
| 119 | + Namespace: "kube-system", |
| 120 | + Operation: admissionv1.Create, |
| 121 | + Object: runtime.RawExtension{ |
| 122 | + Raw: pdbInKubeNSBytes, |
| 123 | + Object: pdbInKubeNS, |
| 124 | + }, |
| 125 | + UserInfo: userInfo, |
| 126 | + }, |
| 127 | + }, |
| 128 | + wantResponse: admission.Allowed(""), |
| 129 | + }, |
| 130 | + "allow UPDATE (non-CREATE operation)": { |
| 131 | + req: admission.Request{ |
| 132 | + AdmissionRequest: admissionv1.AdmissionRequest{ |
| 133 | + Name: "test-pdb", |
| 134 | + Namespace: "default", |
| 135 | + Operation: admissionv1.Update, |
| 136 | + UserInfo: userInfo, |
| 137 | + }, |
| 138 | + }, |
| 139 | + wantResponse: admission.Allowed(""), |
| 140 | + }, |
| 141 | + "allow DELETE (non-CREATE operation)": { |
| 142 | + req: admission.Request{ |
| 143 | + AdmissionRequest: admissionv1.AdmissionRequest{ |
| 144 | + Name: "test-pdb", |
| 145 | + Namespace: "default", |
| 146 | + Operation: admissionv1.Delete, |
| 147 | + UserInfo: userInfo, |
| 148 | + }, |
| 149 | + }, |
| 150 | + wantResponse: admission.Allowed(""), |
| 151 | + }, |
| 152 | + "error on malformed request object": { |
| 153 | + req: admission.Request{ |
| 154 | + AdmissionRequest: admissionv1.AdmissionRequest{ |
| 155 | + Name: "test-pdb", |
| 156 | + Namespace: "default", |
| 157 | + Operation: admissionv1.Create, |
| 158 | + Object: runtime.RawExtension{ |
| 159 | + Raw: []byte("not valid json"), |
| 160 | + }, |
| 161 | + UserInfo: userInfo, |
| 162 | + }, |
| 163 | + }, |
| 164 | + // The exact error message from the decoder is implementation-defined; |
| 165 | + // only the status code and allowed=false are compared. |
| 166 | + wantResponse: admission.Errored(http.StatusBadRequest, errors.New("")), |
| 167 | + }, |
| 168 | + } |
| 169 | + |
| 170 | + for testName, testCase := range testCases { |
| 171 | + t.Run(testName, func(t *testing.T) { |
| 172 | + v := pdbValidator{decoder: decoder} |
| 173 | + gotResponse := v.Handle(context.Background(), testCase.req) |
| 174 | + if diff := cmp.Diff(gotResponse, testCase.wantResponse, cmpopts.IgnoreFields(metav1.Status{}, "Message")); diff != "" { |
| 175 | + t.Errorf("Handle() mismatch (-got +want):\n%s", diff) |
| 176 | + } |
| 177 | + }) |
| 178 | + } |
| 179 | +} |
0 commit comments