Skip to content

Commit

Permalink
Merge pull request #524 from mvshao/restrict-namespace
Browse files Browse the repository at this point in the history
Restrict the controller to watch only kcp-system namespace
  • Loading branch information
kyma-bot authored Nov 26, 2024
2 parents e8fe97e + 6b0f498 commit d45e2fe
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ import (
"github.com/kyma-project/infrastructure-manager/pkg/gardener/kubeconfig"
"github.com/kyma-project/infrastructure-manager/pkg/gardener/shoot/extender/auditlogs"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
k8slabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/flowcontrol"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
Expand Down Expand Up @@ -133,6 +136,7 @@ func main() {
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "f1c68560.kyma-project.io",
Cache: restrictWatchedNamespace(),
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
// when the Manager ends. This requires the binary to immediately end when the
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
Expand Down Expand Up @@ -334,3 +338,26 @@ func refreshRuntimeMetrics(restConfig *rest.Config, logger logr.Logger, metrics
metrics.SetRuntimeStates(rt)
}
}

func restrictWatchedNamespace() cache.Options {
return cache.Options{
ByObject: map[client.Object]cache.ByObject{
&corev1.Secret{}: {
Label: k8slabels.Everything(),
Namespaces: map[string]cache.Config{
"kcp-system": {},
},
},
&infrastructuremanagerv1.Runtime{}: {
Namespaces: map[string]cache.Config{
"kcp-system": {},
},
},
&infrastructuremanagerv1.GardenerCluster{}: {
Namespaces: map[string]cache.Config{
"kcp-system": {},
},
},
},
}
}

0 comments on commit d45e2fe

Please sign in to comment.