-
Notifications
You must be signed in to change notification settings - Fork 123
/
main.go
663 lines (564 loc) · 22.8 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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/*
Copyright (c) 2017, UPMC Enterprises
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name UPMC Enterprises nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL UPMC ENTERPRISES BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
*/
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/cenkalti/backoff"
flag "github.com/spf13/pflag"
"github.com/upmc-enterprises/registry-creds/k8sutil"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
v1 "k8s.io/client-go/pkg/api/v1"
)
const (
// Retry Types
retryTypeSimple = "simple"
retryTypeExponential = "exponential"
dockerCfgTemplate = `{"%s":{"username":"oauth2accesstoken","password":"%s","email":"none"}}`
dockerPrivateRegistryPasswordKey = "DOCKER_PRIVATE_REGISTRY_PASSWORD"
dockerPrivateRegistryServerKey = "DOCKER_PRIVATE_REGISTRY_SERVER"
dockerPrivateRegistryUserKey = "DOCKER_PRIVATE_REGISTRY_USER"
acrURLKey = "ACR_URL"
acrClientIDKey = "ACR_CLIENT_ID"
acrPasswordKey = "ACR_PASSWORD"
tokenGenRetryTypeKey = "TOKEN_RETRY_TYPE"
tokenGenRetriesKey = "TOKEN_RETRIES"
tokenGenRetryDelayKey = "TOKEN_RETRY_DELAY"
defaultTokenGenRetries = 3
defaultTokenGenRetryDelay = 5 // in seconds
defaultTokenGenRetryType = retryTypeSimple
)
var (
flags = flag.NewFlagSet("", flag.ContinueOnError)
argKubecfgFile = flags.String("kubecfg-file", "", `Location of kubecfg file for access to kubernetes master service; --kube_master_url overrides the URL part of this; if neither this nor --kube_master_url are provided, defaults to service account tokens`)
argKubeMasterURL = flags.String("kube-master-url", "", `URL to reach kubernetes master. Env variables in this flag will be expanded.`)
argAWSSecretName = flags.String("aws-secret-name", "awsecr-cred", `Default AWS secret name`)
argDPRSecretName = flags.String("dpr-secret-name", "dpr-secret", `Default Docker Private Registry secret name`)
argGCRSecretName = flags.String("gcr-secret-name", "gcr-secret", `Default GCR secret name`)
argACRSecretName = flags.String("acr-secret-name", "acr-secret", "Default Azure Container Registry secret name")
argGCRURL = flags.String("gcr-url", "https://gcr.io", `Default GCR URL`)
argAWSRegion = flags.String("aws-region", "us-east-1", `Default AWS region`)
argDPRPassword = flags.String("dpr-password", "", "Docker Private Registry password")
argDPRServer = flags.String("dpr-server", "", "Docker Private Registry server")
argDPRUser = flags.String("dpr-user", "", "Docker Private Registry user")
argACRURL = flags.String("acr-url", "", "Azure Container Registry URL")
argACRClientID = flags.String("acr-client-id", "", "Azure Container Registry client ID (user name)")
argACRPassword = flags.String("acr-password", "", "Azure Container Registry password (client secret)")
argRefreshMinutes = flags.Int("refresh-mins", 60, `Default time to wait before refreshing (60 minutes)`)
argSkipKubeSystem = flags.Bool("skip-kube-system", true, `If true, will not attempt to set ImagePullSecrets on the kube-system namespace`)
argAWSAssumeRole = flags.String("aws_assume_role", "", `If specified AWS will assume this role and use it to retrieve tokens`)
argTokenGenFxnRetryType = flags.String("token-retry-type", defaultTokenGenRetryType, `The type of retry timer to use when generating a secret token; either simple or exponential (simple)`)
argTokenGenFxnRetries = flags.Int("token-retries", defaultTokenGenRetries, `Default number of times to retry generating a secret token (3)`)
argTokenGenFxnRetryDelay = flags.Int("token-retry-delay", defaultTokenGenRetryDelay, `Default number of seconds to wait before retrying secret token generation (5 seconds)`)
)
var (
awsAccountIDs []string
// RetryCfg represents the currently-configured number of retries + retry delay
RetryCfg RetryConfig
// The retry backoff timers
simpleBackoff *backoff.ConstantBackOff
exponentialBackoff *backoff.ExponentialBackOff
)
type dockerJSON struct {
Auths map[string]registryAuth `json:"auths,omitempty"`
}
type registryAuth struct {
Auth string `json:"auth"`
Email string `json:"email"`
}
type controller struct {
k8sutil *k8sutil.K8sutilInterface
ecrClient ecrInterface
gcrClient gcrInterface
dprClient dprInterface
acrClient acrInterface
}
// RetryConfig represents the number of retries + the retry delay for retrying an operation if it should fail
type RetryConfig struct {
Type string
NumberOfRetries int
RetryDelayInSeconds int
}
// Docker Private Registry interface
type dprInterface interface {
getAuthToken(server, user, password string) (AuthToken, error)
}
type ecrInterface interface {
GetAuthorizationToken(input *ecr.GetAuthorizationTokenInput) (*ecr.GetAuthorizationTokenOutput, error)
}
type gcrInterface interface {
DefaultTokenSource(ctx context.Context, scope ...string) (oauth2.TokenSource, error)
}
type acrInterface interface {
getAuthToken(registryURL, clientID, password string) (AuthToken, error)
}
func newEcrClient() ecrInterface {
sess := session.Must(session.NewSession())
awsConfig := aws.NewConfig().WithRegion(*argAWSRegion)
if *argAWSAssumeRole != "" {
creds := stscreds.NewCredentials(sess, *argAWSAssumeRole)
awsConfig.Credentials = creds
}
return ecr.New(sess, awsConfig)
}
type dprClient struct{}
func (dpr dprClient) getAuthToken(server, user, password string) (AuthToken, error) {
if server == "" {
return AuthToken{}, fmt.Errorf(fmt.Sprintf("Failed to get auth token for docker private registry: empty value for %s", dockerPrivateRegistryServerKey))
}
if user == "" {
return AuthToken{}, fmt.Errorf(fmt.Sprintf("Failed to get auth token for docker private registry: empty value for %s", dockerPrivateRegistryUserKey))
}
if password == "" {
return AuthToken{}, fmt.Errorf(fmt.Sprintf("Failed to get auth token for docker private registry: empty value for %s", dockerPrivateRegistryPasswordKey))
}
token := base64.StdEncoding.EncodeToString([]byte(strings.Join([]string{user, password}, ":")))
return AuthToken{AccessToken: token, Endpoint: server}, nil
}
func newDprClient() dprInterface {
return dprClient{}
}
type gcrClient struct{}
func (gcr gcrClient) DefaultTokenSource(ctx context.Context, scope ...string) (oauth2.TokenSource, error) {
return google.DefaultTokenSource(ctx, scope...)
}
func newGcrClient() gcrInterface {
return gcrClient{}
}
func (c *controller) getDPRToken() ([]AuthToken, error) {
token, err := c.dprClient.getAuthToken(*argDPRServer, *argDPRUser, *argDPRPassword)
return []AuthToken{token}, err
}
func (c *controller) getGCRAuthorizationKey() ([]AuthToken, error) {
ts, err := c.gcrClient.DefaultTokenSource(context.TODO(), "https://www.googleapis.com/auth/cloud-platform")
if err != nil {
return []AuthToken{}, err
}
token, err := ts.Token()
if err != nil {
return []AuthToken{}, err
}
if !token.Valid() {
return []AuthToken{}, fmt.Errorf("token was invalid")
}
if token.Type() != "Bearer" {
return []AuthToken{}, fmt.Errorf(fmt.Sprintf("expected token type \"Bearer\" but got \"%s\"", token.Type()))
}
tokens := make([]AuthToken, 0)
tokens = append(tokens, AuthToken{token.AccessToken, *argGCRURL})
return tokens, nil
}
func (c *controller) getECRAuthorizationKey() ([]AuthToken, error) {
var tokens []AuthToken
var regIds []*string
regIds = make([]*string, len(awsAccountIDs))
for i, awsAccountID := range awsAccountIDs {
regIds[i] = aws.String(awsAccountID)
}
params := &ecr.GetAuthorizationTokenInput{
RegistryIds: regIds,
}
resp, err := c.ecrClient.GetAuthorizationToken(params)
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
logrus.Println(err.Error())
return []AuthToken{}, err
}
for _, auth := range resp.AuthorizationData {
tokens = append(tokens, AuthToken{
AccessToken: *auth.AuthorizationToken,
Endpoint: *auth.ProxyEndpoint,
})
}
return tokens, nil
}
func generateSecretObj(tokens []AuthToken, isJSONCfg bool, secretName string) (*v1.Secret, error) {
secret := &v1.Secret{
ObjectMeta: v1.ObjectMeta{
Name: secretName,
},
}
if isJSONCfg {
auths := map[string]registryAuth{}
for _, token := range tokens {
auths[token.Endpoint] = registryAuth{
Auth: token.AccessToken,
Email: "none",
}
}
configJSON, err := json.Marshal(dockerJSON{Auths: auths})
if err != nil {
return secret, nil
}
secret.Data = map[string][]byte{".dockerconfigjson": configJSON}
secret.Type = "kubernetes.io/dockerconfigjson"
} else {
if len(tokens) == 1 {
secret.Data = map[string][]byte{
".dockercfg": []byte(fmt.Sprintf(dockerCfgTemplate, tokens[0].Endpoint, tokens[0].AccessToken))}
secret.Type = "kubernetes.io/dockercfg"
}
}
return secret, nil
}
type acrClient struct{}
func (c acrClient) getAuthToken(registryURL, clientID, password string) (AuthToken, error) {
if registryURL == "" {
return AuthToken{}, fmt.Errorf("Azure Container Registry URL is missing; ensure %s parameter is set", acrURLKey)
}
if clientID == "" {
return AuthToken{}, fmt.Errorf("Client ID needed to access Azure Container Registry is missing; ensure %s parameter is set", acrClientIDKey)
}
if password == "" {
return AuthToken{}, fmt.Errorf("Password needed to access Azure Container Registry is missing; ensure %s paremeter is set", acrClientIDKey)
}
token := base64.StdEncoding.EncodeToString([]byte(strings.Join([]string{clientID, password}, ":")))
return AuthToken{AccessToken: token, Endpoint: registryURL}, nil
}
func (c *controller) getACRToken() ([]AuthToken, error) {
token, err := c.acrClient.getAuthToken(*argACRURL, *argACRClientID, *argACRPassword)
return []AuthToken{token}, err
}
func newACRClient() acrInterface {
return acrClient{}
}
// AuthToken represents an Access Token and an Endpoint for a registry service
type AuthToken struct {
AccessToken string
Endpoint string
}
// SecretGenerator represents a token generation function for a registry service
type SecretGenerator struct {
TokenGenFxn func() ([]AuthToken, error)
IsJSONCfg bool
SecretName string
}
func getSecretGenerators(c *controller) []SecretGenerator {
secretGenerators := make([]SecretGenerator, 0)
secretGenerators = append(secretGenerators, SecretGenerator{
TokenGenFxn: c.getGCRAuthorizationKey,
IsJSONCfg: false,
SecretName: *argGCRSecretName,
})
secretGenerators = append(secretGenerators, SecretGenerator{
TokenGenFxn: c.getECRAuthorizationKey,
IsJSONCfg: true,
SecretName: *argAWSSecretName,
})
secretGenerators = append(secretGenerators, SecretGenerator{
TokenGenFxn: c.getDPRToken,
IsJSONCfg: true,
SecretName: *argDPRSecretName,
})
secretGenerators = append(secretGenerators, SecretGenerator{
TokenGenFxn: c.getACRToken,
IsJSONCfg: true,
SecretName: *argACRSecretName,
})
return secretGenerators
}
func (c *controller) processNamespace(namespace *v1.Namespace, secret *v1.Secret) error {
log := logrus.WithField("function", "processNamespace")
// Check if the secret exists for the namespace
log.Debugf("checking for secret %s in namespace %s", secret.Name, namespace.GetName())
_, err := c.k8sutil.GetSecret(namespace.GetName(), secret.Name)
if err != nil {
log.Debugf("Could not find secret %s in namespace %s; will try to create it", secret.Name, namespace.GetName())
// Secret not found, create
err := c.k8sutil.CreateSecret(namespace.GetName(), secret)
if err != nil {
return fmt.Errorf("could not create Secret: %v", err)
}
log.Infof("Created new secret %s in namespace %s", secret.Name, namespace.GetName())
} else {
// Existing secret needs updated
log.Debugf("Found secret %s in namespace %s; will try to update it", secret.Name, namespace.GetName())
err := c.k8sutil.UpdateSecret(namespace.GetName(), secret)
if err != nil {
return fmt.Errorf("could not update Secret: %v", err)
}
log.Infof("Updated secret %s in namespace %s", secret.Name, namespace.GetName())
}
// Check if ServiceAccount exists
serviceAccount, err := c.k8sutil.GetServiceAccount(namespace.GetName(), "default")
if err != nil {
log.Errorf("error getting service account default in namespace %s: %s", namespace.GetName(), err)
return fmt.Errorf("could not get ServiceAccounts: %v", err)
}
// Update existing one if image pull secrets already exists for aws ecr token
imagePullSecretFound := false
for i, imagePullSecret := range serviceAccount.ImagePullSecrets {
if imagePullSecret.Name == secret.Name {
serviceAccount.ImagePullSecrets[i] = v1.LocalObjectReference{Name: secret.Name}
imagePullSecretFound = true
break
}
}
// Append to list of existing service accounts if there isn't one already
if !imagePullSecretFound {
serviceAccount.ImagePullSecrets = append(serviceAccount.ImagePullSecrets, v1.LocalObjectReference{Name: secret.Name})
}
log.Infof("Updating ServiceAccount %s in namespace %s", serviceAccount.Name, namespace.GetName())
err = c.k8sutil.UpdateServiceAccount(namespace.GetName(), serviceAccount)
if err != nil {
log.Errorf("error updating ServiceAccount %s in namespace %s: %s", serviceAccount.Name, namespace.GetName(), err)
return fmt.Errorf("could not update ServiceAccount: %v", err)
}
return nil
}
func (c *controller) generateSecrets() []*v1.Secret {
var secrets []*v1.Secret
secretGenerators := getSecretGenerators(c)
maxTries := RetryCfg.NumberOfRetries + 1
for _, secretGenerator := range secretGenerators {
resetRetryTimer()
logrus.Infof("------------------ [%s] ------------------\n", secretGenerator.SecretName)
var newTokens []AuthToken
tries := 0
for {
tries++
logrus.Infof("Getting secret; try #%d of %d", tries, maxTries)
tokens, err := secretGenerator.TokenGenFxn()
if err != nil {
if tries < maxTries {
delayDuration := nextRetryDuration()
if delayDuration == backoff.Stop {
logrus.Errorf("Error getting secret for provider %s. Retry timer exceeded max tries/duration; will not try again until the next refresh cycle. [Err: %s]", secretGenerator.SecretName, err)
break
}
logrus.Errorf("Error getting secret for provider %s. Will try again after %f seconds. [Err: %s]", secretGenerator.SecretName, delayDuration.Seconds(), err)
<-time.After(delayDuration)
continue
}
logrus.Errorf("Error getting secret for provider %s. Tried %d time(s); will not try again until the next refresh cycle. [Err: %s]", secretGenerator.SecretName, tries, err)
break
} else {
logrus.Infof("Successfully got secret for provider %s after trying %d time(s)", secretGenerator.SecretName, tries)
newTokens = tokens
break
}
}
newSecret, err := generateSecretObj(newTokens, secretGenerator.IsJSONCfg, secretGenerator.SecretName)
if err != nil {
logrus.Errorf("Error generating secret for provider %s. Skipping secret provider until the next refresh cycle! [Err: %s]", secretGenerator.SecretName, err)
} else {
secrets = append(secrets, newSecret)
}
}
return secrets
}
// SetupRetryTimer initializes and configures the Retry Timer
func SetupRetryTimer() {
delayDuration := time.Duration(RetryCfg.RetryDelayInSeconds) * time.Second
switch RetryCfg.Type {
case retryTypeSimple:
simpleBackoff = backoff.NewConstantBackOff(delayDuration)
break
case retryTypeExponential:
exponentialBackoff = backoff.NewExponentialBackOff()
break
}
}
func resetRetryTimer() {
switch RetryCfg.Type {
case retryTypeSimple:
simpleBackoff.Reset()
break
case retryTypeExponential:
exponentialBackoff.Reset()
break
}
}
func nextRetryDuration() time.Duration {
switch RetryCfg.Type {
case retryTypeSimple:
return simpleBackoff.NextBackOff()
case retryTypeExponential:
return exponentialBackoff.NextBackOff()
default:
return time.Duration(defaultTokenGenRetryDelay) * time.Second
}
}
func validateParams() {
// Allow environment variables to overwrite args
awsAccountIDEnv := os.Getenv("awsaccount")
awsRegionEnv := os.Getenv("awsregion")
argAWSAssumeRoleEnv := os.Getenv("aws_assume_role")
dprPassword := os.Getenv(dockerPrivateRegistryPasswordKey)
dprServer := os.Getenv(dockerPrivateRegistryServerKey)
dprUser := os.Getenv(dockerPrivateRegistryUserKey)
acrURL := os.Getenv(acrURLKey)
acrClientID := os.Getenv(acrClientIDKey)
acrPassword := os.Getenv(acrPasswordKey)
gcrURLEnv := os.Getenv("gcrurl")
// initialize the retry configuration using command line values
RetryCfg = RetryConfig{
Type: *argTokenGenFxnRetryType,
NumberOfRetries: *argTokenGenFxnRetries,
RetryDelayInSeconds: *argTokenGenFxnRetryDelay,
}
// ensure command line values are valid
if RetryCfg.Type != retryTypeSimple && RetryCfg.Type != retryTypeExponential {
logrus.Errorf("Unknown Retry Timer type '%s'! Defaulting to %s", RetryCfg.Type, defaultTokenGenRetryType)
RetryCfg.Type = defaultTokenGenRetryType
}
if RetryCfg.NumberOfRetries < 0 {
logrus.Errorf("Cannot use a negative value for the number of retries! Defaulting to %d", defaultTokenGenRetries)
RetryCfg.NumberOfRetries = defaultTokenGenRetries
}
if RetryCfg.RetryDelayInSeconds < 0 {
logrus.Errorf("Cannot use a negative value for the retry delay in seconds! Defaulting to %d", defaultTokenGenRetryDelay)
RetryCfg.RetryDelayInSeconds = defaultTokenGenRetryDelay
}
// look for overrides in environment variables and use them if they exist and are valid
tokenType, ok := os.LookupEnv(tokenGenRetryTypeKey)
if ok && len(tokenType) > 0 {
if tokenType != retryTypeSimple && tokenType != retryTypeExponential {
logrus.Errorf("Unknown Retry Timer type '%s'! Defaulting to %s", tokenType, defaultTokenGenRetryType)
RetryCfg.Type = defaultTokenGenRetryType
} else {
RetryCfg.Type = tokenType
}
}
tokenRetries, ok := os.LookupEnv(tokenGenRetriesKey)
if ok && len(tokenRetries) > 0 {
tokenRetriesInt, err := strconv.Atoi(tokenRetries)
if err != nil {
logrus.Errorf("Unable to parse value of environment variable %s! [Err: %s]", tokenGenRetriesKey, err)
} else {
if tokenRetriesInt < 0 {
logrus.Errorf("Cannot use a negative value for environment variable %s! Defaulting to %d", tokenGenRetriesKey, defaultTokenGenRetries)
RetryCfg.NumberOfRetries = defaultTokenGenRetries
} else {
RetryCfg.NumberOfRetries = tokenRetriesInt
}
}
}
tokenRetryDelay, ok := os.LookupEnv(tokenGenRetryDelayKey)
if ok && len(tokenRetryDelay) > 0 {
tokenRetryDelayInt, err := strconv.Atoi(tokenRetryDelay)
if err != nil {
logrus.Errorf("Unable to parse value of environment variable %s! [Err: %s]", tokenGenRetryDelayKey, err)
} else {
if tokenRetryDelayInt < 0 {
logrus.Errorf("Cannot use a negative value for environment variable %s! Defaulting to %d", tokenGenRetryDelayKey, defaultTokenGenRetryDelay)
RetryCfg.RetryDelayInSeconds = defaultTokenGenRetryDelay
} else {
RetryCfg.RetryDelayInSeconds = tokenRetryDelayInt
}
}
}
// Set up the Retry Timer
SetupRetryTimer()
if len(awsRegionEnv) > 0 {
argAWSRegion = &awsRegionEnv
}
if len(awsAccountIDEnv) > 0 {
awsAccountIDs = strings.Split(awsAccountIDEnv, ",")
} else {
awsAccountIDs = []string{""}
}
if len(dprPassword) > 0 {
argDPRPassword = &dprPassword
}
if len(dprServer) > 0 {
argDPRServer = &dprServer
}
if len(dprUser) > 0 {
argDPRUser = &dprUser
}
if len(gcrURLEnv) > 0 {
argGCRURL = &gcrURLEnv
}
if len(argAWSAssumeRoleEnv) > 0 {
argAWSAssumeRole = &argAWSAssumeRoleEnv
}
if len(acrURL) > 0 {
argACRURL = &acrURL
}
if len(acrClientID) > 0 {
argACRClientID = &acrClientID
}
if len(acrPassword) > 0 {
argACRPassword = &acrPassword
}
}
func handler(c *controller, ns *v1.Namespace) error {
logrus.Infof("Refreshing credentials for namespace %s", ns.GetName())
secrets := c.generateSecrets()
logrus.Infof("Got %d refreshed credentials for namespace %s", len(secrets), ns.GetName())
for _, secret := range secrets {
if *argSkipKubeSystem && ns.GetName() == "kube-system" {
continue
}
logrus.Infof("Processing secret for namespace %s, secret %s", ns.Name, secret.Name)
if err := c.processNamespace(ns, secret); err != nil {
logrus.Errorf("error processing secret for namespace %s, secret %s: %s", ns.Name, secret.Name, err)
return err
}
logrus.Infof("Finished processing secret for namespace %s, secret %s", ns.Name, secret.Name)
}
logrus.Infof("Finished refreshing credentials for namespace %s", ns.GetName())
return nil
}
func main() {
logrus.Info("Starting up...")
err := flags.Parse(os.Args)
if err != nil {
logrus.Fatalf("Could not parse command line arguments! [Err: %s]", err)
}
validateParams()
logrus.Info("Using AWS Account: ", strings.Join(awsAccountIDs, ","))
logrus.Info("Using AWS Region: ", *argAWSRegion)
logrus.Info("Using AWS Assume Role: ", *argAWSAssumeRole)
logrus.Info("Refresh Interval (minutes): ", *argRefreshMinutes)
logrus.Infof("Retry Timer: %s", RetryCfg.Type)
logrus.Info("Token Generation Retries: ", RetryCfg.NumberOfRetries)
logrus.Info("Token Generation Retry Delay (seconds): ", RetryCfg.RetryDelayInSeconds)
util, err := k8sutil.New(*argKubecfgFile, *argKubeMasterURL)
if err != nil {
logrus.Error("Could not create k8s client!!", err)
}
ecrClient := newEcrClient()
gcrClient := newGcrClient()
dprClient := newDprClient()
acrClient := newACRClient()
c := &controller{util, ecrClient, gcrClient, dprClient, acrClient}
util.WatchNamespaces(time.Duration(*argRefreshMinutes)*time.Minute, func(ns *v1.Namespace) error {
return handler(c, ns)
})
}