k8s.io/kubernetes@v1.29.3/test/e2e/apimachinery/protocol.go (about) 1 /* 2 Copyright 2019 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 apimachinery 18 19 import ( 20 "context" 21 "fmt" 22 23 g "github.com/onsi/ginkgo/v2" 24 o "github.com/onsi/gomega" 25 26 v1 "k8s.io/api/core/v1" 27 apierrors "k8s.io/apimachinery/pkg/api/errors" 28 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 29 "k8s.io/apimachinery/pkg/fields" 30 "k8s.io/apimachinery/pkg/watch" 31 "k8s.io/client-go/kubernetes" 32 admissionapi "k8s.io/pod-security-admission/api" 33 34 "k8s.io/kubernetes/test/e2e/framework" 35 ) 36 37 var _ = SIGDescribe("client-go should negotiate", func() { 38 f := framework.NewDefaultFramework("protocol") 39 f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged 40 41 for _, s := range []string{ 42 "application/json", 43 "application/vnd.kubernetes.protobuf", 44 "application/vnd.kubernetes.protobuf,application/json", 45 "application/json,application/vnd.kubernetes.protobuf", 46 } { 47 accept := s 48 g.It(fmt.Sprintf("watch and report errors with accept %q", accept), func() { 49 g.By("creating an object for which we will watch") 50 ns := f.Namespace.Name 51 client := f.ClientSet.CoreV1().ConfigMaps(ns) 52 configMapName := "e2e-client-go-test-negotiation" 53 testConfigMap := &v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: configMapName}} 54 before, err := client.List(context.TODO(), metav1.ListOptions{}) 55 framework.ExpectNoError(err) 56 _, err = client.Create(context.TODO(), testConfigMap, metav1.CreateOptions{}) 57 framework.ExpectNoError(err) 58 opts := metav1.ListOptions{ 59 ResourceVersion: before.ResourceVersion, 60 FieldSelector: fields.SelectorFromSet(fields.Set{"metadata.name": configMapName}).String(), 61 } 62 63 g.By("watching for changes on the object") 64 cfg, err := framework.LoadConfig() 65 framework.ExpectNoError(err) 66 67 cfg.AcceptContentTypes = accept 68 69 c := kubernetes.NewForConfigOrDie(cfg) 70 w, err := c.CoreV1().ConfigMaps(ns).Watch(context.TODO(), opts) 71 framework.ExpectNoError(err) 72 defer w.Stop() 73 74 evt, ok := <-w.ResultChan() 75 o.Expect(ok).To(o.BeTrue()) 76 switch evt.Type { 77 case watch.Added, watch.Modified: 78 // this is allowed 79 case watch.Error: 80 err := apierrors.FromObject(evt.Object) 81 // In Kubernetes 1.17 and earlier, the api server returns both apierrors.StatusReasonExpired and 82 // apierrors.StatusReasonGone for HTTP 410 (Gone) status code responses. In 1.18 the kube server is more consistent 83 // and always returns apierrors.StatusReasonExpired. For backward compatibility we can only remove the apierrs.IsGone 84 // check when we fully drop support for Kubernetes 1.17 servers from reflectors. 85 if apierrors.IsGone(err) || apierrors.IsResourceExpired(err) { 86 // this is allowed, since the kubernetes object could be very old 87 break 88 } 89 if apierrors.IsUnexpectedObjectError(err) { 90 g.Fail(fmt.Sprintf("unexpected object, wanted v1.Status: %#v", evt.Object)) 91 } 92 g.Fail(fmt.Sprintf("unexpected error: %#v", evt.Object)) 93 default: 94 g.Fail(fmt.Sprintf("unexpected type %s: %#v", evt.Type, evt.Object)) 95 } 96 }) 97 } 98 })