github.com/mkimuram/operator-sdk@v0.7.1-0.20190410172100-52ad33a4bda0/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  	namespace, found := os.LookupEnv(k8sutil.WatchNamespaceEnvVar)
    51  	log = log.WithValues("Namespace", namespace)
    52  	if found {
    53  		log.Info("Watching namespace.")
    54  	} else {
    55  		log.Info(fmt.Sprintf("%v environment variable not set. This operator is watching all namespaces.",
    56  			k8sutil.WatchNamespaceEnvVar))
    57  		namespace = metav1.NamespaceAll
    58  	}
    59  
    60  	cfg, err := config.GetConfig()
    61  	if err != nil {
    62  		log.Error(err, "Failed to get config.")
    63  		return err
    64  	}
    65  	mgr, err := manager.New(cfg, manager.Options{
    66  		Namespace: namespace,
    67  	})
    68  	if err != nil {
    69  		log.Error(err, "Failed to create a new manager.")
    70  		return err
    71  	}
    72  
    73  	name, found := os.LookupEnv(k8sutil.OperatorNameEnvVar)
    74  	if !found {
    75  		log.Error(fmt.Errorf("%s environment variable not set", k8sutil.OperatorNameEnvVar), "")
    76  		return err
    77  	}
    78  	// Become the leader before proceeding
    79  	err = leader.Become(context.TODO(), name+"-lock")
    80  	if err != nil {
    81  		log.Error(err, "Failed to become leader.")
    82  		return err
    83  	}
    84  
    85  	done := make(chan error)
    86  	cMap := controllermap.NewControllerMap()
    87  
    88  	// start the proxy
    89  	err = proxy.Run(done, proxy.Options{
    90  		Address:           "localhost",
    91  		Port:              8888,
    92  		KubeConfig:        mgr.GetConfig(),
    93  		Cache:             mgr.GetCache(),
    94  		RESTMapper:        mgr.GetRESTMapper(),
    95  		ControllerMap:     cMap,
    96  		OwnerInjection:    flags.InjectOwnerRef,
    97  		WatchedNamespaces: []string{namespace},
    98  	})
    99  	if err != nil {
   100  		log.Error(err, "Error starting proxy.")
   101  		return err
   102  	}
   103  
   104  	// start the operator
   105  	go operator.Run(done, mgr, flags, cMap)
   106  
   107  	// wait for either to finish
   108  	err = <-done
   109  	if err != nil {
   110  		log.Error(err, "Proxy or operator exited with error.")
   111  		os.Exit(1)
   112  	}
   113  	log.Info("Exiting.")
   114  	return nil
   115  }