main.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/flannel/Godeps/_workspace/src/github.com/coreos/pkg/flagutil"
  24. log "github.com/coreos/flannel/Godeps/_workspace/src/github.com/golang/glog"
  25. "github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
  26. "github.com/coreos/flannel/network"
  27. "github.com/coreos/flannel/remote"
  28. "github.com/coreos/flannel/subnet"
  29. )
  30. type CmdLineOpts struct {
  31. etcdEndpoints string
  32. etcdPrefix string
  33. etcdKeyfile string
  34. etcdCertfile string
  35. etcdCAFile string
  36. help bool
  37. version bool
  38. listen string
  39. remote string
  40. remoteKeyfile string
  41. remoteCertfile string
  42. remoteCAFile string
  43. }
  44. var opts CmdLineOpts
  45. func init() {
  46. 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")
  47. flag.StringVar(&opts.etcdPrefix, "etcd-prefix", "/coreos.com/network", "etcd prefix")
  48. flag.StringVar(&opts.etcdKeyfile, "etcd-keyfile", "", "SSL key file used to secure etcd communication")
  49. flag.StringVar(&opts.etcdCertfile, "etcd-certfile", "", "SSL certification file used to secure etcd communication")
  50. flag.StringVar(&opts.etcdCAFile, "etcd-cafile", "", "SSL Certificate Authority file used to secure etcd communication")
  51. flag.StringVar(&opts.listen, "listen", "", "run as server and listen on specified address (e.g. ':8080')")
  52. flag.StringVar(&opts.remote, "remote", "", "run as client and connect to server on specified address (e.g. '10.1.2.3:8080')")
  53. flag.StringVar(&opts.remoteKeyfile, "remote-keyfile", "", "SSL key file used to secure client/server communication")
  54. flag.StringVar(&opts.remoteCertfile, "remote-certfile", "", "SSL certification file used to secure client/server communication")
  55. flag.StringVar(&opts.remoteCAFile, "remote-cafile", "", "SSL Certificate Authority file used to secure client/server communication")
  56. flag.BoolVar(&opts.help, "help", false, "print this message")
  57. flag.BoolVar(&opts.version, "version", false, "print version and exit")
  58. }
  59. func newSubnetManager() (subnet.Manager, error) {
  60. if opts.remote != "" {
  61. return remote.NewRemoteManager(opts.remote, opts.remoteCAFile, opts.remoteCertfile, opts.remoteKeyfile)
  62. }
  63. cfg := &subnet.EtcdConfig{
  64. Endpoints: strings.Split(opts.etcdEndpoints, ","),
  65. Keyfile: opts.etcdKeyfile,
  66. Certfile: opts.etcdCertfile,
  67. CAFile: opts.etcdCAFile,
  68. Prefix: opts.etcdPrefix,
  69. }
  70. return subnet.NewEtcdManager(cfg)
  71. }
  72. func main() {
  73. // glog will log to tmp files by default. override so all entries
  74. // can flow into journald (if running under systemd)
  75. flag.Set("logtostderr", "true")
  76. // now parse command line args
  77. flag.Parse()
  78. if flag.NArg() > 0 || opts.help {
  79. fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]...\n", os.Args[0])
  80. flag.PrintDefaults()
  81. os.Exit(0)
  82. }
  83. if opts.version {
  84. fmt.Fprintln(os.Stderr, Version)
  85. os.Exit(0)
  86. }
  87. flagutil.SetFlagsFromEnv(flag.CommandLine, "FLANNELD")
  88. sm, err := newSubnetManager()
  89. if err != nil {
  90. log.Error("Failed to create SubnetManager: ", err)
  91. os.Exit(1)
  92. }
  93. // Register for SIGINT and SIGTERM
  94. log.Info("Installing signal handlers")
  95. sigs := make(chan os.Signal, 1)
  96. signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
  97. ctx, cancel := context.WithCancel(context.Background())
  98. var runFunc func(ctx context.Context)
  99. if opts.listen != "" {
  100. if opts.remote != "" {
  101. log.Error("--listen and --remote are mutually exclusive")
  102. os.Exit(1)
  103. }
  104. log.Info("running as server")
  105. runFunc = func(ctx context.Context) {
  106. remote.RunServer(ctx, sm, opts.listen, opts.remoteCAFile, opts.remoteCertfile, opts.remoteKeyfile)
  107. }
  108. } else {
  109. nm, err := network.NewNetworkManager(ctx, sm)
  110. if err != nil {
  111. log.Error("Failed to create NetworkManager: ", err)
  112. os.Exit(1)
  113. }
  114. runFunc = func(ctx context.Context) {
  115. nm.Run()
  116. }
  117. }
  118. wg := sync.WaitGroup{}
  119. wg.Add(1)
  120. go func() {
  121. runFunc(ctx)
  122. wg.Done()
  123. }()
  124. <-sigs
  125. // unregister to get default OS nuke behaviour in case we don't exit cleanly
  126. signal.Stop(sigs)
  127. log.Info("Exiting...")
  128. cancel()
  129. wg.Wait()
  130. }