github.imxd.top/operator-framework/operator-sdk@v0.8.2/pkg/helm/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 helm
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"os"
    21  	"runtime"
    22  
    23  	"github.com/operator-framework/operator-sdk/pkg/helm/controller"
    24  	hoflags "github.com/operator-framework/operator-sdk/pkg/helm/flags"
    25  	"github.com/operator-framework/operator-sdk/pkg/helm/release"
    26  	"github.com/operator-framework/operator-sdk/pkg/helm/watches"
    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  
    31  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    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  	"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
    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 runs the helm operator
    47  func Run(flags *hoflags.HelmOperatorFlags) error {
    48  	printVersion()
    49  
    50  	namespace, found := os.LookupEnv(k8sutil.WatchNamespaceEnvVar)
    51  	log = log.WithValues("Namespace", namespace)
    52  	if found {
    53  		if namespace == metav1.NamespaceAll {
    54  			log.Info("Watching all namespaces.")
    55  		} else {
    56  			log.Info("Watching single namespace.")
    57  		}
    58  	} else {
    59  		log.Info(fmt.Sprintf("%v environment variable not set. Watching all namespaces.",
    60  			k8sutil.WatchNamespaceEnvVar))
    61  		namespace = metav1.NamespaceAll
    62  	}
    63  
    64  	cfg, err := config.GetConfig()
    65  	if err != nil {
    66  		log.Error(err, "Failed to get config.")
    67  		return err
    68  	}
    69  	mgr, err := manager.New(cfg, manager.Options{
    70  		Namespace: namespace,
    71  	})
    72  	if err != nil {
    73  		log.Error(err, "Failed to create a new manager.")
    74  		return err
    75  	}
    76  
    77  	watches, err := watches.Load(flags.WatchesFile)
    78  	if err != nil {
    79  		log.Error(err, "Failed to create new manager factories.")
    80  		return err
    81  	}
    82  
    83  	for _, w := range watches {
    84  		// Register the controller with the factory.
    85  		err := controller.Add(mgr, controller.WatchOptions{
    86  			Namespace:               namespace,
    87  			GVK:                     w.GroupVersionKind,
    88  			ManagerFactory:          release.NewManagerFactory(mgr, w.ChartDir),
    89  			ReconcilePeriod:         flags.ReconcilePeriod,
    90  			WatchDependentResources: w.WatchDependentResources,
    91  		})
    92  		if err != nil {
    93  			log.Error(err, "Failed to add manager factory to controller.")
    94  			return err
    95  		}
    96  	}
    97  
    98  	operatorName, err := k8sutil.GetOperatorName()
    99  	if err != nil {
   100  		log.Error(err, "Failed to get operator name")
   101  		return err
   102  	}
   103  	// Become the leader before proceeding
   104  	err = leader.Become(context.TODO(), operatorName+"-lock")
   105  	if err != nil {
   106  		log.Error(err, "Failed to become leader.")
   107  		return err
   108  	}
   109  
   110  	// Start the Cmd
   111  	if err = mgr.Start(signals.SetupSignalHandler()); err != nil {
   112  		log.Error(err, "Manager exited non-zero.")
   113  		os.Exit(1)
   114  	}
   115  	return nil
   116  }