main.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. "errors"
  17. "flag"
  18. "fmt"
  19. "net"
  20. "net/http"
  21. "os"
  22. "os/signal"
  23. "path/filepath"
  24. "regexp"
  25. "strconv"
  26. "strings"
  27. "syscall"
  28. "github.com/coreos/pkg/flagutil"
  29. log "github.com/golang/glog"
  30. "golang.org/x/net/context"
  31. "github.com/coreos/flannel/network"
  32. "github.com/coreos/flannel/pkg/ip"
  33. "github.com/coreos/flannel/subnet"
  34. "github.com/coreos/flannel/subnet/etcdv2"
  35. "github.com/coreos/flannel/subnet/kube"
  36. "github.com/coreos/flannel/version"
  37. "time"
  38. "github.com/joho/godotenv"
  39. // Backends need to be imported for their init() to get executed and them to register
  40. "github.com/coreos/flannel/backend"
  41. _ "github.com/coreos/flannel/backend/alivpc"
  42. _ "github.com/coreos/flannel/backend/alloc"
  43. _ "github.com/coreos/flannel/backend/awsvpc"
  44. _ "github.com/coreos/flannel/backend/extension"
  45. _ "github.com/coreos/flannel/backend/gce"
  46. _ "github.com/coreos/flannel/backend/hostgw"
  47. _ "github.com/coreos/flannel/backend/udp"
  48. _ "github.com/coreos/flannel/backend/vxlan"
  49. "github.com/coreos/go-systemd/daemon"
  50. )
  51. type CmdLineOpts struct {
  52. etcdEndpoints string
  53. etcdPrefix string
  54. etcdKeyfile string
  55. etcdCertfile string
  56. etcdCAFile string
  57. etcdUsername string
  58. etcdPassword string
  59. help bool
  60. version bool
  61. kubeSubnetMgr bool
  62. iface string
  63. ifaceRegex string
  64. ipMasq bool
  65. subnetFile string
  66. subnetDir string
  67. publicIP string
  68. subnetLeaseRenewMargin int
  69. healthzIP string
  70. healthzPort int
  71. }
  72. var (
  73. opts CmdLineOpts
  74. errInterrupted = errors.New("interrupted")
  75. errCanceled = errors.New("canceled")
  76. )
  77. func init() {
  78. 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")
  79. flag.StringVar(&opts.etcdPrefix, "etcd-prefix", "/coreos.com/network", "etcd prefix")
  80. flag.StringVar(&opts.etcdKeyfile, "etcd-keyfile", "", "SSL key file used to secure etcd communication")
  81. flag.StringVar(&opts.etcdCertfile, "etcd-certfile", "", "SSL certification file used to secure etcd communication")
  82. flag.StringVar(&opts.etcdCAFile, "etcd-cafile", "", "SSL Certificate Authority file used to secure etcd communication")
  83. flag.StringVar(&opts.etcdUsername, "etcd-username", "", "Username for BasicAuth to etcd")
  84. flag.StringVar(&opts.etcdPassword, "etcd-password", "", "Password for BasicAuth to etcd")
  85. flag.StringVar(&opts.iface, "iface", "", "interface to use (IP or name) for inter-host communication")
  86. flag.StringVar(&opts.ifaceRegex, "iface-regex", "", "regex expression to match the first interface to use (IP or name) for inter-host communication. Skipped if the iface option is also specified")
  87. flag.StringVar(&opts.subnetFile, "subnet-file", "/run/flannel/subnet.env", "filename where env variables (subnet, MTU, ... ) will be written to")
  88. flag.StringVar(&opts.publicIP, "public-ip", "", "IP accessible by other nodes for inter-host communication")
  89. flag.IntVar(&opts.subnetLeaseRenewMargin, "subnet-lease-renew-margin", 60, "Subnet lease renewal margin, in minutes.")
  90. flag.BoolVar(&opts.ipMasq, "ip-masq", false, "setup IP masquerade rule for traffic destined outside of overlay network")
  91. flag.BoolVar(&opts.kubeSubnetMgr, "kube-subnet-mgr", false, "Contact the Kubernetes API for subnet assignment instead of etcd.")
  92. flag.BoolVar(&opts.help, "help", false, "print this message")
  93. flag.BoolVar(&opts.version, "version", false, "print version and exit")
  94. flag.StringVar(&opts.healthzIP, "healthz-ip", "0.0.0.0", "The IP address for healthz server to listen")
  95. flag.IntVar(&opts.healthzPort, "healthz-port", 0, "The port for healthz server to listen(0 to disable)")
  96. }
  97. func newSubnetManager() (subnet.Manager, error) {
  98. if opts.kubeSubnetMgr {
  99. return kube.NewSubnetManager()
  100. }
  101. cfg := &etcdv2.EtcdConfig{
  102. Endpoints: strings.Split(opts.etcdEndpoints, ","),
  103. Keyfile: opts.etcdKeyfile,
  104. Certfile: opts.etcdCertfile,
  105. CAFile: opts.etcdCAFile,
  106. Prefix: opts.etcdPrefix,
  107. Username: opts.etcdUsername,
  108. Password: opts.etcdPassword,
  109. }
  110. // Attempt to renew the lease for the subnet specified in the subnetFile
  111. prevSubnet := ReadSubnetFromSubnetFile(opts.subnetFile)
  112. return etcdv2.NewLocalManager(cfg, prevSubnet)
  113. }
  114. func main() {
  115. // glog will log to tmp files by default. override so all entries
  116. // can flow into journald (if running under systemd)
  117. flag.Set("logtostderr", "true")
  118. // now parse command line args
  119. flag.Parse()
  120. if flag.NArg() > 0 || opts.help {
  121. fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]...\n", os.Args[0])
  122. flag.PrintDefaults()
  123. os.Exit(0)
  124. }
  125. if opts.version {
  126. fmt.Fprintln(os.Stderr, version.Version)
  127. os.Exit(0)
  128. }
  129. flagutil.SetFlagsFromEnv(flag.CommandLine, "FLANNELD")
  130. // Work out which interface to use
  131. extIface, err := LookupExtIface(opts.iface, opts.ifaceRegex)
  132. if err != nil {
  133. log.Error("Failed to find interface to use: ", err)
  134. os.Exit(1)
  135. }
  136. sm, err := newSubnetManager()
  137. if err != nil {
  138. log.Error("Failed to create SubnetManager: ", err)
  139. os.Exit(1)
  140. }
  141. log.Infof("Created subnet manager: %+v", sm)
  142. // Register for SIGINT and SIGTERM
  143. log.Info("Installing signal handlers")
  144. sigs := make(chan os.Signal, 1)
  145. signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
  146. ctx, cancel := context.WithCancel(context.Background())
  147. go shutdown(sigs, cancel)
  148. if opts.healthzPort > 0 {
  149. go mustRunHealthz()
  150. }
  151. // Fetch the network config (i.e. what backend to use etc..).
  152. config, err := getConfig(ctx, sm)
  153. if err == errCanceled {
  154. exit()
  155. }
  156. // Create a backend manager then use it to create the backend and register the network with it.
  157. bm := backend.NewManager(ctx, sm, extIface)
  158. be, err := bm.GetBackend(config.BackendType)
  159. if err != nil {
  160. log.Errorf("Error fetching backend: %s", err)
  161. exit()
  162. }
  163. bn, err := be.RegisterNetwork(ctx, config)
  164. if err != nil {
  165. log.Errorf("Error registering network: %s", err)
  166. exit()
  167. }
  168. // Set up ipMasq if needed
  169. if opts.ipMasq {
  170. err = network.SetupIPMasq(config.Network, bn.Lease())
  171. if err != nil {
  172. // Continue, even though it failed.
  173. log.Errorf("Failed to set up IP Masquerade: %v", err)
  174. }
  175. defer func() {
  176. if err := network.TeardownIPMasq(config.Network, bn.Lease()); err != nil {
  177. log.Errorf("Failed to tear down IP Masquerade: %v", err)
  178. }
  179. }()
  180. }
  181. if err := WriteSubnetFile(opts.subnetFile, config.Network, opts.ipMasq, bn); err != nil {
  182. // Continue, even though it failed.
  183. log.Warningf("Failed to write subnet file: %s", err)
  184. } else {
  185. log.Infof("Wrote subnet file to %s", opts.subnetFile)
  186. }
  187. // Start "Running" the backend network. This will block until the context is done so run in another goroutine.
  188. go bn.Run(ctx)
  189. log.Infof("Finished starting backend.")
  190. daemon.SdNotify(false, "READY=1")
  191. // Kube subnet mgr doesn't lease the subnet for this node - it just uses the podCidr that's already assigned.
  192. if opts.kubeSubnetMgr {
  193. // Wait for the shutdown to be signalled
  194. <-ctx.Done()
  195. } else {
  196. // Block waiting to renew the lease
  197. _ = MonitorLease(ctx, sm, bn)
  198. }
  199. // To get to here, the Cancel signal must have been received or the lease has been revoked.
  200. exit()
  201. }
  202. func exit() {
  203. // Wait just a second for the cancel signal to propagate everywhere, then just exit cleanly.
  204. log.Info("Waiting for cancel to propagate...")
  205. time.Sleep(time.Second)
  206. log.Info("Exiting...")
  207. os.Exit(0)
  208. }
  209. func shutdown(sigs chan os.Signal, cancel context.CancelFunc) {
  210. // Wait for the shutdown signal.
  211. <-sigs
  212. // Unregister to get default OS nuke behaviour in case we don't exit cleanly
  213. signal.Stop(sigs)
  214. log.Info("Starting shutdown...")
  215. // Call cancel on the context to close everything down.
  216. cancel()
  217. log.Info("Sent cancel signal...")
  218. }
  219. func getConfig(ctx context.Context, sm subnet.Manager) (*subnet.Config, error) {
  220. // Retry every second until it succeeds
  221. for {
  222. config, err := sm.GetNetworkConfig(ctx)
  223. if err != nil {
  224. log.Errorf("Couldn't fetch network config: %s", err)
  225. } else if config == nil {
  226. log.Warningf("Couldn't find network config: %s", err)
  227. } else {
  228. log.Infof("Found network config - Backend type: %s", config.BackendType)
  229. return config, nil
  230. }
  231. select {
  232. case <-ctx.Done():
  233. return nil, errCanceled
  234. case <-time.After(1 * time.Second):
  235. fmt.Println("timed out")
  236. }
  237. }
  238. }
  239. func MonitorLease(ctx context.Context, sm subnet.Manager, bn backend.Network) error {
  240. // Use the subnet manager to start watching leases.
  241. evts := make(chan subnet.Event)
  242. go subnet.WatchLease(ctx, sm, bn.Lease().Subnet, evts)
  243. renewMargin := time.Duration(opts.subnetLeaseRenewMargin) * time.Minute
  244. dur := bn.Lease().Expiration.Sub(time.Now()) - renewMargin
  245. for {
  246. select {
  247. case <-time.After(dur):
  248. err := sm.RenewLease(ctx, bn.Lease())
  249. if err != nil {
  250. log.Error("Error renewing lease (trying again in 1 min): ", err)
  251. dur = time.Minute
  252. continue
  253. }
  254. log.Info("Lease renewed, new expiration: ", bn.Lease().Expiration)
  255. dur = bn.Lease().Expiration.Sub(time.Now()) - renewMargin
  256. case e := <-evts:
  257. switch e.Type {
  258. case subnet.EventAdded:
  259. bn.Lease().Expiration = e.Lease.Expiration
  260. dur = bn.Lease().Expiration.Sub(time.Now()) - renewMargin
  261. log.Infof("Waiting for %s to renew lease", dur)
  262. case subnet.EventRemoved:
  263. log.Error("Lease has been revoked. Shutting down daemon.")
  264. return errInterrupted
  265. }
  266. case <-ctx.Done():
  267. log.Infof("Stopped monitoring lease")
  268. return errCanceled
  269. }
  270. }
  271. }
  272. func LookupExtIface(ifname string, ifregex string) (*backend.ExternalInterface, error) {
  273. var iface *net.Interface
  274. var ifaceAddr net.IP
  275. var err error
  276. if len(ifname) > 0 {
  277. if ifaceAddr = net.ParseIP(ifname); ifaceAddr != nil {
  278. log.Infof("Searching for interface using %s", ifaceAddr)
  279. iface, err = ip.GetInterfaceByIP(ifaceAddr)
  280. if err != nil {
  281. return nil, fmt.Errorf("error looking up interface %s: %s", ifname, err)
  282. }
  283. } else {
  284. iface, err = net.InterfaceByName(ifname)
  285. if err != nil {
  286. return nil, fmt.Errorf("error looking up interface %s: %s", ifname, err)
  287. }
  288. }
  289. } else if len(ifregex) > 0 {
  290. // Use the regex if specified and the iface option for matching a specific ip or name is not used
  291. ifaces, err := net.Interfaces()
  292. if err != nil {
  293. return nil, fmt.Errorf("error listing all interfaces: %s", err)
  294. }
  295. // Check IP
  296. for _, ifaceToMatch := range ifaces {
  297. ifaceIP, err := ip.GetIfaceIP4Addr(&ifaceToMatch)
  298. if err != nil {
  299. // Skip if there is no IPv4 address
  300. continue
  301. }
  302. matched, err := regexp.MatchString(ifregex, ifaceIP.String())
  303. if err != nil {
  304. return nil, fmt.Errorf("regex error matching pattern %s to %s", ifregex, ifaceIP.String())
  305. }
  306. if matched {
  307. ifaceAddr = ifaceIP
  308. iface = &ifaceToMatch
  309. break
  310. }
  311. }
  312. // Check Name
  313. if iface == nil && ifaceAddr == nil {
  314. for _, ifaceToMatch := range ifaces {
  315. matched, err := regexp.MatchString(ifregex, ifaceToMatch.Name)
  316. if err != nil {
  317. return nil, fmt.Errorf("regex error matching pattern %s to %s", ifregex, ifaceToMatch.Name)
  318. }
  319. if matched {
  320. iface = &ifaceToMatch
  321. break
  322. }
  323. }
  324. }
  325. return nil, fmt.Errorf("Could not match pattern %s to any of the available network interfaces", ifregex)
  326. } else {
  327. log.Info("Determining IP address of default interface")
  328. if iface, err = ip.GetDefaultGatewayIface(); err != nil {
  329. return nil, fmt.Errorf("failed to get default interface: %s", err)
  330. }
  331. }
  332. if ifaceAddr == nil {
  333. ifaceAddr, err = ip.GetIfaceIP4Addr(iface)
  334. if err != nil {
  335. return nil, fmt.Errorf("failed to find IPv4 address for interface %s", iface.Name)
  336. }
  337. }
  338. log.Infof("Using interface with name %s and address %s", iface.Name, ifaceAddr)
  339. if iface.MTU == 0 {
  340. return nil, fmt.Errorf("failed to determine MTU for %s interface", ifaceAddr)
  341. }
  342. var extAddr net.IP
  343. if len(opts.publicIP) > 0 {
  344. extAddr = net.ParseIP(opts.publicIP)
  345. if extAddr == nil {
  346. return nil, fmt.Errorf("invalid public IP address: %s", opts.publicIP)
  347. }
  348. log.Infof("Using %s as external address", extAddr)
  349. }
  350. if extAddr == nil {
  351. log.Infof("Defaulting external address to interface address (%s)", ifaceAddr)
  352. extAddr = ifaceAddr
  353. }
  354. return &backend.ExternalInterface{
  355. Iface: iface,
  356. IfaceAddr: ifaceAddr,
  357. ExtAddr: extAddr,
  358. }, nil
  359. }
  360. func WriteSubnetFile(path string, nw ip.IP4Net, ipMasq bool, bn backend.Network) error {
  361. dir, name := filepath.Split(path)
  362. os.MkdirAll(dir, 0755)
  363. tempFile := filepath.Join(dir, "."+name)
  364. f, err := os.Create(tempFile)
  365. if err != nil {
  366. return err
  367. }
  368. // Write out the first usable IP by incrementing
  369. // sn.IP by one
  370. sn := bn.Lease().Subnet
  371. sn.IP += 1
  372. fmt.Fprintf(f, "FLANNEL_NETWORK=%s\n", nw)
  373. fmt.Fprintf(f, "FLANNEL_SUBNET=%s\n", sn)
  374. fmt.Fprintf(f, "FLANNEL_MTU=%d\n", bn.MTU())
  375. _, err = fmt.Fprintf(f, "FLANNEL_IPMASQ=%v\n", ipMasq)
  376. f.Close()
  377. if err != nil {
  378. return err
  379. }
  380. // rename(2) the temporary file to the desired location so that it becomes
  381. // atomically visible with the contents
  382. return os.Rename(tempFile, path)
  383. //TODO - is this safe? What if it's not on the same FS?
  384. }
  385. func mustRunHealthz() {
  386. address := net.JoinHostPort(opts.healthzIP, strconv.Itoa(opts.healthzPort))
  387. log.Infof("Start healthz server on %s", address)
  388. http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
  389. w.WriteHeader(http.StatusOK)
  390. w.Write([]byte("flanneld is running"))
  391. })
  392. if err := http.ListenAndServe(address, nil); err != nil {
  393. log.Errorf("Start healthz server error. %v", err)
  394. panic(err)
  395. }
  396. }
  397. func ReadSubnetFromSubnetFile(path string) ip.IP4Net {
  398. var prevSubnet ip.IP4Net
  399. if _, err := os.Stat(path); !os.IsNotExist(err) {
  400. prevSubnetVals, err := godotenv.Read(path)
  401. if err != nil {
  402. log.Errorf("Couldn't fetch previous subnet from subnet file at %s: %s", path, err)
  403. } else if prevSubnetString, ok := prevSubnetVals["FLANNEL_SUBNET"]; ok {
  404. err = prevSubnet.UnmarshalJSON([]byte(prevSubnetString))
  405. if err != nil {
  406. log.Errorf("Couldn't parse previous subnet from subnet file at %s: %s", path, err)
  407. }
  408. }
  409. }
  410. return prevSubnet
  411. }