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