forked from spiffe/spire-controller-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
369 lines (320 loc) · 12.3 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
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
/*
Copyright 2021 SPIRE 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 main
import (
"crypto/tls"
"errors"
"flag"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"time"
// 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/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"github.com/spiffe/go-spiffe/v2/spiffeid"
spirev1alpha1 "github.com/spiffe/spire-controller-manager/api/v1alpha1"
"github.com/spiffe/spire-controller-manager/controllers"
"github.com/spiffe/spire-controller-manager/pkg/spireapi"
"github.com/spiffe/spire-controller-manager/pkg/spireentry"
"github.com/spiffe/spire-controller-manager/pkg/spirefederationrelationship"
"github.com/spiffe/spire-controller-manager/pkg/webhookmanager"
//+kubebuilder:scaffold:imports
)
const (
defaultSPIREServerSocketPath = "/spire-server/api.sock"
defaultGCInterval = 10 * time.Second
k8sDefaultService = "kubernetes.default.svc"
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(spirev1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
func main() {
ctrlConfig, options, err := parseConfig()
if err != nil {
setupLog.Error(err, "error parsing configuration")
os.Exit(1)
}
if err := run(ctrlConfig, options); err != nil {
os.Exit(1)
}
}
func parseConfig() (spirev1alpha1.ControllerManagerConfig, ctrl.Options, error) {
var configFileFlag string
var spireAPISocketFlag string
flag.StringVar(&configFileFlag, "config", "",
"The controller will load its initial configuration from this file. "+
"Omit this flag to use the default configuration values. "+
"Command-line flags override configuration from this file.")
flag.StringVar(&spireAPISocketFlag, "spire-api-socket", "", "The path to the SPIRE API socket (deprecated; use the config file)")
// Parse log flags
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
// Set default values
ctrlConfig := spirev1alpha1.ControllerManagerConfig{
IgnoreNamespaces: []string{"kube-system", "kube-public", "spire-system"},
GCInterval: defaultGCInterval,
ValidatingWebhookConfigurationName: "spire-controller-manager-webhook",
}
options := ctrl.Options{Scheme: scheme}
if configFileFlag != "" {
if err := spirev1alpha1.LoadOptionsFromFile(configFileFlag, scheme, &options, &ctrlConfig); err != nil {
return ctrlConfig, options, fmt.Errorf("unable to load the config file: %w", err)
}
}
// Determine the SPIRE Server socket path
switch {
case ctrlConfig.SPIREServerSocketPath == "" && spireAPISocketFlag == "":
// Neither is set. Use the default.
ctrlConfig.SPIREServerSocketPath = defaultSPIREServerSocketPath
case ctrlConfig.SPIREServerSocketPath != "" && spireAPISocketFlag == "":
// Configuration file value is set. Use it.
case ctrlConfig.SPIREServerSocketPath == "" && spireAPISocketFlag != "":
// Deprecated flag value is set. Use it but warn.
ctrlConfig.SPIREServerSocketPath = spireAPISocketFlag
setupLog.Error(nil, "The spire-api-socket flag is deprecated and will be removed in a future release; use the configuration file instead")
case ctrlConfig.SPIREServerSocketPath != "" && spireAPISocketFlag != "":
// Both are set. Warn and ignore the deprecated flag.
setupLog.Error(nil, "Ignoring deprecated spire-api-socket flag which will be removed in a future release")
}
// Attempt to auto detect cluster domain if it wasn't specified
if ctrlConfig.ClusterDomain == "" {
clusterDomain, err := autoDetectClusterDomain()
if err != nil {
setupLog.Error(err, "unable to autodetect cluster domain")
}
ctrlConfig.ClusterDomain = clusterDomain
}
setupLog.Info("Config loaded",
"cluster name", ctrlConfig.ClusterName,
"cluster domain", ctrlConfig.ClusterDomain,
"trust domain", ctrlConfig.TrustDomain,
"ignore namespaces", ctrlConfig.IgnoreNamespaces,
"gc interval", ctrlConfig.GCInterval,
"spire server socket path", ctrlConfig.SPIREServerSocketPath)
switch {
case ctrlConfig.TrustDomain == "":
setupLog.Error(nil, "trust domain is required configuration")
return ctrlConfig, options, errors.New("trust domain is required configuration")
case ctrlConfig.ClusterName == "":
return ctrlConfig, options, errors.New("cluster name is required configuration")
case ctrlConfig.ValidatingWebhookConfigurationName == "":
return ctrlConfig, options, errors.New("validating webhook configuration name is required configuration")
case ctrlConfig.ControllerManagerConfigurationSpec.Webhook.CertDir != "":
setupLog.Info("certDir configuration is ignored", "certDir", ctrlConfig.ControllerManagerConfigurationSpec.Webhook.CertDir)
}
return ctrlConfig, options, nil
}
func run(ctrlConfig spirev1alpha1.ControllerManagerConfig, options ctrl.Options) error {
// It's unfortunate that we have to keep credentials on disk so that the
// manager can load them:
// TODO: upstream a change to the WebhookServer so it can use callbacks to
// obtain the certificates so we don't have to touch disk.
certDir, err := os.MkdirTemp("", "spire-controller-manager-")
if err != nil {
setupLog.Error(err, "failed to create temporary cert directory")
return err
}
defer func() {
if err := os.RemoveAll(certDir); err != nil {
setupLog.Error(err, "failed to remove temporary cert directory", "certDir", certDir)
os.Exit(1)
}
}()
// webhook server credentials are stored in a single file to keep rotation
// simple.
const keyPairName = "keypair.pem"
options.WebhookServer = webhook.NewServer(webhook.Options{
CertDir: certDir,
CertName: keyPairName,
KeyName: keyPairName,
TLSOpts: []func(*tls.Config){
func(s *tls.Config) {
s.MinVersion = tls.VersionTLS12
},
},
})
ctx := ctrl.SetupSignalHandler()
trustDomain, err := spiffeid.TrustDomainFromString(ctrlConfig.TrustDomain)
if err != nil {
setupLog.Error(err, "invalid trust domain name")
return err
}
setupLog.Info("Dialing SPIRE Server socket")
spireClient, err := spireapi.DialSocket(ctx, ctrlConfig.SPIREServerSocketPath)
if err != nil {
setupLog.Error(err, "unable to dial SPIRE Server socket")
return err
}
defer spireClient.Close()
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
if err != nil {
setupLog.Error(err, "unable to start manager")
return err
}
// We need a direct client to query and patch up the webhook. We can't use
// the controller runtime client for this because we can't start the manager
// without the webhook credentials being in place, and the webhook credentials
// need the DNS name of the webhook service from the configuration.
config, err := rest.InClusterConfig()
if err != nil {
setupLog.Error(err, "failed to get in cluster configuration")
return err
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
setupLog.Error(err, "failed to create an API client")
return err
}
webhookID, _ := spiffeid.FromPath(trustDomain, "/spire-controller-manager-webhook")
webhookManager := webhookmanager.New(webhookmanager.Config{
ID: webhookID,
KeyPairPath: filepath.Join(certDir, keyPairName),
WebhookName: ctrlConfig.ValidatingWebhookConfigurationName,
WebhookClient: clientset.AdmissionregistrationV1().ValidatingWebhookConfigurations(),
SVIDClient: spireClient,
BundleClient: spireClient,
})
if err := webhookManager.Init(ctx); err != nil {
setupLog.Error(err, "failed to mint initial webhook certificate")
return err
}
entryReconciler := spireentry.Reconciler(spireentry.ReconcilerConfig{
TrustDomain: trustDomain,
ClusterName: ctrlConfig.ClusterName,
ClusterDomain: ctrlConfig.ClusterDomain,
K8sClient: mgr.GetClient(),
EntryClient: spireClient,
IgnoreNamespaces: ctrlConfig.IgnoreNamespaces,
GCInterval: ctrlConfig.GCInterval,
})
federationRelationshipReconciler := spirefederationrelationship.Reconciler(spirefederationrelationship.ReconcilerConfig{
K8sClient: mgr.GetClient(),
TrustDomainClient: spireClient,
GCInterval: ctrlConfig.GCInterval,
})
if err = (&controllers.ClusterSPIFFEIDReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Triggerer: entryReconciler,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ClusterSPIFFEID")
return err
}
if err = (&controllers.ClusterFederatedTrustDomainReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Triggerer: federationRelationshipReconciler,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ClusterFederatedTrustDomain")
return err
}
if err = (&controllers.ClusterStaticEntryReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Triggerer: entryReconciler,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ClusterStaticEntry")
return err
}
if err = (&spirev1alpha1.ClusterFederatedTrustDomain{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "ClusterFederatedTrustDomain")
return err
}
if err = (&spirev1alpha1.ClusterSPIFFEID{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "ClusterSPIFFEID")
return err
}
//+kubebuilder:scaffold:builder
if err = (&controllers.PodReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Triggerer: entryReconciler,
IgnoreNamespaces: ctrlConfig.IgnoreNamespaces,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Pod")
return err
}
if err = mgr.Add(manager.RunnableFunc(entryReconciler.Run)); err != nil {
setupLog.Error(err, "unable to manage entry reconciler")
return err
}
if err = mgr.Add(manager.RunnableFunc(federationRelationshipReconciler.Run)); err != nil {
setupLog.Error(err, "unable to manage federation relationship reconciler")
return err
}
if err = mgr.Add(webhookManager); err != nil {
setupLog.Error(err, "unable to manage federation relationship reconciler")
return err
}
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
return err
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
return err
}
setupLog.Info("starting manager")
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running manager")
return err
}
return nil
}
func autoDetectClusterDomain() (string, error) {
cname, err := net.LookupCNAME(k8sDefaultService)
if err != nil {
return "", fmt.Errorf("unable to lookup CNAME: %w", err)
}
clusterDomain, err := parseClusterDomainCNAME(cname)
if err != nil {
return "", fmt.Errorf("unable to parse CNAME \"%s\": %w", cname, err)
}
return clusterDomain, nil
}
func parseClusterDomainCNAME(cname string) (string, error) {
clusterDomain := strings.TrimPrefix(cname, k8sDefaultService+".")
if clusterDomain == cname {
return "", errors.New("CNAME did not have expected prefix")
}
// Trim off optional trailing dot
clusterDomain = strings.TrimSuffix(clusterDomain, ".")
if clusterDomain == "" {
return "", errors.New("CNAME did not have a cluster domain")
}
return clusterDomain, nil
}