main.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package main
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "fmt"
  6. "net"
  7. "os"
  8. "os/signal"
  9. "path/filepath"
  10. "strings"
  11. "syscall"
  12. "time"
  13. "github.com/coreos/flannel/Godeps/_workspace/src/github.com/coreos/go-systemd/daemon"
  14. log "github.com/coreos/flannel/Godeps/_workspace/src/github.com/golang/glog"
  15. "github.com/coreos/flannel/backend"
  16. "github.com/coreos/flannel/backend/alloc"
  17. "github.com/coreos/flannel/backend/hostgw"
  18. "github.com/coreos/flannel/backend/udp"
  19. "github.com/coreos/flannel/backend/vxlan"
  20. "github.com/coreos/flannel/pkg/ip"
  21. "github.com/coreos/flannel/pkg/task"
  22. "github.com/coreos/flannel/subnet"
  23. )
  24. type CmdLineOpts struct {
  25. etcdEndpoints string
  26. etcdPrefix string
  27. etcdKeyfile string
  28. etcdCertfile string
  29. etcdCAFile string
  30. help bool
  31. version bool
  32. ipMasq bool
  33. subnetFile string
  34. iface string
  35. }
  36. var opts CmdLineOpts
  37. func init() {
  38. flag.StringVar(&opts.etcdEndpoints, "etcd-endpoints", "http://127.0.0.1:4001", "a comma-delimited list of etcd endpoints")
  39. flag.StringVar(&opts.etcdPrefix, "etcd-prefix", "/coreos.com/network", "etcd prefix")
  40. flag.StringVar(&opts.etcdKeyfile, "etcd-keyfile", "", "SSL key file used to secure etcd communication")
  41. flag.StringVar(&opts.etcdCertfile, "etcd-certfile", "", "SSL certification file used to secure etcd communication")
  42. flag.StringVar(&opts.etcdCAFile, "etcd-cafile", "", "SSL Certificate Authority file used to secure etcd communication")
  43. flag.StringVar(&opts.subnetFile, "subnet-file", "/run/flannel/subnet.env", "filename where env variables (subnet and MTU values) will be written to")
  44. flag.StringVar(&opts.iface, "iface", "", "interface to use (IP or name) for inter-host communication")
  45. flag.BoolVar(&opts.ipMasq, "ip-masq", false, "setup IP masquerade rule for traffic destined outside of overlay network")
  46. flag.BoolVar(&opts.help, "help", false, "print this message")
  47. flag.BoolVar(&opts.version, "version", false, "print version and exit")
  48. }
  49. // TODO: This is yet another copy (others found in etcd, fleet) -- Pull it out!
  50. // flagsFromEnv parses all registered flags in the given flagset,
  51. // and if they are not already set it attempts to set their values from
  52. // environment variables. Environment variables take the name of the flag but
  53. // are UPPERCASE, have the given prefix, and any dashes are replaced by
  54. // underscores - for example: some-flag => PREFIX_SOME_FLAG
  55. func flagsFromEnv(prefix string, fs *flag.FlagSet) {
  56. alreadySet := make(map[string]bool)
  57. fs.Visit(func(f *flag.Flag) {
  58. alreadySet[f.Name] = true
  59. })
  60. fs.VisitAll(func(f *flag.Flag) {
  61. if !alreadySet[f.Name] {
  62. key := strings.ToUpper(prefix + "_" + strings.Replace(f.Name, "-", "_", -1))
  63. val := os.Getenv(key)
  64. if val != "" {
  65. fs.Set(f.Name, val)
  66. }
  67. }
  68. })
  69. }
  70. func writeSubnetFile(sn *backend.SubnetDef) error {
  71. // Write out the first usable IP by incrementing
  72. // sn.IP by one
  73. sn.Net.IP += 1
  74. dir, name := filepath.Split(opts.subnetFile)
  75. os.MkdirAll(dir, 0755)
  76. tempFile := filepath.Join(dir, "."+name)
  77. f, err := os.Create(tempFile)
  78. if err != nil {
  79. return err
  80. }
  81. fmt.Fprintf(f, "FLANNEL_SUBNET=%s\n", sn.Net)
  82. fmt.Fprintf(f, "FLANNEL_MTU=%d\n", sn.MTU)
  83. _, err = fmt.Fprintf(f, "FLANNEL_IPMASQ=%v\n", opts.ipMasq)
  84. f.Close()
  85. if err != nil {
  86. return err
  87. }
  88. // rename(2) the temporary file to the desired location so that it becomes
  89. // atomically visible with the contents
  90. return os.Rename(tempFile, opts.subnetFile)
  91. }
  92. func lookupIface() (*net.Interface, net.IP, error) {
  93. var iface *net.Interface
  94. var ipaddr net.IP
  95. var err error
  96. if len(opts.iface) > 0 {
  97. if ipaddr = net.ParseIP(opts.iface); ipaddr != nil {
  98. iface, err = ip.GetInterfaceByIP(ipaddr)
  99. if err != nil {
  100. return nil, nil, fmt.Errorf("Error looking up interface %s: %s", opts.iface, err)
  101. }
  102. } else {
  103. iface, err = net.InterfaceByName(opts.iface)
  104. if err != nil {
  105. return nil, nil, fmt.Errorf("Error looking up interface %s: %s", opts.iface, err)
  106. }
  107. }
  108. } else {
  109. log.Info("Determining IP address of default interface")
  110. if iface, err = ip.GetDefaultGatewayIface(); err != nil {
  111. return nil, nil, fmt.Errorf("Failed to get default interface: %s", err)
  112. }
  113. }
  114. if ipaddr == nil {
  115. ipaddr, err = ip.GetIfaceIP4Addr(iface)
  116. if err != nil {
  117. return nil, nil, fmt.Errorf("Failed to find IPv4 address for interface %s", iface.Name)
  118. }
  119. }
  120. return iface, ipaddr, nil
  121. }
  122. func newSubnetManager() *subnet.SubnetManager {
  123. peers := strings.Split(opts.etcdEndpoints, ",")
  124. cfg := &subnet.EtcdConfig{
  125. Endpoints: peers,
  126. Keyfile: opts.etcdKeyfile,
  127. Certfile: opts.etcdCertfile,
  128. CAFile: opts.etcdCAFile,
  129. Prefix: opts.etcdPrefix,
  130. }
  131. for {
  132. sm, err := subnet.NewSubnetManager(cfg)
  133. if err == nil {
  134. return sm
  135. }
  136. log.Error("Failed to create SubnetManager: ", err)
  137. time.Sleep(time.Second)
  138. }
  139. }
  140. func newBackend(sm *subnet.SubnetManager) (backend.Backend, error) {
  141. config := sm.GetConfig()
  142. var bt struct {
  143. Type string
  144. }
  145. if len(config.Backend) == 0 {
  146. bt.Type = "udp"
  147. } else {
  148. if err := json.Unmarshal(config.Backend, &bt); err != nil {
  149. return nil, fmt.Errorf("Error decoding Backend property of config: %v", err)
  150. }
  151. }
  152. switch strings.ToLower(bt.Type) {
  153. case "udp":
  154. return udp.New(sm, config.Backend), nil
  155. case "alloc":
  156. return alloc.New(sm), nil
  157. case "host-gw":
  158. return hostgw.New(sm), nil
  159. case "vxlan":
  160. return vxlan.New(sm, config.Backend), nil
  161. default:
  162. return nil, fmt.Errorf("'%v': unknown backend type", bt.Type)
  163. }
  164. }
  165. func run(sm *subnet.SubnetManager, be backend.Backend, exit chan int) {
  166. var err error
  167. defer func() {
  168. if err == nil || err == task.ErrCanceled {
  169. exit <- 0
  170. } else {
  171. log.Error(err)
  172. exit <- 1
  173. }
  174. }()
  175. iface, ipaddr, err := lookupIface()
  176. if err != nil {
  177. return
  178. }
  179. if iface.MTU == 0 {
  180. err = fmt.Errorf("Failed to determine MTU for %s interface", ipaddr)
  181. return
  182. }
  183. log.Infof("Using %s as external interface", ipaddr)
  184. sn, err := be.Init(iface, ipaddr)
  185. if err != nil {
  186. return
  187. }
  188. if opts.ipMasq {
  189. flannelNet := sm.GetConfig().Network
  190. if err = setupIPMasq(flannelNet); err != nil {
  191. return
  192. }
  193. }
  194. writeSubnetFile(sn)
  195. daemon.SdNotify("READY=1")
  196. log.Infof("%s mode initialized", be.Name())
  197. be.Run()
  198. }
  199. func main() {
  200. // glog will log to tmp files by default. override so all entries
  201. // can flow into journald (if running under systemd)
  202. flag.Set("logtostderr", "true")
  203. // now parse command line args
  204. flag.Parse()
  205. if opts.help {
  206. fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]...\n", os.Args[0])
  207. flag.PrintDefaults()
  208. os.Exit(0)
  209. }
  210. if opts.version {
  211. fmt.Fprintln(os.Stderr, Version)
  212. os.Exit(0)
  213. }
  214. flagsFromEnv("FLANNELD", flag.CommandLine)
  215. sm := newSubnetManager()
  216. be, err := newBackend(sm)
  217. if err != nil {
  218. log.Info(err)
  219. os.Exit(1)
  220. }
  221. // Register for SIGINT and SIGTERM and wait for one of them to arrive
  222. log.Info("Installing signal handlers")
  223. sigs := make(chan os.Signal, 1)
  224. signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
  225. exit := make(chan int)
  226. go run(sm, be, exit)
  227. for {
  228. select {
  229. case <-sigs:
  230. // unregister to get default OS nuke behaviour in case we don't exit cleanly
  231. signal.Stop(sigs)
  232. log.Info("Exiting...")
  233. be.Stop()
  234. case code := <-exit:
  235. log.Infof("%s mode exited", be.Name())
  236. os.Exit(code)
  237. }
  238. }
  239. }