forked from openshift/openshift-sdn
/
main.go
161 lines (139 loc) · 5.34 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
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"os/signal"
"path"
"strings"
"syscall"
log "github.com/golang/glog"
"github.com/openshift/openshift-sdn/ovssubnet"
"github.com/openshift/openshift-sdn/ovssubnet/api"
"github.com/openshift/openshift-sdn/ovssubnet/registry"
)
type NetworkManager interface {
StartMaster(sync bool, containerNetwork string, containerSubnetLength uint) error
StartNode(sync, skipsetup bool) error
Stop()
}
type CmdLineOpts struct {
containerNetwork string
containerSubnetLength uint
etcdEndpoints string
etcdPath string
etcdKeyfile string
etcdCertfile string
etcdCAFile string
ip string
hostname string
minionPath string
master bool
minion bool
skipsetup bool
sync bool
kube bool
multitenant bool
help bool
}
var opts CmdLineOpts
func init() {
flag.StringVar(&opts.containerNetwork, "container-network", "10.1.0.0/16", "container network")
flag.UintVar(&opts.containerSubnetLength, "container-subnet-length", 8, "container subnet length")
flag.StringVar(&opts.etcdEndpoints, "etcd-endpoints", "http://127.0.0.1:4001", "a comma-delimited list of etcd endpoints")
flag.StringVar(&opts.etcdPath, "etcd-path", "/registry/sdn/", "etcd path")
flag.StringVar(&opts.minionPath, "minion-path", "/kubernetes.io/minions/", "etcd path that will be watched for minion creation/deletion (Note: -sync flag will override this path with -etcd-path)")
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.ip, "public-ip", "", "Publicly reachable IP address of this host (for node mode).")
flag.StringVar(&opts.hostname, "hostname", "", "Hostname as registered with master (for node mode), will default to 'hostname -f'")
flag.BoolVar(&opts.master, "master", true, "Run in master mode")
flag.BoolVar(&opts.minion, "minion", false, "Run in minion mode")
flag.BoolVar(&opts.skipsetup, "skip-setup", false, "Skip the setup when in minion mode")
flag.BoolVar(&opts.sync, "sync", false, "Sync the minions directly to etcd-path (Do not wait for PaaS to do so!)")
flag.BoolVar(&opts.kube, "kube", false, "Use kubernetes hooks for optimal integration with OVS. This option bypasses the Linux bridge. Any docker containers started manually (not through OpenShift/Kubernetes) will stay local and not connect to the SDN.")
flag.BoolVar(&opts.multitenant, "multitenant", false, "Same as 'kube' but with multitenant capabilities. This option will only be examined if 'kube' option is 'false'.")
flag.BoolVar(&opts.help, "help", false, "print this message")
}
func newNetworkManager() (NetworkManager, error) {
sub, err := newSubnetRegistry()
if err != nil {
return nil, err
}
host := opts.hostname
if host == "" {
output, err := exec.Command("hostname", "-f").CombinedOutput()
if err != nil {
return nil, err
}
host = strings.TrimSpace(string(output))
}
if opts.kube {
return ovssubnet.NewKubeController(sub, string(host), opts.ip, nil)
} else {
if opts.multitenant {
return ovssubnet.NewMultitenantController(sub, string(host), opts.ip, nil)
}
}
// default OVS controller
return ovssubnet.NewDefaultController(sub, string(host), opts.ip, nil)
}
func newSubnetRegistry() (api.SubnetRegistry, error) {
peers := strings.Split(opts.etcdEndpoints, ",")
subnetPath := path.Join(opts.etcdPath, "subnets")
subnetConfigPath := path.Join(opts.etcdPath, "config")
minionPath := opts.minionPath
if opts.sync {
minionPath = path.Join(opts.etcdPath, "minions")
}
cfg := ®istry.EtcdConfig{
Endpoints: peers,
Keyfile: opts.etcdKeyfile,
Certfile: opts.etcdCertfile,
CAFile: opts.etcdCAFile,
SubnetPath: subnetPath,
SubnetConfigPath: subnetConfigPath,
MinionPath: minionPath,
}
return registry.NewEtcdSubnetRegistry(cfg)
}
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 opts.help {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]...\n", os.Args[0])
flag.PrintDefaults()
os.Exit(0)
}
// Register for SIGINT and SIGTERM and wait for one of them to arrive
log.Info("Installing signal handlers")
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
be, err := newNetworkManager()
if err != nil {
log.Fatalf("Failed to create new network manager: %v", err)
}
if opts.minion {
err := be.StartNode(opts.sync, opts.skipsetup)
if err != nil {
log.Fatalf("Failed to start openshift sdn in node mode: %v", err)
}
} else if opts.master {
err := be.StartMaster(opts.sync, opts.containerNetwork, opts.containerSubnetLength)
if err != nil {
log.Fatalf("Failed to start openshift sdn in master mode: %v", err)
}
}
select {
case <-sigs:
// unregister to get default OS nuke behaviour in case we don't exit cleanly
signal.Stop(sigs)
log.Info("Exiting...")
be.Stop()
}
}