forked from kubernetes/kops
/
main.go
179 lines (143 loc) · 4.29 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"flag"
"fmt"
"github.com/golang/glog"
"k8s.io/kops/protokube/pkg/protokube"
"net"
"os"
"strings"
)
func main() {
master := false
flag.BoolVar(&master, "master", master, "Act as master")
containerized := false
flag.BoolVar(&containerized, "containerized", containerized, "Set if we are running containerized.")
dnsZoneName := ""
flag.StringVar(&dnsZoneName, "dns-zone-name", dnsZoneName, "Name of zone to use for DNS")
dnsInternalSuffix := ""
flag.StringVar(&dnsInternalSuffix, "dns-internal-suffix", dnsInternalSuffix, "DNS suffix for internal domain names")
clusterID := ""
flag.StringVar(&clusterID, "cluster-id", clusterID, "Cluster ID")
flagChannels := ""
flag.StringVar(&flagChannels, "channels", flagChannels, "channels to install")
flag.Set("logtostderr", "true")
flag.Parse()
volumes, err := protokube.NewAWSVolumes()
if err != nil {
glog.Errorf("Error initializing AWS: %q", err)
os.Exit(1)
}
if clusterID == "" {
clusterID = volumes.ClusterID()
if clusterID == "" {
glog.Errorf("cluster-id is required (cannot be determined from cloud)")
os.Exit(1)
} else {
glog.Infof("Setting cluster-id from cloud: %s", clusterID)
}
}
if dnsInternalSuffix == "" {
// TODO: Maybe only master needs DNS?
dnsInternalSuffix = ".internal." + clusterID
glog.Infof("Setting dns-internal-suffix to %q", dnsInternalSuffix)
}
// Make sure it's actually a suffix (starts with .)
if !strings.HasPrefix(dnsInternalSuffix, ".") {
dnsInternalSuffix = "." + dnsInternalSuffix
}
if dnsZoneName == "" {
tokens := strings.Split(dnsInternalSuffix, ".")
dnsZoneName = strings.Join(tokens[len(tokens)-2:], ".")
}
// Get internal IP from cloud, to avoid problems if we're in a container
// TODO: Just run with --net=host ??
//internalIP, err := findInternalIP()
//if err != nil {
// glog.Errorf("Error finding internal IP: %q", err)
// os.Exit(1)
//}
internalIP := volumes.InternalIP()
dns, err := protokube.NewRoute53DNSProvider(dnsZoneName)
if err != nil {
glog.Errorf("Error initializing DNS: %q", err)
os.Exit(1)
}
rootfs := "/"
if containerized {
rootfs = "/rootfs/"
}
protokube.RootFS = rootfs
protokube.Containerized = containerized
modelDir := "model/etcd"
var channels []string
if flagChannels != "" {
channels = strings.Split(flagChannels, ",")
}
k := &protokube.KubeBoot{
Master: master,
InternalDNSSuffix: dnsInternalSuffix,
InternalIP: internalIP,
//MasterID : fromVolume
//EtcdClusters : fromVolume
ModelDir: modelDir,
DNS: dns,
Channels: channels,
Kubernetes: protokube.NewKubernetesContext(),
}
k.Init(volumes)
k.RunSyncLoop()
glog.Infof("Unexpected exit")
os.Exit(1)
}
// TODO: run with --net=host ??
func findInternalIP() (net.IP, error) {
var ips []net.IP
networkInterfaces, err := net.Interfaces()
if err != nil {
return nil, fmt.Errorf("error querying interfaces to determine internal ip: %v", err)
}
for i := range networkInterfaces {
networkInterface := &networkInterfaces[i]
flags := networkInterface.Flags
name := networkInterface.Name
if (flags & net.FlagLoopback) != 0 {
glog.V(2).Infof("Ignoring interface %s - loopback", name)
continue
}
// Not a lot else to go on...
if !strings.HasPrefix(name, "eth") {
glog.V(2).Infof("Ignoring interface %s - name does not look like ethernet device", name)
continue
}
addrs, err := networkInterface.Addrs()
if err != nil {
return nil, fmt.Errorf("error querying network interface %s for IP adddresses: %v", name, err)
}
for _, addr := range addrs {
ip, _, err := net.ParseCIDR(addr.String())
if err != nil {
return nil, fmt.Errorf("error parsing address %s on network interface %s: %v", addr.String(), name, err)
}
if ip.IsLoopback() {
glog.V(2).Infof("Ignoring address %s (loopback)", ip)
continue
}
if ip.IsLinkLocalMulticast() || ip.IsLinkLocalUnicast() {
glog.V(2).Infof("Ignoring address %s (link-local)", ip)
continue
}
ips = append(ips, ip)
}
}
if len(ips) == 0 {
return nil, fmt.Errorf("unable to determine internal ip (no adddresses found)")
}
if len(ips) != 1 {
glog.Warningf("Found multiple internal IPs; making arbitrary choice")
for _, ip := range ips {
glog.Warningf("\tip: %s", ip.String())
}
}
return ips[0], nil
}