Skip to content

Commit 4062d6a

Browse files
authored
Merge pull request #789 from fluxcd/no-cache-secrets
Disable caching of Secrets and ConfigMaps
2 parents 60ea92d + c877c07 commit 4062d6a

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

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 kustomize-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.33
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
@@ -22,18 +22,21 @@ import (
2222
"time"
2323

2424
flag "github.com/spf13/pflag"
25+
corev1 "k8s.io/api/core/v1"
2526
"k8s.io/apimachinery/pkg/runtime"
2627
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
2728
_ "k8s.io/client-go/plugin/pkg/client/auth/azure"
2829
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
2930
"sigs.k8s.io/cli-utils/pkg/kstatus/polling"
3031
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/engine"
3132
ctrl "sigs.k8s.io/controller-runtime"
33+
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
3234

3335
"github.com/fluxcd/pkg/runtime/acl"
3436
runtimeClient "github.com/fluxcd/pkg/runtime/client"
3537
runtimeCtrl "github.com/fluxcd/pkg/runtime/controller"
3638
"github.com/fluxcd/pkg/runtime/events"
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"
@@ -42,6 +45,7 @@ import (
4245

4346
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta2"
4447
"github.com/fluxcd/kustomize-controller/controllers"
48+
"github.com/fluxcd/kustomize-controller/internal/features"
4549
"github.com/fluxcd/kustomize-controller/internal/statusreaders"
4650
// +kubebuilder:scaffold:imports
4751
)
@@ -78,6 +82,7 @@ func main() {
7882
noRemoteBases bool
7983
httpRetry int
8084
defaultServiceAccount string
85+
featureGates feathelper.FeatureGates
8186
)
8287

8388
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
@@ -91,21 +96,39 @@ func main() {
9196
"Disallow remote bases usage in Kustomize overlays. When this flag is enabled, all resources must refer to local files included in the source artifact.")
9297
flag.IntVar(&httpRetry, "http-retry", 9, "The maximum number of retries when failing to fetch artifacts over HTTP.")
9398
flag.StringVar(&defaultServiceAccount, "default-service-account", "", "Default service account used for impersonation.")
99+
94100
clientOptions.BindFlags(flag.CommandLine)
95101
logOptions.BindFlags(flag.CommandLine)
96102
leaderElectionOptions.BindFlags(flag.CommandLine)
97103
aclOptions.BindFlags(flag.CommandLine)
98104
kubeConfigOpts.BindFlags(flag.CommandLine)
99105
rateLimiterOptions.BindFlags(flag.CommandLine)
106+
featureGates.BindFlags(flag.CommandLine)
107+
100108
flag.Parse()
101109

110+
if err := featureGates.WithLogger(setupLog).SupportedFeatures(features.FeatureGates()); err != nil {
111+
setupLog.Error(err, "unable to load feature gates")
112+
os.Exit(1)
113+
}
114+
102115
ctrl.SetLogger(logger.NewLogger(logOptions))
103116

104117
watchNamespace := ""
105118
if !watchAllNamespaces {
106119
watchNamespace = os.Getenv("RUNTIME_NAMESPACE")
107120
}
108121

122+
var disableCacheFor []ctrlclient.Object
123+
shouldCache, err := features.Enabled(features.CacheSecretsAndConfigMaps)
124+
if err != nil {
125+
setupLog.Error(err, "unable to check feature gate "+features.CacheSecretsAndConfigMaps)
126+
os.Exit(1)
127+
}
128+
if !shouldCache {
129+
disableCacheFor = append(disableCacheFor, &corev1.Secret{}, &corev1.ConfigMap{})
130+
}
131+
109132
restConfig := runtimeClient.GetConfigOrDie(clientOptions)
110133
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
111134
Scheme: scheme,
@@ -120,6 +143,7 @@ func main() {
120143
LeaderElectionID: fmt.Sprintf("%s-leader-election", controllerName),
121144
Namespace: watchNamespace,
122145
Logger: ctrl.Log,
146+
ClientDisableCacheFor: disableCacheFor,
123147
})
124148
if err != nil {
125149
setupLog.Error(err, "unable to start manager")

0 commit comments

Comments
 (0)