Skip to content

Commit

Permalink
be resilient to degraded apiservices (#2243) (#2244)
Browse files Browse the repository at this point in the history
  • Loading branch information
ozdanborne authored Oct 27, 2022
1 parent 1d72d3e commit 3106a3b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
16 changes: 6 additions & 10 deletions pkg/controller/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
33 changes: 14 additions & 19 deletions pkg/controller/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
})
Expand Down Expand Up @@ -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
}

0 comments on commit 3106a3b

Please sign in to comment.