Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vars overriden by VarsFrom #1096

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func Test_000090_varsfrom_override_and_controlled_outputs_test(t *testing.T) {
expectedOutputValue := map[string]string{
"Name": "tf-output-" + terraformName,
"Namespace": "flux-system",
"Value": "Hello, my overridden cat!",
"Value": "Hello, my cat!",
"OwnerRef[0]": string(createdHelloWorldTF.UID),
}
g.Eventually(func() (map[string]string, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ Inline variables can be set using `vars`. The `varsFrom` field accepts a list of
You may use the `varsKeys` property of `varsFrom` to select specific keys from the input or omit this field
to select all keys from the input source.

Note that in the case of the same variable key being passed multiple times, the controller will use
the lattermost instance of the key passed to `varsFrom`.
Values from `varsFrom` are always read first and then values from `vars` are read. If the same key is used in `varsFrom`
as in `vars`, the value from `varsFrom` will be overwritten. This mechanism allows us to have the global variables set
once in one of the ConfigMaps or Secrets, and if necessary we can overwrite the value directly in the Terraform object.

```yaml hl_lines="15-20 22-28"
apiVersion: infra.contrib.fluxcd.io/v1alpha2
Expand Down
14 changes: 7 additions & 7 deletions runner/server_generate_tf_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,6 @@ func (r *TerraformRunnerServer) GenerateVarsForTF(ctx context.Context, req *Gene
vars["values"] = &apiextensionsv1.JSON{Raw: buf.Bytes()}
}

log.Info("mapping the Spec.Vars")
if len(terraform.Spec.Vars) > 0 {
for _, v := range terraform.Spec.Vars {
vars[v.Name] = v.Value
}
}

log.Info("mapping the Spec.VarsFrom")
// varsFrom overwrite vars
for _, vf := range terraform.Spec.VarsFrom {
Expand Down Expand Up @@ -344,6 +337,13 @@ func (r *TerraformRunnerServer) GenerateVarsForTF(ctx context.Context, req *Gene
}
}

log.Info("mapping the Spec.Vars")
if len(terraform.Spec.Vars) > 0 {
for _, v := range terraform.Spec.Vars {
vars[v.Name] = v.Value
}
}

jsonBytes, err := json2.Marshal(vars)
if err != nil {
log.Error(err, "unable to marshal the data")
Expand Down