From 3106a3b94a81a2ffb7499a05209ba9fa39d993ab Mon Sep 17 00:00:00 2001 From: "Dan (Turk) Osborne" Date: Thu, 27 Oct 2022 18:13:20 -0400 Subject: [PATCH] be resilient to degraded apiservices (#2243) (#2244) --- pkg/controller/utils/utils.go | 16 ++++++--------- pkg/controller/utils/utils_test.go | 33 +++++++++++++----------------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/pkg/controller/utils/utils.go b/pkg/controller/utils/utils.go index 51a22af044..fb8caca401 100644 --- a/pkg/controller/utils/utils.go +++ b/pkg/controller/utils/utils.go @@ -436,7 +436,7 @@ func WaitToAddResourceWatch(controller controller.Controller, client kubernetes. duration = maxDuration } ticker.Reset(duration) - if isResourceReady(client, obj.GetObjectKind().GroupVersionKind().Kind) { + if isCalicoResourceReady(client, obj.GetObjectKind().GroupVersionKind().Kind) { err := controller.Watch(&source.Kind{Type: obj}, &handler.EnqueueRequestForObject{}) if err != nil { log.Info("failed to watch %s resource: %v. Will retry to add watch", obj.GetObjectKind().GroupVersionKind().Kind, err) @@ -448,18 +448,14 @@ func WaitToAddResourceWatch(controller controller.Controller, client kubernetes. } } -func isResourceReady(client kubernetes.Interface, resourceKind string) bool { - _, res, err := client.Discovery().ServerGroupsAndResources() +func isCalicoResourceReady(client kubernetes.Interface, resourceKind string) bool { + res, err := client.Discovery().ServerResourcesForGroupVersion(v3.GroupVersionCurrent) if err != nil { return false } - for _, group := range res { - if group.GroupVersion == v3.GroupVersionCurrent { - for _, r := range group.APIResources { - if r.Kind == resourceKind { - return true - } - } + for _, r := range res.APIResources { + if resourceKind == r.Kind { + return true } } return false diff --git a/pkg/controller/utils/utils_test.go b/pkg/controller/utils/utils_test.go index 17ff20b33d..9982b49499 100644 --- a/pkg/controller/utils/utils_test.go +++ b/pkg/controller/utils/utils_test.go @@ -105,27 +105,22 @@ var _ = Describe("Tigera License polling test", func() { }) It("should be able to verify that the LicenseKey is ready", func() { - discovery.On("ServerGroupsAndResources").Return(nil, []*metav1.APIResourceList{ - { - GroupVersion: "projectcalico.org/v3", - APIResources: []metav1.APIResource{ - {Kind: "LicenseKey"}, - }, - }, + discovery.On("ServerResourcesForGroupVersion", v3.GroupVersionCurrent).Return(&metav1.APIResourceList{ + APIResources: []metav1.APIResource{{ + Kind: "LicenseKey", + }}, }) - Expect(isResourceReady(client, v3.KindLicenseKey)).To(BeTrue()) + Expect(isCalicoResourceReady(client, v3.KindLicenseKey)).To(BeTrue()) discovery.AssertExpectations(GinkgoT()) }) + It("should be able to verify that the LicenseKey is not ready", func() { - discovery.On("ServerGroupsAndResources").Return(nil, []*metav1.APIResourceList{ - { - GroupVersion: "apps/v1", - APIResources: []metav1.APIResource{ - {Kind: "Deployment"}, - }, - }, + discovery.On("ServerResourcesForGroupVersion", v3.GroupVersionCurrent).Return(&metav1.APIResourceList{ + APIResources: []metav1.APIResource{{ + Kind: "Deployment", + }}, }) - Expect(isResourceReady(client, v3.KindLicenseKey)).To(BeFalse()) + Expect(isCalicoResourceReady(client, v3.KindLicenseKey)).To(BeFalse()) discovery.AssertExpectations(GinkgoT()) }) }) @@ -197,7 +192,7 @@ func (m fakeClient) Discovery() discovery.DiscoveryInterface { return m.discovery } -func (m *fakeDiscovery) ServerGroupsAndResources() ([]*metav1.APIGroup, []*metav1.APIResourceList, error) { - args := m.Called() - return nil, args.Get(1).([]*metav1.APIResourceList), nil +func (m *fakeDiscovery) ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error) { + args := m.Called(groupVersion) + return args.Get(0).(*metav1.APIResourceList), nil }