-
Notifications
You must be signed in to change notification settings - Fork 9
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
Refactor GS API bindings #352
base: main
Are you sure you want to change the base?
Conversation
c711676
to
efb22e4
Compare
|
||
// explicitlyFalse returns true if the value of the provided | ||
// bool pointer is set to false, nil and true pointer return false | ||
func explicitlyFalse(b *bool) bool { | ||
return b != nil && !pointer.BoolVal(b) | ||
} | ||
|
||
// prefixConfigurationValidCreate validates that prefixes configured as managed | ||
// are not set. This is only relevant for `Create` operations. | ||
func prefixConfigurationValidCreate(c *Cluster) bool { | ||
var ( | ||
internalV4 = c.InternalIPv4Prefix == nil || explicitlyFalse(c.ManageInternalIPv4Prefix) | ||
externalV4 = c.ExternalIPv4Prefix == nil || explicitlyFalse(c.ManageExternalIPv4Prefix) | ||
externalV6 = c.ExternalIPv6Prefix == nil || explicitlyFalse(c.ManageExternalIPv6Prefix) | ||
) | ||
|
||
return internalV4 && externalV4 && externalV6 | ||
} |
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.
TODO: should probably go into a different hook (maybe a new RequestValidationHook)
var _ = Describe("Create Cluster", func() { | ||
DescribeTable("prefix configurations", func(cluster Cluster, expected error) { | ||
_, err := cluster.FilterAPIRequestBody(types.ContextWithOperation(context.TODO(), types.OperationCreate)) | ||
if expected == nil { | ||
Expect(err).ToNot(HaveOccurred()) | ||
} else { | ||
Expect(err).To(MatchError(expected)) | ||
} | ||
}, | ||
Entry("all prefixes implicitly managed", Cluster{}, nil), | ||
Entry("all prefixes explicitly managed", Cluster{ManageInternalIPv4Prefix: pointer.Bool(true), ManageExternalIPv4Prefix: pointer.Bool(true), ManageExternalIPv6Prefix: pointer.Bool(true)}, nil), | ||
Entry("internal v4 prefix implicitly managed and explicitly provided", Cluster{InternalIPv4Prefix: &common.PartialResource{Identifier: "foo"}}, ErrManagedPrefixSet), | ||
Entry("internal v4 prefix explicitly managed and explicitly provided", Cluster{ManageInternalIPv4Prefix: pointer.Bool(true), InternalIPv4Prefix: &common.PartialResource{Identifier: "foo"}}, ErrManagedPrefixSet), | ||
Entry("internal v4 prefix explicitly unmanaged and provided", Cluster{ManageInternalIPv4Prefix: pointer.Bool(false), InternalIPv4Prefix: &common.PartialResource{Identifier: "foo"}}, nil), | ||
Entry("external v4 prefix implicitly managed and explicitly provided", Cluster{ExternalIPv4Prefix: &common.PartialResource{Identifier: "foo"}}, ErrManagedPrefixSet), | ||
Entry("external v4 prefix explicitly managed and explicitly provided", Cluster{ManageExternalIPv4Prefix: pointer.Bool(true), ExternalIPv4Prefix: &common.PartialResource{Identifier: "foo"}}, ErrManagedPrefixSet), | ||
Entry("external v4 prefix explicitly unmanaged and provided", Cluster{ManageExternalIPv4Prefix: pointer.Bool(false), ExternalIPv4Prefix: &common.PartialResource{Identifier: "foo"}}, nil), | ||
Entry("external v6 prefix implicitly managed and explicitly provided", Cluster{ExternalIPv6Prefix: &common.PartialResource{Identifier: "foo"}}, ErrManagedPrefixSet), | ||
Entry("external v6 prefix explicitly managed and explicitly provided", Cluster{ManageExternalIPv6Prefix: pointer.Bool(true), ExternalIPv6Prefix: &common.PartialResource{Identifier: "foo"}}, ErrManagedPrefixSet), | ||
Entry("external v6 prefix explicitly unmanaged and provided", Cluster{ManageExternalIPv6Prefix: pointer.Bool(false), ExternalIPv6Prefix: &common.PartialResource{Identifier: "foo"}}, nil), | ||
) | ||
}) | ||
|
||
var _ = DescribeTable("explicitlyFalse", | ||
func(b *bool, expected bool) { | ||
Expect(explicitlyFalse(b)).To(Equal(expected)) | ||
}, | ||
Entry("explicitly false", pointer.Bool(false), true), | ||
Entry("explicitly true", pointer.Bool(true), false), | ||
Entry("implicitly false (nil)", nil, false), | ||
) |
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.
TODO: restore with new hook
9931a46
to
3bef5d0
Compare
I don't like how we have a ticket to plan this and then there's lots of code already that I have to parse again ^^` Short look only rn, as I actually tried to get another hour of sleep but had another idea to solve this, but looks like you add code to the generic client to handle specific cases? I think we talked about this before.. it's not gonna be maintainable with all the different behaviors we have in different APIs, that's why it was hooks in the first place Our problem is the hooks being attached to the objects and working on the Object they get via the receiver, right? What if we moved all the hooks away from the object types itself and have them optionally implement an interface "client overrides" where we have those hooks instead?
|
Ah so you are essentially doing those things here:
Maybe we should just have |
I did a benchmark and while flattening has a relative high overhead compared to just json encoding, considering the unit of the extra time needed is not a valid concern for an API client IMHO.
Implementing this with generated code would probably not be as trivial as it appears with the needed support for embedded structs.
With the plan to eventually move everything to GS I think it'd be better located in the client core than overriding it for every resource. |
Description
Checklist
Unreleased
section in CHANGELOG.md, if user facing changeReferences
Community Note