github.com/nais/knorten@v0.0.0-20240104110906-55926958e361/main.go (about) 1 package main 2 3 import ( 4 "context" 5 "flag" 6 "fmt" 7 "os" 8 "time" 9 10 "github.com/nais/knorten/pkg/api" 11 "github.com/nais/knorten/pkg/api/auth" 12 "github.com/nais/knorten/pkg/database" 13 "github.com/nais/knorten/pkg/events" 14 "github.com/nais/knorten/pkg/helm" 15 "github.com/nais/knorten/pkg/imageupdater" 16 "github.com/sirupsen/logrus" 17 ) 18 19 const ( 20 imageUpdaterFrequency = 24 * time.Hour 21 ) 22 23 type Config struct { 24 auth.OauthConfig 25 26 DBConnString string 27 DBEncKey string 28 DryRun bool 29 InCluster bool 30 GCPProject string 31 GCPRegion string 32 GCPZone string 33 AirflowChartVersion string 34 JupyterChartVersion string 35 AdminGroup string 36 SessionKey string 37 } 38 39 func main() { 40 log := logrus.New() 41 log.SetFormatter(&logrus.JSONFormatter{}) 42 43 cfg := Config{} 44 flag.StringVar(&cfg.ClientID, "oauth2-client-id", os.Getenv("AZURE_APP_CLIENT_ID"), "Client ID for azure app") 45 flag.StringVar(&cfg.ClientSecret, "oauth2-client-secret", os.Getenv("AZURE_APP_CLIENT_SECRET"), "Client secret for azure app") 46 flag.StringVar(&cfg.TenantID, "oauth2-tenant-id", os.Getenv("AZURE_APP_TENANT_ID"), "OAuth2 tenant ID") 47 flag.StringVar(&cfg.DBConnString, "db-conn-string", os.Getenv("DB_CONN_STRING"), "Database connection string") 48 flag.StringVar(&cfg.DBEncKey, "db-enc-key", os.Getenv("DB_ENC_KEY"), "Chart value encryption key") 49 flag.BoolVar(&cfg.DryRun, "dry-run", false, "Don't run external commands") 50 flag.BoolVar(&cfg.InCluster, "in-cluster", true, "In cluster configuration for go client") 51 flag.StringVar(&cfg.GCPProject, "project", os.Getenv("GCP_PROJECT"), "GCP project") 52 flag.StringVar(&cfg.GCPRegion, "region", os.Getenv("GCP_REGION"), "GCP region") 53 flag.StringVar(&cfg.GCPZone, "zone", os.Getenv("GCP_ZONE"), "GCP zone") 54 flag.StringVar(&cfg.AirflowChartVersion, "airflow-chart-version", os.Getenv("AIRFLOW_CHART_VERSION"), "The chart version for airflow") 55 flag.StringVar(&cfg.JupyterChartVersion, "jupyter-chart-version", os.Getenv("JUPYTER_CHART_VERSION"), "The chart version for jupyter") 56 flag.StringVar(&cfg.AdminGroup, "admin-group", os.Getenv("ADMIN_GROUP"), "Email of admin group used to authenticate Knorten administrators") 57 flag.StringVar(&cfg.SessionKey, "session-key", os.Getenv("SESSION_KEY"), "The session key for Knorten") 58 flag.Parse() 59 60 dbClient, err := database.New(fmt.Sprintf("%v?sslmode=disable", cfg.DBConnString), cfg.DBEncKey, log.WithField("subsystem", "db")) 61 if err != nil { 62 log.WithError(err).Fatal("setting up database") 63 return 64 } 65 66 azureClient, err := auth.NewAzureClient(cfg.DryRun, cfg.ClientID, cfg.ClientSecret, cfg.TenantID, log.WithField("subsystem", "auth")) 67 if err != nil { 68 log.WithError(err).Fatal("creating azure client") 69 return 70 } 71 72 if !cfg.DryRun { 73 imageUpdater := imageupdater.NewClient(dbClient, log.WithField("subsystem", "imageupdater")) 74 go imageUpdater.Run(imageUpdaterFrequency) 75 76 if err := helm.UpdateHelmRepositories(); err != nil { 77 log.WithError(err).Fatal("updating helm repositories") 78 } 79 } 80 81 eventHandler, err := events.NewHandler(context.Background(), dbClient, azureClient, cfg.GCPProject, cfg.GCPRegion, cfg.GCPZone, cfg.AirflowChartVersion, cfg.JupyterChartVersion, cfg.DryRun, cfg.InCluster, log.WithField("subsystem", "events")) 82 if err != nil { 83 log.WithError(err).Fatal("starting event watcher") 84 return 85 } 86 eventHandler.Run(10 * time.Second) 87 88 router, err := api.New(dbClient, azureClient, cfg.DryRun, cfg.SessionKey, cfg.AdminGroup, cfg.GCPProject, cfg.GCPZone, log.WithField("subsystem", "api")) 89 if err != nil { 90 log.WithError(err).Fatal("creating api") 91 return 92 } 93 94 err = api.Run(router, cfg.InCluster) 95 if err != nil { 96 return 97 } 98 }