Skip to content

Commit 727ae19

Browse files
committed
feat: restrict the cache's watches with label selector
1 parent 485f758 commit 727ae19

File tree

7 files changed

+81
-9
lines changed

7 files changed

+81
-9
lines changed

cmd/run/run.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package run
22

33
import (
4+
"github.com/dapr-sandbox/dapr-kubernetes-operator/pkg/resources"
5+
"github.com/pkg/errors"
46
"github.com/spf13/cobra"
7+
admregv1 "k8s.io/api/admissionregistration/v1"
8+
appsv1 "k8s.io/api/apps/v1"
9+
corev1 "k8s.io/api/core/v1"
10+
rbacv1 "k8s.io/api/rbac/v1"
11+
rtcache "sigs.k8s.io/controller-runtime/pkg/cache"
12+
rtclient "sigs.k8s.io/controller-runtime/pkg/client"
513
"sigs.k8s.io/controller-runtime/pkg/manager"
614

715
"github.com/dapr-sandbox/dapr-kubernetes-operator/pkg/controller"
@@ -38,13 +46,34 @@ func NewRunCmd() *cobra.Command {
3846
Use: "run",
3947
Short: "run",
4048
RunE: func(cmd *cobra.Command, args []string) error {
49+
selector, err := daprCtl.ReleaseSelector()
50+
if err != nil {
51+
return errors.Wrap(err, "unable to compute cache's watch selector")
52+
}
53+
54+
controllerOpts.WatchSelectors = map[rtclient.Object]rtcache.ByObject{
55+
// k8s
56+
&rbacv1.ClusterRole{}: {Label: selector},
57+
&rbacv1.ClusterRoleBinding{}: {Label: selector},
58+
&rbacv1.Role{}: {Label: selector},
59+
&rbacv1.RoleBinding{}: {Label: selector},
60+
&admregv1.MutatingWebhookConfiguration{}: {Label: selector},
61+
&corev1.Secret{}: {Label: selector},
62+
&corev1.Service{}: {Label: selector},
63+
&corev1.ServiceAccount{}: {Label: selector},
64+
&appsv1.StatefulSet{}: {Label: selector},
65+
&appsv1.Deployment{}: {Label: selector},
66+
// dapr
67+
resources.UnstructuredFor("dapr.io", "v1alpha1", "Configuration"): {Label: selector},
68+
}
69+
4170
return controller.Start(controllerOpts, func(manager manager.Manager, opts controller.Options) error {
4271
_, err := daprCtl.NewReconciler(cmd.Context(), manager, helmOpts)
4372
if err != nil {
44-
return err
73+
return errors.Wrap(err, "unable to set-up DaprControlPlane reconciler")
4574
}
4675

47-
return err
76+
return nil
4877
})
4978
},
5079
}

internal/controller/operator/dapr_controller_action_apply.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ func (a *ApplyAction) Run(ctx context.Context, rc *ReconciliationRequest) error
107107
r := gvk.GroupVersion().String() + ":" + gvk.Kind
108108

109109
if _, ok := a.subscriptions[r]; !ok {
110+
111+
a.l.Info("watch", "ref", r)
112+
110113
err = rc.Reconciler.Watch(
111114
&obj,
112115
rc.Reconciler.EnqueueRequestForOwner(&daprApi.DaprControlPlane{}, handler.OnlyControllerOwner()),
@@ -135,6 +138,9 @@ func (a *ApplyAction) Run(ctx context.Context, rc *ReconciliationRequest) error
135138
r := gvk.GroupVersion().String() + ":" + gvk.Kind
136139

137140
if _, ok := a.subscriptions[r]; !ok {
141+
142+
a.l.Info("watch", "ref", r)
143+
138144
err = rc.Reconciler.Watch(
139145
&obj,
140146
rc.Reconciler.EnqueueRequestsFromMapFunc(labelsToRequest),
@@ -196,6 +202,7 @@ func (a *ApplyAction) Run(ctx context.Context, rc *ReconciliationRequest) error
196202

197203
a.l.Info("run",
198204
"apply", "true",
205+
"gen", rc.Resource.Generation,
199206
"ref", resources.Ref(&obj))
200207
}
201208

internal/controller/operator/dapr_controller_support.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,21 @@ func dependantWithLabels(watchUpdate bool, watchDelete bool) predicate.Predicate
8888
},
8989
)
9090
}
91+
92+
func ReleaseSelector() (labels.Selector, error) {
93+
hasReleaseNameLabel, err := labels.NewRequirement(DaprReleaseName, selection.Exists, []string{})
94+
if err != nil {
95+
return nil, err
96+
}
97+
98+
hasReleaseNamespaceLabel, err := labels.NewRequirement(DaprReleaseNamespace, selection.Exists, []string{})
99+
if err != nil {
100+
return nil, err
101+
}
102+
103+
selector := labels.NewSelector().
104+
Add(*hasReleaseNameLabel).
105+
Add(*hasReleaseNamespaceLabel)
106+
107+
return selector, nil
108+
}

pkg/controller/controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"net/http/pprof"
66
"time"
77

8+
"sigs.k8s.io/controller-runtime/pkg/cache"
9+
810
"github.com/pkg/errors"
911

1012
"github.com/dapr-sandbox/dapr-kubernetes-operator/pkg/logger"
@@ -48,6 +50,9 @@ func Start(options Options, setup func(manager.Manager, Options) error) error {
4850
Metrics: metricsserver.Options{
4951
BindAddress: options.MetricsAddr,
5052
},
53+
Cache: cache.Options{
54+
ByObject: options.WatchSelectors,
55+
},
5156
})
5257

5358
if err != nil {

pkg/controller/predicates/dependant.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package predicates
22

33
import (
4-
"encoding/json"
54
"reflect"
65

76
"github.com/wI2L/jsondiff"
@@ -93,18 +92,13 @@ func (p DependentPredicate) Update(e event.UpdateEvent) bool {
9392
log.Error(err, "failed to generate diff")
9493
return true
9594
}
96-
d, err := json.Marshal(patch)
97-
if err != nil {
98-
log.Error(err, "failed to generate diff")
99-
return true
100-
}
10195

10296
log.Info("Reconciling due to dependent resource update",
10397
"name", newObj.GetName(),
10498
"namespace", newObj.GetNamespace(),
10599
"apiVersion", newObj.GroupVersionKind().GroupVersion(),
106100
"kind", newObj.GroupVersionKind().Kind,
107-
"diff", string(d))
101+
"diff", patch.String())
108102

109103
return true
110104
}

pkg/controller/types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package controller
22

3+
import (
4+
rtcache "sigs.k8s.io/controller-runtime/pkg/cache"
5+
rtclient "sigs.k8s.io/controller-runtime/pkg/client"
6+
)
7+
38
type ClusterType string
49

510
const (
@@ -15,4 +20,5 @@ type Options struct {
1520
LeaderElectionNamespace string
1621
EnableLeaderElection bool
1722
ReleaseLeaderElectionOnCancel bool
23+
WatchSelectors map[rtclient.Object]rtcache.ByObject
1824
}

pkg/resources/resources.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package resources
33
import (
44
"fmt"
55

6+
"k8s.io/apimachinery/pkg/runtime/schema"
7+
68
"github.com/dapr-sandbox/dapr-kubernetes-operator/pkg/pointer"
79
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
810
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -67,3 +69,14 @@ func Ref(obj *unstructured.Unstructured) string {
6769
name,
6870
)
6971
}
72+
73+
func UnstructuredFor(group string, version string, kind string) *unstructured.Unstructured {
74+
u := unstructured.Unstructured{}
75+
u.SetGroupVersionKind(schema.GroupVersionKind{
76+
Kind: kind,
77+
Group: group,
78+
Version: version,
79+
})
80+
81+
return &u
82+
}

0 commit comments

Comments
 (0)