Fix(references/appfile): Fix namespace check and Terraform output parsing (#6915)

* fix(references/appfile): correct namespace existence check in addon

The `generateSecretFromTerraformOutput` function was using an incorrect logic to check for namespace existence. It was trying to create the namespace and if it succeeded, it would return an error.

This commit corrects the logic to use `k8sClient.Get` and checks for a `NotFound` error to accurately determine if the namespace exists.

Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com>

* fix(references/appfile): make terraform output parsing robust

The previous implementation for parsing `terraform output` was fragile and could lead to data corruption or errors. It would incorrectly remove all spaces from values and would fail to parse values that contained an equals sign.

This commit refactors the parsing logic to be more robust:
- It no longer removes spaces from output values, preserving them correctly.
- It correctly parses `key=value` pairs by splitting only on the first equals sign in a line.
- It properly handles quoted string values from Terraform.

The corresponding tests in `addon_test.go` have been updated to align with the refactored function signature and verify the new, robust behavior.

Fixes #6916

Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com>

---------

Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com>
This commit is contained in:
AshvinBambhaniya2003 2025-10-07 00:27:37 +05:30 committed by GitHub
parent 7f81d6f2d6
commit 10b45d3a8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 20 deletions

View File

@ -95,11 +95,7 @@ func ApplyTerraform(app *v1beta1.Application, k8sClient client.Client, ioStream
return nil, err
}
outputList := strings.Split(strings.ReplaceAll(string(outputs), " ", ""), "\n")
if outputList[len(outputList)-1] == "" {
outputList = outputList[:len(outputList)-1]
}
if err := generateSecretFromTerraformOutput(k8sClient, outputList, name, namespace); err != nil {
if err := generateSecretFromTerraformOutput(k8sClient, string(outputs), name, namespace); err != nil {
return nil, err
}
default:
@ -135,21 +131,32 @@ func callTerraform(tfJSONDir string) ([]byte, error) {
}
// generateSecretFromTerraformOutput generates secret from Terraform output
func generateSecretFromTerraformOutput(k8sClient client.Client, outputList []string, name, namespace string) error {
func generateSecretFromTerraformOutput(k8sClient client.Client, rawOutput string, name, namespace string) error {
ctx := context.TODO()
err := k8sClient.Create(ctx, &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}})
if err == nil {
return fmt.Errorf("namespace %s doesn't exist", namespace)
// Check if namespace exists
var ns v1.Namespace
if err := k8sClient.Get(ctx, client.ObjectKey{Name: namespace}, &ns); err != nil {
if errors.IsNotFound(err) {
return fmt.Errorf("namespace %s doesn't exist", namespace)
}
return fmt.Errorf("failed to get namespace %s: %w", namespace, err)
}
var cmData = make(map[string]string, len(outputList))
var cmData = make(map[string]string)
outputList := strings.Split(rawOutput, "\n")
for _, i := range outputList {
line := strings.Split(i, "=")
if strings.TrimSpace(i) == "" {
continue
}
line := strings.SplitN(i, "=", 2)
if len(line) != 2 {
return fmt.Errorf("terraform output isn't in the right format")
return fmt.Errorf("terraform output isn't in the right format: %q", i)
}
k := strings.TrimSpace(line[0])
v := strings.TrimSpace(line[1])
if k != "" && v != "" {
// Terraform string outputs are quoted, remove them.
v := strings.Trim(strings.TrimSpace(line[1]), "\"")
if k != "" {
cmData[k] = v
}
}

View File

@ -55,18 +55,18 @@ var _ = Describe("Test generateSecretFromTerraformOutput", func() {
var name = "test-addon-secret"
It("namespace doesn't exist", func() {
badNamespace := "a-not-existed-namespace"
err := generateSecretFromTerraformOutput(k8sClient, nil, name, badNamespace)
err := generateSecretFromTerraformOutput(k8sClient, "", name, badNamespace)
Expect(err).Should(Equal(fmt.Errorf("namespace %s doesn't exist", badNamespace)))
})
It("valid output list", func() {
outputList := []string{"name=aaa", "age=1"}
err := generateSecretFromTerraformOutput(k8sClient, outputList, name, addonNamespace)
rawOutput := "name=aaa\nage=1"
err := generateSecretFromTerraformOutput(k8sClient, rawOutput, name, addonNamespace)
Expect(err).Should(BeNil())
})
It("invalid output list", func() {
outputList := []string{"name"}
err := generateSecretFromTerraformOutput(k8sClient, outputList, name, addonNamespace)
Expect(err).Should(Equal(fmt.Errorf("terraform output isn't in the right format")))
rawOutput := "name"
err := generateSecretFromTerraformOutput(k8sClient, rawOutput, name, addonNamespace)
Expect(err).Should(Equal(fmt.Errorf("terraform output isn't in the right format: %q", rawOutput)))
})
})