main.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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/go-iptables/iptables"
  29. "github.com/coreos/pkg/flagutil"
  30. log "github.com/golang/glog"
  31. "golang.org/x/net/context"
  32. "github.com/coreos/flannel/network"
  33. "github.com/coreos/flannel/pkg/ip"
  34. "github.com/coreos/flannel/subnet"
  35. "github.com/coreos/flannel/subnet/etcdv2"
  36. "github.com/coreos/flannel/subnet/kube"
  37. "github.com/coreos/flannel/version"
  38. "time"
  39. "github.com/joho/godotenv"
  40. "sync"
  41. // Backends need to be imported for their init() to get executed and them to register
  42. "github.com/coreos/flannel/backend"
  43. _ "github.com/coreos/flannel/backend/alivpc"
  44. _ "github.com/coreos/flannel/backend/alloc"
  45. _ "github.com/coreos/flannel/backend/awsvpc"
  46. _ "github.com/coreos/flannel/backend/extension"
  47. _ "github.com/coreos/flannel/backend/gce"
  48. _ "github.com/coreos/flannel/backend/hostgw"
  49. _ "github.com/coreos/flannel/backend/udp"
  50. _ "github.com/coreos/flannel/backend/vxlan"
  51. "github.com/coreos/go-systemd/daemon"
  52. )
  53. type flagSlice []string
  54. func (t *flagSlice) String() string {
  55. return fmt.Sprintf("%v", *t)
  56. }
  57. func (t *flagSlice) Set(val string) error {
  58. *t = append(*t, val)
  59. return nil
  60. }
  61. type CmdLineOpts struct {
  62. etcdEndpoints string
  63. etcdPrefix string
  64. etcdKeyfile string
  65. etcdCertfile string
  66. etcdCAFile string
  67. etcdUsername string
  68. etcdPassword string
  69. help bool
  70. version bool
  71. kubeSubnetMgr bool
  72. kubeApiUrl string
  73. kubeConfigFile string
  74. iface flagSlice
  75. ifaceRegex flagSlice
  76. ipMasq bool
  77. subnetFile string
  78. subnetDir string
  79. publicIP string
  80. subnetLeaseRenewMargin int
  81. healthzIP string
  82. healthzPort int
  83. }
  84. var (
  85. opts CmdLineOpts
  86. errInterrupted = errors.New("interrupted")
  87. errCanceled = errors.New("canceled")
  88. flannelFlags = flag.NewFlagSet("flannel", flag.ExitOnError)
  89. )
  90. func init() {
  91. flannelFlags.StringVar(&opts.etcdEndpoints, "etcd-endpoints", "http://127.0.0.1:4001,http://127.0.0.1:2379", "a comma-delimited list of etcd endpoints")
  92. flannelFlags.StringVar(&opts.etcdPrefix, "etcd-prefix", "/coreos.com/network", "etcd prefix")
  93. flannelFlags.StringVar(&opts.etcdKeyfile, "etcd-keyfile", "", "SSL key file used to secure etcd communication")
  94. flannelFlags.StringVar(&opts.etcdCertfile, "etcd-certfile", "", "SSL certification file used to secure etcd communication")
  95. flannelFlags.StringVar(&opts.etcdCAFile, "etcd-cafile", "", "SSL Certificate Authority file used to secure etcd communication")
  96. flannelFlags.StringVar(&opts.etcdUsername, "etcd-username", "", "username for BasicAuth to etcd")
  97. flannelFlags.StringVar(&opts.etcdPassword, "etcd-password", "", "password for BasicAuth to etcd")
  98. flannelFlags.Var(&opts.iface, "iface", "interface to use (IP or name) for inter-host communication. Can be specified multiple times to check each option in order. Returns the first match found.")
  99. flannelFlags.Var(&opts.ifaceRegex, "iface-regex", "regex expression to match the first interface to use (IP or name) for inter-host communication. Can be specified multiple times to check each regex in order. Returns the first match found. Regexes are checked after specific interfaces specified by the iface option have already been checked.")
  100. flannelFlags.StringVar(&opts.subnetFile, "subnet-file", "/run/flannel/subnet.env", "filename where env variables (subnet, MTU, ... ) will be written to")
  101. flannelFlags.StringVar(&opts.publicIP, "public-ip", "", "IP accessible by other nodes for inter-host communication")
  102. flannelFlags.IntVar(&opts.subnetLeaseRenewMargin, "subnet-lease-renew-margin", 60, "subnet lease renewal margin, in minutes, ranging from 1 to 1439")
  103. flannelFlags.BoolVar(&opts.ipMasq, "ip-masq", false, "setup IP masquerade rule for traffic destined outside of overlay network")
  104. flannelFlags.BoolVar(&opts.kubeSubnetMgr, "kube-subnet-mgr", false, "contact the Kubernetes API for subnet assignment instead of etcd.")
  105. flannelFlags.StringVar(&opts.kubeApiUrl, "kube-api-url", "", "Kubernetes API server URL. Does not need to be specified if flannel is running in a pod.")
  106. flannelFlags.StringVar(&opts.kubeConfigFile, "kubeconfig-file", "", "kubeconfig file location. Does not need to be specified if flannel is running in a pod.")
  107. flannelFlags.BoolVar(&opts.version, "version", false, "print version and exit")
  108. flannelFlags.StringVar(&opts.healthzIP, "healthz-ip", "0.0.0.0", "the IP address for healthz server to listen")
  109. flannelFlags.IntVar(&opts.healthzPort, "healthz-port", 0, "the port for healthz server to listen(0 to disable)")
  110. // glog will log to tmp files by default. override so all entries
  111. // can flow into journald (if running under systemd)
  112. flag.Set("logtostderr", "true")
  113. // Only copy the non file logging options from glog
  114. copyFlag("v")
  115. copyFlag("vmodule")
  116. copyFlag("log_backtrace_at")
  117. // Define the usage function
  118. flannelFlags.Usage = usage
  119. // now parse command line args
  120. flannelFlags.Parse(os.Args[1:])
  121. }
  122. func copyFlag(name string) {
  123. flannelFlags.Var(flag.Lookup(name).Value, flag.Lookup(name).Name, flag.Lookup(name).Usage)
  124. }
  125. func usage() {
  126. fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]...\n", os.Args[0])
  127. flannelFlags.PrintDefaults()
  128. os.Exit(0)
  129. }
  130. func newSubnetManager() (subnet.Manager, error) {
  131. if opts.kubeSubnetMgr {
  132. return kube.NewSubnetManager(opts.kubeApiUrl, opts.kubeConfigFile)
  133. }
  134. cfg := &etcdv2.EtcdConfig{
  135. Endpoints: strings.Split(opts.etcdEndpoints, ","),
  136. Keyfile: opts.etcdKeyfile,
  137. Certfile: opts.etcdCertfile,
  138. CAFile: opts.etcdCAFile,
  139. Prefix: opts.etcdPrefix,
  140. Username: opts.etcdUsername,
  141. Password: opts.etcdPassword,
  142. }
  143. // Attempt to renew the lease for the subnet specified in the subnetFile
  144. prevSubnet := ReadSubnetFromSubnetFile(opts.subnetFile)
  145. return etcdv2.NewLocalManager(cfg, prevSubnet)
  146. }
  147. func main() {
  148. if opts.version {
  149. fmt.Fprintln(os.Stderr, version.Version)
  150. os.Exit(0)
  151. }
  152. flagutil.SetFlagsFromEnv(flannelFlags, "FLANNELD")
  153. // Validate flags
  154. if opts.subnetLeaseRenewMargin >= 24*60 || opts.subnetLeaseRenewMargin <= 0 {
  155. log.Error("Invalid subnet-lease-renew-margin option, out of acceptable range")
  156. os.Exit(1)
  157. }
  158. // Work out which interface to use
  159. var extIface *backend.ExternalInterface
  160. var err error
  161. // Check the default interface only if no interfaces are specified
  162. if len(opts.iface) == 0 && len(opts.ifaceRegex) == 0 {
  163. extIface, err = LookupExtIface("", "")
  164. if err != nil {
  165. log.Error("Failed to find any valid interface to use: ", err)
  166. os.Exit(1)
  167. }
  168. } else {
  169. // Check explicitly specified interfaces
  170. for _, iface := range opts.iface {
  171. extIface, err = LookupExtIface(iface, "")
  172. if err != nil {
  173. log.Infof("Could not find valid interface matching %s: %s", iface, err)
  174. }
  175. if extIface != nil {
  176. break
  177. }
  178. }
  179. // Check interfaces that match any specified regexes
  180. if extIface == nil {
  181. for _, ifaceRegex := range opts.ifaceRegex {
  182. extIface, err = LookupExtIface("", ifaceRegex)
  183. if err != nil {
  184. log.Infof("Could not find valid interface matching %s: %s", ifaceRegex, err)
  185. }
  186. if extIface != nil {
  187. break
  188. }
  189. }
  190. }
  191. if extIface == nil {
  192. // Exit if any of the specified interfaces do not match
  193. log.Error("Failed to find interface to use that matches the interfaces and/or regexes provided")
  194. os.Exit(1)
  195. }
  196. }
  197. sm, err := newSubnetManager()
  198. if err != nil {
  199. log.Error("Failed to create SubnetManager: ", err)
  200. os.Exit(1)
  201. }
  202. log.Infof("Created subnet manager: %s", sm.Name())
  203. // Register for SIGINT and SIGTERM
  204. log.Info("Installing signal handlers")
  205. sigs := make(chan os.Signal, 1)
  206. signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
  207. // This is the main context that everything should run in.
  208. // All spawned goroutines should exit when cancel is called on this context.
  209. // Go routines spawned from main.go coordinate using a WaitGroup. This provides a mechanism to allow the shutdownHandler goroutine
  210. // to block until all the goroutines return . If those goroutines spawn other goroutines then they are responsible for
  211. // blocking and returning only when cancel() is called.
  212. ctx, cancel := context.WithCancel(context.Background())
  213. wg := sync.WaitGroup{}
  214. wg.Add(1)
  215. go func() {
  216. shutdownHandler(ctx, sigs, cancel)
  217. wg.Done()
  218. }()
  219. if opts.healthzPort > 0 {
  220. // It's not super easy to shutdown the HTTP server so don't attempt to stop it cleanly
  221. go mustRunHealthz()
  222. }
  223. // Fetch the network config (i.e. what backend to use etc..).
  224. config, err := getConfig(ctx, sm)
  225. if err == errCanceled {
  226. wg.Wait()
  227. os.Exit(0)
  228. }
  229. // Create a backend manager then use it to create the backend and register the network with it.
  230. bm := backend.NewManager(ctx, sm, extIface)
  231. be, err := bm.GetBackend(config.BackendType)
  232. if err != nil {
  233. log.Errorf("Error fetching backend: %s", err)
  234. cancel()
  235. wg.Wait()
  236. os.Exit(1)
  237. }
  238. bn, err := be.RegisterNetwork(ctx, config)
  239. if err != nil {
  240. log.Errorf("Error registering network: %s", err)
  241. cancel()
  242. wg.Wait()
  243. os.Exit(1)
  244. }
  245. // Set up ipMasq if needed
  246. if opts.ipMasq {
  247. go setupIPMasq(config, bn)
  248. }
  249. if err := WriteSubnetFile(opts.subnetFile, config.Network, opts.ipMasq, bn); err != nil {
  250. // Continue, even though it failed.
  251. log.Warningf("Failed to write subnet file: %s", err)
  252. } else {
  253. log.Infof("Wrote subnet file to %s", opts.subnetFile)
  254. }
  255. // Start "Running" the backend network. This will block until the context is done so run in another goroutine.
  256. log.Info("Running backend.")
  257. wg.Add(1)
  258. go func() {
  259. bn.Run(ctx)
  260. wg.Done()
  261. }()
  262. daemon.SdNotify(false, "READY=1")
  263. // Kube subnet mgr doesn't lease the subnet for this node - it just uses the podCidr that's already assigned.
  264. if !opts.kubeSubnetMgr {
  265. err = MonitorLease(ctx, sm, bn, &wg)
  266. if err == errInterrupted {
  267. // The lease was "revoked" - shut everything down
  268. cancel()
  269. }
  270. }
  271. log.Info("Waiting for all goroutines to exit")
  272. // Block waiting for all the goroutines to finish.
  273. wg.Wait()
  274. log.Info("Exiting cleanly...")
  275. os.Exit(0)
  276. }
  277. func shutdownHandler(ctx context.Context, sigs chan os.Signal, cancel context.CancelFunc) {
  278. // Wait for the context do be Done or for the signal to come in to shutdown.
  279. select {
  280. case <-ctx.Done():
  281. log.Info("Stopping shutdownHandler...")
  282. case <-sigs:
  283. // Call cancel on the context to close everything down.
  284. cancel()
  285. log.Info("shutdownHandler sent cancel signal...")
  286. }
  287. // Unregister to get default OS nuke behaviour in case we don't exit cleanly
  288. signal.Stop(sigs)
  289. }
  290. func getConfig(ctx context.Context, sm subnet.Manager) (*subnet.Config, error) {
  291. // Retry every second until it succeeds
  292. for {
  293. config, err := sm.GetNetworkConfig(ctx)
  294. if err != nil {
  295. log.Errorf("Couldn't fetch network config: %s", err)
  296. } else if config == nil {
  297. log.Warningf("Couldn't find network config: %s", err)
  298. } else {
  299. log.Infof("Found network config - Backend type: %s", config.BackendType)
  300. return config, nil
  301. }
  302. select {
  303. case <-ctx.Done():
  304. return nil, errCanceled
  305. case <-time.After(1 * time.Second):
  306. fmt.Println("timed out")
  307. }
  308. }
  309. }
  310. func MonitorLease(ctx context.Context, sm subnet.Manager, bn backend.Network, wg *sync.WaitGroup) error {
  311. // Use the subnet manager to start watching leases.
  312. evts := make(chan subnet.Event)
  313. wg.Add(1)
  314. go func() {
  315. subnet.WatchLease(ctx, sm, bn.Lease().Subnet, evts)
  316. wg.Done()
  317. }()
  318. renewMargin := time.Duration(opts.subnetLeaseRenewMargin) * time.Minute
  319. dur := bn.Lease().Expiration.Sub(time.Now()) - renewMargin
  320. for {
  321. select {
  322. case <-time.After(dur):
  323. err := sm.RenewLease(ctx, bn.Lease())
  324. if err != nil {
  325. log.Error("Error renewing lease (trying again in 1 min): ", err)
  326. dur = time.Minute
  327. continue
  328. }
  329. log.Info("Lease renewed, new expiration: ", bn.Lease().Expiration)
  330. dur = bn.Lease().Expiration.Sub(time.Now()) - renewMargin
  331. case e := <-evts:
  332. switch e.Type {
  333. case subnet.EventAdded:
  334. bn.Lease().Expiration = e.Lease.Expiration
  335. dur = bn.Lease().Expiration.Sub(time.Now()) - renewMargin
  336. log.Infof("Waiting for %s to renew lease", dur)
  337. case subnet.EventRemoved:
  338. log.Error("Lease has been revoked. Shutting down daemon.")
  339. return errInterrupted
  340. }
  341. case <-ctx.Done():
  342. log.Infof("Stopped monitoring lease")
  343. return errCanceled
  344. }
  345. }
  346. }
  347. func LookupExtIface(ifname string, ifregex string) (*backend.ExternalInterface, error) {
  348. var iface *net.Interface
  349. var ifaceAddr net.IP
  350. var err error
  351. if len(ifname) > 0 {
  352. if ifaceAddr = net.ParseIP(ifname); ifaceAddr != nil {
  353. log.Infof("Searching for interface using %s", ifaceAddr)
  354. iface, err = ip.GetInterfaceByIP(ifaceAddr)
  355. if err != nil {
  356. return nil, fmt.Errorf("error looking up interface %s: %s", ifname, err)
  357. }
  358. } else {
  359. iface, err = net.InterfaceByName(ifname)
  360. if err != nil {
  361. return nil, fmt.Errorf("error looking up interface %s: %s", ifname, err)
  362. }
  363. }
  364. } else if len(ifregex) > 0 {
  365. // Use the regex if specified and the iface option for matching a specific ip or name is not used
  366. ifaces, err := net.Interfaces()
  367. if err != nil {
  368. return nil, fmt.Errorf("error listing all interfaces: %s", err)
  369. }
  370. // Check IP
  371. for _, ifaceToMatch := range ifaces {
  372. ifaceIP, err := ip.GetIfaceIP4Addr(&ifaceToMatch)
  373. if err != nil {
  374. // Skip if there is no IPv4 address
  375. continue
  376. }
  377. matched, err := regexp.MatchString(ifregex, ifaceIP.String())
  378. if err != nil {
  379. return nil, fmt.Errorf("regex error matching pattern %s to %s", ifregex, ifaceIP.String())
  380. }
  381. if matched {
  382. ifaceAddr = ifaceIP
  383. iface = &ifaceToMatch
  384. break
  385. }
  386. }
  387. // Check Name
  388. if iface == nil && ifaceAddr == nil {
  389. for _, ifaceToMatch := range ifaces {
  390. matched, err := regexp.MatchString(ifregex, ifaceToMatch.Name)
  391. if err != nil {
  392. return nil, fmt.Errorf("regex error matching pattern %s to %s", ifregex, ifaceToMatch.Name)
  393. }
  394. if matched {
  395. iface = &ifaceToMatch
  396. break
  397. }
  398. }
  399. }
  400. // Check that nothing was matched
  401. if iface == nil {
  402. return nil, fmt.Errorf("Could not match pattern %s to any of the available network interfaces", ifregex)
  403. }
  404. } else {
  405. log.Info("Determining IP address of default interface")
  406. if iface, err = ip.GetDefaultGatewayIface(); err != nil {
  407. return nil, fmt.Errorf("failed to get default interface: %s", err)
  408. }
  409. }
  410. if ifaceAddr == nil {
  411. ifaceAddr, err = ip.GetIfaceIP4Addr(iface)
  412. if err != nil {
  413. return nil, fmt.Errorf("failed to find IPv4 address for interface %s", iface.Name)
  414. }
  415. }
  416. log.Infof("Using interface with name %s and address %s", iface.Name, ifaceAddr)
  417. if iface.MTU == 0 {
  418. return nil, fmt.Errorf("failed to determine MTU for %s interface", ifaceAddr)
  419. }
  420. var extAddr net.IP
  421. if len(opts.publicIP) > 0 {
  422. extAddr = net.ParseIP(opts.publicIP)
  423. if extAddr == nil {
  424. return nil, fmt.Errorf("invalid public IP address: %s", opts.publicIP)
  425. }
  426. log.Infof("Using %s as external address", extAddr)
  427. }
  428. if extAddr == nil {
  429. log.Infof("Defaulting external address to interface address (%s)", ifaceAddr)
  430. extAddr = ifaceAddr
  431. }
  432. return &backend.ExternalInterface{
  433. Iface: iface,
  434. IfaceAddr: ifaceAddr,
  435. ExtAddr: extAddr,
  436. }, nil
  437. }
  438. func WriteSubnetFile(path string, nw ip.IP4Net, ipMasq bool, bn backend.Network) error {
  439. dir, name := filepath.Split(path)
  440. os.MkdirAll(dir, 0755)
  441. tempFile := filepath.Join(dir, "."+name)
  442. f, err := os.Create(tempFile)
  443. if err != nil {
  444. return err
  445. }
  446. // Write out the first usable IP by incrementing
  447. // sn.IP by one
  448. sn := bn.Lease().Subnet
  449. sn.IP += 1
  450. fmt.Fprintf(f, "FLANNEL_NETWORK=%s\n", nw)
  451. fmt.Fprintf(f, "FLANNEL_SUBNET=%s\n", sn)
  452. fmt.Fprintf(f, "FLANNEL_MTU=%d\n", bn.MTU())
  453. _, err = fmt.Fprintf(f, "FLANNEL_IPMASQ=%v\n", ipMasq)
  454. f.Close()
  455. if err != nil {
  456. return err
  457. }
  458. // rename(2) the temporary file to the desired location so that it becomes
  459. // atomically visible with the contents
  460. return os.Rename(tempFile, path)
  461. //TODO - is this safe? What if it's not on the same FS?
  462. }
  463. func mustRunHealthz() {
  464. address := net.JoinHostPort(opts.healthzIP, strconv.Itoa(opts.healthzPort))
  465. log.Infof("Start healthz server on %s", address)
  466. http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
  467. w.WriteHeader(http.StatusOK)
  468. w.Write([]byte("flanneld is running"))
  469. })
  470. if err := http.ListenAndServe(address, nil); err != nil {
  471. log.Errorf("Start healthz server error. %v", err)
  472. panic(err)
  473. }
  474. }
  475. func setupIPMasq(config *subnet.Config, bn backend.Network) {
  476. ipt, err := iptables.New()
  477. if err != nil {
  478. // if we can't find iptables, give up and return
  479. log.Errorf("Failed to set up IP Masquerade. iptables was not found: %v", err)
  480. return
  481. }
  482. defer func() {
  483. network.TeardownIPMasq(ipt, config.Network, bn.Lease())
  484. }()
  485. for {
  486. // Ensure that all the rules exist every 5 seconds
  487. if err := network.EnsureIPMasq(ipt, config.Network, bn.Lease()); err != nil {
  488. log.Errorf("Failed to ensure IP Masquerade: %v", err)
  489. }
  490. time.Sleep(5 * time.Second)
  491. }
  492. }
  493. func ReadSubnetFromSubnetFile(path string) ip.IP4Net {
  494. var prevSubnet ip.IP4Net
  495. if _, err := os.Stat(path); !os.IsNotExist(err) {
  496. prevSubnetVals, err := godotenv.Read(path)
  497. if err != nil {
  498. log.Errorf("Couldn't fetch previous subnet from subnet file at %s: %s", path, err)
  499. } else if prevSubnetString, ok := prevSubnetVals["FLANNEL_SUBNET"]; ok {
  500. err = prevSubnet.UnmarshalJSON([]byte(prevSubnetString))
  501. if err != nil {
  502. log.Errorf("Couldn't parse previous subnet from subnet file at %s: %s", path, err)
  503. }
  504. }
  505. }
  506. return prevSubnet
  507. }