10 "github.com/sirupsen/logrus"
11 "github.com/urfave/cli"
13 kexec "k8s.io/utils/exec"
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"
24 c.Usage = "run ovn4nfvk8s to start pod watchers"
25 c.Flags = append([]cli.Flag{
29 Usage: "Name of file that will hold the ovn4nfvk8s pid (optional)",
32 c.Action = func(c *cli.Context) error {
36 if err := c.Run(os.Args); err != nil {
41 func delPidfile(pidfile string) {
43 if _, err := os.Stat(pidfile); err == nil {
44 if err := os.Remove(pidfile); err != nil {
45 logrus.Errorf("%s delete failed: %v", pidfile, err)
51 func runOvnKube(ctx *cli.Context) error {
53 _, err := config.InitConfig(ctx)
57 pidfile := ctx.String("pidfile")
59 c := make(chan os.Signal, 2)
60 signal.Notify(c, os.Interrupt, syscall.SIGTERM)
67 defer delPidfile(pidfile)
70 // need to test if already there
71 _, err := os.Stat(pidfile)
73 // Create if it doesn't exist, else exit with error
74 if os.IsNotExist(err) {
75 if err := ioutil.WriteFile(pidfile, []byte(fmt.Sprintf("%d", os.Getpid())), 0644); err != nil {
76 logrus.Errorf("failed to write pidfile %s (%v). Ignoring..", pidfile, err)
79 // get the pid and see if it exists
80 pid, err := ioutil.ReadFile(pidfile)
82 logrus.Errorf("pidfile %s exists but can't be read", pidfile)
85 _, err1 := os.Stat("/proc/" + string(pid[:]) + "/cmdline")
86 if os.IsNotExist(err1) {
87 // Left over pid from dead process
88 if err := ioutil.WriteFile(pidfile, []byte(fmt.Sprintf("%d", os.Getpid())), 0644); err != nil {
89 logrus.Errorf("failed to write pidfile %s (%v). Ignoring..", pidfile, err)
92 logrus.Errorf("pidfile %s exists and ovn4nfvk8s is running", pidfile)
98 if err = util.SetExec(exec); err != nil {
99 logrus.Errorf("Failed to initialize exec helper: %v", err)
103 clientset, err := config.NewClientset(&config.Kubernetes)
108 // Create distributed router and gateway for the deployment
109 err = ovn.SetupMaster("ovn4nfv-master")
111 logrus.Errorf(err.Error())
114 // create factory and start the ovn controller
115 stopChan := make(chan struct{})
116 factory, err := factory.NewWatchFactory(clientset, stopChan)
121 ovnController := ovn.NewOvnController(clientset, factory)
122 if err := ovnController.Run(); err != nil {
123 logrus.Errorf(err.Error())