mirror of https://github.com/kubevela/kubevela.git
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:
parent
7f81d6f2d6
commit
10b45d3a8f
|
@ -95,11 +95,7 @@ func ApplyTerraform(app *v1beta1.Application, k8sClient client.Client, ioStream
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
outputList := strings.Split(strings.ReplaceAll(string(outputs), " ", ""), "\n")
|
if err := generateSecretFromTerraformOutput(k8sClient, string(outputs), name, namespace); err != nil {
|
||||||
if outputList[len(outputList)-1] == "" {
|
|
||||||
outputList = outputList[:len(outputList)-1]
|
|
||||||
}
|
|
||||||
if err := generateSecretFromTerraformOutput(k8sClient, outputList, name, namespace); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
@ -135,21 +131,32 @@ func callTerraform(tfJSONDir string) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// generateSecretFromTerraformOutput generates secret from Terraform output
|
// 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()
|
ctx := context.TODO()
|
||||||
err := k8sClient.Create(ctx, &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}})
|
|
||||||
if err == nil {
|
// 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("namespace %s doesn't exist", namespace)
|
||||||
}
|
}
|
||||||
var cmData = make(map[string]string, len(outputList))
|
return fmt.Errorf("failed to get namespace %s: %w", namespace, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var cmData = make(map[string]string)
|
||||||
|
outputList := strings.Split(rawOutput, "\n")
|
||||||
for _, i := range outputList {
|
for _, i := range outputList {
|
||||||
line := strings.Split(i, "=")
|
if strings.TrimSpace(i) == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
line := strings.SplitN(i, "=", 2)
|
||||||
if len(line) != 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])
|
k := strings.TrimSpace(line[0])
|
||||||
v := strings.TrimSpace(line[1])
|
// Terraform string outputs are quoted, remove them.
|
||||||
if k != "" && v != "" {
|
v := strings.Trim(strings.TrimSpace(line[1]), "\"")
|
||||||
|
if k != "" {
|
||||||
cmData[k] = v
|
cmData[k] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,18 +55,18 @@ var _ = Describe("Test generateSecretFromTerraformOutput", func() {
|
||||||
var name = "test-addon-secret"
|
var name = "test-addon-secret"
|
||||||
It("namespace doesn't exist", func() {
|
It("namespace doesn't exist", func() {
|
||||||
badNamespace := "a-not-existed-namespace"
|
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)))
|
Expect(err).Should(Equal(fmt.Errorf("namespace %s doesn't exist", badNamespace)))
|
||||||
})
|
})
|
||||||
It("valid output list", func() {
|
It("valid output list", func() {
|
||||||
outputList := []string{"name=aaa", "age=1"}
|
rawOutput := "name=aaa\nage=1"
|
||||||
err := generateSecretFromTerraformOutput(k8sClient, outputList, name, addonNamespace)
|
err := generateSecretFromTerraformOutput(k8sClient, rawOutput, name, addonNamespace)
|
||||||
Expect(err).Should(BeNil())
|
Expect(err).Should(BeNil())
|
||||||
})
|
})
|
||||||
|
|
||||||
It("invalid output list", func() {
|
It("invalid output list", func() {
|
||||||
outputList := []string{"name"}
|
rawOutput := "name"
|
||||||
err := generateSecretFromTerraformOutput(k8sClient, outputList, name, addonNamespace)
|
err := generateSecretFromTerraformOutput(k8sClient, rawOutput, name, addonNamespace)
|
||||||
Expect(err).Should(Equal(fmt.Errorf("terraform output isn't in the right format")))
|
Expect(err).Should(Equal(fmt.Errorf("terraform output isn't in the right format: %q", rawOutput)))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue