Skip to content

projects: Skip empty documents when applying init and extra resources - #208

Merged
haarchri merged 1 commit into
crossplane:mainfrom
ytsarev:fix/skip-empty-init-extra-resources
Jul 23, 2026
Merged

projects: Skip empty documents when applying init and extra resources#208
haarchri merged 1 commit into
crossplane:mainfrom
ytsarev:fix/skip-empty-init-extra-resources

Conversation

@ytsarev

@ytsarev ytsarev commented Jul 23, 2026

Copy link
Copy Markdown
Member

Description of your changes

crossplane project run --init-resources/--extra-resources rejected any manifest whose first YAML document is empty — most commonly a file-level comment header before the first ---, a very common convention — with:

encountered an invalid or empty raw resource

The YAML stream loader (LoadYAMLStreamFromFile) splits on --- and only drops byte-empty chunks, so a comment-only chunk survives, unmarshals to an empty runtime.RawExtension, and ApplyResources treated that as fatal. kubectl apply -f on the identical file works because it skips empty/null documents.

This changes ApplyResources to skip empty raw resources (matching kubectl) instead of erroring, which fixes both --init-resources and --extra-resources since they share the path. The existing EmptyRawErrors unit test becomes SkipsEmptyRaw, asserting an empty document is skipped and a real resource alongside it is still applied.

Encountered in modelplaneai/modelplane#365 , see run.sh there for additional context.

Reproduction

$ printf '# header comment\n---\napiVersion: v1\nkind: Namespace\nmetadata:\n  name: demo\n' > prereq.yaml

$ crossplane project run --init-resources prereq.yaml
# before: Error: encountered an invalid or empty raw resource

$ kubectl apply --dry-run=client -f prereq.yaml   # the same file applies fine

Fixes #207

I have:

  • Read and followed Crossplane's contribution process.
  • Run ./nix.sh flake check — passes locally (generate-check, go-lint, and test all green for this commit). Also verified go build ./..., go vet, gofmt, and golangci-lint run (0 issues) directly.
  • Added or updated unit tests.
  • Linked a PR or a docs tracking issue to document this change. Bug fix with no user-facing docs change.
  • Added backport release-x.y labels to auto-backport this PR. Leaving the backport decision to maintainers.

@ytsarev
ytsarev requested review from a team, jcogilvie and tampakrap as code owners July 23, 2026 09:58
@ytsarev
ytsarev requested review from jbw976 and removed request for a team July 23, 2026 09:58
@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

Empty resource handling

Layer / File(s) Summary
Skip empty resources and verify application
internal/project/install.go, internal/project/install_test.go
ApplyResources skips empty RawExtension entries, while tests verify that a neighboring ConfigMap is still applied successfully.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Suggested reviewers: jcogilvie

🚥 Pre-merge checks | ✅ 6
✅ Passed checks (6 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes satisfy #207 by skipping empty raw resources and preserving valid resources in the same manifest.
Out of Scope Changes check ✅ Passed The diff stays focused on the bug fix and its test, with no obvious unrelated changes.
Breaking Changes ✅ Passed Only internal/project files changed; no apis/** or cmd/** fields/flags were removed or renamed, and ApplyResources behavior was relaxed, not removed.
Feature Gate Requirement ✅ Passed No apis/** changes or new experimental surface; the PR only skips empty raw resources in existing ApplyResources behavior.
Title check ✅ Passed The title is descriptive, under the length limit, and matches the main change to skip empty documents during resource application.
Description check ✅ Passed The description clearly explains the bug, the fix, and the updated test behavior, so it is related to the changeset.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/project/install_test.go`:
- Around line 106-124: Convert the SkipsEmptyRaw test into the required
table-driven structure with args, want, and reason fields, covering the empty
raw extension alongside the real ConfigMap. Execute each case through the table
and compare returned errors using cmp.Diff with cmpopts.EquateErrors(), while
retaining assertions that the real resource is applied.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 7db5d502-960b-4229-9610-fe7d182a755b

📥 Commits

Reviewing files that changed from the base of the PR and between 5342ad7 and e8cb075.

📒 Files selected for processing (2)
  • internal/project/install.go
  • internal/project/install_test.go

Comment thread internal/project/install_test.go Outdated
crossplane project run --init-resources/--extra-resources failed with
"encountered an invalid or empty raw resource" whenever a manifest began
with a comment block before the first "---" separator, which is a common
file header. The YAML stream loader returns that comment-only chunk as a
document, it unmarshals to an empty RawExtension, and ApplyResources
rejected it.

kubectl skips empty and null documents; do the same in ApplyResources so
an otherwise valid manifest applies instead of erroring. This fixes both
--init-resources and --extra-resources, which share this code path.

Fixes crossplane#207

Signed-off-by: Yury Tsarev <yury@upbound.io>
@ytsarev
ytsarev force-pushed the fix/skip-empty-init-extra-resources branch from e8cb075 to 776fcd4 Compare July 23, 2026 10:40
ytsarev added a commit to ytsarev/modelplane-public that referenced this pull request Jul 23, 2026
The awk that strips prerequisites.yaml's leading comment-only document
works around a crossplane CLI bug: `crossplane project run
--init-resources` rejects an empty (comment-only) YAML document with
"invalid or empty raw resource", where kubectl skips it.

Filed upstream as crossplane/cli#207 with a fix in crossplane/cli#208.
Once that ships, the awk can be dropped and the file passed through
unmodified.

Signed-off-by: Yury Tsarev <yury@upbound.io>
@haarchri
haarchri merged commit cbdf771 into crossplane:main Jul 23, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: project run --init-resources/--extra-resources reject manifests with a leading comment document

2 participants