-
Notifications
You must be signed in to change notification settings - Fork 1.4k
⚠️ Change ClusterResourceSetBinding Bindings field from []*ResourceSetBinding to []ResourceSetBinding #12476
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -92,14 +92,14 @@ func (r *ResourceSetBinding) SetBinding(resourceBinding ResourceBinding) { | |||||
// GetOrCreateBinding returns the ResourceSetBinding for a given ClusterResourceSet if exists, | ||||||
// otherwise creates one and updates ClusterResourceSet with it. | ||||||
func (c *ClusterResourceSetBinding) GetOrCreateBinding(clusterResourceSet *ClusterResourceSet) *ResourceSetBinding { | ||||||
for _, binding := range c.Spec.Bindings { | ||||||
if binding.ClusterResourceSetName == clusterResourceSet.Name { | ||||||
return binding | ||||||
for i := range c.Spec.Bindings { | ||||||
if c.Spec.Bindings[i].ClusterResourceSetName == clusterResourceSet.Name { | ||||||
return &c.Spec.Bindings[i] | ||||||
} | ||||||
} | ||||||
binding := &ResourceSetBinding{ClusterResourceSetName: clusterResourceSet.Name, Resources: []ResourceBinding{}} | ||||||
binding := ResourceSetBinding{ClusterResourceSetName: clusterResourceSet.Name, Resources: []ResourceBinding{}} | ||||||
c.Spec.Bindings = append(c.Spec.Bindings, binding) | ||||||
return binding | ||||||
return &c.Spec.Bindings[len(c.Spec.Bindings)-1] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think it's simpler but is this to make the format consistent with the above? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simpler but it's not necessarily correct. I'm sure about the case above, I'm not entirely sure if binding here is the same element as in the slice (probably both would work here in this case, but I want to play it safe) |
||||||
} | ||||||
|
||||||
// RemoveBinding removes the ClusterResourceSet from the ClusterResourceSetBinding Bindings list. | ||||||
|
@@ -138,7 +138,7 @@ type ClusterResourceSetBindingSpec struct { | |||||
// bindings is a list of ClusterResourceSets and their resources. | ||||||
// +optional | ||||||
// +kubebuilder:validation:MaxItems=100 | ||||||
Bindings []*ResourceSetBinding `json:"bindings,omitempty"` | ||||||
Bindings []ResourceSetBinding `json:"bindings,omitempty"` | ||||||
|
||||||
// clusterName is the name of the Cluster this binding applies to. | ||||||
// +required | ||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've read the discussion and commented.
I can confirm that the copyloopvar linter is already in place and the original code is fine.
but it's certainly a good idea to keep it this way in terms of visualization.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original code was not fine, that's why I changed it :) (tests broke)
The original code with the pointer change returned a copy of the binding and not the same binding that is part of the c.Spec.Bindings slice
The code is fine for the linter, but that doesn't mean it works as expected :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. Surely the only this codes aren't problem, but considering about entire code base, the copy element might be invalid. This case is good example.
Thanks!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To give some more context.
The previous problem (a few Go versions ago) with returning a pointer to a loop var was that the pointer was basically always referring to the last element that was stored in the loop var.
Then they changed it in go so a pointer to the loop var refers to the correct element.
But the loop var is a copy of the slice element not the actual element