forked from hashicorp/vault-secrets-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
278 lines (254 loc) · 9.67 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"os"
"time"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
secretsv1alpha1 "github.com/hashicorp/vault-secrets-operator/api/v1alpha1"
"github.com/hashicorp/vault-secrets-operator/controllers"
"github.com/hashicorp/vault-secrets-operator/internal/metrics"
vclient "github.com/hashicorp/vault-secrets-operator/internal/vault"
"github.com/hashicorp/vault-secrets-operator/internal/version"
//+kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
finalizerCleanupLog = ctrl.Log.WithName("cleanup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(secretsv1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
func main() {
persistenceModelNone := "none"
persistenceModelDirectUnencrypted := "direct-unencrypted"
persistenceModelDirectEncrypted := "direct-encrypted"
defaultPersistenceModel := persistenceModelNone
vdsOptions := controller.Options{}
cfc := vclient.DefaultCachingClientFactoryConfig()
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
var clientCachePersistenceModel string
var printVersion bool
var outputFormat string
var finalizerCleanup bool
flag.BoolVar(&printVersion, "version", false, "Print the operator version information")
flag.StringVar(&outputFormat, "output", "", "Output format for the operator version information (yaml or json)")
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", true,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.IntVar(&cfc.ClientCacheSize, "client-cache-size", cfc.ClientCacheSize,
"Size of the in-memory LRU client cache.")
flag.StringVar(&clientCachePersistenceModel, "client-cache-persistence-model", defaultPersistenceModel,
fmt.Sprintf(
"The type of client cache persistence model that should be employed."+
"choices=%v", []string{persistenceModelDirectUnencrypted, persistenceModelDirectEncrypted, persistenceModelNone}))
flag.IntVar(&vdsOptions.MaxConcurrentReconciles, "max-concurrent-reconciles-vds", 100,
"Maximum number of concurrent reconciles for the VaultDynamicSecrets controller.")
flag.BoolVar(&finalizerCleanup, "finalizer-cleanup", false, "Remove finalizers from all CRs in preparation for shutdown.")
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
// versionInfo is used when setting up the buildInfo metric below
versionInfo := version.Version()
if printVersion {
outputString := ""
switch outputFormat {
case "":
outputString = fmt.Sprintf("%#v\n", versionInfo)
case "yaml":
yamlBytes, err := yaml.Marshal(&versionInfo)
if err != nil {
os.Exit(1)
}
outputString = string(yamlBytes)
case "json":
jsonBytes, err := json.MarshalIndent(&versionInfo, "", " ")
if err != nil {
os.Exit(1)
}
outputString = string(jsonBytes)
default:
if _, err := os.Stderr.WriteString("--output should be either 'yaml' or 'json'\n"); err != nil {
os.Exit(1)
}
os.Exit(1)
}
if _, err := os.Stdout.WriteString(outputString); err != nil {
os.Exit(1)
}
os.Exit(0)
}
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
config := ctrl.GetConfigOrDie()
// This flag is passed by the pre-delete hook on helm uninstall.
if finalizerCleanup {
finalizerCleanupLog.Info("commencing cleanup of finalizers")
var finalizerCleanupClient client.Client
finalizerCleanupClient, err := client.New(config, client.Options{
Scheme: scheme,
})
d := time.Now().Add(time.Second * 60)
shutdownCtx, cancel := context.WithDeadline(context.Background(), d)
// Even though ctx will be expired, it is good practice to call its
// cancellation function in any case. Failure to do so may keep the
// context and its parent alive longer than necessary.
defer cancel()
finalizerCleanupLog.Info("deleting finalizers")
if err = controllers.RemoveAllFinalizers(shutdownCtx, finalizerCleanupClient, finalizerCleanupLog); err != nil {
finalizerCleanupLog.Error(err, "unable to remove finalizers")
os.Exit(1)
}
return
}
mgr, err := ctrl.NewManager(config, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "b0d477c0.hashicorp.com",
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
// when the Manager ends. This requires the binary to immediately end when the
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
// speeds up voluntary leader transitions as the new leader don't have to wait
// LeaseDuration time first.
//
// In the default scaffold provided, the program ends immediately after
// the manager stops, so would be fine to enable this option. However,
// if you are doing or is intended to do any operation such as perform cleanups
// after the manager stops then its usage might be unsafe.
//LeaderElectionReleaseOnCancel: true,
})
if err != nil {
setupLog.Error(err, "Unable to start manager")
os.Exit(1)
}
ctx := ctrl.SetupSignalHandler()
collectMetrics := metricsAddr != ""
if collectMetrics {
cfc.MetricsRegistry.MustRegister(
metrics.NewBuildInfoGauge(versionInfo),
)
vclient.MustRegisterClientMetrics(cfc.MetricsRegistry)
}
var clientFactory vclient.CachingClientFactory
{
switch clientCachePersistenceModel {
case persistenceModelDirectUnencrypted:
cfc.Persist = true
case persistenceModelDirectEncrypted:
cfc.Persist = true
cfc.StorageConfig.EnforceEncryption = true
case persistenceModelNone:
cfc.Persist = false
default:
setupLog.Error(errors.New("invalid option"),
fmt.Sprintf("Invalid cache pesistence model %q", clientCachePersistenceModel))
os.Exit(1)
}
defaultClient, err := client.New(config, client.Options{
Scheme: mgr.GetScheme(),
})
if err != nil {
setupLog.Error(err, "Failed to instantiate a default Client")
os.Exit(1)
}
cfc.CollectClientCacheMetrics = collectMetrics
cfc.Recorder = mgr.GetEventRecorderFor("vaultClientFactory")
clientFactory, err = vclient.InitCachingClientFactory(ctx, defaultClient, cfc)
if err != nil {
setupLog.Error(err, "Failed to setup the Vault ClientFactory")
os.Exit(1)
}
}
if err = (&controllers.VaultStaticSecretReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("VaultStaticSecret"),
HMACFunc: vclient.NewHMACFromSecretFunc(cfc.StorageConfig.HMACSecretObjKey),
ValidateMACFunc: vclient.NewMACValidateFromSecretFunc(cfc.StorageConfig.HMACSecretObjKey),
ClientFactory: clientFactory,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "Unable to create controller", "controller", "VaultStaticSecret")
os.Exit(1)
}
if err = (&controllers.VaultPKISecretReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ClientFactory: clientFactory,
Recorder: mgr.GetEventRecorderFor("VaultPKISecret"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "Unable to create controller", "controller", "VaultPKISecret")
os.Exit(1)
}
if err = (&controllers.VaultAuthReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("VaultAuth"),
ClientFactory: clientFactory,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "Unable to create controller", "controller", "VaultAuth")
os.Exit(1)
}
if err = (&controllers.VaultConnectionReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("VaultConnection"),
ClientFactory: clientFactory,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "Unable to create controller", "controller", "VaultConnection")
os.Exit(1)
}
if err = (&controllers.VaultDynamicSecretReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("VaultDynamicSecret"),
ClientFactory: clientFactory,
}).SetupWithManager(mgr, vdsOptions); err != nil {
setupLog.Error(err, "Unable to create controller", "controller", "VaultDynamicSecret")
os.Exit(1)
}
//+kubebuilder:scaffold:builder
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "Unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "Unable to set up ready check")
os.Exit(1)
}
setupLog.Info("Starting manager",
"clientCachePersistenceModel", clientCachePersistenceModel,
"clientCacheSize", cfc.ClientCacheSize,
)
mgr.GetCache()
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}