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

Commit

Permalink
feat: allow overriding env vars in flows (#62)
Browse files Browse the repository at this point in the history
* finish override

* refactor apply env var overrides

* remove logs

* revert main

* remove logs

* improve env var names

* nix flake update

* update go mod 2 nix

* remove replace

* update go mod 2 nix

* update go libraries

* update go libraries

* gomod2nix
  • Loading branch information
tedim52 authored Oct 18, 2024
1 parent e397c2e commit d63c2ba
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 30 deletions.
24 changes: 12 additions & 12 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20240814135254-a07
github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20240814135254-a0724da5a551/go.mod h1:Zlbpg7q5pTUaddoMN9zaraCNvvuD4KJNPPC8WyUF8DI=
github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20240814140453-975ebac24b93 h1:hijv6grblylZS82qnFei4L3t/60MD0cPVV0XDrHUL9I=
github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20240814140453-975ebac24b93/go.mod h1:Zlbpg7q5pTUaddoMN9zaraCNvvuD4KJNPPC8WyUF8DI=
github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20241003172041-a1632d5aecd8 h1:I/XvDVzAUvSAo586Qy/ioFfnyk8lXIsYGwmyeoE08W0=
github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20241003172041-a1632d5aecd8/go.mod h1:dQ+ZYcpSrex3FlfyYAvGuhIFHim+oJuJvslNp8rwuFo=
github.com/kurtosis-tech/kardinal/libs/manager-kontrol-api v0.0.0-20240814135254-a0724da5a551 h1:D+ct39Xc/rE4nvgyueIZyc44dFm+Gyp3C9daAjlxjIw=
github.com/kurtosis-tech/kardinal/libs/manager-kontrol-api v0.0.0-20240814135254-a0724da5a551/go.mod h1:lj6oLhpXgnc9ZaulV7jysgRaeZUDPK6XiM2TxpcGRqM=
github.com/kurtosis-tech/kardinal/libs/manager-kontrol-api v0.0.0-20240814140453-975ebac24b93 h1:s5cXvxg52er5nMCy+9v9wR+4nv+CxHOzXwIf2q2vRA0=
Expand Down
14 changes: 12 additions & 2 deletions kontrol-service/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,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
48 changes: 47 additions & 1 deletion kontrol-service/engine/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/samber/lo"
"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 @@ -52,6 +51,9 @@ func GenerateProdDevCluster(baseClusterTopologyMaybeWithTemplateOverrides *resol
clonedWorkloadSpec := workloadSpec.DeepCopy()

// TODO: find a better way to update deploymentSpec, this assumes there is only container in the pod
clonedWorkloadSpec.DeploymentSpec.Template.Spec.Containers[0].Image = item.Image
clonedWorkloadSpec.DeploymentSpec.Template.Spec.Containers[0].Env = applyEnvVarOverrides(item.EnvVarOverrides, item.SecretEnvVarOverrides, clonedWorkloadSpec.DeploymentSpec.Template.Spec.Containers[0].Env)

clonedWorkloadSpec.GetTemplateSpec().Containers[0].Image = item.Image

patches = append(patches, flow_spec.ServicePatch{
Expand All @@ -73,6 +75,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 envVarName, envVarVal := range envVarOverrides {
translatedOverrides[envVarName] = corev1.EnvVar{
Name: envVarName,
Value: envVarVal,
ValueFrom: nil,
}
}

for envVarName, envVarValue := range secretEnvVarOverrides {
translatedOverrides[envVarName] = corev1.EnvVar{
Name: envVarName,
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: envVarValue}, // assume v is the secret name
Key: envVarName,
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,
deploymentConfigs []apitypes.DeploymentConfig,
Expand Down
6 changes: 2 additions & 4 deletions kontrol-service/go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
module kardinal.kontrol-service

go 1.22.0

toolchain go1.22.3
go 1.22.3

require (
github.com/DATA-DOG/go-sqlmock v1.5.2
github.com/dominikbraun/graph v0.23.0
github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20241003172041-a1632d5aecd8
github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20241018141142-4fcbab801cec
github.com/kurtosis-tech/kardinal/libs/manager-kontrol-api v0.0.0-20241003172041-a1632d5aecd8
github.com/kurtosis-tech/stacktrace v0.0.0-20211028211901-1c67a77b5409
github.com/labstack/echo/v4 v4.12.0
Expand Down
4 changes: 2 additions & 2 deletions kontrol-service/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ 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-20241003172041-a1632d5aecd8 h1:I/XvDVzAUvSAo586Qy/ioFfnyk8lXIsYGwmyeoE08W0=
github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20241003172041-a1632d5aecd8/go.mod h1:dQ+ZYcpSrex3FlfyYAvGuhIFHim+oJuJvslNp8rwuFo=
github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20241018141142-4fcbab801cec h1:YoOp+25EgKbVrM+CVro9PoGqRidB+BY7Xo6rqGTguZ0=
github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api v0.0.0-20241018141142-4fcbab801cec/go.mod h1:dQ+ZYcpSrex3FlfyYAvGuhIFHim+oJuJvslNp8rwuFo=
github.com/kurtosis-tech/kardinal/libs/manager-kontrol-api v0.0.0-20241003172041-a1632d5aecd8 h1:+7d/CazuTvkgdY+ppb+vEjvvq0Un0qbUYfnlt3atAKo=
github.com/kurtosis-tech/kardinal/libs/manager-kontrol-api v0.0.0-20241003172041-a1632d5aecd8/go.mod h1:uUIEjxgSYw58hJgD1AwGOBE3LGPwLDiYtNmLGmnO8vI=
github.com/kurtosis-tech/stacktrace v0.0.0-20211028211901-1c67a77b5409 h1:YQTATifMUwZEtZYb0LVA7DK2pj8s71iY8rzweuUQ5+g=
Expand Down
4 changes: 2 additions & 2 deletions kontrol-service/gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ schema = 3
version = "v0.2.0"
hash = "sha256-fadcWxZOORv44oak3jTxm6YcITcFxdGt4bpn869HxUE="
[mod."github.com/kurtosis-tech/kardinal/libs/cli-kontrol-api"]
version = "v0.0.0-20241003172041-a1632d5aecd8"
hash = "sha256-UhlNzM0NsEB3YStUvT6KNWMKsw7bQvvhFSdxiVryG+s="
version = "v0.0.0-20241018141142-4fcbab801cec"
hash = "sha256-xTRlIXQhn2n0pmWfjg34Fhb3Fo5cRGUolkiaARa6f14="
[mod."github.com/kurtosis-tech/kardinal/libs/manager-kontrol-api"]
version = "v0.0.0-20241003172041-a1632d5aecd8"
hash = "sha256-jJPe/rGOg0G1D64rOWk303FF3+uYS5vHfs3lOXhR4tQ="
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
}

0 comments on commit d63c2ba

Please sign in to comment.