Use protobuf for core clients

Signed-off-by: Monis Khan <mok@microsoft.com>

Kubernetes-commit: c2ae465355b5222dbc27b8de8c55095b4f1b431a
This commit is contained in:
Monis Khan 2024-08-19 10:37:15 -04:00 committed by Kubernetes Publisher
parent 1647efd5c4
commit 6f44458e5e
2 changed files with 58 additions and 12 deletions

View File

@ -51,6 +51,8 @@ type Client[T objectWithMeta] struct {
namespace string // "" for non-namespaced clients
newObject func() T
parameterCodec runtime.ParameterCodec
prefersProtobuf bool
}
// ClientWithList represents a client with support for lists.
@ -82,26 +84,37 @@ type alsoApplier[T objectWithMeta, C namedObject] struct {
client *Client[T]
}
type Option[T objectWithMeta] func(*Client[T])
func PrefersProtobuf[T objectWithMeta]() Option[T] {
return func(c *Client[T]) { c.prefersProtobuf = true }
}
// NewClient constructs a client, namespaced or not, with no support for lists or apply.
// Non-namespaced clients are constructed by passing an empty namespace ("").
func NewClient[T objectWithMeta](
resource string, client rest.Interface, parameterCodec runtime.ParameterCodec, namespace string, emptyObjectCreator func() T,
options ...Option[T],
) *Client[T] {
return &Client[T]{
c := &Client[T]{
resource: resource,
client: client,
parameterCodec: parameterCodec,
namespace: namespace,
newObject: emptyObjectCreator,
}
for _, option := range options {
option(c)
}
return c
}
// NewClientWithList constructs a namespaced client with support for lists.
func NewClientWithList[T objectWithMeta, L runtime.Object](
resource string, client rest.Interface, parameterCodec runtime.ParameterCodec, namespace string, emptyObjectCreator func() T,
emptyListCreator func() L,
emptyListCreator func() L, options ...Option[T],
) *ClientWithList[T, L] {
typeClient := NewClient[T](resource, client, parameterCodec, namespace, emptyObjectCreator)
typeClient := NewClient[T](resource, client, parameterCodec, namespace, emptyObjectCreator, options...)
return &ClientWithList[T, L]{
typeClient,
alsoLister[T, L]{typeClient, emptyListCreator},
@ -111,8 +124,9 @@ func NewClientWithList[T objectWithMeta, L runtime.Object](
// NewClientWithApply constructs a namespaced client with support for apply declarative configurations.
func NewClientWithApply[T objectWithMeta, C namedObject](
resource string, client rest.Interface, parameterCodec runtime.ParameterCodec, namespace string, emptyObjectCreator func() T,
options ...Option[T],
) *ClientWithApply[T, C] {
typeClient := NewClient[T](resource, client, parameterCodec, namespace, emptyObjectCreator)
typeClient := NewClient[T](resource, client, parameterCodec, namespace, emptyObjectCreator, options...)
return &ClientWithApply[T, C]{
typeClient,
alsoApplier[T, C]{typeClient},
@ -122,9 +136,9 @@ func NewClientWithApply[T objectWithMeta, C namedObject](
// NewClientWithListAndApply constructs a client with support for lists and applying declarative configurations.
func NewClientWithListAndApply[T objectWithMeta, L runtime.Object, C namedObject](
resource string, client rest.Interface, parameterCodec runtime.ParameterCodec, namespace string, emptyObjectCreator func() T,
emptyListCreator func() L,
emptyListCreator func() L, options ...Option[T],
) *ClientWithListAndApply[T, L, C] {
typeClient := NewClient[T](resource, client, parameterCodec, namespace, emptyObjectCreator)
typeClient := NewClient[T](resource, client, parameterCodec, namespace, emptyObjectCreator, options...)
return &ClientWithListAndApply[T, L, C]{
typeClient,
alsoLister[T, L]{typeClient, emptyListCreator},
@ -146,6 +160,7 @@ func (c *Client[T]) GetNamespace() string {
func (c *Client[T]) Get(ctx context.Context, name string, options metav1.GetOptions) (T, error) {
result := c.newObject()
err := c.client.Get().
UseProtobufAsDefaultIfPreferred(c.prefersProtobuf).
NamespaceIfScoped(c.namespace, c.namespace != "").
Resource(c.resource).
Name(name).
@ -181,6 +196,7 @@ func (l *alsoLister[T, L]) list(ctx context.Context, opts metav1.ListOptions) (L
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second
}
err := l.client.client.Get().
UseProtobufAsDefaultIfPreferred(l.client.prefersProtobuf).
NamespaceIfScoped(l.client.namespace, l.client.namespace != "").
Resource(l.client.resource).
VersionedParams(&opts, l.client.parameterCodec).
@ -198,6 +214,7 @@ func (l *alsoLister[T, L]) watchList(ctx context.Context, opts metav1.ListOption
}
result = l.newList()
err = l.client.client.Get().
UseProtobufAsDefaultIfPreferred(l.client.prefersProtobuf).
NamespaceIfScoped(l.client.namespace, l.client.namespace != "").
Resource(l.client.resource).
VersionedParams(&opts, l.client.parameterCodec).
@ -215,6 +232,7 @@ func (c *Client[T]) Watch(ctx context.Context, opts metav1.ListOptions) (watch.I
}
opts.Watch = true
return c.client.Get().
UseProtobufAsDefaultIfPreferred(c.prefersProtobuf).
NamespaceIfScoped(c.namespace, c.namespace != "").
Resource(c.resource).
VersionedParams(&opts, c.parameterCodec).
@ -226,6 +244,7 @@ func (c *Client[T]) Watch(ctx context.Context, opts metav1.ListOptions) (watch.I
func (c *Client[T]) Create(ctx context.Context, obj T, opts metav1.CreateOptions) (T, error) {
result := c.newObject()
err := c.client.Post().
UseProtobufAsDefaultIfPreferred(c.prefersProtobuf).
NamespaceIfScoped(c.namespace, c.namespace != "").
Resource(c.resource).
VersionedParams(&opts, c.parameterCodec).
@ -239,6 +258,7 @@ func (c *Client[T]) Create(ctx context.Context, obj T, opts metav1.CreateOptions
func (c *Client[T]) Update(ctx context.Context, obj T, opts metav1.UpdateOptions) (T, error) {
result := c.newObject()
err := c.client.Put().
UseProtobufAsDefaultIfPreferred(c.prefersProtobuf).
NamespaceIfScoped(c.namespace, c.namespace != "").
Resource(c.resource).
Name(obj.GetName()).
@ -253,6 +273,7 @@ func (c *Client[T]) Update(ctx context.Context, obj T, opts metav1.UpdateOptions
func (c *Client[T]) UpdateStatus(ctx context.Context, obj T, opts metav1.UpdateOptions) (T, error) {
result := c.newObject()
err := c.client.Put().
UseProtobufAsDefaultIfPreferred(c.prefersProtobuf).
NamespaceIfScoped(c.namespace, c.namespace != "").
Resource(c.resource).
Name(obj.GetName()).
@ -267,6 +288,7 @@ func (c *Client[T]) UpdateStatus(ctx context.Context, obj T, opts metav1.UpdateO
// Delete takes name of the resource and deletes it. Returns an error if one occurs.
func (c *Client[T]) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
return c.client.Delete().
UseProtobufAsDefaultIfPreferred(c.prefersProtobuf).
NamespaceIfScoped(c.namespace, c.namespace != "").
Resource(c.resource).
Name(name).
@ -282,6 +304,7 @@ func (l *alsoLister[T, L]) DeleteCollection(ctx context.Context, opts metav1.Del
timeout = time.Duration(*listOpts.TimeoutSeconds) * time.Second
}
return l.client.client.Delete().
UseProtobufAsDefaultIfPreferred(l.client.prefersProtobuf).
NamespaceIfScoped(l.client.namespace, l.client.namespace != "").
Resource(l.client.resource).
VersionedParams(&listOpts, l.client.parameterCodec).
@ -295,6 +318,7 @@ func (l *alsoLister[T, L]) DeleteCollection(ctx context.Context, opts metav1.Del
func (c *Client[T]) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (T, error) {
result := c.newObject()
err := c.client.Patch(pt).
UseProtobufAsDefaultIfPreferred(c.prefersProtobuf).
NamespaceIfScoped(c.namespace, c.namespace != "").
Resource(c.resource).
Name(name).
@ -321,6 +345,7 @@ func (a *alsoApplier[T, C]) Apply(ctx context.Context, obj C, opts metav1.ApplyO
return *new(T), fmt.Errorf("obj.Name must be provided to Apply")
}
err = a.client.client.Patch(types.ApplyPatchType).
UseProtobufAsDefaultIfPreferred(a.client.prefersProtobuf).
NamespaceIfScoped(a.client.namespace, a.client.namespace != "").
Resource(a.client.resource).
Name(*obj.GetName()).
@ -348,6 +373,7 @@ func (a *alsoApplier[T, C]) ApplyStatus(ctx context.Context, obj C, opts metav1.
result := a.client.newObject()
err = a.client.client.Patch(types.ApplyPatchType).
UseProtobufAsDefaultIfPreferred(a.client.prefersProtobuf).
NamespaceIfScoped(a.client.namespace, a.client.namespace != "").
Resource(a.client.resource).
Name(*obj.GetName()).

View File

@ -176,12 +176,7 @@ func NewRequest(c *RESTClient) *Request {
contentTypeNotSet: contentTypeNotSet,
}
switch {
case len(c.content.AcceptContentTypes) > 0:
r.SetHeader("Accept", c.content.AcceptContentTypes)
case len(c.content.ContentType) > 0:
r.SetHeader("Accept", c.content.ContentType+", */*")
}
r.setAcceptHeader()
return r
}
@ -195,6 +190,31 @@ func NewRequestWithClient(base *url.URL, versionedAPIPath string, content Client
})
}
func (r *Request) UseProtobufAsDefaultIfPreferred(prefersProtobuf bool) *Request {
if prefersProtobuf {
return r.UseProtobufAsDefault()
}
return r
}
func (r *Request) UseProtobufAsDefault() *Request {
if r.contentTypeNotSet && len(r.contentConfig.AcceptContentTypes) == 0 {
r.contentConfig.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
r.contentConfig.ContentType = "application/vnd.kubernetes.protobuf"
r.setAcceptHeader()
}
return r
}
func (r *Request) setAcceptHeader() {
switch {
case len(r.contentConfig.AcceptContentTypes) > 0:
r.SetHeader("Accept", r.contentConfig.AcceptContentTypes)
case len(r.contentConfig.ContentType) > 0:
r.SetHeader("Accept", r.contentConfig.ContentType+", */*")
}
}
// Verb sets the verb this request will use.
func (r *Request) Verb(verb string) *Request {
r.verb = verb