protocol.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package apimachinery
  14. import (
  15. "context"
  16. "fmt"
  17. "strconv"
  18. g "github.com/onsi/ginkgo"
  19. o "github.com/onsi/gomega"
  20. apierrors "k8s.io/apimachinery/pkg/api/errors"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/watch"
  23. "k8s.io/client-go/kubernetes"
  24. "k8s.io/kubernetes/test/e2e/framework"
  25. )
  26. var _ = SIGDescribe("client-go should negotiate", func() {
  27. f := framework.NewDefaultFramework("protocol")
  28. f.SkipNamespaceCreation = true
  29. for _, s := range []string{
  30. "application/json",
  31. "application/vnd.kubernetes.protobuf",
  32. "application/vnd.kubernetes.protobuf,application/json",
  33. "application/json,application/vnd.kubernetes.protobuf",
  34. } {
  35. accept := s
  36. g.It(fmt.Sprintf("watch and report errors with accept %q", accept), func() {
  37. cfg, err := framework.LoadConfig()
  38. framework.ExpectNoError(err)
  39. cfg.AcceptContentTypes = accept
  40. c := kubernetes.NewForConfigOrDie(cfg)
  41. svcs, err := c.CoreV1().Services("default").Get(context.TODO(), "kubernetes", metav1.GetOptions{})
  42. framework.ExpectNoError(err)
  43. rv, err := strconv.Atoi(svcs.ResourceVersion)
  44. framework.ExpectNoError(err)
  45. w, err := c.CoreV1().Services("default").Watch(context.TODO(), metav1.ListOptions{ResourceVersion: strconv.Itoa(rv - 1)})
  46. framework.ExpectNoError(err)
  47. defer w.Stop()
  48. evt, ok := <-w.ResultChan()
  49. o.Expect(ok).To(o.BeTrue())
  50. switch evt.Type {
  51. case watch.Added, watch.Modified:
  52. // this is allowed
  53. case watch.Error:
  54. err := apierrors.FromObject(evt.Object)
  55. // In Kubernetes 1.17 and earlier, the api server returns both apierrors.StatusReasonExpired and
  56. // apierrors.StatusReasonGone for HTTP 410 (Gone) status code responses. In 1.18 the kube server is more consistent
  57. // and always returns apierrors.StatusReasonExpired. For backward compatibility we can only remove the apierrs.IsGone
  58. // check when we fully drop support for Kubernetes 1.17 servers from reflectors.
  59. if apierrors.IsGone(err) || apierrors.IsResourceExpired(err) {
  60. // this is allowed, since the kubernetes object could be very old
  61. break
  62. }
  63. if apierrors.IsUnexpectedObjectError(err) {
  64. g.Fail(fmt.Sprintf("unexpected object, wanted v1.Status: %#v", evt.Object))
  65. }
  66. g.Fail(fmt.Sprintf("unexpected error: %#v", evt.Object))
  67. default:
  68. g.Fail(fmt.Sprintf("unexpected type %s: %#v", evt.Type, evt.Object))
  69. }
  70. })
  71. }
  72. })