diff --git a/pkg/webhook/create_options.go b/pkg/webhook/create_options.go deleted file mode 100644 index a8276556ee77..000000000000 --- a/pkg/webhook/create_options.go +++ /dev/null @@ -1,57 +0,0 @@ -/* -Copyright 2020 The Knative Authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package webhook - -import ( - "context" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - scheme "k8s.io/client-go/kubernetes/scheme" - rest "k8s.io/client-go/rest" -) - -// TODO(https://github.com/knative/serving/issues/7143) We will remove this entire file once we pull in -// k8s version 1.18 client libraries where CreateOptions is included officially. -// This file is mostly pieces backported from the current version of the client. -var newCreateWithOptions func(client rest.Interface, namespace string) podInterface = newPods - -type pods struct { - client rest.Interface - ns string -} - -type podInterface interface { - createWithOptions(ctx context.Context, pod *corev1.Pod, opts metav1.CreateOptions) (*corev1.Pod, error) -} - -// newPods returns a Pods -func newPods(client rest.Interface, namespace string) podInterface { - p := &pods{ - client: client, - ns: namespace, - } - return podInterface(p) -} - -// CreateWithOptions takes the representation of a pod and creates it. -//Returns the server's representation of the pod, and an error, if there is any. -func (c *pods) createWithOptions(ctx context.Context, pod *corev1.Pod, opts metav1.CreateOptions) (result *corev1.Pod, err error) { - result = &corev1.Pod{} - err = c.client.Post().Namespace(c.ns).Resource("pods").VersionedParams(&opts, scheme.ParameterCodec).Body(pod).Do(ctx).Into(result) - return -} diff --git a/pkg/webhook/create_options_test.go b/pkg/webhook/create_options_test.go deleted file mode 100644 index b54b56fc54e1..000000000000 --- a/pkg/webhook/create_options_test.go +++ /dev/null @@ -1,60 +0,0 @@ -/* -Copyright 2020 The Knative Authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package webhook - -import ( - "context" - "errors" - "testing" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - rest "k8s.io/client-go/rest" - kubeclient "knative.dev/pkg/client/injection/kube/client" - fakekubeclient "knative.dev/pkg/client/injection/kube/client/fake" -) - -type tp struct{} - -func newTestPods(client rest.Interface, namespace string) podInterface { - return podInterface(&tp{}) -} - -func (*tp) createWithOptions(ctx context.Context, - pod *corev1.Pod, opts metav1.CreateOptions) (result *corev1.Pod, err error) { - return &corev1.Pod{}, nil -} - -type tp2 struct{} - -func newFailTestPods(client rest.Interface, namespace string) podInterface { - return podInterface(&tp2{}) -} - -func (*tp2) createWithOptions(ctx context.Context, - pod *corev1.Pod, opts metav1.CreateOptions) (result *corev1.Pod, err error) { - return nil, errors.New("fail-reason") -} - -func TestCreateWithOptions(t *testing.T) { - ctx, _ := fakekubeclient.With(context.Background()) - client := kubeclient.Get(ctx) - pod := &corev1.Pod{} - client.CoreV1().Pods("namespace").Create(ctx, pod, metav1.CreateOptions{}) - - newPods(client.CoreV1().RESTClient(), "namespace") -} diff --git a/pkg/webhook/podspec_dryrun.go b/pkg/webhook/podspec_dryrun.go index e3ddcba0028d..e2270be0c214 100644 --- a/pkg/webhook/podspec_dryrun.go +++ b/pkg/webhook/podspec_dryrun.go @@ -75,10 +75,8 @@ func dryRunPodSpec(ctx context.Context, pod *corev1.Pod, mode DryRunMode) *apis. logger := logging.FromContext(ctx) client := kubeclient.Get(ctx) - pods := newCreateWithOptions(client.CoreV1().RESTClient(), pod.GetNamespace()) - options := metav1.CreateOptions{DryRun: []string{metav1.DryRunAll}} - if _, err := pods.createWithOptions(ctx, pod, options); err != nil { + if _, err := client.CoreV1().Pods(pod.GetNamespace()).Create(ctx, pod, options); err != nil { // Ignore failures for implementations that don't support dry-run. // This likely means there are other webhooks on the PodSpec Create action which do not declare sideEffects:none if mode != DryRunStrict && strings.Contains(err.Error(), "does not support dry run") { diff --git a/pkg/webhook/podspec_dryrun_test.go b/pkg/webhook/podspec_dryrun_test.go index 7ffeb56d944e..e461cdb0cf2a 100644 --- a/pkg/webhook/podspec_dryrun_test.go +++ b/pkg/webhook/podspec_dryrun_test.go @@ -25,7 +25,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" - rest "k8s.io/client-go/rest" ktesting "k8s.io/client-go/testing" "knative.dev/pkg/apis" @@ -62,10 +61,8 @@ func TestExtraServiceValidation(t *testing.T) { s *v1.Service want string modifyContext func(context.Context) - podInterface func(client rest.Interface, namespace string) podInterface }{{ - name: "valid run latest", - podInterface: newTestPods, + name: "valid run latest", s: &v1.Service{ ObjectMeta: om, Spec: v1.ServiceSpec{ @@ -80,8 +77,7 @@ func TestExtraServiceValidation(t *testing.T) { }, modifyContext: nil, }, { - name: "dryrun fail", - podInterface: newFailTestPods, + name: "dryrun fail", s: &v1.Service{ ObjectMeta: om, Spec: v1.ServiceSpec{ @@ -94,11 +90,10 @@ func TestExtraServiceValidation(t *testing.T) { }, }, }, - want: "dry run failed with fail-reason: spec.template", + want: "dry run failed with kubeclient error: spec.template", modifyContext: failKubeCalls, }, { - name: "no template found", - podInterface: newTestPods, + name: "no template found", s: &v1.Service{ ObjectMeta: om, Spec: v1.ServiceSpec{ @@ -116,7 +111,6 @@ func TestExtraServiceValidation(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - newCreateWithOptions = test.podInterface ctx, _ := fakekubeclient.With(context.Background()) if test.modifyContext != nil { test.modifyContext(ctx) @@ -167,27 +161,23 @@ func TestConfigurationValidation(t *testing.T) { c *v1.Configuration want string modifyContext func(context.Context) - podInterface func(client rest.Interface, namespace string) podInterface }{{ - name: "valid run latest", - podInterface: newTestPods, + name: "valid run latest", c: &v1.Configuration{ ObjectMeta: om, Spec: goodConfigSpec, }, modifyContext: nil, }, { - name: "dryrun fail", - podInterface: newFailTestPods, + name: "dryrun fail", c: &v1.Configuration{ ObjectMeta: om, Spec: goodConfigSpec, }, - want: "dry run failed with fail-reason: spec.template", + want: "dry run failed with kubeclient error: spec.template", modifyContext: failKubeCalls, }, { - name: "skip service owned", - podInterface: newFailTestPods, + name: "skip service owned", c: &v1.Configuration{ ObjectMeta: metav1.ObjectMeta{ Name: "valid", @@ -206,7 +196,6 @@ func TestConfigurationValidation(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - newCreateWithOptions = test.podInterface ctx, _ := fakekubeclient.With(context.Background()) if test.modifyContext != nil { test.modifyContext(ctx) diff --git a/pkg/webhook/validate_unstructured_test.go b/pkg/webhook/validate_unstructured_test.go index 63c172d964df..dc93de65bd63 100644 --- a/pkg/webhook/validate_unstructured_test.go +++ b/pkg/webhook/validate_unstructured_test.go @@ -25,7 +25,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" - rest "k8s.io/client-go/rest" "knative.dev/pkg/apis" fakekubeclient "knative.dev/pkg/client/injection/kube/client/fake" @@ -46,16 +45,12 @@ var ( ) func TestUnstructuredValidation(t *testing.T) { - newCreateWithOptions = newTestPods - tests := []struct { - name string - data map[string]interface{} - want string - podInterface func(client rest.Interface, namespace string) podInterface + name string + data map[string]interface{} + want string }{{ - name: "valid run latest", - podInterface: newTestPods, + name: "valid run latest", data: map[string]interface{}{ "metadata": validMetadata, "spec": map[string]interface{}{ @@ -208,7 +203,6 @@ func TestDryRunFeatureFlag(t *testing.T) { } func TestSkipUpdate(t *testing.T) { - newCreateWithOptions = newFailTestPods validService := &v1.Service{ ObjectMeta: metav1.ObjectMeta{ Annotations: map[string]string{ @@ -247,7 +241,7 @@ func TestSkipUpdate(t *testing.T) { }, }, }, - want: "dry run failed with fail-reason: spec.template", + want: "dry run failed with kubeclient error: spec.template", }, { name: "skip_identical_old", new: validServiceUns,