forked from gardener-attic/aws-lb-readvertiser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (103 loc) · 3.42 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
// SPDX-FileCopyrightText: 2017 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/gardener/aws-lb-readvertiser/controller"
"k8s.io/client-go/informers"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// AWSReadvertiserOptions are the options for the AWSReadvertiser
type AWSReadvertiserOptions struct {
endpointName string
kubeconfig string
elb string
refreshPeriod int
controllerResyncPeriod int
}
func (a *AWSReadvertiserOptions) addFlags() {
flag.StringVar(&a.kubeconfig, "kubeconfig", "", "kubeconfig")
flag.StringVar(&a.elb, "elb-dns-name", "", "DNS name of elb")
flag.IntVar(&a.refreshPeriod, "refresh-period", 5, "the period at which the Loadbalancer value is checked (in seconds)")
flag.IntVar(&a.controllerResyncPeriod, "resync-period", 30, "the period at which the controller sync with the cache will happen (in seconds)")
flag.Parse()
}
func (a *AWSReadvertiserOptions) validateFlags() error {
if len(a.elb) == 0 {
return fmt.Errorf("The DNS value for the ELB needs to be set properly")
}
// Check to see if the domain is a valid FQDN
if !strings.HasSuffix(a.elb, ".") {
a.elb = fmt.Sprintf("%s.", a.elb)
}
if a.refreshPeriod == 0 {
log.Infof("The refresh period was not set, using default %d", a.refreshPeriod)
return nil
}
if a.controllerResyncPeriod == 0 {
log.Infof("The controller resync period was not set, using default %d", a.controllerResyncPeriod)
}
return nil
}
func (a *AWSReadvertiserOptions) initializeClient() (*kubernetes.Clientset, error) {
var config *rest.Config
switch {
case len(a.kubeconfig) != 0:
log.Infof("Using config from flag --kubeconfig %q", a.kubeconfig)
default:
a.kubeconfig, _ = os.LookupEnv("KUBECONFIG")
log.Infof("Using config from $KUBECONFIG %q", a.kubeconfig)
}
config, err := clientcmd.BuildConfigFromFlags("", a.kubeconfig)
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(config)
}
func (a *AWSReadvertiserOptions) run(ctx context.Context, client kubernetes.Interface) {
var (
sharedInformers = informers.NewSharedInformerFactory(client, time.Duration(a.controllerResyncPeriod)*time.Second)
awsLBReadvertiserController = controller.NewAWSLBEndpointsController(client, sharedInformers.Core().V1().Endpoints(), a.elb, "kubernetes")
refreshTicker = time.NewTicker(time.Duration(a.refreshPeriod) * time.Second)
)
go sharedInformers.Start(ctx.Done())
awsLBReadvertiserController.Run(ctx, refreshTicker)
}
func main() {
awsReadvertiser := new(AWSReadvertiserOptions)
ctx, cancel := context.WithCancel(context.Background())
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
defer func() {
signal.Stop(signalChan)
cancel()
}()
go func() {
select {
case sig := <-signalChan:
log.Printf("received signal: %s", sig.String())
cancel()
case <-ctx.Done():
}
}()
awsReadvertiser.addFlags()
if err := awsReadvertiser.validateFlags(); err != nil {
log.Fatalf("Invalid flags, reason: %+v", err)
}
client, err := awsReadvertiser.initializeClient()
if err != nil {
log.Fatalf("failed to initialize client, error: %+v", err)
}
awsReadvertiser.run(ctx, client)
}