Skip to content

Commit 2f6df31

Browse files
authored
Merge pull request #468 from fluxcd/no-cache-secrets
Disable caching of Secrets and ConfigMaps
2 parents a16f513 + c90e58e commit 2f6df31

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

internal/features/features.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2023 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package features sets the feature gates that notification-controller supports,
18+
// and their default states.
19+
package features
20+
21+
import feathelper "github.com/fluxcd/pkg/runtime/features"
22+
23+
const (
24+
// CacheSecretsAndConfigMaps controls whether Secrets and ConfigMaps should
25+
// be cached.
26+
//
27+
// When enabled, it will cache both object types, resulting in increased
28+
// memory usage and cluster-wide RBAC permissions (list and watch).
29+
CacheSecretsAndConfigMaps = "CacheSecretsAndConfigMaps"
30+
)
31+
32+
var features = map[string]bool{
33+
// CacheSecretsAndConfigMaps
34+
// opt-in from v0.31
35+
CacheSecretsAndConfigMaps: false,
36+
}
37+
38+
// FeatureGates contains a list of all supported feature gates and
39+
// their default values.
40+
func FeatureGates() map[string]bool {
41+
return features
42+
}
43+
44+
// Enabled verifies whether the feature is enabled or not.
45+
//
46+
// This is only a wrapper around the Enabled func in
47+
// pkg/runtime/features, so callers won't need to import both packages
48+
// for checking whether a feature is enabled.
49+
func Enabled(feature string) (bool, error) {
50+
return feathelper.Enabled(feature)
51+
}
52+
53+
// Disable disables the specified feature. If the feature is not
54+
// present, it's a no-op.
55+
func Disable(feature string) {
56+
if _, ok := features[feature]; ok {
57+
features[feature] = false
58+
}
59+
}

main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,26 @@ import (
2525
prommetrics "github.com/slok/go-http-metrics/metrics/prometheus"
2626
"github.com/slok/go-http-metrics/middleware"
2727
flag "github.com/spf13/pflag"
28+
corev1 "k8s.io/api/core/v1"
2829
"k8s.io/apimachinery/pkg/runtime"
2930
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3031
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
3132
ctrl "sigs.k8s.io/controller-runtime"
33+
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
3234
crtlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
3335

3436
"github.com/fluxcd/pkg/runtime/acl"
3537
"github.com/fluxcd/pkg/runtime/client"
3638
helper "github.com/fluxcd/pkg/runtime/controller"
39+
feathelper "github.com/fluxcd/pkg/runtime/features"
3740
"github.com/fluxcd/pkg/runtime/leaderelection"
3841
"github.com/fluxcd/pkg/runtime/logger"
3942
"github.com/fluxcd/pkg/runtime/pprof"
4043
"github.com/fluxcd/pkg/runtime/probes"
4144

4245
apiv1 "github.com/fluxcd/notification-controller/api/v1beta2"
4346
"github.com/fluxcd/notification-controller/controllers"
47+
"github.com/fluxcd/notification-controller/internal/features"
4448
"github.com/fluxcd/notification-controller/internal/server"
4549
// +kubebuilder:scaffold:imports
4650
)
@@ -73,6 +77,7 @@ func main() {
7377
leaderElectionOptions leaderelection.Options
7478
aclOptions acl.Options
7579
rateLimiterOptions helper.RateLimiterOptions
80+
featureGates feathelper.FeatureGates
7681
)
7782

7883
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
@@ -83,13 +88,21 @@ func main() {
8388
flag.BoolVar(&watchAllNamespaces, "watch-all-namespaces", true,
8489
"Watch for custom resources in all namespaces, if set to false it will only watch the runtime namespace.")
8590
flag.DurationVar(&rateLimitInterval, "rate-limit-interval", 5*time.Minute, "Interval in which rate limit has effect.")
91+
8692
clientOptions.BindFlags(flag.CommandLine)
8793
logOptions.BindFlags(flag.CommandLine)
8894
leaderElectionOptions.BindFlags(flag.CommandLine)
8995
aclOptions.BindFlags(flag.CommandLine)
9096
rateLimiterOptions.BindFlags(flag.CommandLine)
97+
featureGates.BindFlags(flag.CommandLine)
98+
9199
flag.Parse()
92100

101+
if err := featureGates.WithLogger(setupLog).SupportedFeatures(features.FeatureGates()); err != nil {
102+
setupLog.Error(err, "unable to load feature gates")
103+
os.Exit(1)
104+
}
105+
93106
log := logger.NewLogger(logOptions)
94107
ctrl.SetLogger(log)
95108

@@ -98,6 +111,16 @@ func main() {
98111
watchNamespace = os.Getenv("RUNTIME_NAMESPACE")
99112
}
100113

114+
var disableCacheFor []ctrlclient.Object
115+
shouldCache, err := features.Enabled(features.CacheSecretsAndConfigMaps)
116+
if err != nil {
117+
setupLog.Error(err, "unable to check feature gate "+features.CacheSecretsAndConfigMaps)
118+
os.Exit(1)
119+
}
120+
if !shouldCache {
121+
disableCacheFor = append(disableCacheFor, &corev1.Secret{}, &corev1.ConfigMap{})
122+
}
123+
101124
restConfig := client.GetConfigOrDie(clientOptions)
102125
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
103126
Scheme: scheme,
@@ -112,6 +135,7 @@ func main() {
112135
LeaderElectionID: fmt.Sprintf("%s-leader-election", controllerName),
113136
Namespace: watchNamespace,
114137
Logger: ctrl.Log,
138+
ClientDisableCacheFor: disableCacheFor,
115139
})
116140
if err != nil {
117141
setupLog.Error(err, "unable to start manager")

0 commit comments

Comments
 (0)