Skip to content

Commit d43e92d

Browse files
Improve caching and fix constant reconciles (#825)
Improve performance by caching objects that only have ClusterExtension as owners. Signed-off-by: Varsha Prasad Narsing <[email protected]>
1 parent 0a58f28 commit d43e92d

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

cmd/manager/main.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ import (
2525
"os"
2626
"time"
2727

28+
"github.com/operator-framework/operator-controller/internal/labels"
29+
2830
"github.com/spf13/pflag"
2931
"go.uber.org/zap/zapcore"
32+
k8slabels "k8s.io/apimachinery/pkg/labels"
33+
"k8s.io/apimachinery/pkg/selection"
3034
_ "k8s.io/client-go/plugin/pkg/client/auth"
3135
ctrl "sigs.k8s.io/controller-runtime"
3236
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -39,12 +43,14 @@ import (
3943
"github.com/operator-framework/rukpak/pkg/storage"
4044
"github.com/operator-framework/rukpak/pkg/util"
4145

46+
"github.com/operator-framework/operator-controller/api/v1alpha1"
4247
"github.com/operator-framework/operator-controller/internal/catalogmetadata/cache"
4348
catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
4449
"github.com/operator-framework/operator-controller/internal/controllers"
4550
"github.com/operator-framework/operator-controller/internal/handler"
4651
"github.com/operator-framework/operator-controller/pkg/features"
4752
"github.com/operator-framework/operator-controller/pkg/scheme"
53+
crcache "sigs.k8s.io/controller-runtime/pkg/cache"
4854
)
4955

5056
var (
@@ -83,13 +89,33 @@ func main() {
8389

8490
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts), zap.StacktraceLevel(zapcore.DPanicLevel)))
8591

92+
if systemNamespace == "" {
93+
systemNamespace = util.PodNamespace()
94+
}
95+
96+
dependentRequirement, err := k8slabels.NewRequirement(labels.OwnerKindKey, selection.In, []string{v1alpha1.ClusterExtensionKind})
97+
if err != nil {
98+
setupLog.Error(err, "unable to create dependent label selector for cache")
99+
os.Exit(1)
100+
}
101+
dependentSelector := k8slabels.NewSelector().Add(*dependentRequirement)
102+
86103
fmt.Println("set up manager")
87104
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
88105
Scheme: scheme.Scheme,
89106
Metrics: server.Options{BindAddress: metricsAddr},
90107
HealthProbeBindAddress: probeAddr,
91108
LeaderElection: enableLeaderElection,
92109
LeaderElectionID: "9c4404e7.operatorframework.io",
110+
Cache: crcache.Options{
111+
ByObject: map[client.Object]crcache.ByObject{
112+
&v1alpha1.ClusterExtension{}: {},
113+
},
114+
DefaultNamespaces: map[string]crcache.Config{
115+
systemNamespace: {},
116+
crcache.AllNamespaces: {LabelSelector: dependentSelector},
117+
},
118+
},
93119
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
94120
// when the Manager ends. This requires the binary to immediately end when the
95121
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
@@ -124,10 +150,6 @@ func main() {
124150
os.Exit(1)
125151
}
126152

127-
if systemNamespace == "" {
128-
systemNamespace = util.PodNamespace()
129-
}
130-
131153
unpacker, err := source.NewDefaultUnpacker(mgr, systemNamespace, unpackImage, (*x509.CertPool)(nil))
132154
if err != nil {
133155
setupLog.Error(err, "unable to create unpacker")

0 commit comments

Comments
 (0)