-
Notifications
You must be signed in to change notification settings - Fork 219
feat(values): dev inspect #4268
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
base: main
Are you sure you want to change the base?
Changes from 98 commits
355d0f5
a6fb3eb
1d9d728
3df0a2c
4d2c610
23a8027
b2db2a3
eb2453f
96eec5d
6aeb2cd
9b9c5ad
6ab0bc3
3ec1b20
1b76f5b
2e98fa9
f542021
061858d
d5fb32d
d802aa7
f2cdbca
65e93db
dd33519
8bba1af
6b6d81e
92e1eb7
b42120b
a1dc7ec
909ab23
9a9c849
766974c
ba4701e
e29a11f
536caa2
c740a31
b5d9a0f
2fe95bd
740749f
87259bc
219b504
a6f467d
7f4228f
2b380a3
d938db7
4a256ee
ceb8cfb
ac88a4a
cc0e2c1
2a3ac66
75e7546
7a7a8c9
07c665e
6e02a3e
234b46d
587f7bd
8eb1283
e1328db
79b9075
9fbb27c
57f6a8a
1d841e0
b25defa
9a37372
f34bc2e
b13eab6
8669d72
174e494
b3f1588
23a60c9
de4a808
2c18659
ab14746
2c53189
89de456
3de01ab
c48dd07
b542964
473c41d
1f0f715
1c648b9
095fbfd
26eed44
e062bce
2d77714
d71a493
4669c88
2fe037f
ded9820
f42c346
26b0876
ba6f9a5
bcf417c
91b3c14
83a1f7c
862de4d
1128221
7810a7d
8ffc674
7b1ebff
7042d4c
d29718b
38d61b8
f9f9613
06d6bd4
f228644
f9a8c2c
1bdfcdf
e659540
580feed
5bb2a70
8fd9d75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
mkcp marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,12 +9,15 @@ import ( | |
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/defenseunicorns/pkg/helpers/v2" | ||
| "github.com/zarf-dev/zarf/src/api/v1alpha1" | ||
| "github.com/zarf-dev/zarf/src/config" | ||
| "github.com/zarf-dev/zarf/src/internal/packager/helm" | ||
| "github.com/zarf-dev/zarf/src/internal/packager/template" | ||
| tmpl "github.com/zarf-dev/zarf/src/internal/template" | ||
| "github.com/zarf-dev/zarf/src/internal/value" | ||
| "github.com/zarf-dev/zarf/src/pkg/packager/layout" | ||
| "github.com/zarf-dev/zarf/src/pkg/packager/load" | ||
| "github.com/zarf-dev/zarf/src/pkg/state" | ||
|
|
@@ -176,6 +179,8 @@ func templateValuesFiles(chart v1alpha1.ZarfChart, valuesDir string, variableCon | |
| type InspectDefinitionResourcesOptions struct { | ||
| CreateSetVariables map[string]string | ||
| DeploySetVariables map[string]string | ||
| ValuesFiles []string | ||
| SetValues map[string]string | ||
|
||
| Flavor string | ||
| KubeVersion string | ||
| // CachePath is used to cache layers from skeleton package pulls | ||
|
|
@@ -205,6 +210,24 @@ func InspectDefinitionResources(ctx context.Context, packagePath string, opts In | |
| return nil, err | ||
| } | ||
|
|
||
| // Parse values from files | ||
| vals, err := value.ParseFiles(ctx, opts.ValuesFiles, value.ParseFilesOptions{}) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unable to parse values files: %w", err) | ||
| } | ||
|
|
||
| // Apply CLI --set-values overrides | ||
| for key, val := range opts.SetValues { | ||
| // Convert key to path format (ensure it starts with .) | ||
| path := value.Path(key) | ||
| if !strings.HasPrefix(key, ".") { | ||
| path = value.Path("." + key) | ||
| } | ||
| if err := vals.Set(path, val); err != nil { | ||
| return nil, fmt.Errorf("unable to set value at path %s: %w", key, err) | ||
| } | ||
| } | ||
|
|
||
| tmpPackagePath, err := utils.MakeTempDir(config.CommonOptions.TempDirectory) | ||
| if err != nil { | ||
| return nil, err | ||
|
|
@@ -229,7 +252,7 @@ func InspectDefinitionResources(ctx context.Context, packagePath string, opts In | |
| } | ||
|
|
||
| for _, zarfChart := range component.Charts { | ||
| chartResource, values, err := getTemplatedChart(ctx, zarfChart, packagePath, compBuildPath, variableConfig, opts.KubeVersion, opts.IsInteractive) | ||
| chartResource, values, err := getTemplatedChart(ctx, zarfChart, component.Name, packagePath, compBuildPath, variableConfig, vals, opts.KubeVersion, opts.IsInteractive) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -253,7 +276,7 @@ func InspectDefinitionResources(ctx context.Context, packagePath string, opts In | |
| } | ||
| } | ||
| for _, manifest := range component.Manifests { | ||
| manifestResources, err := getTemplatedManifests(ctx, manifest, packagePath, compBuildPath, variableConfig) | ||
| manifestResources, err := getTemplatedManifests(ctx, manifest, packagePath, compBuildPath, variableConfig, vals, pkg) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -264,58 +287,91 @@ func InspectDefinitionResources(ctx context.Context, packagePath string, opts In | |
| return resources, nil | ||
| } | ||
|
|
||
| func getTemplatedManifests(ctx context.Context, manifest v1alpha1.ZarfManifest, packagePath string, baseComponentDir string, variableConfig *variables.VariableConfig) ([]Resource, error) { | ||
| func getTemplatedManifests(ctx context.Context, manifest v1alpha1.ZarfManifest, packagePath string, baseComponentDir string, variableConfig *variables.VariableConfig, vals value.Values, pkg v1alpha1.ZarfPackage) (_ []Resource, err error) { | ||
| if err := layout.PackageManifest(ctx, manifest, baseComponentDir, packagePath); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| manifestPath := filepath.Join(baseComponentDir, string(layout.ManifestsComponentDir)) | ||
|
|
||
| var resources []Resource | ||
| err := filepath.Walk(manifestPath, func(manifest string, info os.FileInfo, err error) error { | ||
| err = filepath.Walk(manifestPath, func(manifestFile string, info os.FileInfo, err error) (err2 error) { | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if info.IsDir() { | ||
| return nil | ||
| } | ||
| if err := variableConfig.ReplaceTextTemplate(manifest); err != nil { | ||
|
|
||
| // First apply ###ZARF_VAR_*### and ###ZARF_CONST_*### templating | ||
| if err := variableConfig.ReplaceTextTemplate(manifestFile); err != nil { | ||
| return fmt.Errorf("error templating the manifest: %w", err) | ||
| } | ||
| content, err := os.ReadFile(manifest) | ||
| if err != nil { | ||
| return err | ||
|
|
||
| // Then apply Go template templating if manifest.Template is enabled | ||
| var content []byte | ||
| if manifest.IsTemplate() { | ||
| // Create template objects with values, metadata, build, constants, and variables | ||
| objs := tmpl.NewObjects(vals). | ||
| WithPackage(pkg). | ||
| WithVariables(variableConfig.GetSetVariableMap()) | ||
|
|
||
| // Create a temp file for the output | ||
| tmpDir, err := os.MkdirTemp("", "zarf-inspect-*") | ||
| if err != nil { | ||
| return fmt.Errorf("unable to create temp directory: %w", err) | ||
| } | ||
| defer func() { | ||
| rErr := os.RemoveAll(tmpDir) | ||
| err2 = errors.Join(err2, rErr) | ||
| }() | ||
|
|
||
| tmpFile := filepath.Join(tmpDir, filepath.Base(manifestFile)) | ||
| if err := tmpl.ApplyToFile(ctx, manifestFile, tmpFile, objs); err != nil { | ||
| return fmt.Errorf("error applying Go templates to manifest: %w", err) | ||
| } | ||
|
|
||
| content, err = os.ReadFile(tmpFile) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to read templated manifest: %w", err) | ||
| } | ||
| } else { | ||
| content, err = os.ReadFile(manifestFile) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| resources = append(resources, Resource{ | ||
| Content: string(content), | ||
| Name: manifest, | ||
| Name: manifestFile, | ||
| ResourceType: ManifestResource, | ||
| }) | ||
| return nil | ||
| return err2 | ||
mkcp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return resources, nil | ||
| return resources, err | ||
mkcp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // getTemplatedChart returns a templated chart.yaml as a string after templating | ||
| func getTemplatedChart(ctx context.Context, zarfChart v1alpha1.ZarfChart, packagePath string, baseComponentDir string, variableConfig *variables.VariableConfig, kubeVersion string, isInteractive bool) (Resource, chartutil.Values, error) { | ||
| func getTemplatedChart(ctx context.Context, zarfChart v1alpha1.ZarfChart, componentName string, packagePath string, baseComponentDir string, variableConfig *variables.VariableConfig, vals value.Values, kubeVersion string, isInteractive bool) (Resource, chartutil.Values, error) { | ||
| chartPath := filepath.Join(baseComponentDir, string(layout.ChartsComponentDir)) | ||
| valuesFilePath := filepath.Join(baseComponentDir, string(layout.ValuesComponentDir)) | ||
| if err := layout.PackageChart(ctx, zarfChart, packagePath, chartPath, valuesFilePath); err != nil { | ||
| return Resource{}, chartutil.Values{}, err | ||
| } | ||
|
|
||
| chartOverrides := make(map[string]any) | ||
| for _, variable := range zarfChart.Variables { | ||
| if setVar, ok := variableConfig.GetSetVariable(variable.Name); ok && setVar != nil { | ||
| // Use the variable's path as a key to ensure unique entries for variables with the same name but different paths. | ||
| if err := helpers.MergePathAndValueIntoMap(chartOverrides, variable.Path, setVar.Value); err != nil { | ||
| return Resource{}, chartutil.Values{}, fmt.Errorf("unable to merge path and value into map: %w", err) | ||
| } | ||
| } | ||
| // Generate chart overrides using values | ||
| chartOverrides, err := generateValuesOverrides(ctx, zarfChart, componentName, overrideOpts{ | ||
| variableConfig: variableConfig, | ||
| values: vals, | ||
| valuesOverridesMap: ValuesOverrides{}, | ||
| }) | ||
| if err != nil { | ||
| return Resource{}, chartutil.Values{}, err | ||
| } | ||
|
|
||
| valuesFilePaths, err := helpers.RecursiveFileList(valuesFilePath, nil, false) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.