diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/nfn-operator/nfn-operator.go | 91 | ||||
-rw-r--r-- | cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni.go | 12 | ||||
-rw-r--r-- | cmd/ovn4nfvk8s/ovn4nfvk8s.go | 129 |
3 files changed, 99 insertions, 133 deletions
diff --git a/cmd/nfn-operator/nfn-operator.go b/cmd/nfn-operator/nfn-operator.go new file mode 100644 index 0000000..2925ac6 --- /dev/null +++ b/cmd/nfn-operator/nfn-operator.go @@ -0,0 +1,91 @@ +package main + +import ( + "flag" + "fmt" + "os" + "runtime" + + // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) + _ "k8s.io/client-go/plugin/pkg/client/auth" + + "github.com/spf13/pflag" +// "ovn4nfv-k8s-plugin/pkg/apis" + "ovn4nfv-k8s-plugin/internal/pkg/ovn" + "ovn4nfv-k8s-plugin/pkg/controller" + "sigs.k8s.io/controller-runtime/pkg/client/config" + "sigs.k8s.io/controller-runtime/pkg/log/zap" + "sigs.k8s.io/controller-runtime/pkg/manager" + logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" + "sigs.k8s.io/controller-runtime/pkg/runtime/signals" +) + +var log = logf.Log.WithName("nfn-operator") + +func printVersion() { + log.Info(fmt.Sprintf("Go Version: %s", runtime.Version())) + log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)) +} + +func main() { + + // Add flags registered by imported packages (e.g. glog and + // controller-runtime) + pflag.CommandLine.AddGoFlagSet(flag.CommandLine) + + pflag.Parse() + + // Use a zap logr.Logger implementation. If none of the zap + // flags are configured (or if the zap flag set is not being + // used), this defaults to a production zap logger. + // + // The logger instantiated here can be changed to any logger + // implementing the logr.Logger interface. This logger will + // be propagated through the whole operator, generating + // uniform and structured logs. + logf.SetLogger(zap.Logger(true)) + + printVersion() + + // Create an OVN Controller + _, err := ovn.NewOvnController(nil) + if err != nil { + log.Error(err, "") + os.Exit(1) + } + //Initialize all the controllers that are supported here + + // Get a config to talk to the apiserver + cfg, err := config.GetConfig() + if err != nil { + log.Error(err, "") + os.Exit(1) + } + // Create a new Cmd to provide shared dependencies and start components + mgr, err := manager.New(cfg, manager.Options{}) + if err != nil { + log.Error(err, "") + os.Exit(1) + } + + log.Info("Registering Components.") + + // Setup Scheme for all resources + //if err := apis.AddToScheme(mgr.GetScheme()); err != nil { + // log.Error(err, "") + // os.Exit(1) + //} + + // Setup all Controllers + if err := controller.AddToManager(mgr); err != nil { + log.Error(err, "") + os.Exit(1) + } + log.Info("Starting the Cmd.") + + // Start the Cmd + if err := mgr.Start(signals.SetupSignalHandler()); err != nil { + log.Error(err, "Manager exited non-zero") + os.Exit(1) + } +} diff --git a/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni.go b/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni.go index 3a6b7f0..2585fcf 100644 --- a/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni.go +++ b/cmd/ovn4nfvk8s-cni/ovn4nfvk8s-cni.go @@ -26,6 +26,10 @@ import ( "ovn4nfv-k8s-plugin/internal/pkg/config" ) +const ( + ovn4nfvAnnotationTag = "k8s.plugin.opnfv.org/ovnInterfaces" +) + func argString2Map(args string) (map[string]string, error) { argsMap := make(map[string]string) @@ -110,7 +114,7 @@ func addMultipleInterfaces(args *skel.CmdArgs, ovnAnnotation, namespace, podName var ovnAnnotatedMap []map[string]string ovnAnnotatedMap, err := parseOvnNetworkObject(ovnAnnotation) if err != nil { - logrus.Errorf("addLogicalPort : Error Parsing Ovn Network List %v", ovnAnnotatedMap) + logrus.Errorf("addLogicalPort : Error Parsing Ovn Network List %v %v", ovnAnnotatedMap, err) return nil } if namespace == "" || podName == "" { @@ -275,7 +279,7 @@ func cmdAdd(args *skel.CmdArgs) error { logrus.Warningf("Error while obtaining pod annotations - %v", err) return false, nil } - if _, ok := annotation["ovnIfaceList"]; ok { + if _, ok := annotation[ovn4nfvAnnotationTag]; ok { return true, nil } return false, nil @@ -283,7 +287,7 @@ func cmdAdd(args *skel.CmdArgs) error { return fmt.Errorf("failed to get pod annotation - %v", err) } logrus.Infof("ovn4nfvk8s-cni: Annotation Found ") - ovnAnnotation, ok := annotation["ovnIfaceList"] + ovnAnnotation, ok := annotation[ovn4nfvAnnotationTag] if !ok { return fmt.Errorf("Error while obtaining pod annotations") } @@ -326,7 +330,7 @@ func main() { return err } - skel.PluginMain(cmdAdd, cmdDel, version.All) + skel.PluginMain(cmdAdd, nil, cmdDel, version.All, "") return nil } diff --git a/cmd/ovn4nfvk8s/ovn4nfvk8s.go b/cmd/ovn4nfvk8s/ovn4nfvk8s.go deleted file mode 100644 index 607b07f..0000000 --- a/cmd/ovn4nfvk8s/ovn4nfvk8s.go +++ /dev/null @@ -1,129 +0,0 @@ -package main - -import ( - "fmt" - "io/ioutil" - "os" - "os/signal" - "syscall" - - "github.com/sirupsen/logrus" - "github.com/urfave/cli" - - kexec "k8s.io/utils/exec" - - "ovn4nfv-k8s-plugin/internal/pkg/config" - "ovn4nfv-k8s-plugin/internal/pkg/factory" - "ovn4nfv-k8s-plugin/internal/pkg/ovn" -) - -func main() { - c := cli.NewApp() - c.Name = "ovn4nfvk8s" - c.Usage = "run ovn4nfvk8s to start pod watchers" - c.Flags = append([]cli.Flag{ - // Daemon file - cli.StringFlag{ - Name: "pidfile", - Usage: "Name of file that will hold the ovn4nfvk8s pid (optional)", - }, - }, config.Flags...) - c.Action = func(c *cli.Context) error { - return runOvnKube(c) - } - - if err := c.Run(os.Args); err != nil { - logrus.Fatal(err) - } -} - -func delPidfile(pidfile string) { - if pidfile != "" { - if _, err := os.Stat(pidfile); err == nil { - if err := os.Remove(pidfile); err != nil { - logrus.Errorf("%s delete failed: %v", pidfile, err) - } - } - } -} - -func runOvnKube(ctx *cli.Context) error { - exec := kexec.New() - _, err := config.InitConfig(ctx) - if err != nil { - return err - } - pidfile := ctx.String("pidfile") - - c := make(chan os.Signal, 2) - signal.Notify(c, os.Interrupt, syscall.SIGTERM) - go func() { - <-c - delPidfile(pidfile) - os.Exit(1) - }() - - defer delPidfile(pidfile) - - if pidfile != "" { - // need to test if already there - _, err := os.Stat(pidfile) - - // Create if it doesn't exist, else exit with error - if os.IsNotExist(err) { - if err := ioutil.WriteFile(pidfile, []byte(fmt.Sprintf("%d", os.Getpid())), 0644); err != nil { - logrus.Errorf("failed to write pidfile %s (%v). Ignoring..", pidfile, err) - } - } else { - // get the pid and see if it exists - pid, err := ioutil.ReadFile(pidfile) - if err != nil { - logrus.Errorf("pidfile %s exists but can't be read", pidfile) - return err - } - _, err1 := os.Stat("/proc/" + string(pid[:]) + "/cmdline") - if os.IsNotExist(err1) { - // Left over pid from dead process - if err := ioutil.WriteFile(pidfile, []byte(fmt.Sprintf("%d", os.Getpid())), 0644); err != nil { - logrus.Errorf("failed to write pidfile %s (%v). Ignoring..", pidfile, err) - } - } else { - logrus.Errorf("pidfile %s exists and ovn4nfvk8s is running", pidfile) - os.Exit(1) - } - } - } - - if err = ovn.SetExec(exec); err != nil { - logrus.Errorf("Failed to initialize exec helper: %v", err) - return err - } - - clientset, err := config.NewClientset(&config.Kubernetes) - if err != nil { - panic(err.Error()) - } - - // Create distributed router and gateway for the deployment - err = ovn.SetupDistributedRouter("ovn4nfv-master") - if err != nil { - logrus.Errorf(err.Error()) - panic(err.Error()) - } - // create factory and start the ovn controller - stopChan := make(chan struct{}) - factory, err := factory.NewWatchFactory(clientset, stopChan) - if err != nil { - panic(err.Error) - } - - ovnController := ovn.NewOvnController(clientset, factory) - if err := ovnController.Run(); err != nil { - logrus.Errorf(err.Error()) - panic(err.Error()) - } - // run forever - select {} - - return nil -} |