Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow resources to contain unexported fields #487

Merged
merged 2 commits into from Mar 6, 2024

Conversation

scothis
Copy link
Contributor

@scothis scothis commented Feb 29, 2024

Using unexported fields in Kubernetes resources is not common, but does happen. Previously, these fields would cause cmp.Diff to panic. Now, we ignore the content of unexported fields when computing a diff. As the diff is for logging and test assertions this is relatively safe.

Semantic equality is used for detecting when a managed resource needs to be updated. Custom equality func can be defined separately as needed.

Refs #484

Using unexported fields in Kubernetes resources is not common, but does
happen. Previously, these fields would cause cmp.Diff to panic. Now, we
ignore the content of unexported fields when computing a diff. As the
diff is for logging and test assertions this is relatively safe.

Semantic equality is used for detecting when a managed resource needs to
be updated. Custom equality func can be defined separately as needed.
Copy link

codecov bot commented Feb 29, 2024

Codecov Report

Attention: Patch coverage is 80.00000% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 61.12%. Comparing base (3180680) to head (09436ab).

Files Patch % Lines
testing/subreconciler.go 0.00% 2 Missing ⚠️
testing/webhook.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #487      +/-   ##
==========================================
+ Coverage   61.03%   61.12%   +0.09%     
==========================================
  Files          27       28       +1     
  Lines        2556     2562       +6     
==========================================
+ Hits         1560     1566       +6     
  Misses        909      909              
  Partials       87       87              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

// +kubebuilder:object:generate=true
type TestResourceUnexportedFieldsSpec struct {
Fields map[string]string `json:"fields,omitempty"`
unexportedFields map[string]string
Copy link

Choose a reason for hiding this comment

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

it wasn't clear to me whether the cmp IgnoreUnexported option works recursively or for the top level struct only. IMHO, it'd be useful to have a nested struct with unexported fields as part of this spec.

Copy link

@neowulf neowulf Feb 29, 2024

Choose a reason for hiding this comment

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

I see you have implemented your own opt! How does it handle nested structs with unexported fields?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Within cmp, each option is applied to every path discovered while walking the object tree. If the path is filtered no fields under it are processed.

Copy link
Collaborator

@mamachanko mamachanko left a comment

Choose a reason for hiding this comment

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

⭐ → @scothis! Thank you very much for the effort. I specifically took note of how you are driving it with TestResourceUnexportedFields.

@neowulf would you be able to test-drive this branch with your kit?

Since ExpectConfig is using IgnoreAllUnexported to verify all CRUD events, there would no more immediate need to open up cmp options for configuration as far as #484 is concerned, or is there? Correct me if I am wrong.

internal/resources/resource_with_unexported_fields.go Outdated Show resolved Hide resolved
internal/resources/resource_with_unexported_fields.go Outdated Show resolved Hide resolved
reconcilers/child_test.go Outdated Show resolved Hide resolved
Comment on lines +16 to +24
var IgnoreAllUnexported = cmp.FilterPath(func(p cmp.Path) bool {
// from cmp.IgnoreUnexported with type info removed
sf, ok := p.Index(-1).(cmp.StructField)
if !ok {
return false
}
r, _ := utf8.DecodeRuneInString(sf.Name())
return !unicode.IsUpper(r)
}, cmp.Ignore())
Copy link
Collaborator

Choose a reason for hiding this comment

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

[praise]: This!

I had started looking into it and had planned to put to the test what was suggested by google/go-cmp#313 (comment). Very glad that you were able to bring it here for good effect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Funny, we cribbed from the same source to arrive at the identical outcome

reconcilers/resource_test.go Outdated Show resolved Hide resolved
reconcilers/cmp.go Show resolved Hide resolved
@@ -427,6 +427,7 @@ var (
return str != "" && !strings.HasPrefix(str, "Status")
}, cmp.Ignore())

// Deprecated: use reconcilers.IgnoreAllUnexported instead
Copy link
Collaborator

Choose a reason for hiding this comment

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

[praise]: Thanks for this! Will schedule removal in next+1 version. https://github.com/search?q=SafeDeployDiff&type=code suggests that it's not used by public projects on Github at least.

@mamachanko
Copy link
Collaborator

[question, non-blocking]: @scothis when you say

Custom equality func can be defined separately as needed.

are you meaning that there could potentially be a follow-up PR? I am asking because afaik there's currently no way to provide a custom equality func, or is there?

@scothis
Copy link
Contributor Author

scothis commented Mar 1, 2024

@mamachanko I was basing it on this comment #484 (comment)

@neowulf
Copy link

neowulf commented Mar 4, 2024

@scothis this fix works! Thank you again for the quick turnaround!

@scothis
Copy link
Contributor Author

scothis commented Mar 5, 2024

@neowulf cool, I'll circle back to Max's feedback tomorrow.

Signed-off-by: Scott Andrews <scott@andrews.me>
@mamachanko mamachanko merged commit e41e6dc into vmware-labs:main Mar 6, 2024
3 checks passed
@mamachanko
Copy link
Collaborator

Thank you @scothis !

@vmwclabot
Copy link

@scothis, VMware has approved your signed contributor license agreement.

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.

None yet

4 participants