Skip to content

Commit 4f7543e

Browse files
committed
Upgrade AdmissionReview e2e test image to also support v1
1 parent f4e39af commit 4f7543e

File tree

18 files changed

+353
-105
lines changed

18 files changed

+353
-105
lines changed

pkg/apis/admission/fuzzer/BUILD

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ go_library(
99
name = "go_default_library",
1010
srcs = ["fuzzer.go"],
1111
importpath = "k8s.io/kubernetes/pkg/apis/admission/fuzzer",
12-
deps = ["//staging/src/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library"],
12+
deps = [
13+
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
14+
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
15+
"//staging/src/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
16+
"//vendor/github.com/google/gofuzz:go_default_library",
17+
],
1318
)
1419

1520
filegroup(

pkg/apis/admission/fuzzer/fuzzer.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,23 @@ limitations under the License.
1717
package fuzzer
1818

1919
import (
20+
fuzz "github.com/google/gofuzz"
21+
22+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
23+
"k8s.io/apimachinery/pkg/runtime"
2024
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
2125
)
2226

2327
// Funcs returns the fuzzer functions for the admission api group.
2428
var Funcs = func(codecs runtimeserializer.CodecFactory) []interface{} {
25-
return []interface{}{}
29+
return []interface{}{
30+
func(s *runtime.RawExtension, c fuzz.Continue) {
31+
u := &unstructured.Unstructured{Object: map[string]interface{}{
32+
"apiVersion": "unknown.group/unknown",
33+
"kind": "Something",
34+
"somekey": "somevalue",
35+
}}
36+
s.Object = u
37+
},
38+
}
2639
}

test/images/agnhost/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.4
1+
2.5

test/images/agnhost/agnhost

35.6 MB
Binary file not shown.

test/images/agnhost/crd-conversion-webhook/main.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package crdconvwebhook
1818

1919
import (
20+
"fmt"
2021
"net/http"
2122

2223
"github.com/spf13/cobra"
@@ -27,6 +28,7 @@ import (
2728
var (
2829
certFile string
2930
keyFile string
31+
port int
3032
)
3133

3234
// CmdCrdConversionWebhook is used by agnhost Cobra.
@@ -48,6 +50,8 @@ func init() {
4850
"after server cert.")
4951
CmdCrdConversionWebhook.Flags().StringVar(&keyFile, "tls-private-key-file", "",
5052
"File containing the default x509 private key matching --tls-cert-file.")
53+
CmdCrdConversionWebhook.Flags().IntVar(&port, "port", 443,
54+
"Secure port that the webhook listens on")
5155
}
5256

5357
// Config contains the server (the webhook) cert and key.
@@ -62,8 +66,11 @@ func main(cmd *cobra.Command, args []string) {
6266
http.HandleFunc("/crdconvert", converter.ServeExampleConvert)
6367
clientset := getClient()
6468
server := &http.Server{
65-
Addr: ":443",
69+
Addr: fmt.Sprintf(":%d", port),
6670
TLSConfig: configTLS(config, clientset),
6771
}
68-
server.ListenAndServeTLS("", "")
72+
err := server.ListenAndServeTLS("", "")
73+
if err != nil {
74+
panic(err)
75+
}
6976
}

test/images/agnhost/webhook/BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ go_library(
88
"alwaysdeny.go",
99
"config.go",
1010
"configmap.go",
11+
"convert.go",
1112
"crd.go",
1213
"customresource.go",
1314
"main.go",
@@ -17,7 +18,9 @@ go_library(
1718
importpath = "k8s.io/kubernetes/test/images/agnhost/webhook",
1819
visibility = ["//visibility:public"],
1920
deps = [
21+
"//staging/src/k8s.io/api/admission/v1:go_default_library",
2022
"//staging/src/k8s.io/api/admission/v1beta1:go_default_library",
23+
"//staging/src/k8s.io/api/admissionregistration/v1:go_default_library",
2124
"//staging/src/k8s.io/api/admissionregistration/v1beta1:go_default_library",
2225
"//staging/src/k8s.io/api/core/v1:go_default_library",
2326
"//staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1:go_default_library",
@@ -48,15 +51,22 @@ go_test(
4851
name = "go_default_test",
4952
srcs = [
5053
"addlabel_test.go",
54+
"convert_test.go",
5155
"patch_test.go",
5256
],
5357
embed = [":go_default_library"],
5458
deps = [
59+
"//pkg/apis/admission/fuzzer:go_default_library",
60+
"//staging/src/k8s.io/api/admission/v1:go_default_library",
5561
"//staging/src/k8s.io/api/admission/v1beta1:go_default_library",
5662
"//staging/src/k8s.io/api/core/v1:go_default_library",
63+
"//staging/src/k8s.io/apimachinery/pkg/api/apitesting/fuzzer:go_default_library",
5764
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
5865
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
5966
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
67+
"//staging/src/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
68+
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
6069
"//vendor/github.com/evanphx/json-patch:go_default_library",
70+
"//vendor/github.com/google/gofuzz:go_default_library",
6171
],
6272
)

test/images/agnhost/webhook/addlabel.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package webhook
1919
import (
2020
"encoding/json"
2121

22-
"k8s.io/api/admission/v1beta1"
22+
"k8s.io/api/admission/v1"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424
"k8s.io/klog"
2525
)
@@ -37,7 +37,7 @@ const (
3737
)
3838

3939
// Add a label {"added-label": "yes"} to the object
40-
func addLabel(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
40+
func addLabel(ar v1.AdmissionReview) *v1.AdmissionResponse {
4141
klog.V(2).Info("calling add-label")
4242
obj := struct {
4343
metav1.ObjectMeta `json:"metadata,omitempty"`
@@ -46,13 +46,13 @@ func addLabel(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
4646
err := json.Unmarshal(raw, &obj)
4747
if err != nil {
4848
klog.Error(err)
49-
return toAdmissionResponse(err)
49+
return toV1AdmissionResponse(err)
5050
}
5151

52-
reviewResponse := v1beta1.AdmissionResponse{}
52+
reviewResponse := v1.AdmissionResponse{}
5353
reviewResponse.Allowed = true
5454

55-
pt := v1beta1.PatchTypeJSONPatch
55+
pt := v1.PatchTypeJSONPatch
5656
labelValue, hasLabel := obj.ObjectMeta.Labels["added-label"]
5757
switch {
5858
case len(obj.ObjectMeta.Labels) == 0:

test/images/agnhost/webhook/addlabel_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"testing"
2323

2424
jsonpatch "github.com/evanphx/json-patch"
25-
"k8s.io/api/admission/v1beta1"
25+
"k8s.io/api/admission/v1"
2626
corev1 "k8s.io/api/core/v1"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/runtime"
@@ -58,7 +58,7 @@ func TestAddLabel(t *testing.T) {
5858
if err != nil {
5959
t.Fatal(err)
6060
}
61-
review := v1beta1.AdmissionReview{Request: &v1beta1.AdmissionRequest{Object: runtime.RawExtension{Raw: raw}}}
61+
review := v1.AdmissionReview{Request: &v1.AdmissionRequest{Object: runtime.RawExtension{Raw: raw}}}
6262
response := addLabel(review)
6363
if response.Patch != nil {
6464
patchObj, err := jsonpatch.DecodePatch([]byte(response.Patch))

test/images/agnhost/webhook/alwaysallow.go

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

22-
"k8s.io/api/admission/v1beta1"
22+
"k8s.io/api/admission/v1"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424
"k8s.io/klog"
2525
)
2626

2727
// alwaysAllowDelayFiveSeconds sleeps for five seconds and allows all requests made to this function.
28-
func alwaysAllowDelayFiveSeconds(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
28+
func alwaysAllowDelayFiveSeconds(ar v1.AdmissionReview) *v1.AdmissionResponse {
2929
klog.V(2).Info("always-allow-with-delay sleeping for 5 seconds")
3030
time.Sleep(5 * time.Second)
3131
klog.V(2).Info("calling always-allow")
32-
reviewResponse := v1beta1.AdmissionResponse{}
32+
reviewResponse := v1.AdmissionResponse{}
3333
reviewResponse.Allowed = true
3434
reviewResponse.Result = &metav1.Status{Message: "this webhook allows all requests"}
3535
return &reviewResponse

test/images/agnhost/webhook/alwaysdeny.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ limitations under the License.
1717
package webhook
1818

1919
import (
20-
"k8s.io/api/admission/v1beta1"
20+
"k8s.io/api/admission/v1"
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2222
"k8s.io/klog"
2323
)
2424

2525
// alwaysDeny all requests made to this function.
26-
func alwaysDeny(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
26+
func alwaysDeny(ar v1.AdmissionReview) *v1.AdmissionResponse {
2727
klog.V(2).Info("calling always-deny")
28-
reviewResponse := v1beta1.AdmissionResponse{}
28+
reviewResponse := v1.AdmissionResponse{}
2929
reviewResponse.Allowed = false
3030
reviewResponse.Result = &metav1.Status{Message: "this webhook denies all requests"}
3131
return &reviewResponse

0 commit comments

Comments
 (0)