Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
refactor apply env var overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
tedim52 committed Oct 7, 2024
1 parent ea1b3c3 commit cd23c46
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 49 deletions.
6 changes: 4 additions & 2 deletions kontrol-service/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
76 changes: 46 additions & 30 deletions kontrol-service/engine/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand Down
36 changes: 19 additions & 17 deletions kontrol-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down

0 comments on commit cd23c46

Please sign in to comment.