forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreconciler.go
More file actions
440 lines (364 loc) · 13.6 KB
/
reconciler.go
File metadata and controls
440 lines (364 loc) · 13.6 KB
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package tokenrequestor
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/go-logr/logr"
authenticationv1 "k8s.io/api/authentication/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clientcmdlatest "k8s.io/client-go/tools/clientcmd/api/latest"
clientcmdv1 "k8s.io/client-go/tools/clientcmd/api/v1"
"k8s.io/utils/clock"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
resourcesv1alpha1 "github.com/gardener/gardener/pkg/apis/resources/v1alpha1"
"github.com/gardener/gardener/pkg/controllerutils"
)
const (
defaultExpirationDuration = 12 * time.Hour
maxExpirationDuration = 24 * time.Hour
)
// Reconciler requests and refreshes tokens via the TokenRequest API.
type Reconciler struct {
SourceClient client.Client
TargetClient client.Client
ConcurrentSyncs int
Clock clock.Clock
JitterFunc func(time.Duration, float64) time.Duration
Class *string
APIAudiences []string
CAData []byte
// TargetNamespace is the namespace that requested ServiceAccounts should be created in.
// If TargetNamespace is empty, the controller uses the namespace specified in the
// serviceaccount.resources.gardener.cloud/namespace annotation.
TargetNamespace string
}
// Reconcile requests and populates tokens.
func (r *Reconciler) Reconcile(reconcileCtx context.Context, req reconcile.Request) (reconcile.Result, error) {
log := logf.FromContext(reconcileCtx)
ctx, cancel := controllerutils.GetMainReconciliationContext(reconcileCtx, controllerutils.DefaultReconciliationTimeout)
defer cancel()
secret := &corev1.Secret{}
if err := r.SourceClient.Get(ctx, req.NamespacedName, secret); err != nil {
if apierrors.IsNotFound(err) {
log.V(1).Info("Object is gone, stop reconciling")
return reconcile.Result{}, nil
}
return reconcile.Result{}, fmt.Errorf("error retrieving object from store: %w", err)
}
if !r.isRelevantSecret(secret) {
return reconcile.Result{}, nil
}
mustRequeue, requeueAfter, err := r.requeue(ctx, secret)
if err != nil {
return reconcile.Result{}, err
}
if mustRequeue {
log.Info("No need to generate new token, renewal is scheduled", "after", requeueAfter)
return reconcile.Result{Requeue: true, RequeueAfter: requeueAfter}, nil
}
log.Info("Requesting new token")
serviceAccount, err := r.reconcileServiceAccount(ctx, secret)
if err != nil {
return reconcile.Result{}, err
}
expirationSeconds, err := tokenExpirationSeconds(secret)
if err != nil {
return reconcile.Result{}, err
}
tokenRequest, err := r.createServiceAccountToken(ctx, serviceAccount, expirationSeconds)
if err != nil {
return reconcile.Result{}, err
}
renewDuration := r.renewDuration(tokenRequest.Status.ExpirationTimestamp.Time)
if err := r.reconcileSecret(ctx, log, secret, tokenRequest.Status.Token, renewDuration); err != nil {
return reconcile.Result{}, fmt.Errorf("could not update Secret with token: %w", err)
}
log.Info("Successfully requested token and scheduled renewal", "after", renewDuration)
return reconcile.Result{Requeue: true, RequeueAfter: renewDuration}, nil
}
func (r *Reconciler) reconcileServiceAccount(ctx context.Context, secret *corev1.Secret) (*corev1.ServiceAccount, error) {
serviceAccount := r.getServiceAccountFromAnnotations(secret.Annotations)
var labels map[string]string
if labelsJSON := secret.Annotations[resourcesv1alpha1.ServiceAccountLabels]; labelsJSON != "" {
labels = make(map[string]string)
if err := json.Unmarshal([]byte(labelsJSON), &labels); err != nil {
return nil, fmt.Errorf("failed unmarshalling service account labels from secret annotation %q (%s): %w", resourcesv1alpha1.ServiceAccountLabels, labelsJSON, err)
}
}
if _, err := controllerutil.CreateOrUpdate(ctx, r.TargetClient, serviceAccount, func() error {
serviceAccount.Labels = labels
serviceAccount.AutomountServiceAccountToken = ptr.To(false)
return nil
}); err != nil {
return nil, err
}
return serviceAccount, nil
}
func (r *Reconciler) reconcileSecret(ctx context.Context, log logr.Logger, sourceSecret *corev1.Secret, token string, renewDuration time.Duration) error {
// The "requesting component" (e.g. gardenlet) might concurrently update the kubeconfig field in order to update the
// included CA bundle. Hence, we need to use optimistic locking to ensure we don't accidentally overwrite concurrent
// updates.
// ref https://github.com/gardener/gardener/issues/6092#issuecomment-1152434616
patch := client.MergeFromWithOptions(sourceSecret.DeepCopy(), client.MergeFromWithOptimisticLock{})
metav1.SetMetaDataAnnotation(&sourceSecret.ObjectMeta, resourcesv1alpha1.ServiceAccountTokenRenewTimestamp, r.Clock.Now().UTC().Add(renewDuration).Format(time.RFC3339))
shouldInjectCA, _ := strconv.ParseBool(sourceSecret.Annotations[resourcesv1alpha1.ServiceAccountInjectCABundle])
if shouldInjectCA {
log.Info("Injecting CA bundle into secret")
}
if targetSecret := getTargetSecretFromAnnotations(sourceSecret.Annotations); targetSecret != nil {
log.Info("Populating the token to the target secret", "targetSecret", client.ObjectKeyFromObject(targetSecret))
if _, err := controllerutil.CreateOrUpdate(ctx, r.TargetClient, targetSecret, r.populateSecretData(log, targetSecret, token, shouldInjectCA)); err != nil {
return err
}
log.Info("Depopulating the token from the source secret")
if err := r.depopulateToken(sourceSecret)(); err != nil {
return err
}
} else {
log.Info("Populating the token to the source secret")
if err := r.populateSecretData(log, sourceSecret, token, shouldInjectCA)(); err != nil {
return err
}
}
return r.SourceClient.Patch(ctx, sourceSecret, patch)
}
func (r *Reconciler) populateSecretData(log logr.Logger, secret *corev1.Secret, token string, shouldInjectCA bool) func() error {
return func() error {
if secret.Data == nil {
secret.Data = make(map[string][]byte, 1)
}
var ca []byte
if shouldInjectCA {
ca = r.CAData
}
return updateSecretData(log, secret.Data, token, ca)
}
}
func (r *Reconciler) depopulateToken(secret *corev1.Secret) func() error {
return func() error {
delete(secret.Data, resourcesv1alpha1.DataKeyToken)
delete(secret.Data, resourcesv1alpha1.DataKeyCABundle)
delete(secret.Data, resourcesv1alpha1.DataKeyKubeconfig)
return nil
}
}
func (r *Reconciler) createServiceAccountToken(ctx context.Context, sa *corev1.ServiceAccount, expirationSeconds int64) (*authenticationv1.TokenRequest, error) {
tokenRequest := &authenticationv1.TokenRequest{
Spec: authenticationv1.TokenRequestSpec{
Audiences: r.APIAudiences,
ExpirationSeconds: &expirationSeconds,
},
}
if err := r.TargetClient.SubResource("token").Create(ctx, sa, tokenRequest); err != nil {
return nil, err
}
return tokenRequest, nil
}
func (r *Reconciler) requeue(ctx context.Context, secret *corev1.Secret) (bool, time.Duration, error) {
var (
secretContainingToken = secret // token is expected in source secret by default
renewTimestamp = secret.Annotations[resourcesv1alpha1.ServiceAccountTokenRenewTimestamp]
checkBundle, _ = strconv.ParseBool(secret.Annotations[resourcesv1alpha1.ServiceAccountInjectCABundle])
)
if len(renewTimestamp) == 0 {
return false, 0, nil
}
if targetSecret := getTargetSecretFromAnnotations(secret.Annotations); targetSecret != nil {
if err := r.TargetClient.Get(ctx, client.ObjectKeyFromObject(targetSecret), targetSecret); err != nil {
if !apierrors.IsNotFound(err) {
return false, 0, fmt.Errorf("could not read target secret: %w", err)
}
// target secret is not found, so do not requeue to make sure it gets created
return false, 0, nil
}
secretContainingToken = targetSecret // token is expected in target secret
}
tokenExists, err := tokenExistsInSecretData(secretContainingToken.Data)
if err != nil {
return false, 0, fmt.Errorf("could not check whether token exists in secret data: %w", err)
}
if !tokenExists {
return false, 0, nil
}
if checkBundle {
isBundleOk, err := r.isCABundleUpdated(secretContainingToken.Data)
if err != nil {
return false, 0, fmt.Errorf("could not check whether the caBundle is up to date: %w", err)
}
if !isBundleOk {
return false, 0, nil
}
}
renewTime, err := time.Parse(time.RFC3339, renewTimestamp)
if err != nil {
return false, 0, fmt.Errorf("could not parse renew timestamp: %w", err)
}
if r.Clock.Now().UTC().Before(renewTime.UTC()) {
return true, renewTime.UTC().Sub(r.Clock.Now().UTC()), nil
}
return false, 0, nil
}
func (r *Reconciler) renewDuration(expirationTimestamp time.Time) time.Duration {
expirationDuration := expirationTimestamp.UTC().Sub(r.Clock.Now().UTC())
if expirationDuration >= maxExpirationDuration {
expirationDuration = maxExpirationDuration
}
return r.JitterFunc(expirationDuration*80/100, 0.05)
}
func tokenExpirationSeconds(secret *corev1.Secret) (int64, error) {
var (
expirationDuration = defaultExpirationDuration
err error
)
if v, ok := secret.Annotations[resourcesv1alpha1.ServiceAccountTokenExpirationDuration]; ok {
expirationDuration, err = time.ParseDuration(v)
if err != nil {
return 0, err
}
}
return int64(expirationDuration / time.Second), nil
}
func (r *Reconciler) getServiceAccountFromAnnotations(annotations map[string]string) *corev1.ServiceAccount {
namespace := r.TargetNamespace
if namespace == "" {
namespace = annotations[resourcesv1alpha1.ServiceAccountNamespace]
}
return &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: annotations[resourcesv1alpha1.ServiceAccountName],
Namespace: namespace,
},
}
}
func (r *Reconciler) isCABundleUpdated(data map[string][]byte) (bool, error) {
if _, ok := data[resourcesv1alpha1.DataKeyKubeconfig]; !ok {
return bytes.Equal(data[resourcesv1alpha1.DataKeyCABundle], r.CAData), nil
}
kubeconfig, err := decodeKubeconfig(data[resourcesv1alpha1.DataKeyKubeconfig])
if err != nil {
return false, err
}
cluster, err := getCluster(kubeconfig)
if err != nil {
return false, err
}
return bytes.Equal(cluster.CertificateAuthorityData, r.CAData), nil
}
func getTargetSecretFromAnnotations(annotations map[string]string) *corev1.Secret {
var (
name = annotations[resourcesv1alpha1.TokenRequestorTargetSecretName]
namespace = annotations[resourcesv1alpha1.TokenRequestorTargetSecretNamespace]
)
if name == "" || namespace == "" {
return nil
}
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
}
func updateSecretData(log logr.Logger, data map[string][]byte, token string, caData []byte) error {
if _, ok := data[resourcesv1alpha1.DataKeyKubeconfig]; !ok {
log.Info("Writing token to data")
data[resourcesv1alpha1.DataKeyToken] = []byte(token)
if len(caData) > 0 {
data[resourcesv1alpha1.DataKeyCABundle] = caData
} else {
delete(data, resourcesv1alpha1.DataKeyCABundle)
}
return nil
}
log.Info("Writing token as part of kubeconfig to data")
kubeconfig, err := decodeKubeconfig(data[resourcesv1alpha1.DataKeyKubeconfig])
if err != nil {
return err
}
authInfo, err := getAuthInfo(kubeconfig)
if err != nil {
return err
}
if authInfo != nil {
authInfo.Token = token
}
if len(caData) > 0 {
cluster, err := getCluster(kubeconfig)
if err != nil {
return err
}
cluster.CertificateAuthorityData = caData
}
kubeconfigEncoded, err := runtime.Encode(clientcmdlatest.Codec, kubeconfig)
if err != nil {
return err
}
data[resourcesv1alpha1.DataKeyKubeconfig] = kubeconfigEncoded
return nil
}
func tokenExistsInSecretData(data map[string][]byte) (bool, error) {
if _, ok := data[resourcesv1alpha1.DataKeyKubeconfig]; !ok {
return data[resourcesv1alpha1.DataKeyToken] != nil, nil
}
kubeconfig, err := decodeKubeconfig(data[resourcesv1alpha1.DataKeyKubeconfig])
if err != nil {
return false, err
}
authInfo, err := getAuthInfo(kubeconfig)
if err != nil {
return false, err
}
return authInfo != nil && authInfo.Token != "", nil
}
func decodeKubeconfig(data []byte) (*clientcmdv1.Config, error) {
kubeconfig := &clientcmdv1.Config{}
if _, _, err := clientcmdlatest.Codec.Decode(data, nil, kubeconfig); err != nil {
return nil, err
}
return kubeconfig, nil
}
func getAuthInfo(kubeconfig *clientcmdv1.Config) (*clientcmdv1.AuthInfo, error) {
ctx, err := getCurrentContext(kubeconfig)
if err != nil {
return nil, err
}
for i, authInfo := range kubeconfig.AuthInfos {
if authInfo.Name == ctx.AuthInfo {
return &kubeconfig.AuthInfos[i].AuthInfo, nil
}
}
return nil, fmt.Errorf("did not find authInfo of current context named %s", ctx.AuthInfo)
}
func getCluster(kubeconfig *clientcmdv1.Config) (*clientcmdv1.Cluster, error) {
ctx, err := getCurrentContext(kubeconfig)
if err != nil {
return nil, err
}
for i, cluster := range kubeconfig.Clusters {
if cluster.Name == ctx.Cluster {
return &kubeconfig.Clusters[i].Cluster, nil
}
}
return nil, fmt.Errorf("did not find cluster of current context named %s", ctx.Cluster)
}
func getCurrentContext(kubeconfig *clientcmdv1.Config) (clientcmdv1.Context, error) {
for _, namedContext := range kubeconfig.Contexts {
if namedContext.Name == kubeconfig.CurrentContext {
return namedContext.Context, nil
}
}
return clientcmdv1.Context{}, fmt.Errorf("did not find context defined in current context")
}