|
| 1 | +/* |
| 2 | +Copyright 2025 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 clusterresourceplacementeviction |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/google/go-cmp/cmp" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + admissionv1 "k8s.io/api/admission/v1" |
| 28 | + authenticationv1 "k8s.io/api/authentication/v1" |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + "k8s.io/apimachinery/pkg/runtime" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 34 | + |
| 35 | + placementv1beta1 "github.com/kubefleet-dev/kubefleet/apis/placement/v1beta1" |
| 36 | + "github.com/kubefleet-dev/kubefleet/pkg/utils" |
| 37 | +) |
| 38 | + |
| 39 | +func TestHandle(t *testing.T) { |
| 40 | + validCRPEObject := &placementv1beta1.ClusterResourcePlacementEviction{ |
| 41 | + ObjectMeta: metav1.ObjectMeta{ |
| 42 | + Name: "test-crpe", |
| 43 | + }, |
| 44 | + Spec: placementv1beta1.PlacementEvictionSpec{ |
| 45 | + PlacementName: "test-crp", |
| 46 | + }, |
| 47 | + } |
| 48 | + invalidCRPEObjectInvalidPlacementName := &placementv1beta1.ClusterResourcePlacementEviction{ |
| 49 | + ObjectMeta: metav1.ObjectMeta{ |
| 50 | + Name: "test-crpe", |
| 51 | + }, |
| 52 | + Spec: placementv1beta1.PlacementEvictionSpec{ |
| 53 | + PlacementName: "does-not-exist", |
| 54 | + }, |
| 55 | + } |
| 56 | + invalidCRPEObjectCRPDeleting := &placementv1beta1.ClusterResourcePlacementEviction{ |
| 57 | + ObjectMeta: metav1.ObjectMeta{ |
| 58 | + Name: "test-crpe", |
| 59 | + }, |
| 60 | + Spec: placementv1beta1.PlacementEvictionSpec{ |
| 61 | + PlacementName: "crp-deleting", |
| 62 | + }, |
| 63 | + } |
| 64 | + invalidCRPEObjectInvalidPlacementType := &placementv1beta1.ClusterResourcePlacementEviction{ |
| 65 | + ObjectMeta: metav1.ObjectMeta{ |
| 66 | + Name: "test-crpe", |
| 67 | + }, |
| 68 | + Spec: placementv1beta1.PlacementEvictionSpec{ |
| 69 | + PlacementName: "crp-pickfixed", |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + validCRPEObjectBytes, err := json.Marshal(validCRPEObject) |
| 74 | + assert.Nil(t, err) |
| 75 | + invalidCRPEObjectInvalidPlacementNameBytes, err := json.Marshal(invalidCRPEObjectInvalidPlacementName) |
| 76 | + assert.Nil(t, err) |
| 77 | + invalidCRPEObjectCRPDeletingBytes, err := json.Marshal(invalidCRPEObjectCRPDeleting) |
| 78 | + assert.Nil(t, err) |
| 79 | + invalidCRPEObjectInvalidPlacementTypeBytes, err := json.Marshal(invalidCRPEObjectInvalidPlacementType) |
| 80 | + assert.Nil(t, err) |
| 81 | + |
| 82 | + validCRP := &placementv1beta1.ClusterResourcePlacement{ |
| 83 | + ObjectMeta: metav1.ObjectMeta{ |
| 84 | + Name: "test-crp", |
| 85 | + }, |
| 86 | + Spec: placementv1beta1.ClusterResourcePlacementSpec{ |
| 87 | + ResourceSelectors: []placementv1beta1.ClusterResourceSelector{}, |
| 88 | + Policy: &placementv1beta1.PlacementPolicy{ |
| 89 | + PlacementType: placementv1beta1.PickAllPlacementType, |
| 90 | + }, |
| 91 | + }, |
| 92 | + } |
| 93 | + invalidCRPDeleting := &placementv1beta1.ClusterResourcePlacement{ |
| 94 | + ObjectMeta: metav1.ObjectMeta{ |
| 95 | + Name: "crp-deleting", |
| 96 | + DeletionTimestamp: &metav1.Time{ |
| 97 | + Time: time.Now().Add(10 * time.Minute), |
| 98 | + }, |
| 99 | + Finalizers: []string{placementv1beta1.ClusterResourcePlacementCleanupFinalizer}, |
| 100 | + }, |
| 101 | + Spec: placementv1beta1.ClusterResourcePlacementSpec{ |
| 102 | + ResourceSelectors: []placementv1beta1.ClusterResourceSelector{}, |
| 103 | + Policy: &placementv1beta1.PlacementPolicy{ |
| 104 | + PlacementType: placementv1beta1.PickAllPlacementType, |
| 105 | + }, |
| 106 | + }, |
| 107 | + } |
| 108 | + invalidCRPPickFixed := &placementv1beta1.ClusterResourcePlacement{ |
| 109 | + ObjectMeta: metav1.ObjectMeta{ |
| 110 | + Name: "crp-pickfixed", |
| 111 | + }, |
| 112 | + Spec: placementv1beta1.ClusterResourcePlacementSpec{ |
| 113 | + ResourceSelectors: []placementv1beta1.ClusterResourceSelector{}, |
| 114 | + Policy: &placementv1beta1.PlacementPolicy{ |
| 115 | + PlacementType: placementv1beta1.PickFixedPlacementType, |
| 116 | + ClusterNames: []string{"cluster1", "cluster2"}, |
| 117 | + }, |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + objects := []client.Object{validCRP, invalidCRPDeleting, invalidCRPPickFixed} |
| 122 | + scheme := runtime.NewScheme() |
| 123 | + err = placementv1beta1.AddToScheme(scheme) |
| 124 | + assert.Nil(t, err) |
| 125 | + decoder := admission.NewDecoder(scheme) |
| 126 | + assert.Nil(t, err) |
| 127 | + fakeClient := fake.NewClientBuilder(). |
| 128 | + WithScheme(scheme). |
| 129 | + WithObjects(objects...). |
| 130 | + Build() |
| 131 | + |
| 132 | + testCases := map[string]struct { |
| 133 | + req admission.Request |
| 134 | + resourceValidator clusterResourcePlacementEvictionValidator |
| 135 | + wantResponse admission.Response |
| 136 | + }{ |
| 137 | + "allow CRPE create": { |
| 138 | + req: admission.Request{ |
| 139 | + AdmissionRequest: admissionv1.AdmissionRequest{ |
| 140 | + Name: "test-crpe", |
| 141 | + Object: runtime.RawExtension{ |
| 142 | + Raw: validCRPEObjectBytes, |
| 143 | + Object: validCRPEObject, |
| 144 | + }, |
| 145 | + UserInfo: authenticationv1.UserInfo{ |
| 146 | + Username: "test-user", |
| 147 | + Groups: []string{"system:masters"}, |
| 148 | + }, |
| 149 | + RequestKind: &utils.ClusterResourcePlacementEvictionMetaGVK, |
| 150 | + Operation: admissionv1.Create, |
| 151 | + }, |
| 152 | + }, |
| 153 | + resourceValidator: clusterResourcePlacementEvictionValidator{ |
| 154 | + decoder: decoder, |
| 155 | + client: fakeClient, |
| 156 | + }, |
| 157 | + wantResponse: admission.Allowed("clusterResourcePlacementEviction has valid fields"), |
| 158 | + }, |
| 159 | + "deny CRPE create - invalid CRPE object": { |
| 160 | + req: admission.Request{ |
| 161 | + AdmissionRequest: admissionv1.AdmissionRequest{ |
| 162 | + Name: "test-crpe", |
| 163 | + Object: runtime.RawExtension{ |
| 164 | + Raw: invalidCRPEObjectInvalidPlacementNameBytes, |
| 165 | + Object: invalidCRPEObjectInvalidPlacementName, |
| 166 | + }, |
| 167 | + UserInfo: authenticationv1.UserInfo{ |
| 168 | + Username: "test-user", |
| 169 | + Groups: []string{"system:masters"}, |
| 170 | + }, |
| 171 | + RequestKind: &utils.ClusterResourcePlacementMetaGVK, |
| 172 | + Operation: admissionv1.Create, |
| 173 | + }, |
| 174 | + }, |
| 175 | + resourceValidator: clusterResourcePlacementEvictionValidator{ |
| 176 | + decoder: decoder, |
| 177 | + client: fakeClient, |
| 178 | + }, |
| 179 | + wantResponse: admission.Denied("clusterresourceplacements.placement.kubernetes-fleet.io \"does-not-exist\" not found"), |
| 180 | + }, |
| 181 | + "deny CRPE create - CRP is deleting": { |
| 182 | + req: admission.Request{ |
| 183 | + AdmissionRequest: admissionv1.AdmissionRequest{ |
| 184 | + Name: "test-crpe", |
| 185 | + Object: runtime.RawExtension{ |
| 186 | + Raw: invalidCRPEObjectCRPDeletingBytes, |
| 187 | + Object: invalidCRPEObjectCRPDeleting, |
| 188 | + }, |
| 189 | + UserInfo: authenticationv1.UserInfo{ |
| 190 | + Username: "test-user", |
| 191 | + Groups: []string{"system:masters"}, |
| 192 | + }, |
| 193 | + RequestKind: &utils.ClusterResourcePlacementEvictionMetaGVK, |
| 194 | + Operation: admissionv1.Create, |
| 195 | + }, |
| 196 | + }, |
| 197 | + resourceValidator: clusterResourcePlacementEvictionValidator{ |
| 198 | + decoder: decoder, |
| 199 | + client: fakeClient, |
| 200 | + }, |
| 201 | + wantResponse: admission.Denied("cluster resource placement crp-deleting is being deleted"), |
| 202 | + }, |
| 203 | + "deny CRPE create - CRP with PickFixed placement type": { |
| 204 | + req: admission.Request{ |
| 205 | + AdmissionRequest: admissionv1.AdmissionRequest{ |
| 206 | + Name: "test-crpe", |
| 207 | + Object: runtime.RawExtension{ |
| 208 | + Raw: invalidCRPEObjectInvalidPlacementTypeBytes, |
| 209 | + Object: invalidCRPEObjectInvalidPlacementType, |
| 210 | + }, |
| 211 | + UserInfo: authenticationv1.UserInfo{ |
| 212 | + Username: "test-user", |
| 213 | + Groups: []string{"system:masters"}, |
| 214 | + }, |
| 215 | + RequestKind: &utils.ClusterResourcePlacementEvictionMetaGVK, |
| 216 | + Operation: admissionv1.Create, |
| 217 | + }, |
| 218 | + }, |
| 219 | + resourceValidator: clusterResourcePlacementEvictionValidator{ |
| 220 | + decoder: decoder, |
| 221 | + client: fakeClient, |
| 222 | + }, |
| 223 | + wantResponse: admission.Denied("cluster resource placement policy type PickFixed is not supported"), |
| 224 | + }, |
| 225 | + } |
| 226 | + for testName, testCase := range testCases { |
| 227 | + t.Run(testName, func(t *testing.T) { |
| 228 | + gotResult := testCase.resourceValidator.Handle(context.Background(), testCase.req) |
| 229 | + if diff := cmp.Diff(testCase.wantResponse, gotResult); diff != "" { |
| 230 | + t.Errorf("ClusterResourcePlacementEvictionValidator Handle() mismatch (-want +got):\n%s", diff) |
| 231 | + } |
| 232 | + }) |
| 233 | + } |
| 234 | +} |
0 commit comments