github.com/interconnectedcloud/qdr-operator@v0.0.0-20210826174505-576d2b33dac7/cmd/manager/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"fmt"
     7  	"os"
     8  	"runtime"
     9  
    10  	"github.com/interconnectedcloud/qdr-operator/pkg/apis"
    11  	"github.com/interconnectedcloud/qdr-operator/pkg/controller"
    12  	"github.com/operator-framework/operator-sdk/pkg/k8sutil"
    13  	"github.com/operator-framework/operator-sdk/pkg/leader"
    14  	sdkVersion "github.com/operator-framework/operator-sdk/version"
    15  	_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
    16  	"sigs.k8s.io/controller-runtime/pkg/client/config"
    17  	"sigs.k8s.io/controller-runtime/pkg/manager"
    18  	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
    19  	"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
    20  )
    21  
    22  var log = logf.Log.WithName("cmd")
    23  
    24  func printVersion() {
    25  	log.Info(fmt.Sprintf("Go Version: %s", runtime.Version()))
    26  	log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH))
    27  	log.Info(fmt.Sprintf("Version of operator-sdk: %v", sdkVersion.Version))
    28  }
    29  
    30  func main() {
    31  	flag.Parse()
    32  
    33  	// The logger instantiated here can be changed to any logger
    34  	// implementing the logr.Logger interface. This logger will
    35  	// be propagated through the whole operator, generating
    36  	// uniform and structured logs.
    37  	logf.SetLogger(logf.ZapLogger(false))
    38  
    39  	printVersion()
    40  
    41  	namespace, err := k8sutil.GetWatchNamespace()
    42  	if err != nil {
    43  		log.Error(err, "Failed to get watch namespace")
    44  		os.Exit(1)
    45  	}
    46  
    47  	// Get a config to talk to the apiserver
    48  	cfg, err := config.GetConfig()
    49  	if err != nil {
    50  		log.Error(err, "")
    51  		os.Exit(1)
    52  	}
    53  
    54  	// Become the leader before proceeding
    55  	err = leader.Become(context.TODO(), "qdr-operator-lock")
    56  	if err != nil {
    57  		log.Error(err, "")
    58  		os.Exit(1)
    59  	}
    60  
    61  	// Create a new Cmd to provide shared dependencies and start components
    62  	mgr, err := manager.New(cfg, manager.Options{Namespace: namespace})
    63  	if err != nil {
    64  		log.Error(err, "")
    65  		os.Exit(1)
    66  	}
    67  
    68  	log.Info("Registering Components.")
    69  
    70  	// Setup Scheme for all resources
    71  	if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
    72  		log.Error(err, "")
    73  		os.Exit(1)
    74  	}
    75  
    76  	// Setup all Controllers
    77  	if err := controller.AddToManager(mgr); err != nil {
    78  		log.Error(err, "")
    79  		os.Exit(1)
    80  	}
    81  
    82  	log.Info("Starting the Cmd.")
    83  
    84  	// Start the Cmd
    85  	if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
    86  		log.Error(err, "Manager exited non-zero")
    87  		os.Exit(1)
    88  	}
    89  }