2020-10-21 05:49:41 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-01 17:06:36 +00:00
|
|
|
"errors"
|
2020-10-21 05:49:41 +00:00
|
|
|
"fmt"
|
2023-06-01 17:01:19 +00:00
|
|
|
"net/http"
|
2021-06-25 05:02:01 +00:00
|
|
|
"strings"
|
2020-10-21 05:49:41 +00:00
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
2021-06-25 05:02:01 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-10-21 05:49:41 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/serializer"
|
|
|
|
"k8s.io/client-go/kubernetes/scheme"
|
2021-06-25 05:02:01 +00:00
|
|
|
"k8s.io/client-go/metadata"
|
2020-10-21 05:49:41 +00:00
|
|
|
"k8s.io/client-go/rest"
|
2021-06-25 05:02:01 +00:00
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
|
2021-06-25 05:02:01 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
2020-10-21 05:49:41 +00:00
|
|
|
)
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// Options are creation options for a Client.
|
|
|
|
type Options struct {
|
|
|
|
// HTTPClient is the HTTP client to use for requests.
|
|
|
|
HTTPClient *http.Client
|
|
|
|
|
|
|
|
// Scheme, if provided, will be used to map go structs to GroupVersionKinds
|
|
|
|
Scheme *runtime.Scheme
|
|
|
|
|
|
|
|
// Mapper, if provided, will be used to map GroupVersionKinds to Resources
|
|
|
|
Mapper meta.RESTMapper
|
|
|
|
|
|
|
|
// Cache, if provided, is used to read objects from the cache.
|
|
|
|
Cache *CacheOptions
|
|
|
|
|
|
|
|
// WarningHandler is used to configure the warning handler responsible for
|
|
|
|
// surfacing and handling warnings messages sent by the API server.
|
|
|
|
WarningHandler WarningHandlerOptions
|
|
|
|
|
|
|
|
// DryRun instructs the client to only perform dry run requests.
|
|
|
|
DryRun *bool
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// WarningHandlerOptions are options for configuring a
|
|
|
|
// warning handler for the client which is responsible
|
|
|
|
// for surfacing API Server warnings.
|
|
|
|
type WarningHandlerOptions struct {
|
|
|
|
// SuppressWarnings decides if the warnings from the
|
|
|
|
// API server are suppressed or surfaced in the client.
|
|
|
|
SuppressWarnings bool
|
|
|
|
// AllowDuplicateLogs does not deduplicate the to-be
|
|
|
|
// logged surfaced warnings messages. See
|
|
|
|
// log.WarningHandlerOptions for considerations
|
2023-02-01 17:06:36 +00:00
|
|
|
// regarding deduplication
|
2021-06-25 05:02:01 +00:00
|
|
|
AllowDuplicateLogs bool
|
|
|
|
}
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// CacheOptions are options for creating a cache-backed client.
|
|
|
|
type CacheOptions struct {
|
|
|
|
// Reader is a cache-backed reader that will be used to read objects from the cache.
|
|
|
|
// +required
|
|
|
|
Reader Reader
|
2023-08-28 20:44:55 +00:00
|
|
|
// DisableFor is a list of objects that should never be read from the cache.
|
|
|
|
// Objects configured here always result in a live lookup.
|
2023-06-01 17:01:19 +00:00
|
|
|
DisableFor []Object
|
|
|
|
// Unstructured is a flag that indicates whether the cache-backed client should
|
|
|
|
// read unstructured objects or lists from the cache.
|
2023-08-28 20:44:55 +00:00
|
|
|
// If false, unstructured objects will always result in a live lookup.
|
2023-06-01 17:01:19 +00:00
|
|
|
Unstructured bool
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// NewClientFunc allows a user to define how to create a client.
|
|
|
|
type NewClientFunc func(config *rest.Config, options Options) (Client, error)
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
// New returns a new Client using the provided config and Options.
|
|
|
|
//
|
2024-02-01 13:08:54 +00:00
|
|
|
// The client's read behavior is determined by Options.Cache.
|
|
|
|
// If either Options.Cache or Options.Cache.Reader is nil,
|
|
|
|
// the client reads directly from the API server.
|
|
|
|
// If both Options.Cache and Options.Cache.Reader are non-nil,
|
|
|
|
// the client reads from a local cache. However, specific
|
|
|
|
// resources can still be configured to bypass the cache based
|
|
|
|
// on Options.Cache.Unstructured and Options.Cache.DisableFor.
|
|
|
|
// Write operations are always performed directly on the API server.
|
|
|
|
//
|
|
|
|
// The client understands how to work with normal types (both custom resources
|
|
|
|
// and aggregated/built-in resources), as well as unstructured types.
|
2020-10-21 05:49:41 +00:00
|
|
|
// In the case of normal types, the scheme will be used to look up the
|
|
|
|
// corresponding group, version, and kind for the given type. In the
|
|
|
|
// case of unstructured types, the group, version, and kind will be extracted
|
|
|
|
// from the corresponding fields on the object.
|
2023-06-01 17:01:19 +00:00
|
|
|
func New(config *rest.Config, options Options) (c Client, err error) {
|
|
|
|
c, err = newClient(config, options)
|
|
|
|
if err == nil && options.DryRun != nil && *options.DryRun {
|
|
|
|
c = NewDryRunClient(c)
|
|
|
|
}
|
|
|
|
return c, err
|
2021-06-25 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newClient(config *rest.Config, options Options) (*client, error) {
|
2020-10-21 05:49:41 +00:00
|
|
|
if config == nil {
|
|
|
|
return nil, fmt.Errorf("must provide non-nil rest.Config to client.New")
|
|
|
|
}
|
|
|
|
|
2023-08-14 20:06:34 +00:00
|
|
|
config = rest.CopyConfig(config)
|
|
|
|
if config.UserAgent == "" {
|
|
|
|
config.UserAgent = rest.DefaultKubernetesUserAgent()
|
|
|
|
}
|
|
|
|
|
2024-08-12 20:39:28 +00:00
|
|
|
// By default, we de-duplicate and surface warnings.
|
|
|
|
config.WarningHandler = log.NewKubeAPIWarningLogger(
|
|
|
|
log.Log.WithName("KubeAPIWarningLogger"),
|
|
|
|
log.KubeAPIWarningLoggerOptions{
|
|
|
|
Deduplicate: !options.WarningHandler.AllowDuplicateLogs,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if options.WarningHandler.SuppressWarnings {
|
|
|
|
config.WarningHandler = rest.NoWarnings{}
|
2021-06-25 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// Use the rest HTTP client for the provided config if unset
|
|
|
|
if options.HTTPClient == nil {
|
|
|
|
var err error
|
|
|
|
options.HTTPClient, err = rest.HTTPClientFor(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
// Init a scheme if none provided
|
|
|
|
if options.Scheme == nil {
|
|
|
|
options.Scheme = scheme.Scheme
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init a Mapper if none provided
|
|
|
|
if options.Mapper == nil {
|
|
|
|
var err error
|
2023-06-01 17:01:19 +00:00
|
|
|
options.Mapper, err = apiutil.NewDynamicRESTMapper(config, options.HTTPClient)
|
2020-10-21 05:49:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
resources := &clientRestResources{
|
|
|
|
httpClient: options.HTTPClient,
|
|
|
|
config: config,
|
|
|
|
scheme: options.Scheme,
|
|
|
|
mapper: options.Mapper,
|
|
|
|
codecs: serializer.NewCodecFactory(options.Scheme),
|
2021-06-25 05:02:01 +00:00
|
|
|
|
|
|
|
structuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta),
|
|
|
|
unstructuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta),
|
|
|
|
}
|
|
|
|
|
2023-08-14 20:06:34 +00:00
|
|
|
rawMetaClient, err := metadata.NewForConfigAndClient(metadata.ConfigFor(config), options.HTTPClient)
|
2021-06-25 05:02:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to construct metadata-only client for use as part of client: %w", err)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c := &client{
|
|
|
|
typedClient: typedClient{
|
2023-06-01 17:01:19 +00:00
|
|
|
resources: resources,
|
2020-10-21 05:49:41 +00:00
|
|
|
paramCodec: runtime.NewParameterCodec(options.Scheme),
|
|
|
|
},
|
|
|
|
unstructuredClient: unstructuredClient{
|
2023-06-01 17:01:19 +00:00
|
|
|
resources: resources,
|
2020-10-21 05:49:41 +00:00
|
|
|
paramCodec: noConversionParamCodec{},
|
|
|
|
},
|
2021-06-25 05:02:01 +00:00
|
|
|
metadataClient: metadataClient{
|
|
|
|
client: rawMetaClient,
|
|
|
|
restMapper: options.Mapper,
|
|
|
|
},
|
|
|
|
scheme: options.Scheme,
|
|
|
|
mapper: options.Mapper,
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
2023-06-01 17:01:19 +00:00
|
|
|
if options.Cache == nil || options.Cache.Reader == nil {
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want a cache if we're here.
|
|
|
|
// Set the cache.
|
|
|
|
c.cache = options.Cache.Reader
|
2020-10-21 05:49:41 +00:00
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// Load uncached GVKs.
|
|
|
|
c.cacheUnstructured = options.Cache.Unstructured
|
|
|
|
c.uncachedGVKs = map[schema.GroupVersionKind]struct{}{}
|
|
|
|
for _, obj := range options.Cache.DisableFor {
|
|
|
|
gvk, err := c.GroupVersionKindFor(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.uncachedGVKs[gvk] = struct{}{}
|
|
|
|
}
|
2020-10-21 05:49:41 +00:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Client = &client{}
|
|
|
|
|
2024-02-01 13:08:54 +00:00
|
|
|
// client is a client.Client configured to either read from a local cache or directly from the API server.
|
|
|
|
// Write operations are always performed directly on the API server.
|
2023-06-01 17:01:19 +00:00
|
|
|
// It lazily initializes new clients at the time they are used.
|
2020-10-21 05:49:41 +00:00
|
|
|
type client struct {
|
|
|
|
typedClient typedClient
|
|
|
|
unstructuredClient unstructuredClient
|
2021-06-25 05:02:01 +00:00
|
|
|
metadataClient metadataClient
|
|
|
|
scheme *runtime.Scheme
|
|
|
|
mapper meta.RESTMapper
|
2023-06-01 17:01:19 +00:00
|
|
|
|
|
|
|
cache Reader
|
|
|
|
uncachedGVKs map[schema.GroupVersionKind]struct{}
|
|
|
|
cacheUnstructured bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) shouldBypassCache(obj runtime.Object) (bool, error) {
|
|
|
|
if c.cache == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gvk, err := c.GroupVersionKindFor(obj)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
// TODO: this is producing unsafe guesses that don't actually work,
|
|
|
|
// but it matches ~99% of the cases out there.
|
|
|
|
if meta.IsListType(obj) {
|
|
|
|
gvk.Kind = strings.TrimSuffix(gvk.Kind, "List")
|
|
|
|
}
|
|
|
|
if _, isUncached := c.uncachedGVKs[gvk]; isUncached {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if !c.cacheUnstructured {
|
|
|
|
_, isUnstructured := obj.(runtime.Unstructured)
|
|
|
|
return isUnstructured, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// resetGroupVersionKind is a helper function to restore and preserve GroupVersionKind on an object.
|
|
|
|
func (c *client) resetGroupVersionKind(obj runtime.Object, gvk schema.GroupVersionKind) {
|
|
|
|
if gvk != schema.EmptyObjectKind.GroupVersionKind() {
|
|
|
|
if v, ok := obj.(schema.ObjectKind); ok {
|
|
|
|
v.SetGroupVersionKind(gvk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
// GroupVersionKindFor returns the GroupVersionKind for the given object.
|
|
|
|
func (c *client) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) {
|
|
|
|
return apiutil.GVKForObject(obj, c.scheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsObjectNamespaced returns true if the GroupVersionKind of the object is namespaced.
|
|
|
|
func (c *client) IsObjectNamespaced(obj runtime.Object) (bool, error) {
|
|
|
|
return apiutil.IsObjectNamespaced(obj, c.scheme, c.mapper)
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Scheme returns the scheme this client is using.
|
|
|
|
func (c *client) Scheme() *runtime.Scheme {
|
|
|
|
return c.scheme
|
|
|
|
}
|
|
|
|
|
|
|
|
// RESTMapper returns the scheme this client is using.
|
|
|
|
func (c *client) RESTMapper() meta.RESTMapper {
|
|
|
|
return c.mapper
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create implements client.Client.
|
|
|
|
func (c *client) Create(ctx context.Context, obj Object, opts ...CreateOption) error {
|
|
|
|
switch obj.(type) {
|
2023-06-01 17:01:19 +00:00
|
|
|
case runtime.Unstructured:
|
2020-10-21 05:49:41 +00:00
|
|
|
return c.unstructuredClient.Create(ctx, obj, opts...)
|
2021-06-25 05:02:01 +00:00
|
|
|
case *metav1.PartialObjectMetadata:
|
|
|
|
return fmt.Errorf("cannot create using only metadata")
|
|
|
|
default:
|
|
|
|
return c.typedClient.Create(ctx, obj, opts...)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Update implements client.Client.
|
|
|
|
func (c *client) Update(ctx context.Context, obj Object, opts ...UpdateOption) error {
|
2020-10-21 05:49:41 +00:00
|
|
|
defer c.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
|
2021-06-25 05:02:01 +00:00
|
|
|
switch obj.(type) {
|
2023-06-01 17:01:19 +00:00
|
|
|
case runtime.Unstructured:
|
2020-10-21 05:49:41 +00:00
|
|
|
return c.unstructuredClient.Update(ctx, obj, opts...)
|
2021-06-25 05:02:01 +00:00
|
|
|
case *metav1.PartialObjectMetadata:
|
|
|
|
return fmt.Errorf("cannot update using only metadata -- did you mean to patch?")
|
|
|
|
default:
|
|
|
|
return c.typedClient.Update(ctx, obj, opts...)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Delete implements client.Client.
|
|
|
|
func (c *client) Delete(ctx context.Context, obj Object, opts ...DeleteOption) error {
|
|
|
|
switch obj.(type) {
|
2023-06-01 17:01:19 +00:00
|
|
|
case runtime.Unstructured:
|
2020-10-21 05:49:41 +00:00
|
|
|
return c.unstructuredClient.Delete(ctx, obj, opts...)
|
2021-06-25 05:02:01 +00:00
|
|
|
case *metav1.PartialObjectMetadata:
|
|
|
|
return c.metadataClient.Delete(ctx, obj, opts...)
|
|
|
|
default:
|
|
|
|
return c.typedClient.Delete(ctx, obj, opts...)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// DeleteAllOf implements client.Client.
|
|
|
|
func (c *client) DeleteAllOf(ctx context.Context, obj Object, opts ...DeleteAllOfOption) error {
|
|
|
|
switch obj.(type) {
|
2023-06-01 17:01:19 +00:00
|
|
|
case runtime.Unstructured:
|
2020-10-21 05:49:41 +00:00
|
|
|
return c.unstructuredClient.DeleteAllOf(ctx, obj, opts...)
|
2021-06-25 05:02:01 +00:00
|
|
|
case *metav1.PartialObjectMetadata:
|
|
|
|
return c.metadataClient.DeleteAllOf(ctx, obj, opts...)
|
|
|
|
default:
|
|
|
|
return c.typedClient.DeleteAllOf(ctx, obj, opts...)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Patch implements client.Client.
|
|
|
|
func (c *client) Patch(ctx context.Context, obj Object, patch Patch, opts ...PatchOption) error {
|
2020-10-21 05:49:41 +00:00
|
|
|
defer c.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
|
2021-06-25 05:02:01 +00:00
|
|
|
switch obj.(type) {
|
2023-06-01 17:01:19 +00:00
|
|
|
case runtime.Unstructured:
|
2020-10-21 05:49:41 +00:00
|
|
|
return c.unstructuredClient.Patch(ctx, obj, patch, opts...)
|
2021-06-25 05:02:01 +00:00
|
|
|
case *metav1.PartialObjectMetadata:
|
|
|
|
return c.metadataClient.Patch(ctx, obj, patch, opts...)
|
|
|
|
default:
|
|
|
|
return c.typedClient.Patch(ctx, obj, patch, opts...)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Get implements client.Client.
|
2023-02-01 17:06:36 +00:00
|
|
|
func (c *client) Get(ctx context.Context, key ObjectKey, obj Object, opts ...GetOption) error {
|
2023-06-01 17:01:19 +00:00
|
|
|
if isUncached, err := c.shouldBypassCache(obj); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !isUncached {
|
2023-08-28 20:44:55 +00:00
|
|
|
// Attempt to get from the cache.
|
2023-06-01 17:01:19 +00:00
|
|
|
return c.cache.Get(ctx, key, obj, opts...)
|
|
|
|
}
|
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
// Perform a live lookup.
|
2021-06-25 05:02:01 +00:00
|
|
|
switch obj.(type) {
|
2023-06-01 17:01:19 +00:00
|
|
|
case runtime.Unstructured:
|
2023-02-01 17:06:36 +00:00
|
|
|
return c.unstructuredClient.Get(ctx, key, obj, opts...)
|
2021-06-25 05:02:01 +00:00
|
|
|
case *metav1.PartialObjectMetadata:
|
|
|
|
// Metadata only object should always preserve the GVK coming in from the caller.
|
|
|
|
defer c.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
|
2023-02-01 17:06:36 +00:00
|
|
|
return c.metadataClient.Get(ctx, key, obj, opts...)
|
2021-06-25 05:02:01 +00:00
|
|
|
default:
|
2023-02-01 17:06:36 +00:00
|
|
|
return c.typedClient.Get(ctx, key, obj, opts...)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// List implements client.Client.
|
|
|
|
func (c *client) List(ctx context.Context, obj ObjectList, opts ...ListOption) error {
|
2023-06-01 17:01:19 +00:00
|
|
|
if isUncached, err := c.shouldBypassCache(obj); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !isUncached {
|
2023-08-28 20:44:55 +00:00
|
|
|
// Attempt to get from the cache.
|
2023-06-01 17:01:19 +00:00
|
|
|
return c.cache.List(ctx, obj, opts...)
|
|
|
|
}
|
|
|
|
|
2023-08-28 20:44:55 +00:00
|
|
|
// Perform a live lookup.
|
2021-06-25 05:02:01 +00:00
|
|
|
switch x := obj.(type) {
|
2023-06-01 17:01:19 +00:00
|
|
|
case runtime.Unstructured:
|
2020-10-21 05:49:41 +00:00
|
|
|
return c.unstructuredClient.List(ctx, obj, opts...)
|
2021-06-25 05:02:01 +00:00
|
|
|
case *metav1.PartialObjectMetadataList:
|
|
|
|
// Metadata only object should always preserve the GVK.
|
|
|
|
gvk := obj.GetObjectKind().GroupVersionKind()
|
|
|
|
defer c.resetGroupVersionKind(obj, gvk)
|
|
|
|
|
|
|
|
// Call the list client.
|
|
|
|
if err := c.metadataClient.List(ctx, obj, opts...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the GVK for each item in the list.
|
|
|
|
itemGVK := schema.GroupVersionKind{
|
|
|
|
Group: gvk.Group,
|
|
|
|
Version: gvk.Version,
|
|
|
|
// TODO: this is producing unsafe guesses that don't actually work,
|
|
|
|
// but it matches ~99% of the cases out there.
|
|
|
|
Kind: strings.TrimSuffix(gvk.Kind, "List"),
|
|
|
|
}
|
|
|
|
for i := range x.Items {
|
|
|
|
item := &x.Items[i]
|
|
|
|
item.SetGroupVersionKind(itemGVK)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return c.typedClient.List(ctx, obj, opts...)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 05:02:01 +00:00
|
|
|
// Status implements client.StatusClient.
|
2023-02-01 17:06:36 +00:00
|
|
|
func (c *client) Status() SubResourceWriter {
|
|
|
|
return c.SubResource("status")
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
func (c *client) SubResource(subResource string) SubResourceClient {
|
|
|
|
return &subResourceClient{client: c, subResource: subResource}
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// subResourceClient is client.SubResourceWriter that writes to subresources.
|
|
|
|
type subResourceClient struct {
|
|
|
|
client *client
|
|
|
|
subResource string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure subResourceClient implements client.SubResourceClient.
|
|
|
|
var _ SubResourceClient = &subResourceClient{}
|
|
|
|
|
|
|
|
// SubResourceGetOptions holds all the possible configuration
|
|
|
|
// for a subresource Get request.
|
|
|
|
type SubResourceGetOptions struct {
|
|
|
|
Raw *metav1.GetOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyToSubResourceGet updates the configuaration to the given get options.
|
|
|
|
func (getOpt *SubResourceGetOptions) ApplyToSubResourceGet(o *SubResourceGetOptions) {
|
|
|
|
if getOpt.Raw != nil {
|
|
|
|
o.Raw = getOpt.Raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyOptions applues the given options.
|
|
|
|
func (getOpt *SubResourceGetOptions) ApplyOptions(opts []SubResourceGetOption) *SubResourceGetOptions {
|
|
|
|
for _, o := range opts {
|
|
|
|
o.ApplyToSubResourceGet(getOpt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return getOpt
|
|
|
|
}
|
|
|
|
|
|
|
|
// AsGetOptions returns the configured options as *metav1.GetOptions.
|
|
|
|
func (getOpt *SubResourceGetOptions) AsGetOptions() *metav1.GetOptions {
|
|
|
|
if getOpt.Raw == nil {
|
|
|
|
return &metav1.GetOptions{}
|
|
|
|
}
|
|
|
|
return getOpt.Raw
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubResourceUpdateOptions holds all the possible configuration
|
|
|
|
// for a subresource update request.
|
|
|
|
type SubResourceUpdateOptions struct {
|
|
|
|
UpdateOptions
|
|
|
|
SubResourceBody Object
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyToSubResourceUpdate updates the configuration on the given create options
|
|
|
|
func (uo *SubResourceUpdateOptions) ApplyToSubResourceUpdate(o *SubResourceUpdateOptions) {
|
|
|
|
uo.UpdateOptions.ApplyToUpdate(&o.UpdateOptions)
|
|
|
|
if uo.SubResourceBody != nil {
|
|
|
|
o.SubResourceBody = uo.SubResourceBody
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 05:49:41 +00:00
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// ApplyOptions applies the given options.
|
|
|
|
func (uo *SubResourceUpdateOptions) ApplyOptions(opts []SubResourceUpdateOption) *SubResourceUpdateOptions {
|
|
|
|
for _, o := range opts {
|
|
|
|
o.ApplyToSubResourceUpdate(uo)
|
|
|
|
}
|
|
|
|
|
|
|
|
return uo
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubResourceUpdateAndPatchOption is an option that can be used for either
|
|
|
|
// a subresource update or patch request.
|
|
|
|
type SubResourceUpdateAndPatchOption interface {
|
|
|
|
SubResourceUpdateOption
|
|
|
|
SubResourcePatchOption
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSubResourceBody returns an option that uses the given body
|
|
|
|
// for a subresource Update or Patch operation.
|
|
|
|
func WithSubResourceBody(body Object) SubResourceUpdateAndPatchOption {
|
|
|
|
return &withSubresourceBody{body: body}
|
|
|
|
}
|
|
|
|
|
|
|
|
type withSubresourceBody struct {
|
|
|
|
body Object
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsr *withSubresourceBody) ApplyToSubResourceUpdate(o *SubResourceUpdateOptions) {
|
|
|
|
o.SubResourceBody = wsr.body
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wsr *withSubresourceBody) ApplyToSubResourcePatch(o *SubResourcePatchOptions) {
|
|
|
|
o.SubResourceBody = wsr.body
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubResourceCreateOptions are all the possible configurations for a subresource
|
|
|
|
// create request.
|
|
|
|
type SubResourceCreateOptions struct {
|
|
|
|
CreateOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyOptions applies the given options.
|
|
|
|
func (co *SubResourceCreateOptions) ApplyOptions(opts []SubResourceCreateOption) *SubResourceCreateOptions {
|
|
|
|
for _, o := range opts {
|
|
|
|
o.ApplyToSubResourceCreate(co)
|
|
|
|
}
|
|
|
|
|
|
|
|
return co
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:57:03 +00:00
|
|
|
// ApplyToSubResourceCreate applies the the configuration on the given create options.
|
|
|
|
func (co *SubResourceCreateOptions) ApplyToSubResourceCreate(o *SubResourceCreateOptions) {
|
2023-02-01 17:06:36 +00:00
|
|
|
co.CreateOptions.ApplyToCreate(&co.CreateOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubResourcePatchOptions holds all possible configurations for a subresource patch
|
|
|
|
// request.
|
|
|
|
type SubResourcePatchOptions struct {
|
|
|
|
PatchOptions
|
|
|
|
SubResourceBody Object
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyOptions applies the given options.
|
|
|
|
func (po *SubResourcePatchOptions) ApplyOptions(opts []SubResourcePatchOption) *SubResourcePatchOptions {
|
|
|
|
for _, o := range opts {
|
|
|
|
o.ApplyToSubResourcePatch(po)
|
|
|
|
}
|
|
|
|
|
|
|
|
return po
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyToSubResourcePatch applies the configuration on the given patch options.
|
|
|
|
func (po *SubResourcePatchOptions) ApplyToSubResourcePatch(o *SubResourcePatchOptions) {
|
|
|
|
po.PatchOptions.ApplyToPatch(&o.PatchOptions)
|
|
|
|
if po.SubResourceBody != nil {
|
|
|
|
o.SubResourceBody = po.SubResourceBody
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *subResourceClient) Get(ctx context.Context, obj Object, subResource Object, opts ...SubResourceGetOption) error {
|
2021-06-25 05:02:01 +00:00
|
|
|
switch obj.(type) {
|
2023-06-01 17:01:19 +00:00
|
|
|
case runtime.Unstructured:
|
2023-02-01 17:06:36 +00:00
|
|
|
return sc.client.unstructuredClient.GetSubResource(ctx, obj, subResource, sc.subResource, opts...)
|
|
|
|
case *metav1.PartialObjectMetadata:
|
|
|
|
return errors.New("can not get subresource using only metadata")
|
|
|
|
default:
|
|
|
|
return sc.client.typedClient.GetSubResource(ctx, obj, subResource, sc.subResource, opts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create implements client.SubResourceClient
|
|
|
|
func (sc *subResourceClient) Create(ctx context.Context, obj Object, subResource Object, opts ...SubResourceCreateOption) error {
|
|
|
|
defer sc.client.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
|
|
|
|
defer sc.client.resetGroupVersionKind(subResource, subResource.GetObjectKind().GroupVersionKind())
|
|
|
|
|
|
|
|
switch obj.(type) {
|
2023-06-01 17:01:19 +00:00
|
|
|
case runtime.Unstructured:
|
2023-02-01 17:06:36 +00:00
|
|
|
return sc.client.unstructuredClient.CreateSubResource(ctx, obj, subResource, sc.subResource, opts...)
|
2021-06-25 05:02:01 +00:00
|
|
|
case *metav1.PartialObjectMetadata:
|
|
|
|
return fmt.Errorf("cannot update status using only metadata -- did you mean to patch?")
|
|
|
|
default:
|
2023-02-01 17:06:36 +00:00
|
|
|
return sc.client.typedClient.CreateSubResource(ctx, obj, subResource, sc.subResource, opts...)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
// Update implements client.SubResourceClient
|
|
|
|
func (sc *subResourceClient) Update(ctx context.Context, obj Object, opts ...SubResourceUpdateOption) error {
|
|
|
|
defer sc.client.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
|
|
|
|
switch obj.(type) {
|
2023-06-01 17:01:19 +00:00
|
|
|
case runtime.Unstructured:
|
2023-02-01 17:06:36 +00:00
|
|
|
return sc.client.unstructuredClient.UpdateSubResource(ctx, obj, sc.subResource, opts...)
|
|
|
|
case *metav1.PartialObjectMetadata:
|
|
|
|
return fmt.Errorf("cannot update status using only metadata -- did you mean to patch?")
|
|
|
|
default:
|
|
|
|
return sc.client.typedClient.UpdateSubResource(ctx, obj, sc.subResource, opts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Patch implements client.SubResourceWriter.
|
|
|
|
func (sc *subResourceClient) Patch(ctx context.Context, obj Object, patch Patch, opts ...SubResourcePatchOption) error {
|
|
|
|
defer sc.client.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
|
2021-06-25 05:02:01 +00:00
|
|
|
switch obj.(type) {
|
2023-06-01 17:01:19 +00:00
|
|
|
case runtime.Unstructured:
|
2023-02-01 17:06:36 +00:00
|
|
|
return sc.client.unstructuredClient.PatchSubResource(ctx, obj, sc.subResource, patch, opts...)
|
2021-06-25 05:02:01 +00:00
|
|
|
case *metav1.PartialObjectMetadata:
|
2023-02-01 17:06:36 +00:00
|
|
|
return sc.client.metadataClient.PatchSubResource(ctx, obj, sc.subResource, patch, opts...)
|
2021-06-25 05:02:01 +00:00
|
|
|
default:
|
2023-02-01 17:06:36 +00:00
|
|
|
return sc.client.typedClient.PatchSubResource(ctx, obj, sc.subResource, patch, opts...)
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
}
|