Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#2 from varshaprasad96/clusterscope…
Browse files Browse the repository at this point in the history
…-client

Clusterscope client
  • Loading branch information
fabianvf committed Feb 7, 2022
2 parents ec25409 + 6ff6cd9 commit 33fcbc7
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 92 deletions.
17 changes: 8 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,21 @@ require (
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/klog/v2 v2.40.1 // indirect
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
)

replace k8s.io/api => github.com/ncdc/kubernetes/staging/src/k8s.io/api v0.0.0-20220119183702-4619e1d3975e
replace k8s.io/api => github.com/kcp-dev/kubernetes/staging/src/k8s.io/api v0.0.0-20220203192237-6b61675b7950

replace k8s.io/apiextensions-apiserver => github.com/ncdc/kubernetes/staging/src/k8s.io/apiextensions-apiserver v0.0.0-20220119183702-4619e1d3975e
replace k8s.io/apiextensions-apiserver => github.com/kcp-dev/kubernetes/staging/src/k8s.io/apiextensions-apiserver v0.0.0-20220203192237-6b61675b7950

replace k8s.io/apimachinery => github.com/ncdc/kubernetes/staging/src/k8s.io/apimachinery v0.0.0-20220119183702-4619e1d3975e
replace k8s.io/apimachinery => github.com/kcp-dev/kubernetes/staging/src/k8s.io/apimachinery v0.0.0-20220203192237-6b61675b7950

replace k8s.io/client-go => github.com/ncdc/kubernetes/staging/src/k8s.io/client-go v0.0.0-20220119183702-4619e1d3975e

replace k8s.io/component-base => github.com/ncdc/kubernetes/staging/src/k8s.io/component-base v0.0.0-20220119183702-4619e1d3975e
replace k8s.io/component-base => github.com/kcp-dev/kubernetes/staging/src/k8s.io/component-base v0.0.0-20220203192237-6b61675b7950

//replace k8s.io/utils => github.com/ncdc/kubernetes/staging/src/k8s.io/utils v0.0.0-20220119183702-4619e1d3975e

replace k8s.io/apiserver => github.com/ncdc/kubernetes/staging/src/k8s.io/apiserver v0.0.0-20220119183702-4619e1d3975e
replace k8s.io/apiserver => github.com/kcp-dev/kubernetes/staging/src/k8s.io/apiserver v0.0.0-20220203192237-6b61675b7950

replace k8s.io/code-generator => github.com/kcp-dev/kubernetes/staging/src/k8s.io/code-generator v0.0.0-20220203192237-6b61675b7950

replace k8s.io/code-generator => github.com/ncdc/kubernetes/staging/src/k8s.io/code-generator v0.0.0-20220119183702-4619e1d3975e
replace k8s.io/client-go => github.com/kcp-dev/kubernetes/staging/src/k8s.io/client-go v0.0.0-20220203192237-6b61675b7950
77 changes: 27 additions & 50 deletions go.sum

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ type Options struct {
// surfacing and handling warnings messages sent by the API server.
Opts WarningHandlerOptions

Scope rest.Scope
// Cluster refers to the name of the cluster this request is scoped to
Cluster string
}

// New returns a new Client using the provided config and Options.
Expand Down
98 changes: 98 additions & 0 deletions pkg/client/clusterscoped_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Copyright 2014 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"
"fmt"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)

var _ Reader = &clusterScopedClient{}
var _ Writer = &clusterScopedClient{}
var _ StatusWriter = &clusterScopedClient{}

// client is a client.Client that reads and writes directly from/to an API server. It lazily initializes
// new clients at the time they are used, and caches the client.
type clusterScopedClient struct {
cache *clientCache
paramCodec runtime.ParameterCodec
}

func (cc *clusterScopedClient) Create(ctx context.Context, obj Object, opts ...CreateOption) error {
u, ok := obj.(*unstructured.Unstructured)
if !ok {
return fmt.Errorf("unstructured client did not understand object: %T", obj)
}

gvk := u.GroupVersionKind()

o, err := cc.cache.getObjMeta(obj)
if err != nil {
return err
}

createOpts := &CreateOptions{}
createOpts.ApplyOptions(opts)

result := o.Post().
Cluster(obj.GetClusterName()).
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Body(obj).
VersionedParams(createOpts.AsCreateOptions(), cc.paramCodec).
Do(ctx).
Into(obj)

u.SetGroupVersionKind(gvk)
return result
}

func (cc *clusterScopedClient) Update(ctx context.Context, obj Object, opts ...UpdateOption) error {
return nil
}

func (cc *clusterScopedClient) Delete(ctx context.Context, obj Object, opts ...DeleteOption) error {
return nil
}

func (cc *clusterScopedClient) DeleteAllOf(ctx context.Context, obj Object, opts ...DeleteAllOfOption) error {
return nil
}

// Patch implements client.Client.
func (cc *clusterScopedClient) Patch(ctx context.Context, obj Object, patch Patch, opts ...PatchOption) error {
return nil
}

func (cc *clusterScopedClient) Get(ctx context.Context, key ObjectKey, obj Object) error {
return nil
}

func (cc *clusterScopedClient) List(ctx context.Context, obj ObjectList, opts ...ListOption) error {
return nil
}

func (cc *clusterScopedClient) UpdateStatus(ctx context.Context, obj Object, opts ...UpdateOption) error {
return nil
}

func (cc *clusterScopedClient) PatchStatus(ctx context.Context, obj Object, patch Patch, opts ...PatchOption) error {
return nil
}
6 changes: 6 additions & 0 deletions pkg/client/typed_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (c *typedClient) Create(ctx context.Context, obj Object, opts ...CreateOpti
createOpts := &CreateOptions{}
createOpts.ApplyOptions(opts)
return o.Post().
Cluster(o.GetClusterName()).
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Body(obj).
Expand All @@ -61,6 +62,7 @@ func (c *typedClient) Update(ctx context.Context, obj Object, opts ...UpdateOpti
updateOpts := &UpdateOptions{}
updateOpts.ApplyOptions(opts)
return o.Put().
Cluster(o.GetClusterName()).
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
Expand All @@ -81,6 +83,7 @@ func (c *typedClient) Delete(ctx context.Context, obj Object, opts ...DeleteOpti
deleteOpts.ApplyOptions(opts)

return o.Delete().
Cluster(o.GetClusterName()).
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
Expand All @@ -100,6 +103,7 @@ func (c *typedClient) DeleteAllOf(ctx context.Context, obj Object, opts ...Delet
deleteAllOfOpts.ApplyOptions(opts)

return o.Delete().
Cluster(o.GetClusterName()).
NamespaceIfScoped(deleteAllOfOpts.ListOptions.Namespace, o.isNamespaced()).
Resource(o.resource()).
VersionedParams(deleteAllOfOpts.AsListOptions(), c.paramCodec).
Expand All @@ -122,6 +126,7 @@ func (c *typedClient) Patch(ctx context.Context, obj Object, patch Patch, opts .

patchOpts := &PatchOptions{}
return o.Patch(patch.Type()).
Cluster(o.GetClusterName()).
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
Expand All @@ -138,6 +143,7 @@ func (c *typedClient) Get(ctx context.Context, key ObjectKey, obj Object) error
return err
}
return r.Get().
Cluster(obj.GetClusterName()).
NamespaceIfScoped(key.Namespace, r.isNamespaced()).
Resource(r.resource()).
Name(key.Name).Do(ctx).Into(obj)
Expand Down
1 change: 1 addition & 0 deletions pkg/client/unstructured_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (uc *unstructuredClient) Create(ctx context.Context, obj Object, opts ...Cr
createOpts := &CreateOptions{}
createOpts.ApplyOptions(opts)
result := o.Post().
Cluster(o.GetClusterName()).
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Body(obj).
Expand Down
17 changes: 5 additions & 12 deletions pkg/client/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,12 @@ func NewWithWatch(config *rest.Config, options Options) (WithWatch, error) {
}

var dynamicClient dynamic.Interface
if options.Scope != nil {
dynamicClientScoping, err := dynamic.NewScopingForConfig(config)
if err != nil {
return nil, err
}

dynamicClient = dynamicClientScoping.Scope(options.Scope)
} else {
dynamicClient, err = dynamic.NewForConfig(config)
if err != nil {
return nil, err
}

dynamicClient, err = dynamic.NewForConfig(config)
if err != nil {
return nil, err
}

return &watchingClient{client: client, dynamic: dynamicClient}, nil
}

Expand Down
36 changes: 16 additions & 20 deletions pkg/handler/enqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package handler
import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand All @@ -41,25 +42,16 @@ func (e *EnqueueRequestForObject) Create(evt event.CreateEvent, q workqueue.Rate
enqueueLog.Error(nil, "CreateEvent received with no metadata", "event", evt)
return
}
q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
Name: evt.Object.GetName(),
Namespace: evt.Object.GetNamespace(),
}})
q.Add(request(evt.Object))
}

// Update implements EventHandler.
func (e *EnqueueRequestForObject) Update(evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
switch {
case evt.ObjectNew != nil:
q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
Name: evt.ObjectNew.GetName(),
Namespace: evt.ObjectNew.GetNamespace(),
}})
q.Add(request(evt.ObjectNew))
case evt.ObjectOld != nil:
q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
Name: evt.ObjectOld.GetName(),
Namespace: evt.ObjectOld.GetNamespace(),
}})
q.Add(request(evt.ObjectOld))
default:
enqueueLog.Error(nil, "UpdateEvent received with no metadata", "event", evt)
}
Expand All @@ -71,10 +63,7 @@ func (e *EnqueueRequestForObject) Delete(evt event.DeleteEvent, q workqueue.Rate
enqueueLog.Error(nil, "DeleteEvent received with no metadata", "event", evt)
return
}
q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
Name: evt.Object.GetName(),
Namespace: evt.Object.GetNamespace(),
}})
q.Add(request(evt.Object))
}

// Generic implements EventHandler.
Expand All @@ -83,8 +72,15 @@ func (e *EnqueueRequestForObject) Generic(evt event.GenericEvent, q workqueue.Ra
enqueueLog.Error(nil, "GenericEvent received with no metadata", "event", evt)
return
}
q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
Name: evt.Object.GetName(),
Namespace: evt.Object.GetNamespace(),
}})
q.Add(request(evt.Object))
}

func request(obj client.Object) reconcile.Request {
return reconcile.Request{
NamespacedName: types.NamespacedName{
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
},
ClusterName: obj.GetClusterName(),
}
}
2 changes: 2 additions & 0 deletions pkg/reconcile/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ func (r *Result) IsZero() bool {
// Request contains the information necessary to reconcile a Kubernetes object. This includes the
// information to uniquely identify the object - its Name and Namespace. It does NOT contain information about
// any specific Event or the object contents itself.
// TODO: add a cluster name to key
type Request struct {
// NamespacedName is the name and namespace of the object to reconcile.
types.NamespacedName
ClusterName string
}

/*
Expand Down

0 comments on commit 33fcbc7

Please sign in to comment.