From feec1042db8875c198e4bb58c5bc5f7c034721b5 Mon Sep 17 00:00:00 2001 From: Austin Abro Date: Fri, 20 Sep 2024 15:59:01 +0000 Subject: [PATCH] multiple manifests Signed-off-by: Austin Abro --- src/internal/bigbang/bigbang.go | 106 ++++++++++-------- src/internal/bigbang/bigbang_test.go | 52 ++++----- .../multiple_resources.yaml | 15 +++ .../valid_configmap.yaml | 7 -- 4 files changed, 98 insertions(+), 82 deletions(-) create mode 100644 src/internal/bigbang/testdata/getValuesFromManifest/multiple_resources.yaml delete mode 100644 src/internal/bigbang/testdata/getValuesFromManifest/valid_configmap.yaml diff --git a/src/internal/bigbang/bigbang.go b/src/internal/bigbang/bigbang.go index e01d2f12cb..58cc827f7e 100644 --- a/src/internal/bigbang/bigbang.go +++ b/src/internal/bigbang/bigbang.go @@ -79,16 +79,20 @@ func Create(ctx context.Context, bbOpts Opts) error { } valuesFiles := []string{} - for idx, valuesFile := range bbOpts.ValuesFileManifests { - valuesYaml, err := getValuesFromManifest(valuesFile) + for i, valuesFile := range bbOpts.ValuesFileManifests { + valuesYamls, err := getValuesFromManifest(valuesFile) if err != nil { return err } - valuesFilePath := filepath.Join(tmpDir, fmt.Sprintf("values-%d.yaml", idx)) - if err := os.WriteFile(valuesFilePath, []byte(valuesYaml), helpers.ReadWriteUser); err != nil { - return err + // There can be multiple resources in a single manifests creating values file + // We create a values file for each one, these values files are temporary and allow us to template the chart correctly + for j, valuesYaml := range valuesYamls { + valuesFilePath := filepath.Join(tmpDir, fmt.Sprintf("values-%d-%d.yaml", i, j)) + if err := os.WriteFile(valuesFilePath, []byte(valuesYaml), helpers.ReadWriteUser); err != nil { + return err + } + valuesFiles = append(valuesFiles, valuesFilePath) } - valuesFiles = append(valuesFiles, valuesFilePath) } if !bbOpts.SkipFlux { @@ -251,48 +255,52 @@ func Create(ctx context.Context, bbOpts Opts) error { return utils.WriteYaml(filepath.Join(bbOpts.BaseDir, outputName), pkg, helpers.ReadWriteUser) } -func getValuesFromManifest(valuesFileManifest string) (string, error) { +func getValuesFromManifest(valuesFileManifest string) ([]string, error) { file, err := os.ReadFile(valuesFileManifest) if err != nil { - return "", err - } - var resource unstructured.Unstructured - if err := yaml.Unmarshal(file, &resource); err != nil { - return "", fmt.Errorf("values file manifests must be kubernetes configmap or secret resources: %w", err) - } - var data map[string]string - var found bool - var base64Decode bool - if resource.GetKind() == "Secret" { - data, found, err = unstructured.NestedStringMap(resource.Object, "stringData") - if err != nil || !found { + return nil, err + } + resources, err := utils.SplitYAML(file) + if err != nil { + return nil, fmt.Errorf("values file manifests must be kubernetes manifests: %w", err) + } + var fileContents []string + for _, resource := range resources { + var data map[string]string + var found bool + var base64Decode bool + if resource.GetKind() == "Secret" { + data, found, err = unstructured.NestedStringMap(resource.Object, "stringData") + if err != nil || !found { + data, found, err = unstructured.NestedStringMap(resource.Object, "data") + if err != nil || !found { + return nil, fmt.Errorf("failed to get data from secret: %w", err) + } + base64Decode = true + } + } else if resource.GetKind() == "ConfigMap" { data, found, err = unstructured.NestedStringMap(resource.Object, "data") if err != nil || !found { - return "", fmt.Errorf("failed to get data from secret: %w", err) + return nil, fmt.Errorf("failed to get data from resource: %w", err) } - base64Decode = true - } - } else if resource.GetKind() == "ConfigMap" { - data, found, err = unstructured.NestedStringMap(resource.Object, "data") - if err != nil || !found { - return "", fmt.Errorf("failed to get data from resource: %w", err) + } else { + return nil, errors.New("values manifests must be a Secret or ConfigMap") } - } else { - return "", errors.New("values manifests must be a Secret or ConfigMap") - } - valuesYaml, found := data["values.yaml"] - if !found { - return "", errors.New("values.yaml key must exist in data") - } - if base64Decode { - b, err := base64.StdEncoding.DecodeString(valuesYaml) - if err != nil { - return "", err + valuesYaml, found := data["values.yaml"] + if !found { + return nil, errors.New("values.yaml key must exist in data") + } + if base64Decode { + b, err := base64.StdEncoding.DecodeString(valuesYaml) + if err != nil { + return nil, err + } + valuesYaml = string(b) } - valuesYaml = string(b) + fileContents = append(fileContents, valuesYaml) } - return valuesYaml, nil + return fileContents, nil } // isValidVersion check if the version is 1.54.0 or greater. @@ -468,18 +476,18 @@ func createBBManifests(ctx context.Context, airgap bool, manifestDir string, val if err != nil { return v1alpha1.ZarfManifest{}, err } - var resource unstructured.Unstructured - if err := yaml.Unmarshal(file, &resource); err != nil { + manifest.Files = append(manifest.Files, valuesFile) + resources, err := utils.SplitYAML(file) + if err != nil { return v1alpha1.ZarfManifest{}, err } - - manifest.Files = append(manifest.Files, valuesFile) - - // Add it to the list of valuesFrom for the HelmRelease - hrValues = append(hrValues, fluxHelmCtrl.ValuesReference{ - Kind: resource.GetKind(), - Name: resource.GetName(), - }) + for _, resource := range resources { + // Add it to the list of valuesFrom for the HelmRelease + hrValues = append(hrValues, fluxHelmCtrl.ValuesReference{ + Kind: resource.GetKind(), + Name: resource.GetName(), + }) + } } if spec, ok := helmReleaseObj["spec"].(map[string]interface{}); ok { diff --git a/src/internal/bigbang/bigbang_test.go b/src/internal/bigbang/bigbang_test.go index d001d80d6d..74630ca487 100644 --- a/src/internal/bigbang/bigbang_test.go +++ b/src/internal/bigbang/bigbang_test.go @@ -112,40 +112,40 @@ func TestFindBBResources(t *testing.T) { func TestGetValuesFromManifest(t *testing.T) { t.Parallel() tests := []struct { - name string - fileName string - expectedOutput string - expectedErr error + name string + fileName string + expected []string + expectedErr error }{ { - name: "Valid Secret string data", - fileName: "valid_secret_string_data.yaml", - expectedOutput: "key: value\n", - expectedErr: nil, + name: "Valid Secret string data", + fileName: "valid_secret_string_data.yaml", + expected: []string{"key: value\n"}, + expectedErr: nil, }, { - name: "Valid Secret regular data", - fileName: "valid_secret_data.yaml", - expectedOutput: "key: value", - expectedErr: nil, + name: "Valid Secret regular data", + fileName: "valid_secret_data.yaml", + expected: []string{"key: value"}, + expectedErr: nil, }, { - name: "Valid ConfigMap", - fileName: "valid_configmap.yaml", - expectedOutput: "key: value\n", - expectedErr: nil, + name: "Valid ConfigMap and secret", + fileName: "multiple_resources.yaml", + expected: []string{"key: value\n", "key2: value2\n"}, + expectedErr: nil, }, { - name: "Invalid Kind", - fileName: "invalid_kind.yaml", - expectedOutput: "", - expectedErr: errors.New("values manifests must be a Secret or ConfigMap"), + name: "Invalid Kind", + fileName: "invalid_kind.yaml", + expected: nil, + expectedErr: errors.New("values manifests must be a Secret or ConfigMap"), }, { - name: "Missing values.yaml", - fileName: "missing_values.yaml", - expectedOutput: "", - expectedErr: errors.New("values.yaml key must exist in data"), + name: "Missing values.yaml", + fileName: "missing_values.yaml", + expected: nil, + expectedErr: errors.New("values.yaml key must exist in data"), }, } @@ -153,13 +153,13 @@ func TestGetValuesFromManifest(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() filePath := filepath.Join("testdata", "getValuesFromManifest", tt.fileName) - output, err := getValuesFromManifest(filePath) + actual, err := getValuesFromManifest(filePath) if tt.expectedErr != nil { require.Error(t, err) return } require.NoError(t, err) - require.Equal(t, tt.expectedOutput, output) + require.Equal(t, tt.expected, actual) }) } } diff --git a/src/internal/bigbang/testdata/getValuesFromManifest/multiple_resources.yaml b/src/internal/bigbang/testdata/getValuesFromManifest/multiple_resources.yaml new file mode 100644 index 0000000000..837766866b --- /dev/null +++ b/src/internal/bigbang/testdata/getValuesFromManifest/multiple_resources.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: test-configmap +data: + values.yaml: | + key: value +--- +apiVersion: v1 +kind: Secret +metadata: + name: test-secret +stringData: + values.yaml: | + key2: value2 diff --git a/src/internal/bigbang/testdata/getValuesFromManifest/valid_configmap.yaml b/src/internal/bigbang/testdata/getValuesFromManifest/valid_configmap.yaml deleted file mode 100644 index ab9cb9dea6..0000000000 --- a/src/internal/bigbang/testdata/getValuesFromManifest/valid_configmap.yaml +++ /dev/null @@ -1,7 +0,0 @@ -apiVersion: v1 -kind: ConfigMap -metadata: - name: test-configmap -data: - values.yaml: | - key: value