Skip to content

Commit 0b6ad8b

Browse files
authored
Merge pull request kubernetes#77563 from jpbetz/admission-webhook-options
Pass {Operation}Options to Webhooks
2 parents 0e224ad + 900d652 commit 0b6ad8b

File tree

67 files changed

+783
-470
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+783
-470
lines changed

pkg/apis/admission/types.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ type AdmissionRequest struct {
6363
// Namespace is the namespace associated with the request (if any).
6464
// +optional
6565
Namespace string
66-
// Operation is the operation being performed
66+
// Operation is the operation being performed. This may be different than the operation
67+
// requested. e.g. a patch can result in either a CREATE or UPDATE Operation.
6768
Operation Operation
6869
// UserInfo is information about the requesting user
6970
UserInfo authentication.UserInfo
@@ -78,6 +79,13 @@ type AdmissionRequest struct {
7879
// Defaults to false.
7980
// +optional
8081
DryRun *bool
82+
// Options is the operation option structure of the operation being performed.
83+
// e.g. `meta.k8s.io/v1.DeleteOptions` or `meta.k8s.io/v1.CreateOptions`. This may be
84+
// different than the options the caller provided. e.g. for a patch request the performed
85+
// Operation might be a CREATE, in which case the Options will a
86+
// `meta.k8s.io/v1.CreateOptions` even though the caller provided `meta.k8s.io/v1.PatchOptions`.
87+
// +optional
88+
Options runtime.Object
8189
}
8290

8391
// AdmissionResponse describes an admission response.

pkg/apis/admission/v1beta1/zz_generated.conversion.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/admission/zz_generated.deepcopy.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/pkg/admission/admit/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ go_test(
2222
embed = [":go_default_library"],
2323
deps = [
2424
"//pkg/apis/core:go_default_library",
25+
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
2526
"//staging/src/k8s.io/apiserver/pkg/admission:go_default_library",
2627
],
2728
)

plugin/pkg/admission/admit/admission_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ package admit
1919
import (
2020
"testing"
2121

22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2223
"k8s.io/apiserver/pkg/admission"
2324
api "k8s.io/kubernetes/pkg/apis/core"
2425
)
2526

2627
func TestAdmissionNonNilAttribute(t *testing.T) {
2728
handler := NewAlwaysAdmit()
28-
err := handler.(*alwaysAdmit).Admit(admission.NewAttributesRecord(nil, nil, api.Kind("kind").WithVersion("version"), "namespace", "name", api.Resource("resource").WithVersion("version"), "subresource", admission.Create, false, nil), nil)
29+
err := handler.(*alwaysAdmit).Admit(admission.NewAttributesRecord(nil, nil, api.Kind("kind").WithVersion("version"), "namespace", "name", api.Resource("resource").WithVersion("version"), "subresource", admission.Create, &metav1.CreateOptions{}, false, nil), nil)
2930
if err != nil {
3031
t.Errorf("Unexpected error returned from admission handler")
3132
}

plugin/pkg/admission/alwayspullimages/admission_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestAdmission(t *testing.T) {
4747
},
4848
},
4949
}
50-
err := handler.Admit(admission.NewAttributesRecord(&pod, nil, api.Kind("Pod").WithVersion("version"), pod.Namespace, pod.Name, api.Resource("pods").WithVersion("version"), "", admission.Create, false, nil), nil)
50+
err := handler.Admit(admission.NewAttributesRecord(&pod, nil, api.Kind("Pod").WithVersion("version"), pod.Namespace, pod.Name, api.Resource("pods").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil), nil)
5151
if err != nil {
5252
t.Errorf("Unexpected error returned from admission handler")
5353
}
@@ -84,7 +84,7 @@ func TestValidate(t *testing.T) {
8484
},
8585
}
8686
expectedError := `pods "123" is forbidden: spec.initContainers[0].imagePullPolicy: Unsupported value: "": supported values: "Always"`
87-
err := handler.Validate(admission.NewAttributesRecord(&pod, nil, api.Kind("Pod").WithVersion("version"), pod.Namespace, pod.Name, api.Resource("pods").WithVersion("version"), "", admission.Create, false, nil), nil)
87+
err := handler.Validate(admission.NewAttributesRecord(&pod, nil, api.Kind("Pod").WithVersion("version"), pod.Namespace, pod.Name, api.Resource("pods").WithVersion("version"), "", admission.Create, &metav1.CreateOptions{}, false, nil), nil)
8888
if err == nil {
8989
t.Fatal("missing expected error")
9090
}
@@ -139,7 +139,7 @@ func TestOtherResources(t *testing.T) {
139139
for _, tc := range tests {
140140
handler := &AlwaysPullImages{}
141141

142-
err := handler.Admit(admission.NewAttributesRecord(tc.object, nil, api.Kind(tc.kind).WithVersion("version"), namespace, name, api.Resource(tc.resource).WithVersion("version"), tc.subresource, admission.Create, false, nil), nil)
142+
err := handler.Admit(admission.NewAttributesRecord(tc.object, nil, api.Kind(tc.kind).WithVersion("version"), namespace, name, api.Resource(tc.resource).WithVersion("version"), tc.subresource, admission.Create, &metav1.CreateOptions{}, false, nil), nil)
143143

144144
if tc.expectError {
145145
if err == nil {

plugin/pkg/admission/antiaffinity/admission_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package antiaffinity
1919
import (
2020
"testing"
2121

22-
"k8s.io/api/core/v1"
22+
v1 "k8s.io/api/core/v1"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424
"k8s.io/apimachinery/pkg/runtime"
2525
"k8s.io/apiserver/pkg/admission"
@@ -199,7 +199,7 @@ func TestInterPodAffinityAdmission(t *testing.T) {
199199
}
200200
for _, test := range tests {
201201
pod.Spec.Affinity = test.affinity
202-
err := handler.Validate(admission.NewAttributesRecord(&pod, nil, api.Kind("Pod").WithVersion("version"), "foo", "name", api.Resource("pods").WithVersion("version"), "", "ignored", false, nil), nil)
202+
err := handler.Validate(admission.NewAttributesRecord(&pod, nil, api.Kind("Pod").WithVersion("version"), "foo", "name", api.Resource("pods").WithVersion("version"), "", "ignored", nil, false, nil), nil)
203203

204204
if test.errorExpected && err == nil {
205205
t.Errorf("Expected error for Anti Affinity %+v but did not get an error", test.affinity)
@@ -267,7 +267,7 @@ func TestOtherResources(t *testing.T) {
267267
for _, tc := range tests {
268268
handler := &Plugin{}
269269

270-
err := handler.Validate(admission.NewAttributesRecord(tc.object, nil, api.Kind(tc.kind).WithVersion("version"), namespace, name, api.Resource(tc.resource).WithVersion("version"), tc.subresource, admission.Create, false, nil), nil)
270+
err := handler.Validate(admission.NewAttributesRecord(tc.object, nil, api.Kind(tc.kind).WithVersion("version"), namespace, name, api.Resource(tc.resource).WithVersion("version"), tc.subresource, admission.Create, &metav1.CreateOptions{}, false, nil), nil)
271271

272272
if tc.expectError {
273273
if err == nil {

plugin/pkg/admission/defaulttolerationseconds/admission_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func TestForgivenessAdmission(t *testing.T) {
263263
}
264264

265265
for _, test := range tests {
266-
err := handler.Admit(admission.NewAttributesRecord(&test.requestedPod, nil, api.Kind("Pod").WithVersion("version"), "foo", "name", api.Resource("pods").WithVersion("version"), "", "ignored", false, nil), nil)
266+
err := handler.Admit(admission.NewAttributesRecord(&test.requestedPod, nil, api.Kind("Pod").WithVersion("version"), "foo", "name", api.Resource("pods").WithVersion("version"), "", "ignored", nil, false, nil), nil)
267267
if err != nil {
268268
t.Errorf("[%s]: unexpected error %v for pod %+v", test.description, err, test.requestedPod)
269269
}

plugin/pkg/admission/deny/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ go_test(
2222
embed = [":go_default_library"],
2323
deps = [
2424
"//pkg/apis/core:go_default_library",
25+
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
2526
"//staging/src/k8s.io/apiserver/pkg/admission:go_default_library",
2627
],
2728
)

plugin/pkg/admission/deny/admission_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ package deny
1919
import (
2020
"testing"
2121

22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2223
"k8s.io/apiserver/pkg/admission"
2324
api "k8s.io/kubernetes/pkg/apis/core"
2425
)
2526

2627
func TestAdmission(t *testing.T) {
2728
handler := NewAlwaysDeny()
28-
err := handler.(*alwaysDeny).Admit(admission.NewAttributesRecord(nil, nil, api.Kind("kind").WithVersion("version"), "namespace", "name", api.Resource("resource").WithVersion("version"), "subresource", admission.Create, false, nil), nil)
29+
err := handler.(*alwaysDeny).Admit(admission.NewAttributesRecord(nil, nil, api.Kind("kind").WithVersion("version"), "namespace", "name", api.Resource("resource").WithVersion("version"), "subresource", admission.Create, &metav1.CreateOptions{}, false, nil), nil)
2930
if err == nil {
3031
t.Error("Expected error returned from admission handler")
3132
}

0 commit comments

Comments
 (0)