Skip to content

Commit

Permalink
Use protobuf for core clients
Browse files Browse the repository at this point in the history
Signed-off-by: Monis Khan <mok@microsoft.com>
  • Loading branch information
enj committed Oct 23, 2024
1 parent fe1eda0 commit c2ae465
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 22 deletions.
1 change: 1 addition & 0 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ function codegen::clients() {
--input-base="k8s.io/api" \
--plural-exceptions "${PLURAL_EXCEPTIONS}" \
--apply-configuration-package "${APPLYCONFIG_PKG}" \
--prefers-protobuf \
$(printf -- " --input %s" "${gv_dirs[@]}") \
"$@"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ kube::codegen::gen_client \
--output-pkg "${THIS_PKG}/pkg/client" \
--versioned-name "clientset" \
--boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" \
--prefers-protobuf \
"${SCRIPT_ROOT}/pkg/apis"
38 changes: 32 additions & 6 deletions staging/src/k8s.io/client-go/gentype/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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},
Expand All @@ -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},
Expand All @@ -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},
Expand All @@ -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).
Expand Down Expand Up @@ -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).
Expand All @@ -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).
Expand All @@ -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).
Expand All @@ -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).
Expand All @@ -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()).
Expand All @@ -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()).
Expand All @@ -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).
Expand All @@ -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).
Expand All @@ -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).
Expand All @@ -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()).
Expand Down Expand Up @@ -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()).
Expand Down
32 changes: 26 additions & 6 deletions staging/src/k8s.io/client-go/rest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions staging/src/k8s.io/code-generator/cmd/client-gen/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ type Args struct {
// If non-empty, Apply functions are generated for each type and reference the apply builders.
// If empty (""), Apply functions are not generated.
ApplyConfigurationPackage string

// PrefersProtobuf determines if the generated clientset uses protobuf for API requests.
PrefersProtobuf bool
}

func New() *Args {
Expand Down Expand Up @@ -99,6 +102,8 @@ func (args *Args) AddFlags(fs *pflag.FlagSet, inputBase string) {
"list of comma separated plural exception definitions in Type:PluralizedType form")
fs.StringVar(&args.ApplyConfigurationPackage, "apply-configuration-package", args.ApplyConfigurationPackage,
"optional package of apply configurations, generated by applyconfiguration-gen, that are required to generate Apply functions for each type in the clientset. By default Apply functions are not generated.")
fs.BoolVar(&args.PrefersProtobuf, "prefers-protobuf", args.PrefersProtobuf,
"when set, client-gen will generate a clientset that uses protobuf for API requests")

// support old flags
fs.SetNormalizeFunc(mapFlagName("clientset-path", "output-pkg", fs.GetNormalizeFunc()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func DefaultNameSystem() string {
return "public"
}

func targetForGroup(gv clientgentypes.GroupVersion, typeList []*types.Type, clientsetDir, clientsetPkg string, groupPkgName string, groupGoName string, apiPath string, inputPkg string, applyBuilderPkg string, boilerplate []byte) generator.Target {
func targetForGroup(gv clientgentypes.GroupVersion, typeList []*types.Type, clientsetDir, clientsetPkg string, groupPkgName string, groupGoName string, apiPath string, inputPkg string, applyBuilderPkg string, boilerplate []byte, prefersProtobuf bool) generator.Target {
subdir := []string{"typed", strings.ToLower(groupPkgName), strings.ToLower(gv.Version.NonEmpty())}
gvDir := filepath.Join(clientsetDir, filepath.Join(subdir...))
gvPkg := path.Join(clientsetPkg, path.Join(subdir...))
Expand Down Expand Up @@ -160,6 +160,7 @@ func targetForGroup(gv clientgentypes.GroupVersion, typeList []*types.Type, clie
group: gv.Group.NonEmpty(),
version: gv.Version.String(),
groupGoName: groupGoName,
prefersProtobuf: prefersProtobuf,
typeToMatch: t,
imports: generator.NewImportTrackerForPackage(gvPkg),
})
Expand Down Expand Up @@ -424,7 +425,7 @@ func GetTargets(context *generator.Context, args *args.Args) []generator.Target
targetForGroup(
gv, orderer.OrderTypes(types), clientsetDir, clientsetPkg,
group.PackageName, groupGoNames[gv], args.ClientsetAPIPath,
inputPath, args.ApplyConfigurationPackage, boilerplate))
inputPath, args.ApplyConfigurationPackage, boilerplate, args.PrefersProtobuf))
if args.FakeClient {
targetList = append(targetList,
fake.TargetForGroup(gv, orderer.OrderTypes(types), clientsetDir, clientsetPkg, group.PackageName, groupGoNames[gv], inputPath, args.ApplyConfigurationPackage, boilerplate))
Expand Down
Loading

0 comments on commit c2ae465

Please sign in to comment.