fix: respect --context flag and support Crossplane v2 composition paths #184
+88
−29
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes two related bugs that prevented the tool from working correctly with non-default Kubernetes contexts and Crossplane v2 XRDs.
Bug 1:
--contextflag not being respectedProblem
The
--contextCLI flag was being ignored. The tool always used the default kubeconfig context regardless of what was specified on the command line.Root Cause
Kong's dependency injection was calling
getRestConfig()during provider initialization, which happened before the CLI flags were parsed. This meantCommonCmdFields.Contextwas always empty whengetRestConfig()was called.Fix
--contextflag early in theBeforeApply()hook using a package-level variablegetRestConfig()to reference this captured valuexr.goandcomp.gouseGetRestConfig()method to get the correctly-configured clientBug 2: Crossplane v2 composition selection failing
Problem
For Crossplane v2 XRDs, the tool failed to find
compositionSelectorandcompositionReffields, resulting in:Root Cause
The tool only looked for these fields at
spec.crossplane.compositionSelector(v2 path), but some XRs have these fields directly atspec.compositionSelector(v1-style path, which is also valid for v2).Fix
Modified
makeCrossplaneRefPath()to return multiple possible paths, and updated all functions that use these paths to try both locations:findByDirectReference()findByLabelSelector()getCompositionRevisionRef()getCompositionUpdatePolicy()resourceUsesComposition()Files Changed
cmd/diff/main.go: Capture--contextflag inBeforeApply()cmd/diff/xr.go: UseGetRestConfig()methodcmd/diff/comp.go: UseGetRestConfig()methodcmd/diff/client/crossplane/composition_client.go: Support both v1 and v2 pathsTesting
Tested locally with:
--contextflag pointing to a specific vcluster contextcompositionSelectorwith label matchingBefore fix: Tool found only 10 XRDs (from wrong context) and failed composition selection
After fix: Tool found 44 XRDs (22 v1 + 22 v2) and correctly matched compositions
I have:
earthly +reviewableto ensure this PR is ready for review.- [ ] Followed the API promotion workflow if this PR introduces, removes, or promotes an API.Need help with this checklist? See the cheat sheet.