github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/ctrlcrd/controller.go (about)

     1  package ctrlcrd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	boomv1 "github.com/caos/orbos/internal/api/boom/v1"
     7  	networkingv1 "github.com/caos/orbos/internal/api/networking/v1"
     8  	"github.com/caos/orbos/internal/ctrlcrd/boom"
     9  	"github.com/caos/orbos/internal/ctrlcrd/networking"
    10  	"github.com/caos/orbos/internal/utils/clientgo"
    11  	"github.com/caos/orbos/mntr"
    12  	"github.com/caos/orbos/pkg/kubernetes"
    13  	"k8s.io/apimachinery/pkg/runtime"
    14  	clientgoscheme "k8s.io/client-go/kubernetes/scheme"
    15  	ctrl "sigs.k8s.io/controller-runtime"
    16  )
    17  
    18  const (
    19  	Networking = "networking"
    20  	Boom       = "boom"
    21  )
    22  
    23  var (
    24  	scheme = runtime.NewScheme()
    25  )
    26  
    27  func init() {
    28  	if err := clientgoscheme.AddToScheme(scheme); err != nil {
    29  		panic(fmt.Errorf("adding clientgo to scheme failed: %w", err))
    30  	}
    31  
    32  	if err := networkingv1.AddToScheme(scheme); err != nil {
    33  		panic(fmt.Errorf("adding networking v1 to scheme failed: %w", err))
    34  	}
    35  
    36  	if err := boomv1.AddToScheme(scheme); err != nil {
    37  		panic(fmt.Errorf("adding boom v1 to scheme failed: %w", err))
    38  	}
    39  
    40  }
    41  
    42  func Start(monitor mntr.Monitor, version, toolsDirectoryPath, metricsAddr string, kubeconfig string, features ...string) error {
    43  
    44  	cfg, err := clientgo.GetClusterConfig(monitor, kubeconfig)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	k8sClient, err := kubernetes.NewK8sClientWithConfig(monitor, cfg)
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	monitor.Info("successfully connected to kubernetes cluster")
    55  
    56  	mgr, err := ctrl.NewManager(cfg, ctrl.Options{
    57  		Scheme:             scheme,
    58  		MetricsBindAddress: metricsAddr,
    59  		Port:               9443,
    60  		LeaderElection:     false,
    61  		LeaderElectionID:   "98jasd12l.caos.ch",
    62  	})
    63  	if err != nil {
    64  		return fmt.Errorf("starting manager failed: %w", err)
    65  	}
    66  
    67  	for _, feature := range features {
    68  		switch feature {
    69  		case Networking:
    70  			monitor.Debug("Setting up networking")
    71  			if err = (&networking.Reconciler{
    72  				ClientInt: k8sClient,
    73  				Monitor:   monitor,
    74  				Scheme:    mgr.GetScheme(),
    75  				Version:   version,
    76  			}).SetupWithManager(mgr); err != nil {
    77  				return fmt.Errorf("creating controller failed: %w", err)
    78  			}
    79  			monitor.Debug("Networking setup done")
    80  		case Boom:
    81  			monitor.Debug("Setting up BOOM")
    82  			if err = (&boom.Reconciler{
    83  				ClientInt:          k8sClient,
    84  				Monitor:            monitor,
    85  				Scheme:             mgr.GetScheme(),
    86  				ToolsDirectoryPath: toolsDirectoryPath,
    87  				Version:            version,
    88  			}).SetupWithManager(mgr); err != nil {
    89  				return fmt.Errorf("creating controller failed: %w", err)
    90  			}
    91  			monitor.Debug("BOOM setup done")
    92  		}
    93  	}
    94  
    95  	monitor.Debug("Controller is starting")
    96  	if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
    97  		return fmt.Errorf("running manager failed: %w", err)
    98  	}
    99  	monitor.Debug("Controller is done")
   100  	return nil
   101  }