From dc8c1b9f9d5b9cd19850f3b4e2651a0f1af42177 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Tue, 12 Nov 2019 21:58:52 +0200 Subject: [PATCH] Exclude the metrics APIs from resources discovery --- go.sum | 1 + pkg/cluster/kubernetes/sync.go | 35 ++++++++++++++++++---------------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/go.sum b/go.sum index 54450fa8b..3e6f28348 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,7 @@ github.com/Azure/go-autorest v11.7.1+incompatible h1:M2YZIajBBVekV86x0rr1443Lc1F github.com/Azure/go-autorest v11.7.1+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802 h1:1BDTz0u9nC3//pOCMdNH+CiXJVYJh5UQNCOBG7jbELc= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo= github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc= diff --git a/pkg/cluster/kubernetes/sync.go b/pkg/cluster/kubernetes/sync.go index b391d5406..8d98ea420 100644 --- a/pkg/cluster/kubernetes/sync.go +++ b/pkg/cluster/kubernetes/sync.go @@ -22,7 +22,6 @@ import ( meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/client-go/discovery" "k8s.io/client-go/rest" "github.com/fluxcd/flux/pkg/cluster" @@ -208,21 +207,25 @@ func (c *Cluster) getAllowedResourcesBySelector(selector string) (map[string]*ku listOptions.LabelSelector = selector } - _, resources, err := c.client.discoveryClient.ServerGroupsAndResources() - if err != nil { - discErr, ok := err.(*discovery.ErrGroupDiscoveryFailed) - if !ok { - return nil, err - } - for gv, e := range discErr.Groups { - if gv.Group == "metrics" || strings.HasSuffix(gv.Group, "metrics.k8s.io") { - // The Metrics API tends to be misconfigured, causing errors. - // We just ignore them, since it doesn't make sense to sync metrics anyways. - continue - } - // Tolerate empty GroupVersions due to e.g. misconfigured custom metrics - if e.Error() != fmt.Sprintf("Got empty response for: %v", gv) { - return nil, err + sgs, err := c.client.discoveryClient.ServerGroups() + if sgs == nil { + return nil, err + } + + resources := []*meta_v1.APIResourceList{} + for i := range sgs.Groups { + gv := sgs.Groups[i].PreferredVersion.GroupVersion + // exclude the *.metrics.k8s.io resources to avoid querying the cluster metrics + if sgs.Groups[i].Name != "metrics.k8s.io" && !strings.HasSuffix(sgs.Groups[i].Name, ".metrics.k8s.io") { + if r, err := c.client.discoveryClient.ServerResourcesForGroupVersion(gv); err == nil { + if r != nil { + resources = append(resources, r) + } + } else { + // ignore errors for resources with empty group version instead of failing to sync + if err.Error() != fmt.Sprintf("Got empty response for: %v", gv) { + return nil, err + } } } }