-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
96 lines (80 loc) · 2.56 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
package main
import (
"flag"
"fmt"
"github.com/zhangguanzhang/dummy-tool/app"
utilnet "github.com/zhangguanzhang/dummy-tool/pkg/net"
"log"
"net"
"os"
"runtime"
"strings"
"time"
)
var (
toolApp *app.App
Version = "UNKNOWN"
gitCommit string
gitTreeState = "" // state of git tree, either "clean" or "dirty"
buildDate = "1970-01-01T00:00:00Z" // build date, output of $(date +'%Y-%m-%dT%H:%M:%S')
)
func init() {
cfg, err := parseAndValidateFlags()
if err != nil {
log.Fatalf("Failed to obtain conf instance, err %v", err)
}
toolApp = app.NewApp(cfg)
}
func parseAndValidateFlags() (*app.Conf, error) {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprint(os.Stderr, "Run as a dummy interface tool at initContainer or a Container in kubernetes pod\n")
flag.PrintDefaults()
}
params := &app.Conf{}
var (
LocalIPStr string
versionPrt bool
)
flag.StringVar(&LocalIPStr, "local-ip", "", "comma-separated string of ip addresses to bind localdns process to")
flag.StringVar(¶ms.HealthPort, "health-port", "8080", "port used by health check, ex: 0.0.0.0:8080")
flag.BoolVar(¶ms.SetupInterface, "setup-interface", true, "indicates whether network interface should be setup")
flag.BoolVar(¶ms.ExitRemove, "exit-remove", true, "will remove the interface when exit")
flag.StringVar(¶ms.InterfaceName, "interface-name", "nodelocaldns", "name of the interface to be created")
flag.DurationVar(¶ms.Interval, "check-interval", time.Second*5, "interval(in seconds) to check for interface status and addr")
flag.BoolVar(&versionPrt, "version", false, "print version info and exit")
flag.Parse()
versionPrint()
if versionPrt {
os.Exit(0)
}
for _, ipstr := range strings.Split(LocalIPStr, ",") {
newIP := net.ParseIP(ipstr)
if newIP == nil {
return nil, fmt.Errorf("invalid localip specified - %q", ipstr)
}
params.LocalIPs = append(params.LocalIPs, newIP)
}
// validate all the IPs have the same IP family
for _, ip := range params.LocalIPs {
if utilnet.IsIPv6(params.LocalIPs[0]) != utilnet.IsIPv6(ip) {
return nil, fmt.Errorf("unexpected IP Family for localIP - %q, want IPv6=%v", ip, utilnet.IsIPv6(params.LocalIPs[0]))
}
}
return params, nil
}
func main() {
toolApp.Init()
toolApp.RunApp()
}
func versionPrint() {
fmt.Printf(`Name: dummy-tool
Version: %s
CommitID: %s
GitTreeState: %s
BuildDate: %s
GoVersion: %s
Compiler: %s
Platform: %s/%s
`, Version, gitCommit, gitTreeState, buildDate, runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH)
}