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