Skip to content

Commit

Permalink
Merge pull request #22 from stevekuznetsov/skuznets/clustered-dyanmic…
Browse files Browse the repository at this point in the history
…-client

HACK: scope dynamic and discovery clients by cluster
  • Loading branch information
imjasonh committed Nov 23, 2021
2 parents bed16d2 + 5ec8e3b commit d0ac9bf
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 25 deletions.

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.

16 changes: 14 additions & 2 deletions staging/src/k8s.io/client-go/discovery/discovery_client.go
Expand Up @@ -130,6 +130,18 @@ type OpenAPISchemaInterface interface {
// DiscoveryClient implements the functions that discover server-supported API groups,
// versions and resources.
type DiscoveryClient struct {
*scopedClient
cluster string
}

func (d *DiscoveryClient) WithCluster(cluster string) DiscoveryInterface {
return &DiscoveryClient{
scopedClient: d.scopedClient,
cluster: cluster,
}
}

type scopedClient struct {
restClient restclient.Interface

LegacyPrefix string
Expand Down Expand Up @@ -488,7 +500,7 @@ func NewDiscoveryClientForConfig(c *restclient.Config) (*DiscoveryClient, error)
return nil, err
}
client, err := restclient.UnversionedRESTClientFor(&config)
return &DiscoveryClient{restClient: client, LegacyPrefix: "/api"}, err
return &DiscoveryClient{scopedClient: &scopedClient{restClient: client, LegacyPrefix: "/api"}}, err
}

// NewDiscoveryClientForConfigOrDie creates a new DiscoveryClient for the given config. If
Expand All @@ -504,7 +516,7 @@ func NewDiscoveryClientForConfigOrDie(c *restclient.Config) *DiscoveryClient {

// NewDiscoveryClient returns a new DiscoveryClient for the given RESTClient.
func NewDiscoveryClient(c restclient.Interface) *DiscoveryClient {
return &DiscoveryClient{restClient: c, LegacyPrefix: "/api"}
return &DiscoveryClient{scopedClient: &scopedClient{restClient: c, LegacyPrefix: "/api"}}
}

// RESTClient returns a RESTClient that is used to communicate
Expand Down
58 changes: 48 additions & 10 deletions staging/src/k8s.io/client-go/dynamic/simple.go
Expand Up @@ -30,11 +30,43 @@ import (
"k8s.io/client-go/rest"
)

type dynamicClient struct {
// NewClusterForConfig creates a new Cluster for the given config.
// If config's RateLimiter is not set and QPS and Burst are acceptable,
// NewClusterForConfig will generate a rate-limiter in configShallowCopy.
func NewClusterForConfig(c *rest.Config) (*Cluster, error) {
cs, err := NewForConfig(c)
if err != nil {
return nil, err
}
return &Cluster{scopedClient: cs.scopedClient}, nil
}

type ClusterInterface interface {
Cluster(name string) Interface
}

type Cluster struct {
*scopedClient
}

// Cluster sets the cluster for a Clientset.
func (c *Cluster) Cluster(name string) Interface {
return &DynamicClient{
scopedClient: c.scopedClient,
cluster: name,
}
}

type DynamicClient struct {
*scopedClient
cluster string
}

type scopedClient struct {
client *rest.RESTClient
}

var _ Interface = &dynamicClient{}
var _ Interface = &DynamicClient{}

// ConfigFor returns a copy of the provided config with the
// appropriate dynamic client defaults set.
Expand All @@ -51,7 +83,7 @@ func ConfigFor(inConfig *rest.Config) *rest.Config {

// NewForConfigOrDie creates a new Interface for the given config and
// panics if there is an error in the config.
func NewForConfigOrDie(c *rest.Config) Interface {
func NewForConfigOrDie(c *rest.Config) *DynamicClient {
ret, err := NewForConfig(c)
if err != nil {
panic(err)
Expand All @@ -60,7 +92,7 @@ func NewForConfigOrDie(c *rest.Config) Interface {
}

// NewForConfig creates a new dynamic client or returns an error.
func NewForConfig(inConfig *rest.Config) (Interface, error) {
func NewForConfig(inConfig *rest.Config) (*DynamicClient, error) {
config := ConfigFor(inConfig)
// for serializing the options
config.GroupVersion = &schema.GroupVersion{}
Expand All @@ -71,16 +103,16 @@ func NewForConfig(inConfig *rest.Config) (Interface, error) {
return nil, err
}

return &dynamicClient{client: restClient}, nil
return &DynamicClient{scopedClient: &scopedClient{client: restClient}}, nil
}

type dynamicResourceClient struct {
client *dynamicClient
client *DynamicClient
namespace string
resource schema.GroupVersionResource
}

func (c *dynamicClient) Resource(resource schema.GroupVersionResource) NamespaceableResourceInterface {
func (c *DynamicClient) Resource(resource schema.GroupVersionResource) NamespaceableResourceInterface {
return &dynamicResourceClient{client: c, resource: resource}
}

Expand Down Expand Up @@ -109,6 +141,7 @@ func (c *dynamicResourceClient) Create(ctx context.Context, obj *unstructured.Un

result := c.client.client.
Post().
Cluster(c.client.cluster).
AbsPath(append(c.makeURLSegments(name), subresources...)...).
Body(outBytes).
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
Expand Down Expand Up @@ -144,6 +177,7 @@ func (c *dynamicResourceClient) Update(ctx context.Context, obj *unstructured.Un

result := c.client.client.
Put().
Cluster(c.client.cluster).
AbsPath(append(c.makeURLSegments(name), subresources...)...).
Body(outBytes).
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
Expand Down Expand Up @@ -180,6 +214,7 @@ func (c *dynamicResourceClient) UpdateStatus(ctx context.Context, obj *unstructu

result := c.client.client.
Put().
Cluster(c.client.cluster).
AbsPath(append(c.makeURLSegments(name), "status")...).
Body(outBytes).
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
Expand Down Expand Up @@ -210,6 +245,7 @@ func (c *dynamicResourceClient) Delete(ctx context.Context, name string, opts me

result := c.client.client.
Delete().
Cluster(c.client.cluster).
AbsPath(append(c.makeURLSegments(name), subresources...)...).
Body(deleteOptionsByte).
Do(ctx)
Expand All @@ -224,6 +260,7 @@ func (c *dynamicResourceClient) DeleteCollection(ctx context.Context, opts metav

result := c.client.client.
Delete().
Cluster(c.client.cluster).
AbsPath(c.makeURLSegments("")...).
Body(deleteOptionsByte).
SpecificallyVersionedParams(&listOptions, dynamicParameterCodec, versionV1).
Expand All @@ -235,7 +272,7 @@ func (c *dynamicResourceClient) Get(ctx context.Context, name string, opts metav
if len(name) == 0 {
return nil, fmt.Errorf("name is required")
}
result := c.client.client.Get().AbsPath(append(c.makeURLSegments(name), subresources...)...).SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).Do(ctx)
result := c.client.client.Get().Cluster(c.client.cluster).AbsPath(append(c.makeURLSegments(name), subresources...)...).SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).Do(ctx)
if err := result.Error(); err != nil {
return nil, err
}
Expand All @@ -251,7 +288,7 @@ func (c *dynamicResourceClient) Get(ctx context.Context, name string, opts metav
}

func (c *dynamicResourceClient) List(ctx context.Context, opts metav1.ListOptions) (*unstructured.UnstructuredList, error) {
result := c.client.client.Get().AbsPath(c.makeURLSegments("")...).SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).Do(ctx)
result := c.client.client.Get().Cluster(c.client.cluster).AbsPath(c.makeURLSegments("")...).SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).Do(ctx)
if err := result.Error(); err != nil {
return nil, err
}
Expand All @@ -276,7 +313,7 @@ func (c *dynamicResourceClient) List(ctx context.Context, opts metav1.ListOption

func (c *dynamicResourceClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
opts.Watch = true
return c.client.client.Get().AbsPath(c.makeURLSegments("")...).
return c.client.client.Get().Cluster(c.client.cluster).AbsPath(c.makeURLSegments("")...).
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
Watch(ctx)
}
Expand All @@ -287,6 +324,7 @@ func (c *dynamicResourceClient) Patch(ctx context.Context, name string, pt types
}
result := c.client.client.
Patch(pt).
Cluster(c.client.cluster).
AbsPath(append(c.makeURLSegments(name), subresources...)...).
Body(data).
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
Expand Down
2 changes: 1 addition & 1 deletion staging/src/k8s.io/client-go/kubernetes/clientset.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -177,7 +177,7 @@ func (c *Clientset) Discovery() $.DiscoveryInterface|raw$ {
if c == nil {
return nil
}
return c.DiscoveryClient
return c.DiscoveryClient.WithCluster(c.cluster)
}
`

Expand Down

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.

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.

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.

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.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d0ac9bf

Please sign in to comment.