forked from janeczku/external-lb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (102 loc) · 3.07 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
package main
import (
"flag"
"github.com/Sirupsen/logrus"
"github.com/rancher/external-lb/metadata"
"github.com/rancher/external-lb/providers"
_ "github.com/rancher/external-lb/providers/f5"
"os"
"time"
)
const (
poll = 1000
// if metadata wasn't updated in 1 min, force update would be executed
forceUpdateInterval = 1
)
type Op struct {
Name string
}
var (
Add = Op{Name: "Add"}
Remove = Op{Name: "Remove"}
Update = Op{Name: "Update"}
)
var (
providerName = flag.String("provider", "", "External LB provider name")
debug = flag.Bool("debug", false, "Debug")
logFile = flag.String("log", "", "Log file")
provider providers.Provider
m *metadata.MetadataClient
lbEndpointServiceLabel string
targetRancherSuffix string
)
func setEnv() {
flag.Parse()
provider = providers.GetProvider(*providerName)
if *debug {
logrus.SetLevel(logrus.DebugLevel)
}
if *logFile != "" {
if output, err := os.OpenFile(*logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666); err != nil {
logrus.Fatalf("Failed to log to file %s: %v", *logFile, err)
} else {
logrus.SetOutput(output)
formatter := &logrus.TextFormatter{
FullTimestamp: true,
}
logrus.SetFormatter(formatter)
}
}
// configure metadata client
mClient, err := metadata.NewMetadataClient()
if err != nil {
logrus.Fatalf("Failed to configure rancher-metadata client: %v", err)
}
m = mClient
targetRancherSuffix = os.Getenv("LB_TARGET_RANCHER_SUFFIX")
if len(targetRancherSuffix) == 0 {
logrus.Info("LB_TARGET_RANCHER_SUFFIX is not set, using default suffix 'rancher.internal'")
targetRancherSuffix = "rancher.internal"
}
lbEndpointServiceLabel = "io.rancher.service.external_lb_endpoint"
}
func main() {
logrus.Infof("Starting Rancher External LoadBalancer service")
setEnv()
logrus.Infof("Powered by %s", provider.GetName())
go startHealthcheck()
version := "init"
lastUpdated := time.Now()
for {
newVersion, err := m.GetVersion()
update := false
if err != nil {
logrus.Errorf("Error reading metadata version: %v", err)
} else if version != newVersion {
logrus.Debugf("Metadata version has been changed. Old version: %s. New version: %s.", version, newVersion)
version = newVersion
update = true
} else {
//logrus.Debugf("No changes in metadata version: %s", newVersion)
if time.Since(lastUpdated).Minutes() >= forceUpdateInterval {
logrus.Debugf("Executing force update as metadata version hasn't been changed in: %v minutes", forceUpdateInterval)
update = true
}
}
if update {
// get records from metadata
metadataLBConfigs, err := m.GetMetadataLBConfigs(lbEndpointServiceLabel, targetRancherSuffix)
if err != nil {
logrus.Errorf("Error reading metadata lb entries: %v", err)
}
logrus.Debugf("LB configs from metadata: %v", metadataLBConfigs)
/*update provider*/
err = UpdateProviderLBConfigs(metadataLBConfigs)
if err != nil {
logrus.Errorf("Error reading provider lb entries: %v", err)
}
lastUpdated = time.Now()
}
time.Sleep(time.Duration(poll) * time.Millisecond)
}
}