diff --git a/kontrol-service/api/server.go b/kontrol-service/api/server.go index 9de868e..dc81796 100644 --- a/kontrol-service/api/server.go +++ b/kontrol-service/api/server.go @@ -192,16 +192,18 @@ func (sv *Server) PostTenantUuidFlowCreate(_ context.Context, request api.PostTe envVarOverrides := map[string]string{} if serviceUpdate.EnvVarOverrides != nil { envVarOverrides = *serviceUpdate.EnvVarOverrides + logrus.Infof("ENV VAR OVERRIDES: %v", envVarOverrides) } secretEnvVarOverrides := map[string]string{} if serviceUpdate.SecretEnvVarOverrides != nil { secretEnvVarOverrides = *serviceUpdate.SecretEnvVarOverrides + logrus.Infof("SECRET ENV VAR OVERRIDES: %v", secretEnvVarOverrides) } patch := flow_spec.ServicePatchSpec{ Service: serviceUpdate.ServiceName, Image: serviceUpdate.ImageLocator, - SecretEnvVarOverrides: envVarOverrides, - EnvVarOverrides: secretEnvVarOverrides, + EnvVarOverrides: envVarOverrides, + SecretEnvVarOverrides: secretEnvVarOverrides, } logrus.Infof("starting new dev flow for service %v on image %v", patch.Service, patch.Image) patches = append(patches, patch) diff --git a/kontrol-service/engine/docker.go b/kontrol-service/engine/docker.go index c84cca6..052faf5 100644 --- a/kontrol-service/engine/docker.go +++ b/kontrol-service/engine/docker.go @@ -36,6 +36,7 @@ func GenerateProdOnlyCluster( func GenerateProdDevCluster(baseClusterTopologyMaybeWithTemplateOverrides *resolved.ClusterTopology, baseTopology *resolved.ClusterTopology, pluginRunner *plugins.PluginRunner, flowSpec flow_spec.FlowPatchSpec) (*resolved.ClusterTopology, error) { patches := []flow_spec.ServicePatch{} for _, item := range flowSpec.ServicePatches { + logrus.Infof("ITEM: %v", item) devServiceName := item.Service devService, err := baseClusterTopologyMaybeWithTemplateOverrides.GetService(devServiceName) if err != nil { @@ -49,36 +50,7 @@ func GenerateProdDevCluster(baseClusterTopologyMaybeWithTemplateOverrides *resol // TODO: find a better way to update deploymentSpec, this assumes there is only container in the pod deploymentSpec.Template.Spec.Containers[0].Image = item.Image - - // merge environment variables and env var overrides - envVarOverrides := map[string]corev1.EnvVar{} - for k, v := range item.EnvVarOverrides { - envVarOverrides[k] = corev1.EnvVar{ - Name: k, - Value: v, - ValueFrom: nil, - } - } - for k, v := range item.SecretEnvVarOverrides { - envVarOverrides[k] = corev1.EnvVar{ - Name: k, - ValueFrom: &corev1.EnvVarSource{ - SecretKeyRef: &corev1.SecretKeySelector{ - LocalObjectReference: corev1.LocalObjectReference{Name: v}, // assume v is the secret name - Key: k, - Optional: nil, - }, - }, - } - } - envVars := []corev1.EnvVar{} - for _, envVar := range deploymentSpec.Template.Spec.Containers[0].Env { - if override, ok := envVarOverrides[envVar.Name]; ok { - envVars = append(envVars, override) - } else { - envVars = append(envVars, envVar) - } - } + deploymentSpec.Template.Spec.Containers[0].Env = applyEnvVarOverrides(item.EnvVarOverrides, item.SecretEnvVarOverrides, deploymentSpec.Template.Spec.Containers[0].Env) patches = append(patches, flow_spec.ServicePatch{ Service: devServiceName, @@ -99,6 +71,50 @@ func GenerateProdDevCluster(baseClusterTopologyMaybeWithTemplateOverrides *resol return clusterTopology, nil } +func applyEnvVarOverrides( + envVarOverrides map[string]string, + secretEnvVarOverrides map[string]string, + envVars []corev1.EnvVar) []corev1.EnvVar { + // merge environment variables and env var overrides + translatedOverrides := map[string]corev1.EnvVar{} + for k, v := range envVarOverrides { + translatedOverrides[k] = corev1.EnvVar{ + Name: k, + Value: v, + ValueFrom: nil, + } + } + + for k, v := range secretEnvVarOverrides { + translatedOverrides[k] = corev1.EnvVar{ + Name: k, + ValueFrom: &corev1.EnvVarSource{ + SecretKeyRef: &corev1.SecretKeySelector{ + LocalObjectReference: corev1.LocalObjectReference{Name: v}, // assume v is the secret name + Key: k, + Optional: nil, + }, + }, + } + } + + newEnvVars := []corev1.EnvVar{} + for _, envVar := range envVars { + if override, ok := translatedOverrides[envVar.Name]; ok { + newEnvVars = append(newEnvVars, override) + delete(translatedOverrides, envVar.Name) + } else { + newEnvVars = append(newEnvVars, envVar) + } + } + + for _, envVar := range translatedOverrides { + newEnvVars = append(newEnvVars, envVar) + } + + return newEnvVars +} + func generateClusterTopology( serviceConfigs []apitypes.ServiceConfig, ingressConfigs []apitypes.IngressConfig, diff --git a/kontrol-service/main.go b/kontrol-service/main.go index 87f11df..77eb80c 100644 --- a/kontrol-service/main.go +++ b/kontrol-service/main.go @@ -2,17 +2,15 @@ package main import ( "flag" - "net/http" - "os" - "runtime/debug" - "strconv" - cli_api "github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api/api/golang/server" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" "github.com/sirupsen/logrus" "kardinal.kontrol-service/api" "kardinal.kontrol-service/database" + "net/http" + "os" + "runtime/debug" ) func main() { @@ -36,23 +34,27 @@ func main() { } func startServer(isDevMode bool) { - dbHostname := os.Getenv("DB_HOSTNAME") - dbUsername := os.Getenv("DB_USERNAME") - dbPassword := os.Getenv("DB_PASSWORD") - dbName := os.Getenv("DB_NAME") - if dbHostname == "" || dbUsername == "" || dbPassword == "" || dbName == "" { - logrus.Fatal("One of the following environment variables is not set: DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_NAME") - } - dbPort, err := strconv.Atoi(os.Getenv("DB_PORT")) - if err != nil { - logrus.Fatal("An error occurred parsing the DB port number", err) - } + //dbHostname := os.Getenv("DB_HOSTNAME") + dbHostname := "localhost" + //dbUsername := os.Getenv("DB_USERNAME") + dbUsername := "postgres" + //dbPassword := os.Getenv("DB_PASSWORD") + dbPassword := "MyPassword1!" + //dbName := os.Getenv("DB_NAME") + //if dbHostname == "" || dbUsername == "" || dbPassword == "" || dbName == "" { + // logrus.Fatal("One of the following environment variables is not set: DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_NAME") + //} + dbName := "postgres" + //dbPort, err := strconv.Atoi(os.Getenv("DB_PORT")) + //if err != nil { + // logrus.Fatal("An error occurred parsing the DB port number", err) + //} dbConnectionInfo, err := database.NewDatabaseConnectionInfo( dbUsername, dbPassword, dbHostname, - uint16(dbPort), + uint16(51940), dbName, ) if err != nil {