Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Exclude the metrics APIs from resources discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprodan committed Nov 12, 2019
1 parent c7ccb73 commit 1f7340f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
36 changes: 20 additions & 16 deletions pkg/cluster/kubernetes/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -208,21 +207,23 @@ 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 !strings.Contains(sgs.Groups[i].Name, "metrics.k8s.io") {
if r, err := c.client.discoveryClient.ServerResourcesForGroupVersion(gv); err == 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
}
}
}
}
Expand All @@ -239,6 +240,9 @@ func (c *Cluster) getAllowedResourcesBySelector(selector string) (map[string]*ku
}

for _, resource := range resources {
if resource == nil {
continue
}
for _, apiResource := range resource.APIResources {
verbs := apiResource.Verbs
if !contains(verbs, "list") {
Expand Down

0 comments on commit 1f7340f

Please sign in to comment.