Skip to content

Commit 254da3b

Browse files
Merge pull request GoogleCloudPlatform#4707 from SwarnaBharathiMantena/swarnabm/kubectl_source_url_support
Update process to filter out starting comments in a source yaml file
2 parents 52c9602 + 5655588 commit 254da3b

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

  • modules/management/kubectl-apply/kubectl

modules/management/kubectl-apply/kubectl/main.tf

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,18 @@ locals {
3939

4040
# --- CONSOLIDATE & PROCESS ---
4141
# Coalesce finds the first non-null content from the methods above.
42-
yaml_body = coalesce(local.content_yaml_body, local.yaml_file_content, local.template_file_content, " ")
43-
yaml_body_docs = [for doc in split(local.yaml_separator, local.yaml_body) : trimspace(doc) if length(trimspace(doc)) > 0]
42+
yaml_body = coalesce(local.content_yaml_body, local.yaml_file_content, local.template_file_content, " ")
43+
# Ensure only valid YAML is processed
44+
# It explicitly tests if the content can be decoded before including it.
45+
yaml_body_docs = compact(flatten([
46+
for doc in split(local.yaml_separator, local.yaml_body) : [
47+
for content in [trimspace(doc)] : (
48+
# Use a temporary local variable and can() to test for successful YAML decoding.
49+
# This handles malformed documents (like comment blocks) which cause yamldecode() to fail.
50+
can(yamldecode(content)) && length(yamldecode(content)) > 0 ? content : null
51+
)
52+
]
53+
]))
4454

4555
# --- METHOD 4: Directory of Files ---
4656
# If no content was found via the methods above AND the source path looks like a directory,

0 commit comments

Comments
 (0)