-
Notifications
You must be signed in to change notification settings - Fork 139
/
cmd.go
402 lines (359 loc) · 13.2 KB
/
cmd.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
Copyright 2018 The OpenShift Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"context"
"flag"
"io/ioutil"
golog "log"
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
"time"
"github.com/go-logr/logr"
"github.com/golang/glog"
"github.com/google/uuid"
"github.com/openshift/cloud-credential-operator/pkg/operator/constants"
"github.com/openshift/cloud-credential-operator/pkg/operator/credentialsrequest"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
configv1 "github.com/openshift/api/config/v1"
minterv1 "github.com/openshift/cloud-credential-operator/pkg/apis/cloudcredential/v1"
controller "github.com/openshift/cloud-credential-operator/pkg/operator"
"github.com/openshift/cloud-credential-operator/pkg/operator/platform"
"github.com/openshift/cloud-credential-operator/pkg/util"
"github.com/openshift/library-go/pkg/controller/fileobserver"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"sigs.k8s.io/controller-runtime/pkg/client/config"
ctrlruntimelog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
)
const (
defaultLogLevel = "info"
leaderElectionLockName = "cloud-credential-operator-leader"
caConfigMapMountPath = "/var/run/configmaps/trusted-ca-bundle"
caConfigMapName = "tls-ca-bundle.pem"
)
type ControllerManagerOptions struct {
LogLevel string
}
func NewOperator() *cobra.Command {
opts := &ControllerManagerOptions{}
cmd := &cobra.Command{
Use: "operator",
Short: "Run cloud credential operator",
Run: func(cmd *cobra.Command, args []string) {
// Set log level
level, err := log.ParseLevel(opts.LogLevel)
if err != nil {
log.WithError(err).Fatal("Cannot parse log level")
}
log.SetLevel(level)
log.Debug("debug logging enabled")
// Get a config to talk to the apiserver
log.Info("setting up client for manager")
cfg, err := config.GetConfig()
if err != nil {
log.WithError(err).Fatal("unable to set up client config")
}
run := func(ctx context.Context) {
// This is required because controller-runtime expects its consumers to
// set a logger through log.SetLogger within 30 seconds of the program's
// initalization. We have our own logger and can configure controller-runtime's
// logger to do nothing.
ctrlruntimelog.SetLogger(logr.New(ctrlruntimelog.NullLogSink{}))
log.Info("checking prerequisites")
kubeconfigCommandLinePath := cmd.PersistentFlags().Lookup("kubeconfig").Value.String()
rules := clientcmd.NewDefaultClientConfigLoadingRules()
rules.ExplicitPath = kubeconfigCommandLinePath
kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, &clientcmd.ConfigOverrides{})
cfg, err := kubeconfig.ClientConfig()
if err != nil {
log.WithError(err).Fatal("failed to parse kubeconfig")
}
coreClient, err := corev1client.NewForConfig(cfg)
if err != nil {
log.WithError(err).Fatal("failed to set up client")
}
infraStatus, err := platform.GetInfraStatusUsingKubeconfig(kubeconfigCommandLinePath)
if err != nil {
log.Fatal(err)
}
platformType := platform.GetType(infraStatus)
// The status quo for this controller is to LIST + WATCH all Secrets on the cluster. This consumes
// more resources than necessary on clusters where users put other data in Secrets themselves, as we
// hold that data in our cache and never do anything with it. The reconcilers mainly need to react to
// changes in Secrets created for CredentialRequests, which they control and can label, allowing us
// to filter the LIST + WATCH down and hold the minimal set of data in memory. However, on existing
// clusters, secrets exist from previous work these reconcilers did that will not have Secrets labelled.
//
// We solve this second issue two-fold:
// - a new controller that labels all previous Secrets, but does not restrict the watch stream
// - a check on startup that detects that no Secrets remain to be labelled and applies the filtering
var filteredWatchPossible bool
secrets, err := coreClient.Secrets(metav1.NamespaceAll).List(ctx, metav1.ListOptions{})
if err != nil {
log.WithError(err).Fatal("failed to list secrets")
}
var missing []types.NamespacedName
for _, secret := range secrets.Items {
if credentialsrequest.IsMissingSecretLabel(&secret) {
missing = append(missing, types.NamespacedName{
Namespace: secret.Namespace,
Name: secret.Name,
})
}
}
if len(missing) != 0 {
log.WithField("missing", missing).Warn("not all secrets labelled, falling back to caching all secrets on cluster")
} else {
log.Info("filtering the set of secrets we watch")
filteredWatchPossible = true
}
objectSelectors := map[client.Object]cache.ByObject{
&corev1.ConfigMap{}: {
Field: fields.SelectorFromSet(fields.Set{
"metadata.namespace": minterv1.CloudCredOperatorNamespace,
"metadata.name": constants.CloudCredOperatorConfigMap,
}),
},
}
if filteredWatchPossible {
log.Infof("adding label selector %s=%s to cache options for Secrets", minterv1.LabelCredentialsRequest, minterv1.LabelCredentialsRequestValue)
objectSelectors[&corev1.Secret{}] = cache.ByObject{
Label: labels.SelectorFromSet(labels.Set{
minterv1.LabelCredentialsRequest: minterv1.LabelCredentialsRequestValue,
}),
}
}
// Create a new Cmd to provide shared dependencies and start components
log.Info("setting up managers")
mgr, err := manager.New(cfg, manager.Options{
MetricsBindAddress: ":2112",
Cache: cache.Options{
ByObject: objectSelectors,
},
PprofBindAddress: ":6060",
})
if err != nil {
log.WithError(err).Fatal("unable to set up overall controller manager")
}
rootMgr, err := manager.New(cfg, manager.Options{
Cache: cache.Options{
ByObject: map[client.Object]cache.ByObject{
&corev1.Secret{}: {
Field: selectorForRootCredential(platformType),
},
},
},
})
if err != nil {
log.WithError(err).Fatal("unable to set up root credential controller manager")
}
log.Info("registering components")
// Setup Scheme for all resources
util.SetupScheme(mgr.GetScheme())
util.SetupScheme(rootMgr.GetScheme())
// Setup all Controllers
log.Info("setting up controllers")
if err := controller.AddToManager(mgr, rootMgr, kubeconfigCommandLinePath, coreClient); err != nil {
log.WithError(err).Fatal("unable to register controllers to the manager")
}
// Start the managers
log.Info("starting the managers")
runCtx := signals.SetupSignalHandler()
errs := make(chan error)
wg := sync.WaitGroup{}
for _, m := range []manager.Manager{mgr, rootMgr} {
wg.Add(1)
go func(m manager.Manager, ctx context.Context) {
defer wg.Done()
errs <- m.Start(ctx)
}(m, runCtx)
}
go func() {
wg.Wait()
close(errs)
}()
for err := range errs {
if err != nil {
log.WithError(err).Fatal("unable to run the manager")
}
}
}
// Leader election code based on:
// https://github.com/kubernetes/kubernetes/blob/f7e3bcdec2e090b7361a61e21c20b3dbbb41b7f0/staging/src/k8s.io/client-go/examples/leader-election/main.go#L92-L154
// This gives us ReleaseOnCancel which is not presently exposed in controller-runtime.
// use a Go context so we can tell the leaderelection code when we want to step down
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// If optional cco-trusted-ca configmap exists, run a file observer to watch for changes
caConfigMapPath := filepath.Join(caConfigMapMountPath, caConfigMapName)
if _, err := os.Stat(caConfigMapPath); err == nil {
terminateWhenProxyChanges(caConfigMapPath, cancel, ctx.Done())
}
// listen for interrupts or the Linux SIGTERM signal and cancel
// our context, which the leader election code will observe and
// step down
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
go func() {
<-ch
log.Info("received termination, signaling shutdown")
cancel()
}()
id := uuid.New().String()
leLog := log.WithField("id", id)
leLog.Info("generated leader election ID")
kubeClient := kubernetes.NewForConfigOrDie(cfg)
lock, err := resourcelock.New(
resourcelock.ConfigMapsLeasesResourceLock,
minterv1.CloudCredOperatorNamespace,
leaderElectionLockName,
kubeClient.CoreV1(),
kubeClient.CoordinationV1(),
resourcelock.ResourceLockConfig{
Identity: id,
},
)
if err != nil {
log.WithError(err).Fatal("failed to create lock for leader election config")
}
if os.Getenv("CCO_SKIP_LEADER_ELECTION") != "" {
run(ctx)
} else {
// start the leader election code loop
leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{
Lock: lock,
ReleaseOnCancel: true,
LeaseDuration: 360 * time.Second,
RenewDeadline: 270 * time.Second,
RetryPeriod: 90 * time.Second,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: func(ctx context.Context) {
run(ctx)
},
OnStoppedLeading: func() {
// we can do cleanup here if necessary
leLog.Infof("leader lost")
cancel()
},
OnNewLeader: func(identity string) {
if identity == id {
// We just became the leader
leLog.Info("became leader")
return
}
log.Infof("current leader: %s", identity)
},
},
})
}
},
}
cmd.PersistentFlags().StringVar(&opts.LogLevel, "log-level", defaultLogLevel, "Log level (debug,info,warn,error,fatal)")
cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
initializeGlog(cmd.PersistentFlags())
flag.CommandLine.Parse([]string{})
return cmd
}
func selectorForRootCredential(platformType configv1.PlatformType) fields.Selector {
var name string
switch platformType {
case configv1.AWSPlatformType:
name = constants.AWSCloudCredSecretName
case configv1.AzurePlatformType:
name = constants.AzureCloudCredSecretName
case configv1.GCPPlatformType:
name = constants.GCPCloudCredSecretName
case configv1.OpenStackPlatformType:
name = constants.OpenStackCloudCredsSecretName
case configv1.OvirtPlatformType:
name = constants.OvirtCloudCredsSecretName
case configv1.KubevirtPlatformType:
name = constants.KubevirtCloudCredSecretName
case configv1.VSpherePlatformType:
name = constants.VSphereCloudCredSecretName
default:
return fields.Nothing()
}
selector := fields.SelectorFromSet(fields.Set{
"metadata.namespace": constants.CloudCredSecretNamespace,
"metadata.name": name,
})
log.WithField("selector", selector.String()).Info("setting up field selector for root credential Secret")
return selector
}
func initializeGlog(flags *pflag.FlagSet) {
golog.SetOutput(glogWriter{}) // Redirect all regular go log output to glog
golog.SetFlags(0)
go wait.Forever(glog.Flush, 5*time.Second) // Periodically flush logs
f := flags.Lookup("logtostderr") // Default to logging to stderr
if f != nil {
f.Value.Set("true")
}
}
type glogWriter struct{}
func (writer glogWriter) Write(data []byte) (n int, err error) {
glog.Info(string(data))
return len(data), nil
}
func terminateWhenProxyChanges(path string, cancel context.CancelFunc, done <-chan struct{}) {
// read the contents of the configmap
fileContents := map[string][]byte{}
var filenames []string
fileBytes, err := ioutil.ReadFile(path)
if err != nil {
log.WithError(err).Fatal("Unable to read proxy CA config map")
}
fileContents[path] = fileBytes
filenames = append(filenames, path)
// create the file observer
obs, err := fileobserver.NewObserver(10 * time.Second)
if err != nil {
log.WithError(err).Fatal("could not set up file observer for proxy CA change")
}
// add reactor for proxy files
obs.AddReactor(
func(file string, action fileobserver.ActionType) error {
log.Info("Proxy CA configmap change detected, restarting pod")
cancel()
return nil
},
fileContents,
filenames...,
)
// run the file observer
go func() {
log.WithField("file", path).Info("running file observer")
obs.Run(done)
log.Fatal("file observer stopped")
}()
}