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

Commit

Permalink
Make Kubernetes resources exclusion configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprodan committed Jan 16, 2020
1 parent 57802b0 commit c405035
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
6 changes: 3 additions & 3 deletions cmd/fluxd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ func main() {
k8sNamespaceWhitelist = fs.StringSlice("k8s-namespace-whitelist", []string{}, "restrict the view of the cluster to the namespaces listed. All namespaces are included if this is not set")
k8sAllowNamespace = fs.StringSlice("k8s-allow-namespace", []string{}, "restrict all operations to the provided namespaces")
k8sDefaultNamespace = fs.String("k8s-default-namespace", "", "the namespace to use for resources where a namespace is not specified")

k8sVerbosity = fs.Int("k8s-verbosity", 0, "klog verbosity level")
k8sExcludeResource = fs.StringSlice("k8s-exclude-resource", []string{"*metrics.k8s.io/*", "webhook.certmanager.k8s.io/*", "v1/Event"}, "do not attempt to obtain cluster resources whose group/version/kind matches these glob expressions")
k8sVerbosity = fs.Int("k8s-verbosity", 0, "klog verbosity level")

// SSH key generation
sshKeyBits = optionalVar(fs, &ssh.KeyBitsValue{}, "ssh-keygen-bits", "-b argument to ssh-keygen (default unspecified)")
Expand Down Expand Up @@ -505,7 +505,7 @@ func main() {
for _, n := range append(*k8sNamespaceWhitelist, *k8sAllowNamespace...) {
allowedNamespaces[n] = struct{}{}
}
k8sInst := kubernetes.NewCluster(client, kubectlApplier, sshKeyRing, logger, allowedNamespaces, *registryExcludeImage)
k8sInst := kubernetes.NewCluster(client, kubectlApplier, sshKeyRing, logger, allowedNamespaces, *registryExcludeImage, *k8sExcludeResource)
k8sInst.GC = *syncGC
k8sInst.DryGC = *dryGC

Expand Down
22 changes: 12 additions & 10 deletions pkg/cluster/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,22 @@ type Cluster struct {
allowedNamespaces map[string]struct{}
loggedAllowedNS map[string]bool // to keep track of whether we've logged a problem with seeing an allowed namespace

imageExcludeList []string
mu sync.Mutex
imageExcludeList []string
resourceExcludeList []string
mu sync.Mutex
}

// NewCluster returns a usable cluster.
func NewCluster(client ExtendedClient, applier Applier, sshKeyRing ssh.KeyRing, logger log.Logger, allowedNamespaces map[string]struct{}, imageExcludeList []string) *Cluster {
func NewCluster(client ExtendedClient, applier Applier, sshKeyRing ssh.KeyRing, logger log.Logger, allowedNamespaces map[string]struct{}, imageExcludeList []string, resourceExcludeList []string) *Cluster {
c := &Cluster{
client: client,
applier: applier,
logger: logger,
sshKeyRing: sshKeyRing,
allowedNamespaces: allowedNamespaces,
loggedAllowedNS: map[string]bool{},
imageExcludeList: imageExcludeList,
client: client,
applier: applier,
logger: logger,
sshKeyRing: sshKeyRing,
allowedNamespaces: allowedNamespaces,
loggedAllowedNS: map[string]bool{},
imageExcludeList: imageExcludeList,
resourceExcludeList: resourceExcludeList,
}

return c
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func testGetAllowedNamespaces(t *testing.T, namespace []string, expected []strin
for _, n := range namespace {
allowedNamespaces[n] = struct{}{}
}
c := NewCluster(client, nil, nil, log.NewNopLogger(), allowedNamespaces, []string{})
c := NewCluster(client, nil, nil, log.NewNopLogger(), allowedNamespaces, []string{}, []string{})

namespaces, err := c.getAllowedAndExistingNamespaces(context.Background())
if err != nil {
Expand Down
30 changes: 26 additions & 4 deletions pkg/cluster/kubernetes/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/ryanuber/go-glob"
"io"
"os/exec"
"sort"
Expand Down Expand Up @@ -201,7 +202,28 @@ func (r *kuberesource) GetGCMark() string {
return r.obj.GetLabels()[gcMarkLabel]
}

var excludedGroups = []string{"metrics.k8s.io", "webhook.certmanager.k8s.io"}
func (c *Cluster) filterResources(resources *meta_v1.APIResourceList) *meta_v1.APIResourceList {
list := []meta_v1.APIResource{}
for _, apiResource := range resources.APIResources {
fullName := fmt.Sprintf("%s/%s", resources.GroupVersion, apiResource.Kind)
excluded := false
for _, exp := range c.resourceExcludeList {
if glob.Glob(exp, fullName) {
excluded = true
break
}
}
if !excluded {
list = append(list, apiResource)
}
}

return &meta_v1.APIResourceList{
TypeMeta: resources.TypeMeta,
GroupVersion: resources.GroupVersion,
APIResources: list,
}
}

func (c *Cluster) getAllowedResourcesBySelector(selector string) (map[string]*kuberesource, error) {
listOptions := meta_v1.ListOptions{}
Expand All @@ -219,8 +241,8 @@ func (c *Cluster) getAllowedResourcesBySelector(selector string) (map[string]*ku
gv := sgs.Groups[i].PreferredVersion.GroupVersion

excluded := false
for _, exg := range excludedGroups {
if strings.HasSuffix(sgs.Groups[i].Name, exg) {
for _, exp := range c.resourceExcludeList {
if glob.Glob(exp, fmt.Sprintf("%s/", gv)) {
excluded = true
break
}
Expand All @@ -229,7 +251,7 @@ func (c *Cluster) getAllowedResourcesBySelector(selector string) (map[string]*ku
if !excluded {
if r, err := c.client.discoveryClient.ServerResourcesForGroupVersion(gv); err == nil {
if r != nil {
resources = append(resources, r)
resources = append(resources, c.filterResources(r))
}
} else {
// ignore errors for resources with empty group version instead of failing to sync
Expand Down
7 changes: 4 additions & 3 deletions pkg/cluster/kubernetes/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,10 @@ func setup(t *testing.T) (*Cluster, *fakeApplier, func()) {
clients, cancel := fakeClients()
applier := &fakeApplier{dynamicClient: clients.dynamicClient, coreClient: clients.coreClient, defaultNS: defaultTestNamespace}
kube := &Cluster{
applier: applier,
client: clients,
logger: log.NewLogfmtLogger(os.Stdout),
applier: applier,
client: clients,
logger: log.NewLogfmtLogger(os.Stdout),
resourceExcludeList: []string{"*metrics.k8s.io/*", "webhook.certmanager.k8s.io/v1beta1/*"},
}
return kube, applier, cancel
}
Expand Down

0 comments on commit c405035

Please sign in to comment.