Skip to content

Commit 6ab00f2

Browse files
committed
fix: skip comment-only YAML documents during parsing
Comment-only documents in concatenated .tekton files caused "Object 'Kind' is missing" errors, blocking valid PipelineRuns from executing. Skip these documents before decoding. Signed-off-by: Akshay Pant <akpant@redhat.com>
1 parent 8181703 commit 6ab00f2

3 files changed

Lines changed: 135 additions & 1 deletion

File tree

‎pkg/resolve/resolve.go‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,29 @@ type Opts struct {
150150
RepositoryRevision string // revision for repository-local Task and Pipeline references
151151
}
152152

153+
// hasYAMLContent checks if a document contains actual YAML content
154+
// beyond comments and whitespace. Returns false for comment-only or empty documents.
155+
func hasYAMLContent(doc string) bool {
156+
for _, line := range strings.Split(doc, "\n") {
157+
trimmed := strings.TrimSpace(line)
158+
// Skip empty lines and comment lines
159+
if trimmed == "" || strings.HasPrefix(trimmed, "#") {
160+
continue
161+
}
162+
// Found non-comment, non-whitespace content
163+
return true
164+
}
165+
return false
166+
}
167+
153168
func ReadTektonTypes(ctx context.Context, log *zap.SugaredLogger, data string) (TektonTypes, error) {
154169
types := NewTektonTypes()
155170
decoder := k8scheme.Codecs.UniversalDeserializer()
156171
docs := yamlDocSeparatorRe.Split(data, -1)
157172
debugf(log, "ReadTektonTypes: data length=%d docs=%d", len(data), len(docs))
158173

159174
for _, doc := range docs {
160-
if strings.TrimSpace(doc) == "" {
175+
if !hasYAMLContent(doc) {
161176
continue
162177
}
163178

‎pkg/resolve/resolve_test.go‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,123 @@ func TestIgnoreDocSpace(t *testing.T) {
506506
assert.NilError(t, err)
507507
}
508508

509+
func TestReadTektonTypesCommentOnlyDocuments(t *testing.T) {
510+
tests := []struct {
511+
name string
512+
yamlContent string
513+
pipelineRuns int
514+
pipelines int
515+
tasks int
516+
validErrors int
517+
}{
518+
{
519+
name: "comment-only document should be skipped",
520+
yamlContent: `---
521+
# This is just a comment
522+
# Another comment
523+
---
524+
apiVersion: tekton.dev/v1
525+
kind: PipelineRun
526+
metadata:
527+
name: test-pr
528+
spec:
529+
pipelineSpec:
530+
tasks: []`,
531+
pipelineRuns: 1,
532+
validErrors: 0,
533+
},
534+
{
535+
name: "whitespace and comments should be skipped",
536+
yamlContent: `---
537+
538+
# Comment with empty lines above
539+
540+
---
541+
apiVersion: tekton.dev/v1
542+
kind: Task
543+
metadata:
544+
name: test-task
545+
spec:
546+
steps:
547+
- name: step1
548+
image: alpine`,
549+
tasks: 1,
550+
validErrors: 0,
551+
},
552+
{
553+
name: "comment before YAML content should process normally",
554+
yamlContent: `---
555+
# Configuration for pipeline
556+
apiVersion: tekton.dev/v1
557+
kind: Pipeline
558+
metadata:
559+
name: test-pipeline
560+
spec:
561+
tasks: []`,
562+
pipelines: 1,
563+
validErrors: 0,
564+
},
565+
{
566+
name: "multiple comment-only docs with one valid doc",
567+
yamlContent: `---
568+
# First comment block
569+
---
570+
# Second comment block
571+
# More comments
572+
---
573+
apiVersion: tekton.dev/v1
574+
kind: PipelineRun
575+
metadata:
576+
name: test-pr
577+
spec:
578+
pipelineSpec:
579+
tasks: []
580+
---
581+
# Trailing comment block
582+
---`,
583+
pipelineRuns: 1,
584+
validErrors: 0,
585+
},
586+
{
587+
name: "NetworkManager comment case from bug report",
588+
yamlContent: `---
589+
# --- NetworkManager ---
590+
---
591+
apiVersion: tekton.dev/v1
592+
kind: PipelineRun
593+
metadata:
594+
name: networkmanager-test
595+
spec:
596+
pipelineSpec:
597+
tasks: []`,
598+
pipelineRuns: 1,
599+
validErrors: 0,
600+
},
601+
{
602+
name: "all comment-only documents",
603+
yamlContent: `---
604+
# Just comments
605+
---
606+
# More comments
607+
# And more
608+
---
609+
# Final comments`,
610+
validErrors: 0,
611+
},
612+
}
613+
614+
for _, tt := range tests {
615+
t.Run(tt.name, func(t *testing.T) {
616+
types, err := ReadTektonTypes(context.TODO(), nil, tt.yamlContent)
617+
assert.NilError(t, err)
618+
assert.Equal(t, len(types.PipelineRuns), tt.pipelineRuns, "unexpected number of PipelineRuns")
619+
assert.Equal(t, len(types.Pipelines), tt.pipelines, "unexpected number of Pipelines")
620+
assert.Equal(t, len(types.Tasks), tt.tasks, "unexpected number of Tasks")
621+
assert.Equal(t, len(types.ValidationErrors), tt.validErrors, "unexpected number of ValidationErrors")
622+
})
623+
}
624+
}
625+
509626
func TestPipelineV1StayV1(t *testing.T) {
510627
got, _, err := readTDfile(t, "pipelinev1asv1", false, true)
511628
assert.NilError(t, err)

‎test/testdata/always-good-pipelinerun.yaml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
---
2+
# This is a pipelinerun which should always pass.
3+
---
24
apiVersion: tekton.dev/v1beta1
35
kind: PipelineRun
46
metadata:

0 commit comments

Comments
 (0)