Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion site/src/content/docs/commands/zarf_dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Commands useful for developing packages
* [zarf dev find-images](/commands/zarf_dev_find-images/) - Evaluates components in a Zarf file to identify images specified in their helm charts and manifests.
* [zarf dev generate](/commands/zarf_dev_generate/) - Creates a zarf.yaml automatically from a given remote (git) Helm chart
* [zarf dev generate-config](/commands/zarf_dev_generate-config/) - Generates a config file for Zarf
* [zarf dev generate-schema](/commands/zarf_dev_generate-schema/) - Generates a JSON schema for Zarf values based on the package definition and chart defaults
* [zarf dev generate-schema](/commands/zarf_dev_generate-schema/) - Generates a JSON schema for Zarf values based on the package definition, chart defaults, and chart schemas
* [zarf dev inspect](/commands/zarf_dev_inspect/) - Commands to gather information about a Zarf package using its package definition
* [zarf dev lint](/commands/zarf_dev_lint/) - Lints the given package for valid schema and recommended practices
* [zarf dev patch-git](/commands/zarf_dev_patch-git/) - Converts all .git URLs to the specified Zarf HOST and with the Zarf URL pattern in a given FILE. NOTE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tableOfContents: false

## zarf dev generate-schema

Generates a JSON schema for Zarf values based on the package definition and chart defaults
Generates a JSON schema for Zarf values based on the package definition, chart defaults, and chart schemas

```
zarf dev generate-schema [ DIRECTORY ] [flags]
Expand Down
55 changes: 54 additions & 1 deletion src/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,19 @@ type devGenerateSchemaOptions struct {
deleteNotFound bool
}

type mappedChartSchema struct {
sourcePath value.Path
schema map[string]any
excludePath []value.Path
}

func newDevGenerateSchemaCommand(v *viper.Viper) *cobra.Command {
o := &devGenerateSchemaOptions{}

cmd := &cobra.Command{
Use: "generate-schema [ DIRECTORY ]",
Args: cobra.MaximumNArgs(1),
Short: "Generates a JSON schema for Zarf values based on the package definition and chart defaults",
Short: "Generates a JSON schema for Zarf values based on the package definition, chart defaults, and chart schemas",
Comment thread
AustinAbro321 marked this conversation as resolved.
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(cmd.Context(), args)
},
Expand Down Expand Up @@ -149,6 +155,8 @@ func (o *devGenerateSchemaOptions) run(ctx context.Context, args []string) error
return fmt.Errorf("unable to parse package values files: %w", err)
}

var mappedSchemas []mappedChartSchema

// Step 2: Discover source target mappings and load defaults from chart values where Zarf Value defaults aren't specified
tmpDir, err := utils.MakeTempDir(config.CommonOptions.TempDirectory)
if err != nil {
Expand Down Expand Up @@ -178,6 +186,21 @@ func (o *devGenerateSchemaOptions) run(ctx context.Context, args []string) error
}

appliedValues := helpers.MergeMapRecursive(helmChart.Values, valuesFilesValues)
var chartSchema map[string]any
if len(helmChart.Schema) > 0 {
if err := json.Unmarshal(helmChart.Schema, &chartSchema); err != nil {
l.Warn("unable to parse Helm chart values schema; falling back to inferred types", "chart", chart.Name, "error", err)
chartSchema = nil
} else {
chartSchema = value.FilterChartSchema(chartSchema)
if chartSchema != nil {
if err := value.ValidateSchemaDocument(chartSchema); err != nil {
l.Warn("unable to validate Helm chart values schema; falling back to inferred types", "chart", chart.Name, "error", err)
chartSchema = nil
}
}
}
}

// Map ChartValues' Source to Target and merge into zarfValues if not already present
for _, cv := range chart.Values {
Expand All @@ -192,6 +215,26 @@ func (o *devGenerateSchemaOptions) run(ctx context.Context, args []string) error
if err := zarfValues.Set(value.Path(cv.SourcePath), val); err != nil {
return fmt.Errorf("unable to set chart %q value at sourcePath %q: %w", chart.Name, cv.SourcePath, err)
}

if chartSchema != nil {
targetSchema, found, err := value.ExtractJSONSchema(chartSchema, value.Path(cv.TargetPath))
if err != nil {
return fmt.Errorf("unable to inspect chart %q schema at targetPath %q: %w", chart.Name, cv.TargetPath, err)
}
if found {
excludes := make([]value.Path, len(cv.ExcludePaths))
for i, excludePath := range cv.ExcludePaths {
excludes[i] = value.Path(excludePath)
}
mappedSchemas = append(mappedSchemas, mappedChartSchema{
sourcePath: value.Path(cv.SourcePath),
schema: targetSchema,
excludePath: excludes,
})
} else {
l.Warn("chart values schema does not define mapped target; falling back to inferred types", "chart", chart.Name, "targetPath", cv.TargetPath)
}
}
for _, excludePath := range cv.ExcludePaths {
if err := zarfValues.Delete(value.Path(excludePath)); err != nil {
return fmt.Errorf("unable to exclude path %q from schema for chart %q: %w", excludePath, chart.Name, err)
Expand All @@ -203,6 +246,16 @@ func (o *devGenerateSchemaOptions) run(ctx context.Context, args []string) error

// Step 3: Generate JSON generatedSchema from the final map
generatedSchema := value.GenerateJSONSchema(zarfValues)
for _, mapped := range mappedSchemas {
if err := value.MergeJSONSchemaAtPath(generatedSchema, mapped.sourcePath, mapped.schema); err != nil {
return fmt.Errorf("unable to apply Helm schema at sourcePath %q: %w", mapped.sourcePath, err)
}
for _, excludePath := range mapped.excludePath {
if err := value.DeleteJSONSchemaAtPath(generatedSchema, excludePath); err != nil {
return fmt.Errorf("unable to exclude schema path %q: %w", excludePath, err)
}
}
}

// Step 4: Merge and reconcile any existing schema
existingSchema, mergeErr := value.MergeSchemaFiles(defined.Pkg.Values.Schema, defined.ImportedSchemas, basePath)
Expand Down
Loading