-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
131 lines (111 loc) · 3.72 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
package main
import (
"context"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"
kingpin "github.com/alecthomas/kingpin/v2"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/sts"
log "github.com/sirupsen/logrus"
"github.com/zalando-incubator/kube-aws-iam-controller/pkg/clientset"
v1 "k8s.io/api/core/v1"
)
const (
defaultInterval = "10s"
defaultRefreshLimit = "15m"
defaultClientGOTimeout = 30 * time.Second
)
var (
config struct {
Debug bool
Interval time.Duration
RefreshLimit time.Duration
BaseRoleARN string
APIServer *url.URL
Namespace string
AssumeRole string
}
)
func main() {
kingpin.Flag("debug", "Enable debug logging.").BoolVar(&config.Debug)
kingpin.Flag("interval", "Interval between syncing secrets.").
Default(defaultInterval).DurationVar(&config.Interval)
kingpin.Flag("refresh-limit", "Time limit when AWS IAM credentials should be refreshed. I.e. 15 min. before they expire.").
Default(defaultRefreshLimit).DurationVar(&config.RefreshLimit)
kingpin.Flag("base-role-arn", "Base Role ARN. If not defined it will be autodiscovered from EC2 Metadata.").
StringVar(&config.BaseRoleARN)
kingpin.Flag("assume-role", "Assume Role can be specified to assume a role at start-up which is used for further assuming other roles managed by the controller.").
StringVar(&config.AssumeRole)
kingpin.Flag("namespace", "Limit the controller to a certain namespace.").
Default(v1.NamespaceAll).StringVar(&config.Namespace)
kingpin.Flag("apiserver", "API server url.").URLVar(&config.APIServer)
kingpin.Parse()
if config.Debug {
log.SetLevel(log.DebugLevel)
}
ctx, cancel := context.WithCancel(context.Background())
kubeConfig, err := clientset.ConfigureKubeConfig(config.APIServer, defaultClientGOTimeout, ctx.Done())
if err != nil {
log.Fatalf("Failed to set up Kubernetes config: %v", err)
}
client, err := clientset.NewForConfig(kubeConfig)
if err != nil {
log.Fatalf("Failed to initialize Kubernetes client: %v.", err)
}
awsCfg, err := awsconfig.LoadDefaultConfig(context.Background())
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
if config.BaseRoleARN == "" {
config.BaseRoleARN, err = GetBaseRoleARN(context.Background(), awsCfg)
if err != nil {
log.Fatalf("Failed to autodiscover Base Role ARN: %v", err)
}
log.Infof("Autodiscovered Base Role ARN: %s", config.BaseRoleARN)
}
baseRoleARNPrefix, err := GetPrefixFromARN(config.BaseRoleARN)
if err != nil {
log.Fatalf("Failed to parse ARN prefix from Base Role ARN: %v", err)
}
log.Debugf("Parsed Base Role ARN prefix: %s", baseRoleARNPrefix)
if config.AssumeRole != "" {
if !strings.HasPrefix(config.AssumeRole, baseRoleARNPrefix) {
config.AssumeRole = config.BaseRoleARN + config.AssumeRole
}
log.Infof("Using custom Assume Role: %s", config.AssumeRole)
stssvc := sts.NewFromConfig(awsCfg)
creds := stscreds.NewAssumeRoleProvider(stssvc, config.AssumeRole)
awsCfg.Credentials = creds
}
credsGetter := NewSTSCredentialsGetter(awsCfg, config.BaseRoleARN, baseRoleARNPrefix)
controller := NewSecretsController(
client,
config.Namespace,
config.Interval,
config.RefreshLimit,
credsGetter,
)
go handleSigterm(cancel)
awsIAMRoleController := NewAWSIAMRoleController(
client,
config.Interval,
config.RefreshLimit,
credsGetter,
config.Namespace,
)
go awsIAMRoleController.Run(ctx)
controller.Run(ctx)
}
// handleSigterm handles SIGTERM signal sent to the process.
func handleSigterm(cancelFunc func()) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGTERM)
<-signals
log.Info("Received Term signal. Terminating...")
cancelFunc()
}