fix(types): make secret reference fields optional in initProvider - #727
fix(types): make secret reference fields optional in initProvider#727pujitha24 wants to merge 1 commit into
Conversation
Motivation: NewSensitiveField (pkg/types/field.go) only wrapped a generated secret-reference field (e.g. *v1.SecretKeySelector) in a pointer with an omitempty JSON tag when the underlying Terraform field was itself Optional. When the Terraform field was Required, the generated field stayed a non-pointer type with no omitempty tag. Since forProvider and initProvider share the same Field.FieldType and Field.JSONTag for secret references, this made the initProvider secret reference required in the CRD schema too, even though initProvider fields are always meant to be optional. Users who wanted to set a secret only in forProvider were forced to duplicate it into initProvider as well. Approach: Make the pointer-wrap and omitempty tag unconditional in NewSensitiveField, independent of the source field's Terraform required/optional-ness. This matches how ordinary (non-sensitive) fields already behave: scalar Go types are always pointers (pkg/types/builder.go buildSchema), and the base JSON tag always defaults to omitempty (pkg/types/field.go NewField); required-ness for forProvider is enforced separately via an explicit kubebuilder marker and a CEL validation rule, not via the Go type/tag, so forProvider's required behavior for a required sensitive field is unchanged. Validation: Extended the table-driven TestBuild cases Sensitive_Fields and Sensitive_Fields_namespaced in pkg/types/builder_test.go to assert on the generated initProvider struct, not just forProvider. Confirmed via `git stash` on pkg/types/field.go alone that these assertions FAIL against the pre-fix code and PASS with the fix applied: go test ./pkg/types/... -run TestBuild -v Also ran, all passing: go build ./... go vet ./pkg/... go test ./pkg/... Report: crossplane#456 Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
📝 WalkthroughWalkthroughSensitive secret-reference fields now generate optional pointer types with ChangesSensitive field reference generation
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 7✅ Passed checks (7 passed)
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. Comment |
There was a problem hiding this comment.
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 `@pkg/types/builder_test.go`:
- Around line 871-875: Update the InitProvider assertion in the builder test to
fail when tc.want.initProvider is non-empty and g.InitProviderType is nil,
before calling Obj().String(). Preserve the existing cmp.Diff validation for
non-nil generated types.
🪄 Autofix
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: f3929ed1-373b-49ca-a673-8f500339c0b3
📒 Files selected for processing (2)
pkg/types/builder_test.gopkg/types/field.go
| if tc.want.initProvider != "" && g.InitProviderType != nil { | ||
| if diff := cmp.Diff(tc.want.initProvider, g.InitProviderType.Obj().String()); diff != "" { | ||
| t.Fatalf("Build(...): -want initProvider, +got initProvider: %s", diff) | ||
| } | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Fail when the generated InitProvider type is missing.
If tc.want.initProvider is non-empty but g.InitProviderType is nil, this condition skips the assertion and the test passes. Check for nil and fail before calling Obj().String().
Proposed fix
- if tc.want.initProvider != "" && g.InitProviderType != nil {
+ if tc.want.initProvider != "" {
+ if g.InitProviderType == nil {
+ t.Fatalf("Build(...): expected initProvider type, got nil")
+ }
if diff := cmp.Diff(tc.want.initProvider, g.InitProviderType.Obj().String()); diff != "" {
t.Fatalf("Build(...): -want initProvider, +got initProvider: %s", diff)
}
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if tc.want.initProvider != "" && g.InitProviderType != nil { | |
| if diff := cmp.Diff(tc.want.initProvider, g.InitProviderType.Obj().String()); diff != "" { | |
| t.Fatalf("Build(...): -want initProvider, +got initProvider: %s", diff) | |
| } | |
| } | |
| if tc.want.initProvider != "" { | |
| if g.InitProviderType == nil { | |
| t.Fatalf("Build(...): expected initProvider type, got nil") | |
| } | |
| if diff := cmp.Diff(tc.want.initProvider, g.InitProviderType.Obj().String()); diff != "" { | |
| t.Fatalf("Build(...): -want initProvider, +got initProvider: %s", diff) | |
| } | |
| } |
🤖 Prompt for 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.
In `@pkg/types/builder_test.go` around lines 871 - 875, Update the InitProvider
assertion in the builder test to fail when tc.want.initProvider is non-empty and
g.InitProviderType is nil, before calling Obj().String(). Preserve the existing
cmp.Diff validation for non-nil generated types.
|
This has been sitting green and rebased for a couple weeks now — happy to make any changes that would help move review along. |
Fixes #456
I have:
make reviewableto ensure this PR is ready for review.backport release-x.ylabels to auto-backport this PR if necessary.How has this code been tested
Extended the table-driven
TestBuildcasesSensitive_FieldsandSensitive_Fields_namespacedinpkg/types/builder_test.goto assert on the generatedinitProviderstruct, not justforProvider. Confirmed viagit stashonpkg/types/field.goalone that these assertions FAIL against the pre-fix code and PASS with the fix applied:Also ran, all passing:
AI assistance: this change was drafted with Claude Code.