github.com/jmrodri/operator-sdk@v0.5.0/pkg/scaffold/cmd_test.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 scaffold
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/operator-framework/operator-sdk/internal/util/diffutil"
    21  )
    22  
    23  func TestCmd(t *testing.T) {
    24  	s, buf := setupScaffoldAndWriter()
    25  	err := s.Execute(appConfig, &Cmd{})
    26  	if err != nil {
    27  		t.Fatalf("Failed to execute the scaffold: (%v)", err)
    28  	}
    29  
    30  	if cmdExp != buf.String() {
    31  		diffs := diffutil.Diff(cmdExp, buf.String())
    32  		t.Fatalf("Expected vs actual differs.\n%v", diffs)
    33  	}
    34  }
    35  
    36  const cmdExp = `package main
    37  
    38  import (
    39  	"context"
    40  	"flag"
    41  	"fmt"
    42  	"os"
    43  	"runtime"
    44  
    45  	"github.com/example-inc/app-operator/pkg/apis"
    46  	"github.com/example-inc/app-operator/pkg/controller"
    47  
    48  	"github.com/operator-framework/operator-sdk/pkg/k8sutil"
    49  	"github.com/operator-framework/operator-sdk/pkg/leader"
    50  	"github.com/operator-framework/operator-sdk/pkg/log/zap"
    51  	"github.com/operator-framework/operator-sdk/pkg/metrics"
    52  	sdkVersion "github.com/operator-framework/operator-sdk/version"
    53  	"github.com/spf13/pflag"
    54  	_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
    55  	"sigs.k8s.io/controller-runtime/pkg/client/config"
    56  	"sigs.k8s.io/controller-runtime/pkg/manager"
    57  	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
    58  	"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
    59  )
    60  
    61  // Change below variables to serve metrics on different host or port.
    62  var (
    63  	metricsHost       = "0.0.0.0"
    64  	metricsPort int32 = 8383
    65  )
    66  var log = logf.Log.WithName("cmd")
    67  
    68  func printVersion() {
    69  	log.Info(fmt.Sprintf("Go Version: %s", runtime.Version()))
    70  	log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH))
    71  	log.Info(fmt.Sprintf("Version of operator-sdk: %v", sdkVersion.Version))
    72  }
    73  
    74  func main() {
    75  	// Add the zap logger flag set to the CLI. The flag set must
    76  	// be added before calling pflag.Parse().
    77  	pflag.CommandLine.AddFlagSet(zap.FlagSet())
    78  
    79  	// Add flags registered by imported packages (e.g. glog and
    80  	// controller-runtime)
    81  	pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
    82  
    83  	pflag.Parse()
    84  
    85  	// Use a zap logr.Logger implementation. If none of the zap
    86  	// flags are configured (or if the zap flag set is not being
    87  	// used), this defaults to a production zap logger.
    88  	//
    89  	// The logger instantiated here can be changed to any logger
    90  	// implementing the logr.Logger interface. This logger will
    91  	// be propagated through the whole operator, generating
    92  	// uniform and structured logs.
    93  	logf.SetLogger(zap.Logger())
    94  
    95  	printVersion()
    96  
    97  	namespace, err := k8sutil.GetWatchNamespace()
    98  	if err != nil {
    99  		log.Error(err, "Failed to get watch namespace")
   100  		os.Exit(1)
   101  	}
   102  
   103  	// Get a config to talk to the apiserver
   104  	cfg, err := config.GetConfig()
   105  	if err != nil {
   106  		log.Error(err, "")
   107  		os.Exit(1)
   108  	}
   109  
   110  	ctx := context.TODO()
   111  
   112  	// Become the leader before proceeding
   113  	err = leader.Become(ctx, "app-operator-lock")
   114  	if err != nil {
   115  		log.Error(err, "")
   116  		os.Exit(1)
   117  	}
   118  
   119  	// Create a new Cmd to provide shared dependencies and start components
   120  	mgr, err := manager.New(cfg, manager.Options{
   121  		Namespace:          namespace,
   122  		MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
   123  	})
   124  	if err != nil {
   125  		log.Error(err, "")
   126  		os.Exit(1)
   127  	}
   128  
   129  	log.Info("Registering Components.")
   130  
   131  	// Setup Scheme for all resources
   132  	if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
   133  		log.Error(err, "")
   134  		os.Exit(1)
   135  	}
   136  
   137  	// Setup all Controllers
   138  	if err := controller.AddToManager(mgr); err != nil {
   139  		log.Error(err, "")
   140  		os.Exit(1)
   141  	}
   142  
   143  	// Create Service object to expose the metrics port.
   144  	_, err = metrics.ExposeMetricsPort(ctx, metricsPort)
   145  	if err != nil {
   146  		log.Info(err.Error())
   147  	}
   148  
   149  	log.Info("Starting the Cmd.")
   150  
   151  	// Start the Cmd
   152  	if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
   153  		log.Error(err, "Manager exited non-zero")
   154  		os.Exit(1)
   155  	}
   156  }
   157  `