Skip to content

Commit 739dbc8

Browse files
committed
Scope Deployment and PodDisruptionBudget cache to maintenance objects
Without ByObject entries these are cached cluster-wide. The maintenance controller only creates them in kube-system with the cobaltcore-maintenance-controller label.
1 parent 0e8e58c commit 739dbc8

1 file changed

Lines changed: 22 additions & 11 deletions

File tree

cmd/main.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ import (
3333

3434
"go.uber.org/zap"
3535
"go.uber.org/zap/zapcore"
36+
appsv1 "k8s.io/api/apps/v1"
3637
corev1 "k8s.io/api/core/v1"
38+
policyv1 "k8s.io/api/policy/v1"
3739
"k8s.io/apimachinery/pkg/labels"
3840
"k8s.io/apimachinery/pkg/runtime"
3941
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -179,7 +181,24 @@ func main() {
179181
bininfo.CommitOr("edge"))
180182

181183
leaderElectionID := "4c28796a.cloud.sap"
182-
var cacheOptions cache.Options
184+
// Deployments and PodDisruptionBudgets owned by the maintenance controller
185+
// always live in kube-system and carry the cobaltcore-maintenance-controller
186+
// label; restrict the cache to exactly those objects.
187+
maintenanceSelector, err := labels.Parse("cobaltcore-maintenance-controller")
188+
if err != nil {
189+
setupLog.Error(err, "unable to parse maintenance label selector")
190+
os.Exit(1)
191+
}
192+
maintenanceCacheConfig := cache.ByObject{
193+
Namespaces: map[string]cache.Config{"kube-system": {}},
194+
Label: maintenanceSelector,
195+
}
196+
cacheOptions := cache.Options{
197+
ByObject: map[client.Object]cache.ByObject{
198+
&appsv1.Deployment{}: maintenanceCacheConfig,
199+
&policyv1.PodDisruptionBudget{}: maintenanceCacheConfig,
200+
},
201+
}
183202
if global.LabelSelector != "" {
184203
setupLog.Info("setting up cache with label selector", "selector", global.LabelSelector)
185204
selector, err := labels.Parse(global.LabelSelector)
@@ -188,16 +207,8 @@ func main() {
188207
os.Exit(1)
189208
}
190209

191-
cacheOptions = cache.Options{
192-
ByObject: map[client.Object]cache.ByObject{
193-
&corev1.Node{}: {
194-
Label: selector,
195-
},
196-
&kvmv1.Hypervisor{}: {
197-
Label: selector,
198-
},
199-
},
200-
}
210+
cacheOptions.ByObject[&corev1.Node{}] = cache.ByObject{Label: selector}
211+
cacheOptions.ByObject[&kvmv1.Hypervisor{}] = cache.ByObject{Label: selector}
201212

202213
h := sha256.New()
203214
h.Write([]byte(leaderElectionID)) // Seed it with something "unique" to the project

0 commit comments

Comments
 (0)