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

feat: allow overriding env vars in flows #62

Merged
merged 15 commits into from
Oct 18, 2024
14 changes: 12 additions & 2 deletions kontrol-service/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,19 @@ func (sv *Server) PostTenantUuidFlowCreate(_ context.Context, request api.PostTe

patches := []flow_spec.ServicePatchSpec{}
for _, serviceUpdate := range serviceUpdates {
envVarOverrides := map[string]string{}
if serviceUpdate.EnvVarOverrides != nil {
envVarOverrides = *serviceUpdate.EnvVarOverrides
}
secretEnvVarOverrides := map[string]string{}
if serviceUpdate.SecretEnvVarOverrides != nil {
secretEnvVarOverrides = *serviceUpdate.SecretEnvVarOverrides
}
patch := flow_spec.ServicePatchSpec{
Service: serviceUpdate.ServiceName,
Image: serviceUpdate.ImageLocator,
Service: serviceUpdate.ServiceName,
Image: serviceUpdate.ImageLocator,
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
46 changes: 45 additions & 1 deletion kontrol-service/engine/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"

corev1 "k8s.io/api/core/v1"
net "k8s.io/api/networking/v1"
gateway "sigs.k8s.io/gateway-api/apis/v1"
Expand Down Expand Up @@ -50,6 +49,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
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 @@ -70,6 +70,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 {
tedim52 marked this conversation as resolved.
Show resolved Hide resolved
translatedOverrides[k] = corev1.EnvVar{
Name: k,
Value: v,
ValueFrom: nil,
}
}

for k, v := range secretEnvVarOverrides {
tedim52 marked this conversation as resolved.
Show resolved Hide resolved
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
2 changes: 2 additions & 0 deletions kontrol-service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.22.0

toolchain go1.22.3

replace github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api => ../../kardinal/libs/cli-kontrol-api

require (
github.com/DATA-DOG/go-sqlmock v1.5.2
github.com/dominikbraun/graph v0.23.0
Expand Down
2 changes: 0 additions & 2 deletions kontrol-service/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20240930151959-22270454aa6b h1:pJlgnXZCdSVl1ts0gDagES1TRWXzA/WOrKjl0SCSES0=
github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20240930151959-22270454aa6b/go.mod h1:dQ+ZYcpSrex3FlfyYAvGuhIFHim+oJuJvslNp8rwuFo=
github.com/kurtosis-tech/kardinal/libs/manager-kontrol-api v0.0.0-20240926004715-b5a991623aeb h1:rMT8EvgX+dFL0Aq2Ea1lBeIFxCPHPDSF59FktBnKhRs=
github.com/kurtosis-tech/kardinal/libs/manager-kontrol-api v0.0.0-20240926004715-b5a991623aeb/go.mod h1:uUIEjxgSYw58hJgD1AwGOBE3LGPwLDiYtNmLGmnO8vI=
github.com/kurtosis-tech/stacktrace v0.0.0-20211028211901-1c67a77b5409 h1:YQTATifMUwZEtZYb0LVA7DK2pj8s71iY8rzweuUQ5+g=
Expand Down
9 changes: 4 additions & 5 deletions kontrol-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ 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"
"strconv"
)

func main() {
Expand Down
6 changes: 4 additions & 2 deletions kontrol-service/types/flow_spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ type FlowPatchSpec struct {
}

type ServicePatchSpec struct {
Service string
Image string
Service string
Image string
EnvVarOverrides map[string]string
SecretEnvVarOverrides map[string]string
}