main.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2015 flannel authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "flag"
  17. "fmt"
  18. "os"
  19. "os/signal"
  20. "strings"
  21. "sync"
  22. "syscall"
  23. "github.com/coreos/pkg/flagutil"
  24. log "github.com/golang/glog"
  25. "golang.org/x/net/context"
  26. "github.com/coreos/flannel/network"
  27. "github.com/coreos/flannel/remote"
  28. "github.com/coreos/flannel/subnet"
  29. "github.com/coreos/flannel/subnet/kube"
  30. "github.com/coreos/flannel/version"
  31. // Backends need to be imported for their init() to get executed and them to register
  32. _ "github.com/coreos/flannel/backend/alloc"
  33. _ "github.com/coreos/flannel/backend/awsvpc"
  34. _ "github.com/coreos/flannel/backend/gce"
  35. _ "github.com/coreos/flannel/backend/hostgw"
  36. _ "github.com/coreos/flannel/backend/udp"
  37. _ "github.com/coreos/flannel/backend/vxlan"
  38. )
  39. type CmdLineOpts struct {
  40. etcdEndpoints string
  41. etcdPrefix string
  42. etcdKeyfile string
  43. etcdCertfile string
  44. etcdCAFile string
  45. etcdUsername string
  46. etcdPassword string
  47. help bool
  48. version bool
  49. listen string
  50. remote string
  51. remoteKeyfile string
  52. remoteCertfile string
  53. remoteCAFile string
  54. kubeSubnetMgr bool
  55. }
  56. var opts CmdLineOpts
  57. func init() {
  58. 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")
  59. flag.StringVar(&opts.etcdPrefix, "etcd-prefix", "/coreos.com/network", "etcd prefix")
  60. flag.StringVar(&opts.etcdKeyfile, "etcd-keyfile", "", "SSL key file used to secure etcd communication")
  61. flag.StringVar(&opts.etcdCertfile, "etcd-certfile", "", "SSL certification file used to secure etcd communication")
  62. flag.StringVar(&opts.etcdCAFile, "etcd-cafile", "", "SSL Certificate Authority file used to secure etcd communication")
  63. flag.StringVar(&opts.etcdUsername, "etcd-username", "", "Username for BasicAuth to etcd")
  64. flag.StringVar(&opts.etcdPassword, "etcd-password", "", "Password for BasicAuth to etcd")
  65. flag.StringVar(&opts.listen, "listen", "", "run as server and listen on specified address (e.g. ':8080')")
  66. flag.StringVar(&opts.remote, "remote", "", "run as client and connect to server on specified address (e.g. '10.1.2.3:8080')")
  67. flag.StringVar(&opts.remoteKeyfile, "remote-keyfile", "", "SSL key file used to secure client/server communication")
  68. flag.StringVar(&opts.remoteCertfile, "remote-certfile", "", "SSL certification file used to secure client/server communication")
  69. flag.StringVar(&opts.remoteCAFile, "remote-cafile", "", "SSL Certificate Authority file used to secure client/server communication")
  70. flag.BoolVar(&opts.kubeSubnetMgr, "kube-subnet-mgr", false, "Contact the Kubernetes API for subnet assignement instead of etcd or flannel-server.")
  71. flag.BoolVar(&opts.help, "help", false, "print this message")
  72. flag.BoolVar(&opts.version, "version", false, "print version and exit")
  73. }
  74. func newSubnetManager() (subnet.Manager, error) {
  75. if opts.remote != "" {
  76. return remote.NewRemoteManager(opts.remote, opts.remoteCAFile, opts.remoteCertfile, opts.remoteKeyfile)
  77. }
  78. if opts.kubeSubnetMgr {
  79. return kube.NewSubnetManager()
  80. }
  81. cfg := &subnet.EtcdConfig{
  82. Endpoints: strings.Split(opts.etcdEndpoints, ","),
  83. Keyfile: opts.etcdKeyfile,
  84. Certfile: opts.etcdCertfile,
  85. CAFile: opts.etcdCAFile,
  86. Prefix: opts.etcdPrefix,
  87. Username: opts.etcdUsername,
  88. Password: opts.etcdPassword,
  89. }
  90. return subnet.NewLocalManager(cfg)
  91. }
  92. func main() {
  93. // glog will log to tmp files by default. override so all entries
  94. // can flow into journald (if running under systemd)
  95. flag.Set("logtostderr", "true")
  96. // now parse command line args
  97. flag.Parse()
  98. if flag.NArg() > 0 || opts.help {
  99. fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]...\n", os.Args[0])
  100. flag.PrintDefaults()
  101. os.Exit(0)
  102. }
  103. if opts.version {
  104. fmt.Fprintln(os.Stderr, version.Version)
  105. os.Exit(0)
  106. }
  107. flagutil.SetFlagsFromEnv(flag.CommandLine, "FLANNELD")
  108. sm, err := newSubnetManager()
  109. if err != nil {
  110. log.Error("Failed to create SubnetManager: ", err)
  111. os.Exit(1)
  112. }
  113. // Register for SIGINT and SIGTERM
  114. log.Info("Installing signal handlers")
  115. sigs := make(chan os.Signal, 1)
  116. signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
  117. ctx, cancel := context.WithCancel(context.Background())
  118. var runFunc func(ctx context.Context)
  119. if opts.listen != "" {
  120. if opts.remote != "" {
  121. log.Error("--listen and --remote are mutually exclusive")
  122. os.Exit(1)
  123. }
  124. log.Info("running as server")
  125. runFunc = func(ctx context.Context) {
  126. remote.RunServer(ctx, sm, opts.listen, opts.remoteCAFile, opts.remoteCertfile, opts.remoteKeyfile)
  127. }
  128. } else {
  129. nm, err := network.NewNetworkManager(ctx, sm)
  130. if err != nil {
  131. log.Error("Failed to create NetworkManager: ", err)
  132. os.Exit(1)
  133. }
  134. runFunc = func(ctx context.Context) {
  135. nm.Run(ctx)
  136. }
  137. }
  138. wg := sync.WaitGroup{}
  139. wg.Add(1)
  140. go func() {
  141. runFunc(ctx)
  142. wg.Done()
  143. }()
  144. <-sigs
  145. // unregister to get default OS nuke behaviour in case we don't exit cleanly
  146. signal.Stop(sigs)
  147. log.Info("Exiting...")
  148. cancel()
  149. wg.Wait()
  150. }