Skip to content

Commit

Permalink
Ignore tmpl files to be validated (#830)
Browse files Browse the repository at this point in the history
* fix: ignore tmpl files to be validated

(cherry picked from commit 5402068)

* docs for validate tmpl exclusion

* fix possible paterns

* refactor: remove template file exclusion logic from stack config processing

---------

Co-authored-by: Erik Osterman (CEO @ Cloud Posse) <[email protected]>
Co-authored-by: Andriy Knysh <[email protected]>
  • Loading branch information
3 people authored Dec 10, 2024
1 parent bf867be commit abddf14
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 18 deletions.
11 changes: 8 additions & 3 deletions internal/exec/validate_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,13 @@ func ValidateStacks(cliConfig schema.CliConfiguration) error {

// Include (process and validate) all YAML files in the `stacks` folder in all subfolders
includedPaths := []string{"**/*"}
// Don't exclude any YAML files for validation
excludedPaths := []string{}
// Don't exclude any YAML files for validation except template files
excludedPaths := []string{
// Exclude template files from validation since they may contain invalid YAML before being rendered
"**/*.tmpl",
"**/*.yaml.tmpl",
"**/*.yml.tmpl",
}
includeStackAbsPaths, err := u.JoinAbsolutePathWithPaths(cliConfig.StacksBaseAbsolutePath, includedPaths)
if err != nil {
return err
Expand All @@ -125,7 +130,7 @@ func ValidateStacks(cliConfig schema.CliConfiguration) error {
return err
}

u.LogDebug(cliConfig, fmt.Sprintf("Validating all YAML files in the '%s' folder and all subfolders\n",
u.LogDebug(cliConfig, fmt.Sprintf("Validating all YAML files in the '%s' folder and all subfolders (excluding template files)\n",
path.Join(cliConfig.BasePath, cliConfig.Stacks.BasePath)))

for _, filePath := range stackConfigFilesAbsolutePaths {
Expand Down
36 changes: 21 additions & 15 deletions pkg/config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func FindAllStackConfigsInPathsForStack(

ext := filepath.Ext(p)
if ext == "" {
patterns = getStackFilePatterns(p)
// Get all patterns since filtering is done later
patterns = getStackFilePatterns(p, true)
}

var allMatches []string
Expand Down Expand Up @@ -125,7 +126,8 @@ func FindAllStackConfigsInPaths(

ext := filepath.Ext(p)
if ext == "" {
patterns = getStackFilePatterns(p)
// Get all patterns since filtering is done later
patterns = getStackFilePatterns(p, true)
}

var allMatches []string
Expand Down Expand Up @@ -656,18 +658,26 @@ func GetStackNameFromContextAndStackNamePattern(
}

// getStackFilePatterns returns a slice of possible file patterns for a given base path
func getStackFilePatterns(basePath string) []string {
return []string{
basePath + u.DefaultStackConfigFileExtension,
basePath + u.DefaultStackConfigFileExtension + u.TemplateExtension,
basePath + u.YamlTemplateExtension,
basePath + u.YmlTemplateExtension,
func getStackFilePatterns(basePath string, includeTemplates bool) []string {
patterns := []string{
basePath + u.YamlFileExtension,
basePath + u.YmlFileExtension,
}

if includeTemplates {
patterns = append(patterns,
basePath+u.YamlTemplateExtension,
basePath+u.YmlTemplateExtension,
)
}

return patterns
}

// matchesStackFilePattern checks if a file path matches any of the valid stack file patterns
func matchesStackFilePattern(filePath, stackName string) bool {
patterns := getStackFilePatterns(stackName)
// Always include template files for normal operations (imports, etc.)
patterns := getStackFilePatterns(stackName, true)
for _, pattern := range patterns {
if strings.HasSuffix(filePath, pattern) {
return true
Expand All @@ -685,12 +695,8 @@ func getConfigFilePatterns(path string, forGlobMatch bool) []string {
return []string{path}
}

patterns := []string{
path + u.DefaultStackConfigFileExtension,
path + u.DefaultStackConfigFileExtension + u.TemplateExtension,
path + u.YamlTemplateExtension,
path + u.YmlTemplateExtension,
}
// include template files for normal operations
patterns := getStackFilePatterns(path, true)
if !forGlobMatch {
// For direct file search, include the exact path without extension
patterns = append([]string{path}, patterns...)
Expand Down
2 changes: 2 additions & 0 deletions website/docs/cli/commands/validate/validate-stacks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ atmos validate stacks
This command validates Atmos stack manifests and checks the following:

- All YAML manifest files for YAML errors and inconsistencies
- Note: Template files (`.yaml.tmpl`, `.yml.tmpl`, `.tmpl`) are excluded from validation since they may contain template placeholders that are invalid YAML before being rendered
- Template files are still automatically detected and processed during normal operations (imports, etc.)

- All imports: if they are configured correctly, have valid data types, and point to existing manifest files

Expand Down
9 changes: 9 additions & 0 deletions website/docs/core-concepts/stacks/imports.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ For example, if you import `catalog/file1`, Atmos will:

This feature makes it easier to work with templated configurations as you don't need to explicitly specify the template file extension - Atmos will automatically use the template version when available.

:::note Template File Validation
While template files are automatically detected and processed during normal operations (imports, etc.), they are excluded from YAML validation (`atmos validate stacks`) since they may contain template placeholders that are invalid YAML before being rendered.

This means:
- Template files are fully supported for imports and normal operations
- Template files are skipped during `atmos validate stacks` to prevent validation errors from unrendered templates
- You don't need to explicitly specify template extensions - Atmos will find them automatically
:::

## Conventions

We recommend placing all baseline "imports" in the `stacks/catalog` folder, however, they can exist anywhere.
Expand Down
6 changes: 6 additions & 0 deletions website/docs/core-concepts/stacks/templates/templates.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import SecondaryCTA from '@site/src/components/SecondaryCTA'
Atmos supports [Go templates](https://pkg.go.dev/text/template) in stack manifests and functions to customize Stack configurations.
</Intro>

:::note Template File Validation
Template files (`.yaml.tmpl`, `.yml.tmpl`, `.tmpl`) are automatically detected and processed during normal operations (imports, etc.).
However, they are excluded from YAML validation (`atmos validate stacks`) since they may contain template placeholders that are invalid YAML before being rendered.
This ensures that template files can contain valid Go template syntax without causing validation errors.
:::

### Enable Templating

Templating in Atmos stack manifests is configured in the `atmos.yaml` [CLI config file](/cli/configuration) in the
Expand Down

0 comments on commit abddf14

Please sign in to comment.