github.com/michaelhenkel/operator-sdk@v0.8.1/test/test-framework/cmd/manager/main.go (about)

     1  // Copyright 2018 The Operator-SDK Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"context"
    19  	"flag"
    20  	"fmt"
    21  	"os"
    22  	"runtime"
    23  
    24  	// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
    25  	_ "k8s.io/client-go/plugin/pkg/client/auth"
    26  
    27  	"github.com/operator-framework/operator-sdk/pkg/k8sutil"
    28  	"github.com/operator-framework/operator-sdk/pkg/leader"
    29  	"github.com/operator-framework/operator-sdk/pkg/log/zap"
    30  	"github.com/operator-framework/operator-sdk/pkg/ready"
    31  	"github.com/operator-framework/operator-sdk/test/test-framework/pkg/apis"
    32  	"github.com/operator-framework/operator-sdk/test/test-framework/pkg/controller"
    33  	sdkVersion "github.com/operator-framework/operator-sdk/version"
    34  	"github.com/spf13/pflag"
    35  	"sigs.k8s.io/controller-runtime/pkg/client/config"
    36  	"sigs.k8s.io/controller-runtime/pkg/manager"
    37  	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
    38  	"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
    39  )
    40  
    41  var log = logf.Log.WithName("cmd")
    42  
    43  func printVersion() {
    44  	log.Info(fmt.Sprintf("Go Version: %s", runtime.Version()))
    45  	log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH))
    46  	log.Info(fmt.Sprintf("Version of operator-sdk: %v", sdkVersion.Version))
    47  }
    48  
    49  func main() {
    50  	// Add the zap logger flag set to the CLI. The flag set must
    51  	// be added before calling pflag.Parse().
    52  	pflag.CommandLine.AddFlagSet(zap.FlagSet())
    53  
    54  	// Add flags registered by imported packages (e.g. glog and
    55  	// controller-runtime)
    56  	pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
    57  
    58  	pflag.Parse()
    59  
    60  	// Use a zap logr.Logger implementation. If none of the zap
    61  	// flags are configured (or if the zap flag set is not being
    62  	// used), this defaults to a production zap logger.
    63  	//
    64  	// The logger instantiated here can be changed to any logger
    65  	// implementing the logr.Logger interface. This logger will
    66  	// be propagated through the whole operator, generating
    67  	// uniform and structured logs.
    68  	logf.SetLogger(zap.Logger())
    69  
    70  	printVersion()
    71  
    72  	namespace, err := k8sutil.GetWatchNamespace()
    73  	if err != nil {
    74  		log.Error(err, "Failed to get watch namespace")
    75  		os.Exit(1)
    76  	}
    77  
    78  	// Get a config to talk to the apiserver
    79  	cfg, err := config.GetConfig()
    80  	if err != nil {
    81  		log.Error(err, "")
    82  		os.Exit(1)
    83  	}
    84  
    85  	// Become the leader before proceeding
    86  	if err := leader.Become(context.TODO(), "memcached-operator-lock"); err != nil {
    87  		log.Error(err, "Failed to become leader")
    88  		return
    89  	}
    90  
    91  	r := ready.NewFileReady()
    92  	err = r.Set()
    93  	if err != nil {
    94  		log.Error(err, "")
    95  		os.Exit(1)
    96  	}
    97  	defer func() {
    98  		if err := r.Unset(); err != nil {
    99  			log.Error(err, "Failed to unset file ready")
   100  		}
   101  	}()
   102  
   103  	// Create a new Cmd to provide shared dependencies and start components
   104  	mgr, err := manager.New(cfg, manager.Options{Namespace: namespace})
   105  	if err != nil {
   106  		log.Error(err, "")
   107  		os.Exit(1)
   108  	}
   109  
   110  	log.Info("Registering Components.")
   111  
   112  	// Setup Scheme for all resources
   113  	if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
   114  		log.Error(err, "")
   115  		os.Exit(1)
   116  	}
   117  
   118  	// Setup all Controllers
   119  	if err := controller.AddToManager(mgr); err != nil {
   120  		log.Error(err, "")
   121  		os.Exit(1)
   122  	}
   123  
   124  	log.Info("Starting the Cmd.")
   125  
   126  	// Start the Cmd
   127  	if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
   128  		log.Error(err, "Manager exited non-zero")
   129  		os.Exit(1)
   130  	}
   131  }