Simple Main

Cmd

The main program lives under the cmd/ package created by kubebuilder init.It does not need to be changed by the user for most cases.

The main program starts the Controllers that have been registered with the Manager.Scaffolded Controllers are automatically registered with the Manager by scaffoldingan init function to the controller package. Scaffolded Resources are automatically registered with the Manager Scheme by scaffolding an initfunction to the apis package.

  • Get a kubeconfig to talk to an apiserver
  • Add APIs to the Manager's Scheme
  • Add Controllers to the Manager
  • Start the Manager
  1. func main() {
  2. // Get a config to talk to the apiserver
  3. cfg, err := config.GetConfig()
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. // Create a new Cmd to provide shared dependencies and start components
  8. mgr, err := manager.New(cfg, manager.Options{})
  9. if err != nil {
  10. log.Fatal(err)
  11. }
  12. // Setup Scheme for all resources
  13. if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
  14. log.Fatal(err)
  15. }
  16. // Setup all Controllers
  17. if err := controller.AddToManager(mgr); err != nil {
  18. log.Fatal(err)
  19. }
  20. // Start the Cmd
  21. log.Fatal(mgr.Start(signals.SetupSignalHandler()))}