Skip to content

Commit 758d268

Browse files
authored
Merge pull request #1284 from estroz/feature/wh-admissionreview-v1
⚠️ pkg/webhook/admission: upgrade v1beta1 admission types to v1
2 parents f349d96 + 8161d1c commit 758d268

12 files changed

+318
-292
lines changed

pkg/builder/webhook_test.go

+217-202
Large diffs are not rendered by default.

pkg/webhook/admission/decode_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
. "github.com/onsi/ginkgo"
2121
. "github.com/onsi/gomega"
2222

23-
admissionv1beta1 "k8s.io/api/admission/v1beta1"
23+
admissionv1 "k8s.io/api/admission/v1"
2424
corev1 "k8s.io/api/core/v1"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -39,7 +39,7 @@ var _ = Describe("Admission Webhook Decoder", func() {
3939
})
4040

4141
req := Request{
42-
AdmissionRequest: admissionv1beta1.AdmissionRequest{
42+
AdmissionRequest: admissionv1.AdmissionRequest{
4343
Object: runtime.RawExtension{
4444
Raw: []byte(`{
4545
"apiVersion": "v1",

pkg/webhook/admission/http.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
"io/ioutil"
2525
"net/http"
2626

27+
v1 "k8s.io/api/admission/v1"
2728
"k8s.io/api/admission/v1beta1"
28-
admissionv1beta1 "k8s.io/api/admission/v1beta1"
2929
"k8s.io/apimachinery/pkg/runtime"
3030
"k8s.io/apimachinery/pkg/runtime/serializer"
3131
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -35,7 +35,8 @@ var admissionScheme = runtime.NewScheme()
3535
var admissionCodecs = serializer.NewCodecFactory(admissionScheme)
3636

3737
func init() {
38-
utilruntime.Must(admissionv1beta1.AddToScheme(admissionScheme))
38+
utilruntime.Must(v1.AddToScheme(admissionScheme))
39+
utilruntime.Must(v1beta1.AddToScheme(admissionScheme))
3940
}
4041

4142
var _ http.Handler = &Webhook{}
@@ -70,11 +71,15 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7071
return
7172
}
7273

74+
// Both v1 and v1beta1 AdmissionReview types are exactly the same, so the v1beta1 type can
75+
// be decoded into the v1 type. However the runtime codec's decoder guesses which type to
76+
// decode into by type name if an Object's TypeMeta isn't set. By setting TypeMeta of an`
77+
// unregistered type to the v1 GVK, the decoder will coerce a v1beta1 AdmissionReview to v1.
7378
req := Request{}
74-
ar := v1beta1.AdmissionReview{
75-
// avoid an extra copy
76-
Request: &req.AdmissionRequest,
77-
}
79+
ar := unversionedAdmissionReview{}
80+
// avoid an extra copy
81+
ar.Request = &req.AdmissionRequest
82+
ar.SetGroupVersionKind(v1.SchemeGroupVersion.WithKind("AdmissionReview"))
7883
if _, _, err := admissionCodecs.UniversalDeserializer().Decode(body, nil, &ar); err != nil {
7984
wh.log.Error(err, "unable to decode the request")
8085
reviewResponse = Errored(http.StatusBadRequest, err)
@@ -90,7 +95,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
9095

9196
func (wh *Webhook) writeResponse(w io.Writer, response Response) {
9297
encoder := json.NewEncoder(w)
93-
responseAdmissionReview := v1beta1.AdmissionReview{
98+
responseAdmissionReview := v1.AdmissionReview{
9499
Response: &response.AdmissionResponse,
95100
}
96101
err := encoder.Encode(responseAdmissionReview)
@@ -107,3 +112,10 @@ func (wh *Webhook) writeResponse(w io.Writer, response Response) {
107112
}
108113
}
109114
}
115+
116+
// unversionedAdmissionReview is used to decode both v1 and v1beta1 AdmissionReview types.
117+
type unversionedAdmissionReview struct {
118+
v1.AdmissionReview
119+
}
120+
121+
var _ runtime.Object = &unversionedAdmissionReview{}

pkg/webhook/admission/http_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ import (
2727
. "github.com/onsi/ginkgo"
2828
. "github.com/onsi/gomega"
2929

30-
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
30+
admissionv1 "k8s.io/api/admission/v1"
3131

32-
admissionv1beta1 "k8s.io/api/admission/v1beta1"
3332
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
33+
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
3434
)
3535

3636
var _ = Describe("Admission Webhooks", func() {
@@ -155,7 +155,7 @@ func (h *fakeHandler) Handle(ctx context.Context, req Request) Response {
155155
if h.fn != nil {
156156
return h.fn(ctx, req)
157157
}
158-
return Response{AdmissionResponse: admissionv1beta1.AdmissionResponse{
158+
return Response{AdmissionResponse: admissionv1.AdmissionResponse{
159159
Allowed: true,
160160
}}
161161
}

pkg/webhook/admission/multi.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import (
2222
"fmt"
2323
"net/http"
2424

25-
"gomodules.xyz/jsonpatch/v2"
26-
admissionv1beta1 "k8s.io/api/admission/v1beta1"
25+
jsonpatch "gomodules.xyz/jsonpatch/v2"
26+
admissionv1 "k8s.io/api/admission/v1"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
2829
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
2930
)
3031

@@ -37,10 +38,10 @@ func (hs multiMutating) Handle(ctx context.Context, req Request) Response {
3738
if !resp.Allowed {
3839
return resp
3940
}
40-
if resp.PatchType != nil && *resp.PatchType != admissionv1beta1.PatchTypeJSONPatch {
41+
if resp.PatchType != nil && *resp.PatchType != admissionv1.PatchTypeJSONPatch {
4142
return Errored(http.StatusInternalServerError,
4243
fmt.Errorf("unexpected patch type returned by the handler: %v, only allow: %v",
43-
resp.PatchType, admissionv1beta1.PatchTypeJSONPatch))
44+
resp.PatchType, admissionv1.PatchTypeJSONPatch))
4445
}
4546
patches = append(patches, resp.Patches...)
4647
}
@@ -50,13 +51,13 @@ func (hs multiMutating) Handle(ctx context.Context, req Request) Response {
5051
return Errored(http.StatusBadRequest, fmt.Errorf("error when marshaling the patch: %w", err))
5152
}
5253
return Response{
53-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
54+
AdmissionResponse: admissionv1.AdmissionResponse{
5455
Allowed: true,
5556
Result: &metav1.Status{
5657
Code: http.StatusOK,
5758
},
5859
Patch: marshaledPatch,
59-
PatchType: func() *admissionv1beta1.PatchType { pt := admissionv1beta1.PatchTypeJSONPatch; return &pt }(),
60+
PatchType: func() *admissionv1.PatchType { pt := admissionv1.PatchTypeJSONPatch; return &pt }(),
6061
},
6162
}
6263
}
@@ -94,7 +95,7 @@ func (hs multiValidating) Handle(ctx context.Context, req Request) Response {
9495
}
9596
}
9697
return Response{
97-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
98+
AdmissionResponse: admissionv1.AdmissionResponse{
9899
Allowed: true,
99100
Result: &metav1.Status{
100101
Code: http.StatusOK,

pkg/webhook/admission/multi_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ import (
2222
. "github.com/onsi/ginkgo"
2323
. "github.com/onsi/gomega"
2424

25-
"gomodules.xyz/jsonpatch/v2"
26-
admissionv1beta1 "k8s.io/api/admission/v1beta1"
25+
jsonpatch "gomodules.xyz/jsonpatch/v2"
26+
admissionv1 "k8s.io/api/admission/v1"
2727
)
2828

2929
var _ = Describe("Multi-Handler Admission Webhooks", func() {
3030
alwaysAllow := &fakeHandler{
3131
fn: func(ctx context.Context, req Request) Response {
3232
return Response{
33-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
33+
AdmissionResponse: admissionv1.AdmissionResponse{
3434
Allowed: true,
3535
},
3636
}
@@ -39,7 +39,7 @@ var _ = Describe("Multi-Handler Admission Webhooks", func() {
3939
alwaysDeny := &fakeHandler{
4040
fn: func(ctx context.Context, req Request) Response {
4141
return Response{
42-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
42+
AdmissionResponse: admissionv1.AdmissionResponse{
4343
Allowed: false,
4444
},
4545
}
@@ -82,9 +82,9 @@ var _ = Describe("Multi-Handler Admission Webhooks", func() {
8282
Value: "2",
8383
},
8484
},
85-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
85+
AdmissionResponse: admissionv1.AdmissionResponse{
8686
Allowed: true,
87-
PatchType: func() *admissionv1beta1.PatchType { pt := admissionv1beta1.PatchTypeJSONPatch; return &pt }(),
87+
PatchType: func() *admissionv1.PatchType { pt := admissionv1.PatchTypeJSONPatch; return &pt }(),
8888
},
8989
}
9090
},
@@ -99,9 +99,9 @@ var _ = Describe("Multi-Handler Admission Webhooks", func() {
9999
Value: "world",
100100
},
101101
},
102-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
102+
AdmissionResponse: admissionv1.AdmissionResponse{
103103
Allowed: true,
104-
PatchType: func() *admissionv1beta1.PatchType { pt := admissionv1beta1.PatchTypeJSONPatch; return &pt }(),
104+
PatchType: func() *admissionv1.PatchType { pt := admissionv1.PatchTypeJSONPatch; return &pt }(),
105105
},
106106
}
107107
},

pkg/webhook/admission/response.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ package admission
1919
import (
2020
"net/http"
2121

22-
"gomodules.xyz/jsonpatch/v2"
23-
24-
admissionv1beta1 "k8s.io/api/admission/v1beta1"
22+
jsonpatch "gomodules.xyz/jsonpatch/v2"
23+
admissionv1 "k8s.io/api/admission/v1"
2524
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2625
)
2726

@@ -50,7 +49,7 @@ func Patched(reason string, patches ...jsonpatch.JsonPatchOperation) Response {
5049
// Errored creates a new Response for error-handling a request.
5150
func Errored(code int32, err error) Response {
5251
return Response{
53-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
52+
AdmissionResponse: admissionv1.AdmissionResponse{
5453
Allowed: false,
5554
Result: &metav1.Status{
5655
Code: code,
@@ -67,7 +66,7 @@ func ValidationResponse(allowed bool, reason string) Response {
6766
code = http.StatusOK
6867
}
6968
resp := Response{
70-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
69+
AdmissionResponse: admissionv1.AdmissionResponse{
7170
Allowed: allowed,
7271
Result: &metav1.Status{
7372
Code: int32(code),
@@ -90,17 +89,17 @@ func PatchResponseFromRaw(original, current []byte) Response {
9089
}
9190
return Response{
9291
Patches: patches,
93-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
92+
AdmissionResponse: admissionv1.AdmissionResponse{
9493
Allowed: true,
95-
PatchType: func() *admissionv1beta1.PatchType { pt := admissionv1beta1.PatchTypeJSONPatch; return &pt }(),
94+
PatchType: func() *admissionv1.PatchType { pt := admissionv1.PatchTypeJSONPatch; return &pt }(),
9695
},
9796
}
9897
}
9998

10099
// validationResponseFromStatus returns a response for admitting a request with provided Status object.
101100
func validationResponseFromStatus(allowed bool, status metav1.Status) Response {
102101
resp := Response{
103-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
102+
AdmissionResponse: admissionv1.AdmissionResponse{
104103
Allowed: allowed,
105104
Result: &status,
106105
},

pkg/webhook/admission/response_test.go

+17-17
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import (
2222

2323
. "github.com/onsi/ginkgo"
2424
. "github.com/onsi/gomega"
25-
"gomodules.xyz/jsonpatch/v2"
2625

27-
admissionv1beta1 "k8s.io/api/admission/v1beta1"
26+
jsonpatch "gomodules.xyz/jsonpatch/v2"
27+
admissionv1 "k8s.io/api/admission/v1"
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
)
3030

@@ -33,7 +33,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
3333
It("should return an 'allowed' response", func() {
3434
Expect(Allowed("")).To(Equal(
3535
Response{
36-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
36+
AdmissionResponse: admissionv1.AdmissionResponse{
3737
Allowed: true,
3838
Result: &metav1.Status{
3939
Code: http.StatusOK,
@@ -46,7 +46,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
4646
It("should populate a status with a reason when a reason is given", func() {
4747
Expect(Allowed("acceptable")).To(Equal(
4848
Response{
49-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
49+
AdmissionResponse: admissionv1.AdmissionResponse{
5050
Allowed: true,
5151
Result: &metav1.Status{
5252
Code: http.StatusOK,
@@ -62,7 +62,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
6262
It("should return a 'not allowed' response", func() {
6363
Expect(Denied("")).To(Equal(
6464
Response{
65-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
65+
AdmissionResponse: admissionv1.AdmissionResponse{
6666
Allowed: false,
6767
Result: &metav1.Status{
6868
Code: http.StatusForbidden,
@@ -75,7 +75,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
7575
It("should populate a status with a reason when a reason is given", func() {
7676
Expect(Denied("UNACCEPTABLE!")).To(Equal(
7777
Response{
78-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
78+
AdmissionResponse: admissionv1.AdmissionResponse{
7979
Allowed: false,
8080
Result: &metav1.Status{
8181
Code: http.StatusForbidden,
@@ -102,7 +102,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
102102
It("should return an 'allowed' response with the given patches", func() {
103103
Expect(Patched("", ops...)).To(Equal(
104104
Response{
105-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
105+
AdmissionResponse: admissionv1.AdmissionResponse{
106106
Allowed: true,
107107
Result: &metav1.Status{
108108
Code: http.StatusOK,
@@ -115,7 +115,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
115115
It("should populate a status with a reason when a reason is given", func() {
116116
Expect(Patched("some changes", ops...)).To(Equal(
117117
Response{
118-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
118+
AdmissionResponse: admissionv1.AdmissionResponse{
119119
Allowed: true,
120120
Result: &metav1.Status{
121121
Code: http.StatusOK,
@@ -132,7 +132,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
132132
It("should return a denied response with an error", func() {
133133
err := errors.New("this is an error")
134134
expected := Response{
135-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
135+
AdmissionResponse: admissionv1.AdmissionResponse{
136136
Allowed: false,
137137
Result: &metav1.Status{
138138
Code: http.StatusBadRequest,
@@ -150,7 +150,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
150150
By("checking that a message is populated for 'allowed' responses")
151151
Expect(ValidationResponse(true, "acceptable")).To(Equal(
152152
Response{
153-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
153+
AdmissionResponse: admissionv1.AdmissionResponse{
154154
Allowed: true,
155155
Result: &metav1.Status{
156156
Code: http.StatusOK,
@@ -163,7 +163,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
163163
By("checking that a message is populated for 'denied' responses")
164164
Expect(ValidationResponse(false, "UNACCEPTABLE!")).To(Equal(
165165
Response{
166-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
166+
AdmissionResponse: admissionv1.AdmissionResponse{
167167
Allowed: false,
168168
Result: &metav1.Status{
169169
Code: http.StatusForbidden,
@@ -178,7 +178,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
178178
By("checking that it returns an 'allowed' response when allowed is true")
179179
Expect(ValidationResponse(true, "")).To(Equal(
180180
Response{
181-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
181+
AdmissionResponse: admissionv1.AdmissionResponse{
182182
Allowed: true,
183183
Result: &metav1.Status{
184184
Code: http.StatusOK,
@@ -190,7 +190,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
190190
By("checking that it returns an 'denied' response when allowed is false")
191191
Expect(ValidationResponse(false, "")).To(Equal(
192192
Response{
193-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
193+
AdmissionResponse: admissionv1.AdmissionResponse{
194194
Allowed: false,
195195
Result: &metav1.Status{
196196
Code: http.StatusForbidden,
@@ -207,9 +207,9 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
207207
Patches: []jsonpatch.JsonPatchOperation{
208208
{Operation: "replace", Path: "/a", Value: "bar"},
209209
},
210-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
210+
AdmissionResponse: admissionv1.AdmissionResponse{
211211
Allowed: true,
212-
PatchType: func() *admissionv1beta1.PatchType { pt := admissionv1beta1.PatchTypeJSONPatch; return &pt }(),
212+
PatchType: func() *admissionv1.PatchType { pt := admissionv1.PatchTypeJSONPatch; return &pt }(),
213213
},
214214
}
215215
resp := PatchResponseFromRaw([]byte(`{"a": "foo"}`), []byte(`{"a": "bar"}`))
@@ -220,7 +220,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
220220
Describe("WithWarnings", func() {
221221
It("should add the warnings to the existing response without removing any existing warnings", func() {
222222
initialResponse := Response{
223-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
223+
AdmissionResponse: admissionv1.AdmissionResponse{
224224
Allowed: true,
225225
Result: &metav1.Status{
226226
Code: http.StatusOK,
@@ -230,7 +230,7 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
230230
}
231231
warnings := []string{"additional-warning-1", "additional-warning-2"}
232232
expectedResponse := Response{
233-
AdmissionResponse: admissionv1beta1.AdmissionResponse{
233+
AdmissionResponse: admissionv1.AdmissionResponse{
234234
Allowed: true,
235235
Result: &metav1.Status{
236236
Code: http.StatusOK,

0 commit comments

Comments
 (0)