| 1 |
/* |
| 2 |
Copyright 2024 The Kubernetes 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 mutating |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"github.com/google/go-cmp/cmp" |
| 22 |
"testing" |
| 23 |
"time" |
| 24 |
|
| 25 |
admissionregistrationv1 "k8s.io/api/admissionregistration/v1" |
| 26 |
"k8s.io/api/admissionregistration/v1alpha1" |
| 27 |
appsv1 "k8s.io/api/apps/v1" |
| 28 |
corev1 "k8s.io/api/core/v1" |
| 29 |
"k8s.io/apimachinery/pkg/api/equality" |
| 30 |
"k8s.io/apimachinery/pkg/api/meta" |
| 31 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 |
"k8s.io/apimachinery/pkg/runtime" |
| 33 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 34 |
"k8s.io/apimachinery/pkg/util/wait" |
| 35 |
"k8s.io/apiserver/pkg/admission" |
| 36 |
"k8s.io/apiserver/pkg/admission/plugin/policy/generic" |
| 37 |
"k8s.io/apiserver/pkg/admission/plugin/policy/matching" |
| 38 |
"k8s.io/apiserver/pkg/admission/plugin/policy/mutating/patch" |
| 39 |
"k8s.io/client-go/informers" |
| 40 |
"k8s.io/client-go/kubernetes/fake" |
| 41 |
"k8s.io/client-go/openapi/openapitest" |
| 42 |
"k8s.io/utils/ptr" |
| 43 |
) |
| 44 |
|
| 45 |
func TestDispatcher(t *testing.T) { |
| 46 |
deploymentGVK := schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"} |
| 47 |
deploymentGVR := schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"} |
| 48 |
testCases := []struct { |
| 49 |
name string |
| 50 |
object, oldObject runtime.Object |
| 51 |
gvk schema.GroupVersionKind |
| 52 |
gvr schema.GroupVersionResource |
| 53 |
params []runtime.Object // All params are expected to be ConfigMap for this test. |
| 54 |
policyHooks []PolicyHook |
| 55 |
expect runtime.Object |
| 56 |
}{ |
| 57 |
{ |
| 58 |
name: "simple patch", |
| 59 |
gvk: deploymentGVK, |
| 60 |
gvr: deploymentGVR, |
| 61 |
object: &appsv1.Deployment{ |
| 62 |
TypeMeta: metav1.TypeMeta{ |
| 63 |
Kind: "Deployment", |
| 64 |
APIVersion: "apps/v1", |
| 65 |
}, |
| 66 |
ObjectMeta: metav1.ObjectMeta{ |
| 67 |
Name: "d1", |
| 68 |
Namespace: "default", |
| 69 |
}, |
| 70 |
Spec: appsv1.DeploymentSpec{ |
| 71 |
Replicas: ptr.To[int32](1), |
| 72 |
Template: corev1.PodTemplateSpec{ |
| 73 |
Spec: corev1.PodSpec{ |
| 74 |
Volumes: []corev1.Volume{{Name: "x"}}, |
| 75 |
}, |
| 76 |
}, |
| 77 |
}}, |
| 78 |
policyHooks: []generic.PolicyHook[*Policy, *PolicyBinding, PolicyEvaluator]{ |
| 79 |
{ |
| 80 |
Policy: mutations(matchConstraints(policy("policy1"), &v1alpha1.MatchResources{ |
| 81 |
MatchPolicy: ptr.To(v1alpha1.Equivalent), |
| 82 |
NamespaceSelector: &metav1.LabelSelector{}, |
| 83 |
ObjectSelector: &metav1.LabelSelector{}, |
| 84 |
ResourceRules: []v1alpha1.NamedRuleWithOperations{ |
| 85 |
{ |
| 86 |
RuleWithOperations: v1alpha1.RuleWithOperations{ |
| 87 |
Rule: v1alpha1.Rule{ |
| 88 |
APIGroups: []string{"apps"}, |
| 89 |
APIVersions: []string{"v1"}, |
| 90 |
Resources: []string{"deployments"}, |
| 91 |
}, |
| 92 |
Operations: []admissionregistrationv1.OperationType{"*"}, |
| 93 |
}, |
| 94 |
}, |
| 95 |
}, |
| 96 |
}), v1alpha1.Mutation{ |
| 97 |
PatchType: v1alpha1.PatchTypeApplyConfiguration, |
| 98 |
ApplyConfiguration: &v1alpha1.ApplyConfiguration{ |
| 99 |
Expression: `Object{ |
| 100 |
spec: Object.spec{ |
| 101 |
replicas: object.spec.replicas + 100 |
| 102 |
} |
| 103 |
}`, |
| 104 |
}}), |
| 105 |
Bindings: []*PolicyBinding{{ |
| 106 |
ObjectMeta: metav1.ObjectMeta{Name: "binding"}, |
| 107 |
Spec: v1alpha1.MutatingAdmissionPolicyBindingSpec{ |
| 108 |
PolicyName: "policy1", |
| 109 |
}, |
| 110 |
}}, |
| 111 |
}, |
| 112 |
}, |
| 113 |
expect: &appsv1.Deployment{ |
| 114 |
ObjectMeta: metav1.ObjectMeta{ |
| 115 |
Name: "d1", |
| 116 |
Namespace: "default", |
| 117 |
}, |
| 118 |
Spec: appsv1.DeploymentSpec{ |
| 119 |
Replicas: ptr.To[int32](101), |
| 120 |
Template: corev1.PodTemplateSpec{ |
| 121 |
Spec: corev1.PodSpec{ |
| 122 |
Volumes: []corev1.Volume{{Name: "x"}}, |
| 123 |
}, |
| 124 |
}, |
| 125 |
Strategy: appsv1.DeploymentStrategy{ |
| 126 |
Type: appsv1.RollingUpdateDeploymentStrategyType, |
| 127 |
}, |
| 128 |
}}, |
| 129 |
}, |
| 130 |
{ |
| 131 |
name: "with param", |
| 132 |
gvk: deploymentGVK, |
| 133 |
gvr: deploymentGVR, |
| 134 |
object: &appsv1.Deployment{ |
| 135 |
TypeMeta: metav1.TypeMeta{ |
| 136 |
Kind: "Deployment", |
| 137 |
APIVersion: "apps/v1", |
| 138 |
}, |
| 139 |
ObjectMeta: metav1.ObjectMeta{ |
| 140 |
Name: "d1", |
| 141 |
Namespace: "default", |
| 142 |
}, |
| 143 |
Spec: appsv1.DeploymentSpec{ |
| 144 |
Replicas: ptr.To[int32](1), |
| 145 |
Template: corev1.PodTemplateSpec{ |
| 146 |
Spec: corev1.PodSpec{ |
| 147 |
Volumes: []corev1.Volume{{Name: "x"}}, |
| 148 |
}, |
| 149 |
}, |
| 150 |
Strategy: appsv1.DeploymentStrategy{ |
| 151 |
Type: appsv1.RollingUpdateDeploymentStrategyType, |
| 152 |
}, |
| 153 |
}}, |
| 154 |
params: []runtime.Object{ |
| 155 |
&corev1.ConfigMap{ |
| 156 |
TypeMeta: metav1.TypeMeta{ |
| 157 |
APIVersion: "v1", |
| 158 |
Kind: "ConfigMap", |
| 159 |
}, |
| 160 |
ObjectMeta: metav1.ObjectMeta{ |
| 161 |
Name: "cm1", |
| 162 |
Namespace: "default", |
| 163 |
}, |
| 164 |
Data: map[string]string{ |
| 165 |
"key": "10", |
| 166 |
}, |
| 167 |
}, |
| 168 |
}, |
| 169 |
policyHooks: []generic.PolicyHook[*Policy, *PolicyBinding, PolicyEvaluator]{ |
| 170 |
{ |
| 171 |
Policy: paramKind(mutations(matchConstraints(policy("policy1"), &v1alpha1.MatchResources{ |
| 172 |
MatchPolicy: ptr.To(v1alpha1.Equivalent), |
| 173 |
NamespaceSelector: &metav1.LabelSelector{}, |
| 174 |
ObjectSelector: &metav1.LabelSelector{}, |
| 175 |
ResourceRules: []v1alpha1.NamedRuleWithOperations{ |
| 176 |
{ |
| 177 |
RuleWithOperations: v1alpha1.RuleWithOperations{ |
| 178 |
Rule: v1alpha1.Rule{ |
| 179 |
APIGroups: []string{"apps"}, |
| 180 |
APIVersions: []string{"v1"}, |
| 181 |
Resources: []string{"deployments"}, |
| 182 |
}, |
| 183 |
Operations: []admissionregistrationv1.OperationType{"*"}, |
| 184 |
}, |
| 185 |
}, |
| 186 |
}}), |
| 187 |
v1alpha1.Mutation{ |
| 188 |
PatchType: v1alpha1.PatchTypeApplyConfiguration, |
| 189 |
ApplyConfiguration: &v1alpha1.ApplyConfiguration{ |
| 190 |
Expression: `Object{ |
| 191 |
spec: Object.spec{ |
| 192 |
replicas: object.spec.replicas + int(params.data['key']) |
| 193 |
} |
| 194 |
}`, |
| 195 |
}}), |
| 196 |
&v1alpha1.ParamKind{ |
| 197 |
APIVersion: "v1", |
| 198 |
Kind: "ConfigMap", |
| 199 |
}), |
| 200 |
Bindings: []*PolicyBinding{{ |
| 201 |
ObjectMeta: metav1.ObjectMeta{Name: "binding"}, |
| 202 |
Spec: v1alpha1.MutatingAdmissionPolicyBindingSpec{ |
| 203 |
PolicyName: "policy1", |
| 204 |
ParamRef: &v1alpha1.ParamRef{Name: "cm1", Namespace: "default"}, |
| 205 |
}, |
| 206 |
}}, |
| 207 |
}, |
| 208 |
}, |
| 209 |
expect: &appsv1.Deployment{ |
| 210 |
ObjectMeta: metav1.ObjectMeta{ |
| 211 |
Name: "d1", |
| 212 |
Namespace: "default", |
| 213 |
}, |
| 214 |
Spec: appsv1.DeploymentSpec{ |
| 215 |
Replicas: ptr.To[int32](11), |
| 216 |
Template: corev1.PodTemplateSpec{ |
| 217 |
Spec: corev1.PodSpec{ |
| 218 |
Volumes: []corev1.Volume{{Name: "x"}}, |
| 219 |
}, |
| 220 |
}, |
| 221 |
Strategy: appsv1.DeploymentStrategy{ |
| 222 |
Type: appsv1.RollingUpdateDeploymentStrategyType, |
| 223 |
}, |
| 224 |
}}, |
| 225 |
}, |
| 226 |
{ |
| 227 |
name: "both policies reinvoked", |
| 228 |
gvk: deploymentGVK, |
| 229 |
gvr: deploymentGVR, |
| 230 |
object: &appsv1.Deployment{ |
| 231 |
TypeMeta: metav1.TypeMeta{ |
| 232 |
Kind: "Deployment", |
| 233 |
APIVersion: "apps/v1", |
| 234 |
}, |
| 235 |
ObjectMeta: metav1.ObjectMeta{ |
| 236 |
Name: "d1", |
| 237 |
Namespace: "default", |
| 238 |
}, |
| 239 |
Spec: appsv1.DeploymentSpec{ |
| 240 |
Template: corev1.PodTemplateSpec{ |
| 241 |
Spec: corev1.PodSpec{ |
| 242 |
Volumes: []corev1.Volume{{Name: "x"}}, |
| 243 |
}, |
| 244 |
}, |
| 245 |
Strategy: appsv1.DeploymentStrategy{ |
| 246 |
Type: appsv1.RollingUpdateDeploymentStrategyType, |
| 247 |
}, |
| 248 |
}}, |
| 249 |
policyHooks: []generic.PolicyHook[*Policy, *PolicyBinding, PolicyEvaluator]{ |
| 250 |
{ |
| 251 |
Policy: mutations(matchConstraints(policy("policy1"), &v1alpha1.MatchResources{ |
| 252 |
MatchPolicy: ptr.To(v1alpha1.Equivalent), |
| 253 |
NamespaceSelector: &metav1.LabelSelector{}, |
| 254 |
ObjectSelector: &metav1.LabelSelector{}, |
| 255 |
ResourceRules: []v1alpha1.NamedRuleWithOperations{ |
| 256 |
{ |
| 257 |
RuleWithOperations: v1alpha1.RuleWithOperations{ |
| 258 |
Rule: v1alpha1.Rule{ |
| 259 |
APIGroups: []string{"apps"}, |
| 260 |
APIVersions: []string{"v1"}, |
| 261 |
Resources: []string{"deployments"}, |
| 262 |
}, |
| 263 |
Operations: []admissionregistrationv1.OperationType{"*"}, |
| 264 |
}, |
| 265 |
}, |
| 266 |
}, |
| 267 |
}), v1alpha1.Mutation{ |
| 268 |
PatchType: v1alpha1.PatchTypeApplyConfiguration, |
| 269 |
ApplyConfiguration: &v1alpha1.ApplyConfiguration{ |
| 270 |
Expression: `Object{ |
| 271 |
metadata: Object.metadata{ |
| 272 |
labels: {"policy1": string(int(object.?metadata.labels["count"].orValue("1")) + 1)} |
| 273 |
} |
| 274 |
}`, |
| 275 |
}}), |
| 276 |
Bindings: []*PolicyBinding{{ |
| 277 |
ObjectMeta: metav1.ObjectMeta{Name: "binding"}, |
| 278 |
Spec: v1alpha1.MutatingAdmissionPolicyBindingSpec{ |
| 279 |
PolicyName: "policy1", |
| 280 |
}, |
| 281 |
}}, |
| 282 |
}, |
| 283 |
{ |
| 284 |
Policy: mutations(matchConstraints(policy("policy2"), &v1alpha1.MatchResources{ |
| 285 |
MatchPolicy: ptr.To(v1alpha1.Equivalent), |
| 286 |
NamespaceSelector: &metav1.LabelSelector{}, |
| 287 |
ObjectSelector: &metav1.LabelSelector{}, |
| 288 |
ResourceRules: []v1alpha1.NamedRuleWithOperations{ |
| 289 |
{ |
| 290 |
RuleWithOperations: v1alpha1.RuleWithOperations{ |
| 291 |
Rule: v1alpha1.Rule{ |
| 292 |
APIGroups: []string{"apps"}, |
| 293 |
APIVersions: []string{"v1"}, |
| 294 |
Resources: []string{"deployments"}, |
| 295 |
}, |
| 296 |
Operations: []admissionregistrationv1.OperationType{"*"}, |
| 297 |
}, |
| 298 |
}, |
| 299 |
}, |
| 300 |
}), v1alpha1.Mutation{ |
| 301 |
PatchType: v1alpha1.PatchTypeApplyConfiguration, |
| 302 |
ApplyConfiguration: &v1alpha1.ApplyConfiguration{ |
| 303 |
Expression: `Object{ |
| 304 |
metadata: Object.metadata{ |
| 305 |
labels: {"policy2": string(int(object.?metadata.labels["count"].orValue("1")) + 1)} |
| 306 |
} |
| 307 |
}`, |
| 308 |
}}), |
| 309 |
Bindings: []*PolicyBinding{{ |
| 310 |
ObjectMeta: metav1.ObjectMeta{Name: "binding"}, |
| 311 |
Spec: v1alpha1.MutatingAdmissionPolicyBindingSpec{ |
| 312 |
PolicyName: "policy2", |
| 313 |
}, |
| 314 |
}}, |
| 315 |
}, |
| 316 |
}, |
| 317 |
expect: &appsv1.Deployment{ |
| 318 |
ObjectMeta: metav1.ObjectMeta{ |
| 319 |
Name: "d1", |
| 320 |
Namespace: "default", |
| 321 |
Labels: map[string]string{ |
| 322 |
"policy1": "2", |
| 323 |
"policy2": "2", |
| 324 |
}, |
| 325 |
}, |
| 326 |
Spec: appsv1.DeploymentSpec{ |
| 327 |
Template: corev1.PodTemplateSpec{ |
| 328 |
Spec: corev1.PodSpec{ |
| 329 |
Volumes: []corev1.Volume{{Name: "x"}}, |
| 330 |
}, |
| 331 |
}, |
| 332 |
Strategy: appsv1.DeploymentStrategy{ |
| 333 |
Type: appsv1.RollingUpdateDeploymentStrategyType, |
| 334 |
}, |
| 335 |
}}, |
| 336 |
}, |
| 337 |
{ |
| 338 |
name: "1st policy sets match condition that 2nd policy matches", |
| 339 |
gvk: deploymentGVK, |
| 340 |
gvr: deploymentGVR, |
| 341 |
object: &appsv1.Deployment{ |
| 342 |
TypeMeta: metav1.TypeMeta{ |
| 343 |
Kind: "Deployment", |
| 344 |
APIVersion: "apps/v1", |
| 345 |
}, |
| 346 |
ObjectMeta: metav1.ObjectMeta{ |
| 347 |
Name: "d1", |
| 348 |
Namespace: "default", |
| 349 |
}, |
| 350 |
Spec: appsv1.DeploymentSpec{ |
| 351 |
Template: corev1.PodTemplateSpec{ |
| 352 |
Spec: corev1.PodSpec{ |
| 353 |
Volumes: []corev1.Volume{{Name: "x"}}, |
| 354 |
}, |
| 355 |
}, |
| 356 |
Strategy: appsv1.DeploymentStrategy{ |
| 357 |
Type: appsv1.RollingUpdateDeploymentStrategyType, |
| 358 |
}, |
| 359 |
}}, |
| 360 |
policyHooks: []generic.PolicyHook[*Policy, *PolicyBinding, PolicyEvaluator]{ |
| 361 |
{ |
| 362 |
Policy: &v1alpha1.MutatingAdmissionPolicy{ |
| 363 |
ObjectMeta: metav1.ObjectMeta{ |
| 364 |
Name: "policy1", |
| 365 |
}, |
| 366 |
Spec: v1alpha1.MutatingAdmissionPolicySpec{ |
| 367 |
MatchConstraints: &v1alpha1.MatchResources{ |
| 368 |
MatchPolicy: ptr.To(v1alpha1.Equivalent), |
| 369 |
NamespaceSelector: &metav1.LabelSelector{}, |
| 370 |
ObjectSelector: &metav1.LabelSelector{}, |
| 371 |
ResourceRules: []v1alpha1.NamedRuleWithOperations{ |
| 372 |
{ |
| 373 |
RuleWithOperations: v1alpha1.RuleWithOperations{ |
| 374 |
Rule: v1alpha1.Rule{ |
| 375 |
APIGroups: []string{"apps"}, |
| 376 |
APIVersions: []string{"v1"}, |
| 377 |
Resources: []string{"deployments"}, |
| 378 |
}, |
| 379 |
Operations: []admissionregistrationv1.OperationType{"*"}, |
| 380 |
}, |
| 381 |
}, |
| 382 |
}, |
| 383 |
}, |
| 384 |
Mutations: []v1alpha1.Mutation{{ |
| 385 |
PatchType: v1alpha1.PatchTypeApplyConfiguration, |
| 386 |
ApplyConfiguration: &v1alpha1.ApplyConfiguration{ |
| 387 |
Expression: `Object{ |
| 388 |
metadata: Object.metadata{ |
| 389 |
labels: {"environment": "production"} |
| 390 |
} |
| 391 |
}`}}, |
| 392 |
}, |
| 393 |
}, |
| 394 |
}, |
| 395 |
Bindings: []*PolicyBinding{{ |
| 396 |
ObjectMeta: metav1.ObjectMeta{Name: "binding"}, |
| 397 |
Spec: v1alpha1.MutatingAdmissionPolicyBindingSpec{ |
| 398 |
PolicyName: "policy1", |
| 399 |
}, |
| 400 |
}}, |
| 401 |
}, |
| 402 |
{ |
| 403 |
Policy: &v1alpha1.MutatingAdmissionPolicy{ |
| 404 |
ObjectMeta: metav1.ObjectMeta{ |
| 405 |
Name: "policy2", |
| 406 |
}, |
| 407 |
Spec: v1alpha1.MutatingAdmissionPolicySpec{ |
| 408 |
MatchConstraints: &v1alpha1.MatchResources{ |
| 409 |
MatchPolicy: ptr.To(v1alpha1.Equivalent), |
| 410 |
NamespaceSelector: &metav1.LabelSelector{}, |
| 411 |
ObjectSelector: &metav1.LabelSelector{}, |
| 412 |
ResourceRules: []v1alpha1.NamedRuleWithOperations{ |
| 413 |
{ |
| 414 |
RuleWithOperations: v1alpha1.RuleWithOperations{ |
| 415 |
Rule: v1alpha1.Rule{ |
| 416 |
APIGroups: []string{"apps"}, |
| 417 |
APIVersions: []string{"v1"}, |
| 418 |
Resources: []string{"deployments"}, |
| 419 |
}, |
| 420 |
Operations: []admissionregistrationv1.OperationType{"*"}, |
| 421 |
}, |
| 422 |
}, |
| 423 |
}, |
| 424 |
}, |
| 425 |
MatchConditions: []v1alpha1.MatchCondition{ |
| 426 |
{ |
| 427 |
Name: "prodonly", |
| 428 |
Expression: `object.?metadata.labels["environment"].orValue("") == "production"`, |
| 429 |
}, |
| 430 |
}, |
| 431 |
Mutations: []v1alpha1.Mutation{{ |
| 432 |
PatchType: v1alpha1.PatchTypeApplyConfiguration, |
| 433 |
ApplyConfiguration: &v1alpha1.ApplyConfiguration{ |
| 434 |
Expression: `Object{ |
| 435 |
metadata: Object.metadata{ |
| 436 |
labels: {"policy1invoked": "true"} |
| 437 |
} |
| 438 |
}`}}, |
| 439 |
}, |
| 440 |
}, |
| 441 |
}, |
| 442 |
Bindings: []*PolicyBinding{{ |
| 443 |
ObjectMeta: metav1.ObjectMeta{Name: "binding"}, |
| 444 |
Spec: v1alpha1.MutatingAdmissionPolicyBindingSpec{ |
| 445 |
PolicyName: "policy2", |
| 446 |
}, |
| 447 |
}}, |
| 448 |
}, |
| 449 |
}, |
| 450 |
expect: &appsv1.Deployment{ |
| 451 |
ObjectMeta: metav1.ObjectMeta{ |
| 452 |
Name: "d1", |
| 453 |
Namespace: "default", |
| 454 |
Labels: map[string]string{ |
| 455 |
"environment": "production", |
| 456 |
"policy1invoked": "true", |
| 457 |
}, |
| 458 |
}, |
| 459 |
Spec: appsv1.DeploymentSpec{ |
| 460 |
Template: corev1.PodTemplateSpec{ |
| 461 |
Spec: corev1.PodSpec{ |
| 462 |
Volumes: []corev1.Volume{{Name: "x"}}, |
| 463 |
}, |
| 464 |
}, |
| 465 |
Strategy: appsv1.DeploymentStrategy{ |
| 466 |
Type: appsv1.RollingUpdateDeploymentStrategyType, |
| 467 |
}, |
| 468 |
}}, |
| 469 |
}, |
| 470 |
{ |
| 471 |
// TODO: This behavior pre-exists with webhook match conditions but should be reconsidered |
| 472 |
name: "1st policy still does not match after 2nd policy sets match condition", |
| 473 |
gvk: deploymentGVK, |
| 474 |
gvr: deploymentGVR, |
| 475 |
object: &appsv1.Deployment{ |
| 476 |
TypeMeta: metav1.TypeMeta{ |
| 477 |
Kind: "Deployment", |
| 478 |
APIVersion: "apps/v1", |
| 479 |
}, |
| 480 |
ObjectMeta: metav1.ObjectMeta{ |
| 481 |
Name: "d1", |
| 482 |
Namespace: "default", |
| 483 |
}, |
| 484 |
Spec: appsv1.DeploymentSpec{ |
| 485 |
Template: corev1.PodTemplateSpec{ |
| 486 |
Spec: corev1.PodSpec{ |
| 487 |
Volumes: []corev1.Volume{{Name: "x"}}, |
| 488 |
}, |
| 489 |
}, |
| 490 |
Strategy: appsv1.DeploymentStrategy{ |
| 491 |
Type: appsv1.RollingUpdateDeploymentStrategyType, |
| 492 |
}, |
| 493 |
}}, |
| 494 |
policyHooks: []generic.PolicyHook[*Policy, *PolicyBinding, PolicyEvaluator]{ |
| 495 |
{ |
| 496 |
Policy: &v1alpha1.MutatingAdmissionPolicy{ |
| 497 |
ObjectMeta: metav1.ObjectMeta{ |
| 498 |
Name: "policy1", |
| 499 |
}, |
| 500 |
Spec: v1alpha1.MutatingAdmissionPolicySpec{ |
| 501 |
MatchConstraints: &v1alpha1.MatchResources{ |
| 502 |
MatchPolicy: ptr.To(v1alpha1.Equivalent), |
| 503 |
NamespaceSelector: &metav1.LabelSelector{}, |
| 504 |
ObjectSelector: &metav1.LabelSelector{}, |
| 505 |
ResourceRules: []v1alpha1.NamedRuleWithOperations{ |
| 506 |
{ |
| 507 |
RuleWithOperations: v1alpha1.RuleWithOperations{ |
| 508 |
Rule: v1alpha1.Rule{ |
| 509 |
APIGroups: []string{"apps"}, |
| 510 |
APIVersions: []string{"v1"}, |
| 511 |
Resources: []string{"deployments"}, |
| 512 |
}, |
| 513 |
Operations: []admissionregistrationv1.OperationType{"*"}, |
| 514 |
}, |
| 515 |
}, |
| 516 |
}, |
| 517 |
}, |
| 518 |
MatchConditions: []v1alpha1.MatchCondition{ |
| 519 |
{ |
| 520 |
Name: "prodonly", |
| 521 |
Expression: `object.?metadata.labels["environment"].orValue("") == "production"`, |
| 522 |
}, |
| 523 |
}, |
| 524 |
Mutations: []v1alpha1.Mutation{{ |
| 525 |
PatchType: v1alpha1.PatchTypeApplyConfiguration, |
| 526 |
ApplyConfiguration: &v1alpha1.ApplyConfiguration{ |
| 527 |
Expression: `Object{ |
| 528 |
metadata: Object.metadata{ |
| 529 |
labels: {"policy1invoked": "true"} |
| 530 |
} |
| 531 |
}`}}, |
| 532 |
}, |
| 533 |
}, |
| 534 |
}, |
| 535 |
Bindings: []*PolicyBinding{{ |
| 536 |
ObjectMeta: metav1.ObjectMeta{Name: "binding"}, |
| 537 |
Spec: v1alpha1.MutatingAdmissionPolicyBindingSpec{ |
| 538 |
PolicyName: "policy1", |
| 539 |
}, |
| 540 |
}}, |
| 541 |
}, |
| 542 |
{ |
| 543 |
Policy: &v1alpha1.MutatingAdmissionPolicy{ |
| 544 |
ObjectMeta: metav1.ObjectMeta{ |
| 545 |
Name: "policy2", |
| 546 |
}, |
| 547 |
Spec: v1alpha1.MutatingAdmissionPolicySpec{ |
| 548 |
MatchConstraints: &v1alpha1.MatchResources{ |
| 549 |
MatchPolicy: ptr.To(v1alpha1.Equivalent), |
| 550 |
NamespaceSelector: &metav1.LabelSelector{}, |
| 551 |
ObjectSelector: &metav1.LabelSelector{}, |
| 552 |
ResourceRules: []v1alpha1.NamedRuleWithOperations{ |
| 553 |
{ |
| 554 |
RuleWithOperations: v1alpha1.RuleWithOperations{ |
| 555 |
Rule: v1alpha1.Rule{ |
| 556 |
APIGroups: []string{"apps"}, |
| 557 |
APIVersions: []string{"v1"}, |
| 558 |
Resources: []string{"deployments"}, |
| 559 |
}, |
| 560 |
Operations: []admissionregistrationv1.OperationType{"*"}, |
| 561 |
}, |
| 562 |
}, |
| 563 |
}, |
| 564 |
}, |
| 565 |
Mutations: []v1alpha1.Mutation{{ |
| 566 |
PatchType: v1alpha1.PatchTypeApplyConfiguration, |
| 567 |
ApplyConfiguration: &v1alpha1.ApplyConfiguration{ |
| 568 |
Expression: `Object{ |
| 569 |
metadata: Object.metadata{ |
| 570 |
labels: {"environment": "production"} |
| 571 |
} |
| 572 |
}`}}, |
| 573 |
}, |
| 574 |
}, |
| 575 |
}, |
| 576 |
Bindings: []*PolicyBinding{{ |
| 577 |
ObjectMeta: metav1.ObjectMeta{Name: "binding"}, |
| 578 |
Spec: v1alpha1.MutatingAdmissionPolicyBindingSpec{ |
| 579 |
PolicyName: "policy2", |
| 580 |
}, |
| 581 |
}}, |
| 582 |
}, |
| 583 |
}, |
| 584 |
expect: &appsv1.Deployment{ |
| 585 |
ObjectMeta: metav1.ObjectMeta{ |
| 586 |
Name: "d1", |
| 587 |
Namespace: "default", |
| 588 |
Labels: map[string]string{ |
| 589 |
"environment": "production", |
| 590 |
}, |
| 591 |
}, |
| 592 |
Spec: appsv1.DeploymentSpec{ |
| 593 |
Template: corev1.PodTemplateSpec{ |
| 594 |
Spec: corev1.PodSpec{ |
| 595 |
Volumes: []corev1.Volume{{Name: "x"}}, |
| 596 |
}, |
| 597 |
}, |
| 598 |
Strategy: appsv1.DeploymentStrategy{ |
| 599 |
Type: appsv1.RollingUpdateDeploymentStrategyType, |
| 600 |
}, |
| 601 |
}}, |
| 602 |
}, |
| 603 |
} |
| 604 |
ctx, cancel := context.WithCancel(context.Background()) |
| 605 |
defer cancel() |
| 606 |
tcManager := patch.NewTypeConverterManager(nil, openapitest.NewEmbeddedFileClient()) |
| 607 |
go tcManager.Run(ctx) |
| 608 |
|
| 609 |
err := wait.PollUntilContextTimeout(ctx, 100*time.Millisecond, time.Second, true, func(context.Context) (done bool, err error) { |
| 610 |
converter := tcManager.GetTypeConverter(deploymentGVK) |
| 611 |
return converter != nil, nil |
| 612 |
}) |
| 613 |
if err != nil { |
| 614 |
t.Fatal(err) |
| 615 |
} |
| 616 |
|
| 617 |
scheme := runtime.NewScheme() |
| 618 |
err = appsv1.AddToScheme(scheme) |
| 619 |
if err != nil { |
| 620 |
t.Fatal(err) |
| 621 |
} |
| 622 |
err = corev1.AddToScheme(scheme) |
| 623 |
if err != nil { |
| 624 |
t.Fatal(err) |
| 625 |
} |
| 626 |
|
| 627 |
// Register a fake defaulter since registering the full defaulter adds noise |
| 628 |
// and creates dep cycles. |
| 629 |
scheme.AddTypeDefaultingFunc(&appsv1.Deployment{}, |
| 630 |
func(obj interface{}) { fakeSetDefaultForDeployment(obj.(*appsv1.Deployment)) }) |
| 631 |
|
| 632 |
objectInterfaces := admission.NewObjectInterfacesFromScheme(scheme) |
| 633 |
|
| 634 |
for _, tc := range testCases { |
| 635 |
t.Run(tc.name, func(t *testing.T) { |
| 636 |
client := fake.NewClientset(tc.params...) |
| 637 |
|
| 638 |
// always include default namespace |
| 639 |
err := client.Tracker().Add(&corev1.Namespace{ |
| 640 |
TypeMeta: metav1.TypeMeta{ |
| 641 |
APIVersion: "v1", |
| 642 |
Kind: "Namespace", |
| 643 |
}, |
| 644 |
ObjectMeta: metav1.ObjectMeta{ |
| 645 |
Name: "default", |
| 646 |
}, |
| 647 |
Spec: corev1.NamespaceSpec{}, |
| 648 |
}) |
| 649 |
if err != nil { |
| 650 |
t.Fatal(err) |
| 651 |
} |
| 652 |
|
| 653 |
informerFactory := informers.NewSharedInformerFactory(client, 0) |
| 654 |
matcher := matching.NewMatcher(informerFactory.Core().V1().Namespaces().Lister(), client) |
| 655 |
paramInformer, err := informerFactory.ForResource(schema.GroupVersionResource{Version: "v1", Resource: "configmaps"}) |
| 656 |
if err != nil { |
| 657 |
t.Fatal(err) |
| 658 |
} |
| 659 |
informerFactory.Start(ctx.Done()) |
| 660 |
informerFactory.WaitForCacheSync(ctx.Done()) |
| 661 |
for i, h := range tc.policyHooks { |
| 662 |
tc.policyHooks[i].ParamInformer = paramInformer |
| 663 |
tc.policyHooks[i].ParamScope = testParamScope{} |
| 664 |
tc.policyHooks[i].Evaluator = compilePolicy(h.Policy) |
| 665 |
} |
| 666 |
|
| 667 |
dispatcher := NewDispatcher(fakeAuthorizer{}, matcher, tcManager) |
| 668 |
err = dispatcher.Start(ctx) |
| 669 |
if err != nil { |
| 670 |
t.Fatalf("error starting dispatcher: %v", err) |
| 671 |
} |
| 672 |
|
| 673 |
metaAccessor, err := meta.Accessor(tc.object) |
| 674 |
if err != nil { |
| 675 |
t.Fatal(err) |
| 676 |
} |
| 677 |
|
| 678 |
attrs := admission.NewAttributesRecord(tc.object, tc.oldObject, tc.gvk, |
| 679 |
metaAccessor.GetNamespace(), metaAccessor.GetName(), tc.gvr, |
| 680 |
"", admission.Create, &metav1.CreateOptions{}, false, nil) |
| 681 |
vAttrs := &admission.VersionedAttributes{ |
| 682 |
Attributes: attrs, |
| 683 |
VersionedKind: tc.gvk, |
| 684 |
VersionedObject: tc.object, |
| 685 |
VersionedOldObject: tc.oldObject, |
| 686 |
} |
| 687 |
|
| 688 |
err = dispatcher.Dispatch(ctx, vAttrs, objectInterfaces, tc.policyHooks) |
| 689 |
if err != nil { |
| 690 |
t.Fatalf("error dispatching policy hooks: %v", err) |
| 691 |
} |
| 692 |
|
| 693 |
obj := vAttrs.VersionedObject |
| 694 |
if !equality.Semantic.DeepEqual(obj, tc.expect) { |
| 695 |
t.Errorf("unexpected result, got diff:\n%s\n", cmp.Diff(tc.expect, obj)) |
| 696 |
} |
| 697 |
}) |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
type testParamScope struct{} |
| 702 |
|
| 703 |
func (t testParamScope) Name() meta.RESTScopeName { |
| 704 |
return meta.RESTScopeNameNamespace |
| 705 |
} |
| 706 |
|
| 707 |
var _ meta.RESTScope = testParamScope{} |
| 708 |
|
| 709 |
func fakeSetDefaultForDeployment(obj *appsv1.Deployment) { |
| 710 |
// Just default strategy type so the tests have a defaulted field to observe |
| 711 |
strategy := &obj.Spec.Strategy |
| 712 |
if strategy.Type == "" { |
| 713 |
strategy.Type = appsv1.RollingUpdateDeploymentStrategyType |
| 714 |
} |
| 715 |
} |