github.com/kyma-project/kyma-environment-broker@v0.0.1/cmd/runtimereconciler/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"log"
     6  	"time"
     7  
     8  	btpmanager "github.com/kyma-project/kyma-environment-broker/internal/btpmanager/credentials"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/events"
    10  	"github.com/kyma-project/kyma-environment-broker/internal/process/input"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/provisioner"
    12  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    13  	"github.com/sirupsen/logrus"
    14  	"github.com/vrischmann/envconfig"
    15  	"sigs.k8s.io/controller-runtime/pkg/client"
    16  	"sigs.k8s.io/controller-runtime/pkg/client/config"
    17  )
    18  
    19  type Config struct {
    20  	Database                             storage.Config
    21  	Events                               events.Config
    22  	Provisioner                          input.Config
    23  	DryRun                               bool   `envconfig:"default=true"`
    24  	BtpManagerSecretWatcherAddr          string `envconfig:"default=0"`
    25  	BtpManagerSecretWatcherComponentName string `envconfig:"default=NA"`
    26  	WatcherEnabled                       bool   `envconfig:"default=false"`
    27  	JobEnabled                           bool   `envconfig:"default=false"`
    28  	JobInterval                          int    `envconfig:"default=24"`
    29  	JobReconciliationDelay               string `envconfig:"default=0s"`
    30  }
    31  
    32  func main() {
    33  	ctx, cancel := context.WithCancel(context.Background())
    34  	defer cancel()
    35  
    36  	logs := logrus.New()
    37  	logs.SetFormatter(&logrus.JSONFormatter{})
    38  
    39  	logs.Info("runtime-reconciler started")
    40  	logs.Info("runtime-reconciler debug version: 1")
    41  
    42  	var cfg Config
    43  	err := envconfig.InitWithPrefix(&cfg, "RUNTIME_RECONCILER")
    44  	fatalOnError(err)
    45  	logs.Info("runtime-reconciler config loaded")
    46  	if !cfg.JobEnabled && !cfg.WatcherEnabled {
    47  		logs.Info("both job and listener are disabled, module stopped.")
    48  		return
    49  	}
    50  	logs.Infof("runtime-listener runing as dry run? %t", cfg.DryRun)
    51  
    52  	cipher := storage.NewEncrypter(cfg.Database.SecretKey)
    53  
    54  	db, _, err := storage.NewFromConfig(cfg.Database, cfg.Events, cipher, logs.WithField("service", "storage"))
    55  	fatalOnError(err)
    56  	logs.Info("runtime-reconciler connected to database")
    57  
    58  	kcpK8sConfig, err := config.GetConfig()
    59  	fatalOnError(err)
    60  	kcpK8sClient, err := client.New(kcpK8sConfig, client.Options{})
    61  	fatalOnError(err)
    62  
    63  	provisionerClient := provisioner.NewProvisionerClient(cfg.Provisioner.URL, false)
    64  
    65  	btpOperatorManager := btpmanager.NewManager(ctx, kcpK8sClient, db.Instances(), logs, cfg.DryRun, provisionerClient)
    66  
    67  	logs.Infof("job enabled? %t", cfg.JobEnabled)
    68  	if cfg.JobEnabled {
    69  		btpManagerCredentialsJob := btpmanager.NewJob(btpOperatorManager, logs)
    70  		logs.Infof("runtime-reconciler created job every %d m", cfg.JobInterval)
    71  
    72  		jobReconciliationDelay, _ := time.ParseDuration(cfg.JobReconciliationDelay)
    73  		btpManagerCredentialsJob.Start(cfg.JobInterval, jobReconciliationDelay)
    74  	}
    75  
    76  	logs.Infof("watcher enabled? %t", cfg.WatcherEnabled)
    77  	if cfg.WatcherEnabled {
    78  		btpManagerCredentialsWatcher := btpmanager.NewWatcher(ctx, cfg.BtpManagerSecretWatcherAddr, cfg.BtpManagerSecretWatcherComponentName, btpOperatorManager, logs)
    79  		logs.Infof("runtime-reconciler created watcher %s on %s", cfg.BtpManagerSecretWatcherComponentName, cfg.BtpManagerSecretWatcherAddr)
    80  		go btpManagerCredentialsWatcher.ReactOnSkrEvent()
    81  	}
    82  
    83  	<-ctx.Done()
    84  }
    85  
    86  func fatalOnError(err error) {
    87  	if err != nil {
    88  		log.Fatal(err)
    89  	}
    90  }