Skip to content

Commit

Permalink
Support dependent env vars (#390)
Browse files Browse the repository at this point in the history
* Expand dependent env variables from double curly braces {{ENV_VAR_NAME}} to $(ENV_VAR_NAME)
  • Loading branch information
marcinc authored Mar 4, 2021
1 parent 98d599f commit 04e26de
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
17 changes: 16 additions & 1 deletion docs/reference/config-params.md
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ All traffic on the port you specify will be forwarded to the service allowing an

Again, it is ideal for exposing a service or app to the internet under a single IP address.

Practically, in non development environments, a LoadBalancer will be used to route traffic to an Ingress to expose multiple services under the same IP address and keep your costs down.
Practically, in non development environments, a LoadBalancer will be used to route traffic to an Ingress to expose multiple services under the same IP address and keep your costs down.


## kev.service.nodeport.port
Expand Down Expand Up @@ -842,6 +842,21 @@ services:
ENV_VAR_A: some-literal-value # Literal value
```
When there is a need to reference any dependent environment variables it can be achieved by using double curly braces
> Environment variable with as literal string referencing dependent environment variables:
```yaml
version: 3.7
services:
my-service:
labels:
...
environment:
ENV_VAR_A: foo
ENV_VAR_B: bar
ENV_VAR_C: {{ENV_VAR_A}}/{{ENV_VAR_B}} # referencing other dependent environment variables
```
## Reference K8s secret key value
To set an environment variable with a value taken from Kubernetes secret, use the following shortcut: `secret.{secret-name}.{secret-key}`.
Expand Down
25 changes: 21 additions & 4 deletions pkg/kev/converter/kubernetes/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,7 @@ func (k *Kubernetes) configPVCVolumeSource(name string, readonly bool) *v1.Volum
// @orig: https://github.com/kubernetes/kompose/blob/master/pkg/transformer/kubernetes/kubernetes.go#L961
func (k *Kubernetes) configEnvs(projectService ProjectService) ([]v1.EnvVar, error) {
envs := EnvSort{}
envsWithDeps := []v1.EnvVar{}

// @step load up the environment variables
for k, v := range projectService.environment() {
Expand Down Expand Up @@ -1347,17 +1348,33 @@ func (k *Kubernetes) configEnvs(projectService ProjectService) ([]v1.EnvVar, err
}, "Unsupported Container resource reference: %s", resource)
}
default:
envs = append(envs, v1.EnvVar{
Name: k,
Value: *v,
})
if strings.Contains(*v, "{{") && strings.Contains(*v, "}}") {
*v = strings.ReplaceAll(*v, "{{", "$(")
*v = strings.ReplaceAll(*v, "}}", ")")

envsWithDeps = append(envsWithDeps, v1.EnvVar{
Name: k,
Value: *v,
})
} else {
envs = append(envs, v1.EnvVar{
Name: k,
Value: *v,
})
}
}
}

// Stable sorts data while keeping the original order of equal elements
// we need this because envs are not populated in any random order
// this sorting ensures they are populated in a particular order
sort.Stable(envs)

// append dependent env variables at the end to ensure proper expansion in K8s
for _, de := range envsWithDeps {
envs = append(envs, de)
}

return envs, nil
}

Expand Down
19 changes: 19 additions & 0 deletions pkg/kev/converter/kubernetes/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,25 @@ var _ = Describe("Transform", func() {
})
})

Context("for env dependend vars containing double curly braces e.g. {{OTHER_ENV_VAR_NAME}} ", func() {

secretRef := "postgres://{{USER}}:{{PASS}}@{{HOST}}:{{PORT}}/{{DB}}"

BeforeEach(func() {
projectService.Environment = composego.MappingWithEquals{
"MY_SECRET": &secretRef,
}
})

It("expands that env variable value to reference dependet variables", func() {
vars, err := k.configEnvs(projectService)

Expect(vars[0].Value).To(Equal("postgres://$(USER):$(PASS)@$(HOST):$(PORT)/$(DB)"))
Expect(err).ToNot(HaveOccurred())
})

})

Context("for env vars with symbolic values", func() {

Context("as secret.secret-name.secret-key", func() {
Expand Down

0 comments on commit 04e26de

Please sign in to comment.