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