Seed code for the Plugin
[ovn4nfv-k8s-plugin.git] / cmd / ovn4nfvk8s / ovn4nfvk8s.go
1 package main
2
3 import (
4         "fmt"
5         "io/ioutil"
6         "os"
7         "os/signal"
8         "syscall"
9
10         "github.com/sirupsen/logrus"
11         "github.com/urfave/cli"
12
13         kexec "k8s.io/utils/exec"
14
15         "ovn4nfv-k8s-plugin/internal/pkg/config"
16         "ovn4nfv-k8s-plugin/internal/pkg/factory"
17         "ovn4nfv-k8s-plugin/internal/pkg/ovn"
18         "ovn4nfv-k8s-plugin/internal/pkg/util"
19 )
20
21 func main() {
22         c := cli.NewApp()
23         c.Name = "ovn4nfvk8s"
24         c.Usage = "run ovn4nfvk8s to start pod watchers"
25         c.Version = config.Version
26         c.Flags = append([]cli.Flag{
27                 // Daemon file
28                 cli.StringFlag{
29                         Name:  "pidfile",
30                         Usage: "Name of file that will hold the ovn4nfvk8s pid (optional)",
31                 },
32         }, config.Flags...)
33         c.Action = func(c *cli.Context) error {
34                 return runOvnKube(c)
35         }
36
37         if err := c.Run(os.Args); err != nil {
38                 logrus.Fatal(err)
39         }
40 }
41
42 func delPidfile(pidfile string) {
43         if pidfile != "" {
44                 if _, err := os.Stat(pidfile); err == nil {
45                         if err := os.Remove(pidfile); err != nil {
46                                 logrus.Errorf("%s delete failed: %v", pidfile, err)
47                         }
48                 }
49         }
50 }
51
52 func runOvnKube(ctx *cli.Context) error {
53         fmt.Println("ovn4nfvk8s started")
54         exec := kexec.New()
55         _, err := config.InitConfig(ctx, exec, nil)
56         if err != nil {
57                 return err
58         }
59         pidfile := ctx.String("pidfile")
60
61         c := make(chan os.Signal, 2)
62         signal.Notify(c, os.Interrupt, syscall.SIGTERM)
63         go func() {
64                 <-c
65                 delPidfile(pidfile)
66                 os.Exit(1)
67         }()
68
69         defer delPidfile(pidfile)
70
71         if pidfile != "" {
72                 // need to test if already there
73                 _, err := os.Stat(pidfile)
74
75                 // Create if it doesn't exist, else exit with error
76                 if os.IsNotExist(err) {
77                         if err := ioutil.WriteFile(pidfile, []byte(fmt.Sprintf("%d", os.Getpid())), 0644); err != nil {
78                                 logrus.Errorf("failed to write pidfile %s (%v). Ignoring..", pidfile, err)
79                         }
80                 } else {
81                         // get the pid and see if it exists
82                         pid, err := ioutil.ReadFile(pidfile)
83                         if err != nil {
84                                 logrus.Errorf("pidfile %s exists but can't be read", pidfile)
85                                 return err
86                         }
87                         _, err1 := os.Stat("/proc/" + string(pid[:]) + "/cmdline")
88                         if os.IsNotExist(err1) {
89                                 // Left over pid from dead process
90                                 if err := ioutil.WriteFile(pidfile, []byte(fmt.Sprintf("%d", os.Getpid())), 0644); err != nil {
91                                         logrus.Errorf("failed to write pidfile %s (%v). Ignoring..", pidfile, err)
92                                 }
93                         } else {
94                                 logrus.Errorf("pidfile %s exists and ovn4nfvk8s is running", pidfile)
95                                 os.Exit(1)
96                         }
97                 }
98         }
99
100         if err = util.SetExec(exec); err != nil {
101                 logrus.Errorf("Failed to initialize exec helper: %v", err)
102                 return err
103         }
104
105         clientset, err := config.NewClientset(&config.Kubernetes)
106         if err != nil {
107                 panic(err.Error())
108         }
109
110         // Create distributed router and gateway for the deployment
111         err = ovn.SetupMaster("ovn4nfv-master")
112         if err != nil {
113                 logrus.Errorf(err.Error())
114                 panic(err.Error())
115         }
116         // create factory and start the ovn controller
117         stopChan := make(chan struct{})
118         factory, err := factory.NewWatchFactory(clientset, stopChan)
119         if err != nil {
120                 panic(err.Error)
121         }
122
123         ovnController := ovn.NewOvnController(clientset, factory)
124         if err := ovnController.Run(); err != nil {
125                 logrus.Errorf(err.Error())
126                 panic(err.Error())
127         }
128         // run forever
129         select {}
130
131         return nil
132 }