-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (52 loc) · 1.45 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
package main
import (
"strings"
"time"
"github.com/chickenzord/kube-node-publish/pkg/config"
"github.com/chickenzord/kube-node-publish/pkg/dns"
"github.com/chickenzord/kube-node-publish/pkg/kube"
"github.com/joho/godotenv"
log "github.com/sirupsen/logrus"
)
func main() {
godotenv.Load()
dns.InitConfig()
cfg := config.NewConfigFromEnv()
// logging config
logLevel, _ := log.ParseLevel(cfg.LogLevel)
log.SetLevel(logLevel)
// check
log.Printf("version %s (%s)", config.Version, config.GitSha)
log.Println(cfg)
if cfg.DomainName == "" {
log.Fatal("domain name required")
}
// prepare client
clientset, _ := kube.NewClientSet(cfg.InCluster, cfg.KubeConfig)
if cfg.InCluster {
log.Info("using in-cluster kubeconfig")
} else {
log.Infof("using kubeconfig: %s", cfg.KubeConfig)
}
// start updating
var curValue = ""
for {
ipAddresses, _ := kube.GetNodeAddresses(clientset)
newValue := strings.Join(ipAddresses, " ")
if curValue == newValue {
log.Debug("no ip address changes")
} else {
log.Infof("found %d ip address: %v", len(ipAddresses), ipAddresses)
if err := dns.EnsureRecord(cfg.DomainName, "A", newValue); err != nil {
log.Errorf("cannot update dns record: %v", err)
} else {
log.Infof("updated A %s record: %s", cfg.DomainName, newValue)
curValue = newValue
}
}
// delay
duration, _ := time.ParseDuration(cfg.CheckPeriod)
log.Debugf("waiting %s", cfg.CheckPeriod)
time.Sleep(duration)
}
}