Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ In dry-run, relative and recurring schedules are logged without saving state or

By default, the controller discovers resources across all namespaces and watches those that support `get`, `list`, `watch`, `patch`, and `delete`. It skips subresources and APIs that lack these verbs. An explicit watch pattern that selects an unsupported API causes a discovery error unless an ignore rule excludes that API.

Resource filters select which API types to watch. Namespace filters limit which objects the controller acts on, but the cache still reads across all namespaces. Namespace filters do not provide tenant isolation.
Resource filters select which API types to watch. The controller reads, deletes, and restarts resources only in the filtered namespaces. Namespace patterns limit deletions and restarts but require cluster-wide reads.

- `--watch-resource` (repeatable) - glob pattern for resources to watch.
- Format - `<resource>.<group>` for grouped APIs (e.g., `deployments.apps`) or `<resource>` for core APIs (e.g., `pods`).
- If not provided, all eligible resources are watched unless excluded by ignore rules. Literal `*.*` is not equivalent to this default: it does not match core keys such as `pods`.
- Broad patterns can select APIs that lack the required verbs. Prefer exact resource names.
- `--ignore-resource` (repeatable) - glob pattern for resources to strictly ignore. Takes precedence over watch rules.
- `--watch-namespace` (repeatable) - glob pattern for namespaces to watch (e.g. `default`, `dev-*`).
- If provided, the controller acts only on objects in matching namespaces. It excludes other cluster-scoped objects, except watched `Namespace` objects whose names match the pattern.
- If provided, the controller deletes or restarts only objects in matching namespaces. Namespace objects also require `--watch-resource=namespaces`. Other cluster-scoped resources are excluded.
- `--ignore-namespace` (repeatable) - glob pattern for namespaces to strictly ignore. Takes precedence over watch rules.

### RBAC
Expand Down
20 changes: 13 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,20 @@ func main() {
metricsServerOptions.KeyName = metricsCertKey
}

scopeConfig := controller.ScopeConfig{
WatchResources: watchResources,
IgnoreResources: ignoreResources,
WatchNamespaces: watchNamespaces,
IgnoreNamespaces: ignoreNamespaces,
}
cacheOptions, err := scopeConfig.CacheOptions()
if err != nil {
setupLog.Error(err, "invalid namespace watch configuration")
os.Exit(1)
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Cache: cacheOptions,
Scheme: scheme,
Metrics: metricsServerOptions,
WebhookServer: webhookServer,
Expand All @@ -190,13 +203,6 @@ func main() {
os.Exit(1)
}

scopeConfig := controller.ScopeConfig{
WatchResources: watchResources,
IgnoreResources: ignoreResources,
WatchNamespaces: watchNamespaces,
IgnoreNamespaces: ignoreNamespaces,
}

setupLog.Info("Starting with scope configuration",
"watchResources", watchResources,
"ignoreResources", ignoreResources,
Expand Down
36 changes: 36 additions & 0 deletions internal/controller/cache_scope.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package controller

import (
"fmt"
"strings"

"k8s.io/apimachinery/pkg/util/validation"
"sigs.k8s.io/controller-runtime/pkg/cache"
)

// CacheOptions restricts API reads when the watch list contains exact namespace names.
func (c ScopeConfig) CacheOptions() (cache.Options, error) {
if len(c.WatchNamespaces) == 0 {
return cache.Options{}, nil
}
for _, namespace := range c.WatchNamespaces {
if strings.ContainsAny(namespace, "*?[]\\") {
return cache.Options{}, nil
}
}
for _, namespace := range c.WatchNamespaces {
if problems := validation.IsDNS1123Label(namespace); len(problems) > 0 {
return cache.Options{}, fmt.Errorf("invalid namespace name %q", namespace)
}
}
options := cache.Options{DefaultNamespaces: make(map[string]cache.Config)}
for _, namespace := range c.WatchNamespaces {
if c.IsNamespaceAllowed(namespace) {
options.DefaultNamespaces[namespace] = cache.Config{}
}
}
if len(options.DefaultNamespaces) == 0 {
return cache.Options{}, fmt.Errorf("watch list requires at least one namespace after ignore rules")
}
return options, nil
}
133 changes: 0 additions & 133 deletions internal/controller/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import (
"errors"
"fmt"
"net/http"
"sort"
"strings"
"sync"
"time"

"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/discovery"
"k8s.io/client-go/util/workqueue"
Expand All @@ -28,9 +25,6 @@ import (

const discoveryRetryInterval = 30 * time.Second

// Every watched resource may carry either deletion or restart annotations.
var requiredWatchVerbs = sets.New("get", "list", "watch", "patch", "delete")

type preferredResourceDiscovery interface {
ServerPreferredResources() ([]*metav1.APIResourceList, error)
}
Expand Down Expand Up @@ -231,133 +225,6 @@ func (*coverageSyncRunnable) NeedLeaderElection() bool {
return true
}

func planResources(lists []*metav1.APIResourceList, config ScopeConfig, logger logr.Logger) (*resourcePlan, error) {
plan := &resourcePlan{counts: make(map[[2]string]int)}
resolvedPatterns := make(map[string]bool, len(config.WatchResources))
var unwatchable []string

for _, list := range lists {
gv, err := schema.ParseGroupVersion(list.GroupVersion)
if err != nil {
plan.count(discoveryResultFailed, discoveryReasonInvalidGroup)
publishPlanMetrics(plan)
logger.Error(err, "Invalid group version returned by discovery", "groupVersion", list.GroupVersion)
return nil, fmt.Errorf("invalid discovered group version %q: %w", list.GroupVersion, err)
}

for _, resource := range list.APIResources {
key := resourceKey(resource.Name, gv.Group)
requested := matchingPatterns(config.WatchResources, key)
ignored := matches(config.IgnoreResources, key)

if ignored {
// Ignore precedence removes this match from the required effective watch set.
for _, pattern := range requested {
resolvedPatterns[pattern] = true
}
plan.skip(logger, key, discoveryReasonConfigured)
continue
}
if len(config.WatchResources) > 0 && len(requested) == 0 {
plan.skip(logger, key, discoveryReasonConfigured)
continue
}
for _, pattern := range requested {
resolvedPatterns[pattern] = true
}
if strings.Contains(resource.Name, "/") {
plan.skip(logger, key, discoveryReasonSubresource)
if len(requested) > 0 {
unwatchable = append(unwatchable, key)
}
continue
}

verbs := sets.New(resource.Verbs...)
if !verbs.HasAll(requiredWatchVerbs.UnsortedList()...) {
plan.skip(logger, key, discoveryReasonUnsupportedVerbs)
if len(requested) > 0 {
unwatchable = append(unwatchable, key)
}
continue
}

plan.resources = append(plan.resources, discoveredResource{
key: key,
gvk: schema.GroupVersionKind{Group: gv.Group, Version: gv.Version, Kind: resource.Kind},
})
plan.count(discoveryResultDiscovered, discoveryReasonWatchable)
}
}

var missing []string
ignoredPatterns := sets.New(config.IgnoreResources...)
for _, pattern := range config.WatchResources {
if !resolvedPatterns[pattern] && !ignoredPatterns.Has(pattern) {
missing = append(missing, pattern)
}
}
sort.Strings(missing)
sort.Strings(unwatchable)
if len(missing) > 0 || len(unwatchable) > 0 {
if len(missing) > 0 {
plan.countBy(discoveryResultFailed, discoveryReasonRequestMissing, len(missing))
for _, pattern := range missing {
logger.Error(errors.New("requested resource pattern was not discovered"), "Required resource is missing", "pattern", pattern)
}
}
if len(unwatchable) > 0 {
plan.countBy(discoveryResultFailed, discoveryReasonRequestUnusable, len(unwatchable))
for _, key := range unwatchable {
logger.Error(errors.New("required watch verbs are unavailable"), "Required resource is unwatchable", "resource", key)
}
}
publishPlanMetrics(plan)
return nil, fmt.Errorf("explicit resource coverage incomplete: missing patterns=%v, unwatchable resources=%v", missing, unwatchable)
}

sort.Slice(plan.resources, func(i, j int) bool { return plan.resources[i].key < plan.resources[j].key })
publishPlanMetrics(plan)
return plan, nil
}

func resourceKey(resource, group string) string {
if group == "" {
return resource
}
return resource + "." + group
}

func matchingPatterns(patterns []string, key string) []string {
var matched []string
for _, pattern := range patterns {
if matches([]string{pattern}, key) {
matched = append(matched, pattern)
}
}
return matched
}

func (p *resourcePlan) count(result, reason string) {
p.counts[[2]string{result, reason}]++
}

func (p *resourcePlan) countBy(result, reason string, count int) {
p.counts[[2]string{result, reason}] += count
}

func (p *resourcePlan) skip(logger logr.Logger, key, reason string) {
p.count(discoveryResultSkipped, reason)
logger.V(1).Info("Skipping discovered resource", "resource", key, "reason", reason)
}

func publishPlanMetrics(plan *resourcePlan) {
resetDiscoveryResourceMetrics()
for labels, count := range plan.counts {
setDiscoveryResourceMetric(labels[0], labels[1], count)
}
}

type trackedSource struct {
name string
source source.TypedSyncingSource[resourceRequest]
Expand Down
Loading
Loading