Skip to content

Commit

Permalink
multiple manifests
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <AustinAbro321@gmail.com>
  • Loading branch information
AustinAbro321 committed Sep 20, 2024
1 parent a5ccbb2 commit feec104
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 82 deletions.
106 changes: 57 additions & 49 deletions src/internal/bigbang/bigbang.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
52 changes: 26 additions & 26 deletions src/internal/bigbang/bigbang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,54 +112,54 @@ 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"),
},
}

for _, tt := range tests {
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)
})
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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

This file was deleted.

0 comments on commit feec104

Please sign in to comment.