github.com/fabianvf/ocp-release-operator-sdk@v0.0.0-20190426141702-57620ee2f090/pkg/ansible/run.go (about) 1 // Copyright 2019 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 ansible 16 17 import ( 18 "context" 19 "fmt" 20 "os" 21 "runtime" 22 23 aoflags "github.com/operator-framework/operator-sdk/pkg/ansible/flags" 24 "github.com/operator-framework/operator-sdk/pkg/ansible/operator" 25 proxy "github.com/operator-framework/operator-sdk/pkg/ansible/proxy" 26 "github.com/operator-framework/operator-sdk/pkg/ansible/proxy/controllermap" 27 "github.com/operator-framework/operator-sdk/pkg/k8sutil" 28 "github.com/operator-framework/operator-sdk/pkg/leader" 29 sdkVersion "github.com/operator-framework/operator-sdk/version" 30 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 31 32 "sigs.k8s.io/controller-runtime/pkg/client/config" 33 "sigs.k8s.io/controller-runtime/pkg/manager" 34 logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" 35 ) 36 37 var log = logf.Log.WithName("cmd") 38 39 func printVersion() { 40 log.Info(fmt.Sprintf("Go Version: %s", runtime.Version())) 41 log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)) 42 log.Info(fmt.Sprintf("Version of operator-sdk: %v", sdkVersion.Version)) 43 } 44 45 // Run will start the ansible operator and proxy, blocking until one of them 46 // returns. 47 func Run(flags *aoflags.AnsibleOperatorFlags) error { 48 printVersion() 49 50 printVersion() 51 52 namespace, found := os.LookupEnv(k8sutil.WatchNamespaceEnvVar) 53 log = log.WithValues("Namespace", namespace) 54 if found { 55 log.Info("Watching namespace.") 56 } else { 57 log.Info(fmt.Sprintf("%v environment variable not set. This operator is watching all namespaces.", 58 k8sutil.WatchNamespaceEnvVar)) 59 namespace = metav1.NamespaceAll 60 } 61 62 cfg, err := config.GetConfig() 63 if err != nil { 64 log.Error(err, "Failed to get config.") 65 return err 66 } 67 mgr, err := manager.New(cfg, manager.Options{ 68 Namespace: namespace, 69 }) 70 if err != nil { 71 log.Error(err, "Failed to create a new manager.") 72 return err 73 } 74 75 name, found := os.LookupEnv(k8sutil.OperatorNameEnvVar) 76 if !found { 77 log.Error(fmt.Errorf("%s environment variable not set", k8sutil.OperatorNameEnvVar), "") 78 return err 79 } 80 // Become the leader before proceeding 81 err = leader.Become(context.TODO(), name+"-lock") 82 if err != nil { 83 log.Error(err, "Failed to become leader.") 84 return err 85 } 86 87 done := make(chan error) 88 cMap := controllermap.NewControllerMap() 89 90 // start the proxy 91 err = proxy.Run(done, proxy.Options{ 92 Address: "localhost", 93 Port: 8888, 94 KubeConfig: mgr.GetConfig(), 95 Cache: mgr.GetCache(), 96 RESTMapper: mgr.GetRESTMapper(), 97 ControllerMap: cMap, 98 OwnerInjection: flags.InjectOwnerRef, 99 WatchedNamespaces: []string{namespace}, 100 }) 101 if err != nil { 102 log.Error(err, "Error starting proxy.") 103 return err 104 } 105 106 // start the operator 107 go operator.Run(done, mgr, flags, cMap) 108 109 // wait for either to finish 110 err = <-done 111 if err != nil { 112 log.Error(err, "Proxy or operator exited with error.") 113 os.Exit(1) 114 } 115 log.Info("Exiting.") 116 return nil 117 }