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

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/kyma-project/control-plane/components/schema-migrator/cleaner"
     8  	"github.com/kyma-project/kyma-environment-broker/internal/broker"
     9  	"github.com/kyma-project/kyma-environment-broker/internal/cis"
    10  	"github.com/kyma-project/kyma-environment-broker/internal/events"
    11  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    12  
    13  	"github.com/sirupsen/logrus"
    14  	"github.com/vrischmann/envconfig"
    15  )
    16  
    17  type Config struct {
    18  	ClientVersion string
    19  	CIS           cis.Config
    20  	Database      storage.Config
    21  	Broker        broker.ClientConfig
    22  }
    23  
    24  func main() {
    25  	time.Sleep(20 * time.Second)
    26  
    27  	// create context
    28  	ctx, cancel := context.WithCancel(context.Background())
    29  	defer cancel()
    30  
    31  	// create and fill config
    32  	var cfg Config
    33  	err := envconfig.InitWithPrefix(&cfg, "APP")
    34  	fatalOnError(err)
    35  
    36  	// create logs
    37  	logs := logrus.New()
    38  	logs.SetFormatter(&logrus.JSONFormatter{})
    39  
    40  	// create CIS client
    41  	var client cis.CisClient
    42  	switch cfg.ClientVersion {
    43  	case "v1.0":
    44  		client = cis.NewClientVer1(ctx, cfg.CIS, logs)
    45  	case "v2.0":
    46  		client = cis.NewClient(ctx, cfg.CIS, logs)
    47  	default:
    48  		logs.Fatalf("Client version %s is not supported", cfg.ClientVersion)
    49  	}
    50  
    51  	// create storage connection
    52  	cipher := storage.NewEncrypter(cfg.Database.SecretKey)
    53  	db, conn, err := storage.NewFromConfig(cfg.Database, events.Config{}, cipher, logs.WithField("service", "storage"))
    54  	fatalOnError(err)
    55  
    56  	// create broker client
    57  	brokerClient := broker.NewClient(ctx, cfg.Broker)
    58  	brokerClient.UserAgent = broker.AccountCleanupJob
    59  
    60  	// create SubAccountCleanerService and execute process
    61  	sacs := cis.NewSubAccountCleanupService(client, brokerClient, db.Instances(), logs)
    62  	fatalOnError(sacs.Run())
    63  
    64  	// do not use defer, close must be done before halting
    65  	err = conn.Close()
    66  	if err != nil {
    67  		fatalOnError(err)
    68  	}
    69  
    70  	cleaner.HaltIstioSidecar()
    71  	err = cleaner.Halt()
    72  	fatalOnError(err)
    73  
    74  	time.Sleep(5 * time.Second)
    75  }
    76  
    77  func fatalOnError(err error) {
    78  	if err != nil {
    79  		logrus.Fatal(err)
    80  	}
    81  }