forked from flannel-io/flannel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
302 lines (259 loc) · 8.82 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"net"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"github.com/coreos/flannel/Godeps/_workspace/src/github.com/coreos/go-systemd/daemon"
log "github.com/coreos/flannel/Godeps/_workspace/src/github.com/golang/glog"
"github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/coreos/flannel/backend"
"github.com/coreos/flannel/network"
"github.com/coreos/flannel/pkg/ip"
"github.com/coreos/flannel/remote"
"github.com/coreos/flannel/subnet"
)
type CmdLineOpts struct {
etcdEndpoints string
etcdPrefix string
etcdKeyfile string
etcdCertfile string
etcdCAFile string
help bool
version bool
ipMasq bool
subnetFile string
subnetDir string
iface string
listen string
remote string
remoteKeyfile string
remoteCertfile string
remoteCAFile string
networks string
}
var opts CmdLineOpts
func init() {
flag.StringVar(&opts.etcdEndpoints, "etcd-endpoints", "http://127.0.0.1:4001,http://127.0.0.1:2379", "a comma-delimited list of etcd endpoints")
flag.StringVar(&opts.etcdPrefix, "etcd-prefix", "/coreos.com/network", "etcd prefix")
flag.StringVar(&opts.etcdKeyfile, "etcd-keyfile", "", "SSL key file used to secure etcd communication")
flag.StringVar(&opts.etcdCertfile, "etcd-certfile", "", "SSL certification file used to secure etcd communication")
flag.StringVar(&opts.etcdCAFile, "etcd-cafile", "", "SSL Certificate Authority file used to secure etcd communication")
flag.StringVar(&opts.subnetFile, "subnet-file", "/run/flannel/subnet.env", "filename where env variables (subnet, MTU, ... ) will be written to")
flag.StringVar(&opts.subnetDir, "subnet-dir", "/run/flannel/networks", "directory where files with env variables (subnet, MTU, ...) will be written to")
flag.StringVar(&opts.iface, "iface", "", "interface to use (IP or name) for inter-host communication")
flag.StringVar(&opts.listen, "listen", "", "run as server and listen on specified address (e.g. ':8080')")
flag.StringVar(&opts.remote, "remote", "", "run as client and connect to server on specified address (e.g. '10.1.2.3:8080')")
flag.StringVar(&opts.remoteKeyfile, "remote-keyfile", "", "SSL key file used to secure client/server communication")
flag.StringVar(&opts.remoteCertfile, "remote-certfile", "", "SSL certification file used to secure client/server communication")
flag.StringVar(&opts.remoteCAFile, "remote-cafile", "", "SSL Certificate Authority file used to secure client/server communication")
flag.StringVar(&opts.networks, "networks", "", "run in multi-network mode and service the specified networks")
flag.BoolVar(&opts.ipMasq, "ip-masq", false, "setup IP masquerade rule for traffic destined outside of overlay network")
flag.BoolVar(&opts.help, "help", false, "print this message")
flag.BoolVar(&opts.version, "version", false, "print version and exit")
}
// TODO: This is yet another copy (others found in etcd, fleet) -- Pull it out!
// flagsFromEnv parses all registered flags in the given flagset,
// and if they are not already set it attempts to set their values from
// environment variables. Environment variables take the name of the flag but
// are UPPERCASE, have the given prefix, and any dashes are replaced by
// underscores - for example: some-flag => PREFIX_SOME_FLAG
func flagsFromEnv(prefix string, fs *flag.FlagSet) {
alreadySet := make(map[string]bool)
fs.Visit(func(f *flag.Flag) {
alreadySet[f.Name] = true
})
fs.VisitAll(func(f *flag.Flag) {
if !alreadySet[f.Name] {
key := strings.ToUpper(prefix + "_" + strings.Replace(f.Name, "-", "_", -1))
val := os.Getenv(key)
if val != "" {
fs.Set(f.Name, val)
}
}
})
}
func writeSubnetFile(path string, sn *backend.SubnetDef) error {
dir, name := filepath.Split(path)
os.MkdirAll(dir, 0755)
tempFile := filepath.Join(dir, "."+name)
f, err := os.Create(tempFile)
if err != nil {
return err
}
// Write out the first usable IP by incrementing
// sn.IP by one
sn.Net.IP += 1
fmt.Fprintf(f, "FLANNEL_SUBNET=%s\n", sn.Net)
fmt.Fprintf(f, "FLANNEL_MTU=%d\n", sn.MTU)
_, err = fmt.Fprintf(f, "FLANNEL_IPMASQ=%v\n", opts.ipMasq)
f.Close()
if err != nil {
return err
}
// rename(2) the temporary file to the desired location so that it becomes
// atomically visible with the contents
return os.Rename(tempFile, path)
}
func lookupIface() (*net.Interface, net.IP, error) {
var iface *net.Interface
var ipaddr net.IP
var err error
if len(opts.iface) > 0 {
if ipaddr = net.ParseIP(opts.iface); ipaddr != nil {
iface, err = ip.GetInterfaceByIP(ipaddr)
if err != nil {
return nil, nil, fmt.Errorf("Error looking up interface %s: %s", opts.iface, err)
}
} else {
iface, err = net.InterfaceByName(opts.iface)
if err != nil {
return nil, nil, fmt.Errorf("Error looking up interface %s: %s", opts.iface, err)
}
}
} else {
log.Info("Determining IP address of default interface")
if iface, err = ip.GetDefaultGatewayIface(); err != nil {
return nil, nil, fmt.Errorf("Failed to get default interface: %s", err)
}
}
if ipaddr == nil {
ipaddr, err = ip.GetIfaceIP4Addr(iface)
if err != nil {
return nil, nil, fmt.Errorf("Failed to find IPv4 address for interface %s", iface.Name)
}
}
return iface, ipaddr, nil
}
func isMultiNetwork() bool {
return len(opts.networks) > 0
}
func newSubnetManager() (subnet.Manager, error) {
if opts.remote != "" {
return remote.NewRemoteManager(opts.remote, opts.remoteCAFile, opts.remoteCertfile, opts.remoteKeyfile)
}
cfg := &subnet.EtcdConfig{
Endpoints: strings.Split(opts.etcdEndpoints, ","),
Keyfile: opts.etcdKeyfile,
Certfile: opts.etcdCertfile,
CAFile: opts.etcdCAFile,
Prefix: opts.etcdPrefix,
}
return subnet.NewEtcdManager(cfg)
}
func initAndRun(ctx context.Context, sm subnet.Manager, netnames []string) {
iface, ipaddr, err := lookupIface()
if err != nil {
log.Error(err)
return
}
if iface.MTU == 0 {
log.Errorf("Failed to determine MTU for %s interface", ipaddr)
return
}
log.Infof("Using %s as external interface", ipaddr)
nets := []*network.Network{}
for _, n := range netnames {
nets = append(nets, network.New(sm, n, opts.ipMasq))
}
wg := sync.WaitGroup{}
for _, n := range nets {
go func(n *network.Network) {
wg.Add(1)
defer wg.Done()
sn := n.Init(ctx, iface, ipaddr)
if sn != nil {
if isMultiNetwork() {
path := filepath.Join(opts.subnetDir, n.Name) + ".env"
if err := writeSubnetFile(path, sn); err != nil {
return
}
} else {
if err := writeSubnetFile(opts.subnetFile, sn); err != nil {
return
}
daemon.SdNotify("READY=1")
}
n.Run(ctx)
log.Infof("%v exited", n.Name)
}
}(n)
}
wg.Wait()
}
func main() {
// glog will log to tmp files by default. override so all entries
// can flow into journald (if running under systemd)
flag.Set("logtostderr", "true")
// now parse command line args
flag.Parse()
if flag.NArg() > 0 || opts.help {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]...\n", os.Args[0])
flag.PrintDefaults()
os.Exit(0)
}
if opts.version {
fmt.Fprintln(os.Stderr, Version)
os.Exit(0)
}
flagsFromEnv("FLANNELD", flag.CommandLine)
sm, err := newSubnetManager()
if err != nil {
log.Error("Failed to create SubnetManager: ", err)
os.Exit(1)
}
var runFunc func(ctx context.Context)
if opts.listen != "" {
if opts.remote != "" {
log.Error("--listen and --remote are mutually exclusive")
os.Exit(1)
}
log.Info("running as server")
runFunc = func(ctx context.Context) {
remote.RunServer(ctx, sm, opts.listen, opts.remoteCAFile, opts.remoteCertfile, opts.remoteKeyfile)
}
} else {
networks := strings.Split(opts.networks, ",")
if len(networks) == 0 {
networks = append(networks, "")
}
runFunc = func(ctx context.Context) {
initAndRun(ctx, sm, networks)
}
}
// Register for SIGINT and SIGTERM
log.Info("Installing signal handlers")
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
runFunc(ctx)
wg.Done()
}()
<-sigs
// unregister to get default OS nuke behaviour in case we don't exit cleanly
signal.Stop(sigs)
log.Info("Exiting...")
cancel()
wg.Wait()
}