main.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/subnet"
  28. "github.com/coreos/flannel/subnet/etcdv2"
  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/alivpc"
  33. _ "github.com/coreos/flannel/backend/alloc"
  34. _ "github.com/coreos/flannel/backend/awsvpc"
  35. _ "github.com/coreos/flannel/backend/gce"
  36. _ "github.com/coreos/flannel/backend/hostgw"
  37. _ "github.com/coreos/flannel/backend/udp"
  38. _ "github.com/coreos/flannel/backend/vxlan"
  39. )
  40. type CmdLineOpts struct {
  41. etcdEndpoints string
  42. etcdPrefix string
  43. etcdKeyfile string
  44. etcdCertfile string
  45. etcdCAFile string
  46. etcdUsername string
  47. etcdPassword string
  48. help bool
  49. version bool
  50. kubeSubnetMgr bool
  51. }
  52. var opts CmdLineOpts
  53. func init() {
  54. 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")
  55. flag.StringVar(&opts.etcdPrefix, "etcd-prefix", "/coreos.com/network", "etcd prefix")
  56. flag.StringVar(&opts.etcdKeyfile, "etcd-keyfile", "", "SSL key file used to secure etcd communication")
  57. flag.StringVar(&opts.etcdCertfile, "etcd-certfile", "", "SSL certification file used to secure etcd communication")
  58. flag.StringVar(&opts.etcdCAFile, "etcd-cafile", "", "SSL Certificate Authority file used to secure etcd communication")
  59. flag.StringVar(&opts.etcdUsername, "etcd-username", "", "Username for BasicAuth to etcd")
  60. flag.StringVar(&opts.etcdPassword, "etcd-password", "", "Password for BasicAuth to etcd")
  61. flag.BoolVar(&opts.kubeSubnetMgr, "kube-subnet-mgr", false, "Contact the Kubernetes API for subnet assignement instead of etcd or flannel-server.")
  62. flag.BoolVar(&opts.help, "help", false, "print this message")
  63. flag.BoolVar(&opts.version, "version", false, "print version and exit")
  64. }
  65. func newSubnetManager() (subnet.Manager, error) {
  66. if opts.kubeSubnetMgr {
  67. return kube.NewSubnetManager()
  68. }
  69. cfg := &etcdv2.EtcdConfig{
  70. Endpoints: strings.Split(opts.etcdEndpoints, ","),
  71. Keyfile: opts.etcdKeyfile,
  72. Certfile: opts.etcdCertfile,
  73. CAFile: opts.etcdCAFile,
  74. Prefix: opts.etcdPrefix,
  75. Username: opts.etcdUsername,
  76. Password: opts.etcdPassword,
  77. }
  78. return etcdv2.NewLocalManager(cfg)
  79. }
  80. func main() {
  81. // glog will log to tmp files by default. override so all entries
  82. // can flow into journald (if running under systemd)
  83. flag.Set("logtostderr", "true")
  84. // now parse command line args
  85. flag.Parse()
  86. if flag.NArg() > 0 || opts.help {
  87. fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]...\n", os.Args[0])
  88. flag.PrintDefaults()
  89. os.Exit(0)
  90. }
  91. if opts.version {
  92. fmt.Fprintln(os.Stderr, version.Version)
  93. os.Exit(0)
  94. }
  95. flagutil.SetFlagsFromEnv(flag.CommandLine, "FLANNELD")
  96. sm, err := newSubnetManager()
  97. if err != nil {
  98. log.Error("Failed to create SubnetManager: ", err)
  99. os.Exit(1)
  100. }
  101. // Register for SIGINT and SIGTERM
  102. log.Info("Installing signal handlers")
  103. sigs := make(chan os.Signal, 1)
  104. signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
  105. ctx, cancel := context.WithCancel(context.Background())
  106. var runFunc func(ctx context.Context)
  107. nm, err := network.NewNetworkManager(ctx, sm)
  108. if err != nil {
  109. log.Error("Failed to create NetworkManager: ", err)
  110. os.Exit(1)
  111. }
  112. runFunc = func(ctx context.Context) {
  113. nm.Run(ctx)
  114. }
  115. wg := sync.WaitGroup{}
  116. wg.Add(1)
  117. go func() {
  118. runFunc(ctx)
  119. wg.Done()
  120. }()
  121. <-sigs
  122. // unregister to get default OS nuke behaviour in case we don't exit cleanly
  123. signal.Stop(sigs)
  124. log.Info("Exiting...")
  125. cancel()
  126. wg.Wait()
  127. }