Skip to content
Open
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
15 changes: 13 additions & 2 deletions kubernetes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,9 +725,11 @@ func parsedManifestFileContentUncached(ctx context.Context, d *plugin.QueryData,
return nil, err
}

// Check for the start of the document
// Check for the start of the document. Split only on YAML document separators
// that appear at the start of a line, so that literal strings containing
// '---' (for example in a block scalar) do not break parsing.
pos := 0
for _, resource := range strings.Split(string(content), "---") {
for _, resource := range splitYAMLDocuments(string(content)) {
// Skip empty documents, `Decode` will fail on them
// Also, increment the pos to include the separator position (e.g. ---)
if len(resource) == 0 {
Expand Down Expand Up @@ -791,6 +793,15 @@ func parsedManifestFileContentUncached(ctx context.Context, d *plugin.QueryData,
return parsedContents, nil
}

// splitYAMLDocuments splits a YAML stream into documents using only valid YAML
// document separators: lines that consist of '---' (with optional trailing
// whitespace) at the start of a line. This avoids splitting on '---' that
// appear inside block scalars or other string values.
func splitYAMLDocuments(content string) []string {
docSeparator := regexp.MustCompile(`(?m)^---\s*(#.*)?$`)
return docSeparator.Split(content, -1)
}

// Returns the list of file paths/glob patterns after resolving all the given manifest file paths.
func resolveManifestFilePaths(ctx context.Context, d *plugin.QueryData) ([]string, error) {
// Read the config
Expand Down
Loading