Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

allow env vars for all exec units from config #152

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ type (
}

ExecutionUnit struct {
Type string `json:"type" yaml:"type" toml:"type"`
NetworkPlacement string `json:"network_placement,omitempty" yaml:"network_placement,omitempty" toml:"network_placement,omitempty"`
HelmChartOptions *HelmChartOptions `json:"helm_chart_options,omitempty" yaml:"helm_chart_options,omitempty" toml:"helm_chart_options,omitempty"`
InfraParams InfraParams `json:"pulumi_params,omitempty" yaml:"pulumi_params,omitempty" toml:"pulumi_params,omitempty"`
Type string `json:"type" yaml:"type" toml:"type"`
NetworkPlacement string `json:"network_placement,omitempty" yaml:"network_placement,omitempty" toml:"network_placement,omitempty"`
EnvironmentVariables map[string]string `json:"environment_variables,omitempty" yaml:"environment_variables,omitempty" toml:"environment_variables,omitempty"`
HelmChartOptions *HelmChartOptions `json:"helm_chart_options,omitempty" yaml:"helm_chart_options,omitempty" toml:"helm_chart_options,omitempty"`
InfraParams InfraParams `json:"pulumi_params,omitempty" yaml:"pulumi_params,omitempty" toml:"pulumi_params,omitempty"`
}

// A HelmChartOptions represents configuration for execution units attempting to generate helm charts
Expand Down Expand Up @@ -153,6 +154,10 @@ func (cfg *ExecutionUnit) Merge(other ExecutionUnit) {
if other.NetworkPlacement == "" {
cfg.NetworkPlacement = "private"
}
cfg.EnvironmentVariables = other.EnvironmentVariables
if cfg.EnvironmentVariables == nil {
cfg.EnvironmentVariables = make(map[string]string)
}
cfg.HelmChartOptions = other.HelmChartOptions
cfg.InfraParams.Merge(other.InfraParams)
}
Expand Down
10 changes: 9 additions & 1 deletion pkg/exec_unit/plugin_exec_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ func (p ExecUnitPlugin) Transform(result *core.CompilationResult, deps *core.Dep
Name: "main",
Executable: core.NewExecutable(),
}
unit.ExecType = p.Config.GetExecutionUnit(unit.Name).Type
cfg := p.Config.GetExecutionUnit(unit.Name)
unit.ExecType = cfg.Type

for key, value := range cfg.EnvironmentVariables {
unit.EnvironmentVariables = append(unit.EnvironmentVariables, core.EnvironmentVariable{
Name: key,
Value: value,
})
}

for _, f := range inputR.(*core.InputFiles).Files() {
if _, ok := f.(*core.SourceFile); ok {
Expand Down
85 changes: 85 additions & 0 deletions pkg/exec_unit/plugin_exec_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package execunit

import (
"strings"
"testing"

"github.com/klothoplatform/klotho/pkg/config"
"github.com/klothoplatform/klotho/pkg/core"
"github.com/stretchr/testify/assert"
)

func Test_environmentVarsAddedToUnit(t *testing.T) {
tests := []struct {
name string
envVars map[string]string
want []core.EnvironmentVariable
wantExecUnit bool
}{
{
name: "no exec unit",
envVars: map[string]string{
"key1": "value1",
"key2": "value2",
},
wantExecUnit: false,
},
{
name: "add env vars",
envVars: map[string]string{
"key1": "value1",
"key2": "value2",
},
want: []core.EnvironmentVariable{
{
Name: "key1",
Value: "value1",
},
{
Name: "key2",
Value: "value2",
},
},
wantExecUnit: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)

cfg := config.Application{
ExecutionUnits: map[string]*config.ExecutionUnit{
"main": {EnvironmentVariables: tt.envVars},
},
}
p := ExecUnitPlugin{Config: &cfg}
result := &core.CompilationResult{}

inputFiles := &core.InputFiles{}
if tt.wantExecUnit {
f, err := core.NewSourceFile("test", strings.NewReader("test"), testAnnotationLang)
if assert.Nil(err) {
inputFiles.Add(f)
}
} else {
inputFiles.Add(&core.FileRef{
FPath: "test",
})
}

result.Add(inputFiles)
err := p.Transform(result, &core.Dependencies{})
if !assert.NoError(err) {
return
}
units := core.GetResourcesOfType[*core.ExecutionUnit](result)
if tt.wantExecUnit {
assert.Len(units, 1)
assert.ElementsMatch(tt.want, units[0].EnvironmentVariables)
} else {
assert.Len(units, 0)
}

})
}
}
8 changes: 6 additions & 2 deletions pkg/infra/pulumi_aws/deploylib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,12 @@ export class CloudCCLib {

if (env_vars) {
for (const v of env_vars) {
const result = this.getEnvVarForDependency(v)
additionalEnvVars[result[0]] = result[1]
if (v.Kind == '') {
additionalEnvVars[v.Name] = v.Value
} else {
const result = this.getEnvVarForDependency(v)
additionalEnvVars[result[0]] = result[1]
}
}
}

Expand Down