-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
- Loading branch information
Showing
6 changed files
with
793 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
toolscache "k8s.io/client-go/tools/cache" | ||
"sigs.k8s.io/controller-runtime/pkg/cache" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const ( | ||
ClusterNameIndex = "cluster/name" | ||
ClusterIndex = "cluster" | ||
) | ||
|
||
var _ cache.Cache = &NamespacedCache{} | ||
|
||
type NamespacedCache struct { | ||
clusterName string | ||
cache.Cache | ||
} | ||
|
||
func (c *NamespacedCache) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { | ||
if key.Namespace != "default" { | ||
return apierrors.NewNotFound(schema.GroupResource{}, key.Name) | ||
} | ||
key.Namespace = c.clusterName | ||
if err := c.Cache.Get(ctx, key, obj, opts...); err != nil { | ||
return err | ||
} | ||
obj.SetNamespace("default") | ||
return nil | ||
} | ||
|
||
func (c *NamespacedCache) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { | ||
var listOpts client.ListOptions | ||
for _, o := range opts { | ||
o.ApplyToList(&listOpts) | ||
} | ||
|
||
switch { | ||
case listOpts.FieldSelector != nil: | ||
reqs := listOpts.FieldSelector.Requirements() | ||
flds := make(map[string]string, len(reqs)) | ||
for i := range reqs { | ||
flds[fmt.Sprintf("cluster/%s", reqs[i].Field)] = fmt.Sprintf("%s/%s", c.clusterName, reqs[i].Value) | ||
} | ||
opts = append(opts, client.MatchingFields(flds)) | ||
case listOpts.Namespace != "": | ||
opts = append(opts, client.MatchingFields(map[string]string{ClusterIndex: c.clusterName})) | ||
if c.clusterName == "*" { | ||
listOpts.Namespace = "" | ||
} else if listOpts.Namespace == "default" { | ||
listOpts.Namespace = c.clusterName | ||
} | ||
default: | ||
opts = append(opts, client.MatchingFields(map[string]string{ClusterIndex: c.clusterName})) | ||
} | ||
|
||
if err := c.Cache.List(ctx, list, opts...); err != nil { | ||
return err | ||
} | ||
|
||
return meta.EachListItem(list, func(obj runtime.Object) error { | ||
obj.(client.Object).SetNamespace("default") | ||
return nil | ||
}) | ||
} | ||
|
||
func (c *NamespacedCache) GetInformer(ctx context.Context, obj client.Object, opts ...cache.InformerGetOption) (cache.Informer, error) { | ||
inf, err := c.Cache.GetInformer(ctx, obj, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ScopedInformer{clusterName: c.clusterName, Informer: inf}, nil | ||
} | ||
|
||
func (c *NamespacedCache) GetInformerForKind(ctx context.Context, gvk schema.GroupVersionKind, opts ...cache.InformerGetOption) (cache.Informer, error) { | ||
inf, err := c.Cache.GetInformerForKind(ctx, gvk, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ScopedInformer{clusterName: c.clusterName, Informer: inf}, nil | ||
} | ||
|
||
func (c *NamespacedCache) RemoveInformer(ctx context.Context, obj client.Object) error { | ||
return errors.New("informer cannot be removed from scoped cache") | ||
} | ||
|
||
type ScopedInformer struct { | ||
clusterName string | ||
cache.Informer | ||
} | ||
|
||
func (i *ScopedInformer) AddEventHandler(handler toolscache.ResourceEventHandler) (toolscache.ResourceEventHandlerRegistration, error) { | ||
return i.Informer.AddEventHandler(toolscache.ResourceEventHandlerDetailedFuncs{ | ||
AddFunc: func(obj interface{}, isInInitialList bool) { | ||
cobj := obj.(client.Object) | ||
if cobj.GetNamespace() == i.clusterName { | ||
cobj := cobj.DeepCopyObject().(client.Object) | ||
cobj.SetNamespace("default") | ||
handler.OnAdd(cobj, isInInitialList) | ||
} | ||
}, | ||
UpdateFunc: func(oldObj, newObj interface{}) { | ||
cobj := newObj.(client.Object) | ||
if cobj.GetNamespace() == i.clusterName { | ||
cobj := cobj.DeepCopyObject().(client.Object) | ||
cobj.SetNamespace("default") | ||
handler.OnUpdate(oldObj, cobj) | ||
} | ||
}, | ||
DeleteFunc: func(obj interface{}) { | ||
if tombStone, ok := obj.(toolscache.DeletedFinalStateUnknown); ok { | ||
obj = tombStone.Obj | ||
} | ||
cobj := obj.(client.Object) | ||
if cobj.GetNamespace() == i.clusterName { | ||
cobj := cobj.DeepCopyObject().(client.Object) | ||
cobj.SetNamespace("default") | ||
handler.OnDelete(cobj) | ||
} | ||
}, | ||
}) | ||
} | ||
|
||
func (i *ScopedInformer) AddEventHandlerWithResyncPeriod(handler toolscache.ResourceEventHandler, resyncPeriod time.Duration) (toolscache.ResourceEventHandlerRegistration, error) { | ||
return i.Informer.AddEventHandlerWithResyncPeriod(toolscache.ResourceEventHandlerDetailedFuncs{ | ||
AddFunc: func(obj interface{}, isInInitialList bool) { | ||
cobj := obj.(client.Object) | ||
if cobj.GetNamespace() == i.clusterName { | ||
cobj := cobj.DeepCopyObject().(client.Object) | ||
cobj.SetNamespace("default") | ||
handler.OnAdd(cobj, isInInitialList) | ||
} | ||
}, | ||
UpdateFunc: func(oldObj, newObj interface{}) { | ||
cobj := newObj.(client.Object) | ||
if cobj.GetNamespace() == i.clusterName { | ||
cobj := cobj.DeepCopyObject().(client.Object) | ||
cobj.SetNamespace("default") | ||
handler.OnUpdate(oldObj, cobj) | ||
} | ||
}, | ||
DeleteFunc: func(obj interface{}) { | ||
if tombStone, ok := obj.(toolscache.DeletedFinalStateUnknown); ok { | ||
obj = tombStone.Obj | ||
} | ||
cobj := obj.(client.Object) | ||
if cobj.GetNamespace() == i.clusterName { | ||
cobj := cobj.DeepCopyObject().(client.Object) | ||
cobj.SetNamespace("default") | ||
handler.OnDelete(cobj) | ||
} | ||
}, | ||
}, resyncPeriod) | ||
} | ||
|
||
func (i *ScopedInformer) AddIndexers(indexers toolscache.Indexers) error { | ||
return errors.New("indexes cannot be added to scoped informers") | ||
} | ||
|
||
type NamespaceScopeableCache struct { | ||
cache.Cache | ||
} | ||
|
||
func (f *NamespaceScopeableCache) IndexField(ctx context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error { | ||
return f.IndexField(ctx, obj, "cluster/"+field, func(obj client.Object) []string { | ||
keys := extractValue(obj) | ||
withCluster := make([]string, len(keys)*2) | ||
for i, key := range keys { | ||
withCluster[i] = fmt.Sprintf("%s/%s", obj.GetNamespace(), key) | ||
withCluster[i+len(keys)] = fmt.Sprintf("*/%s", key) | ||
} | ||
return withCluster | ||
}) | ||
} | ||
|
||
func (f *NamespaceScopeableCache) Start(ctx context.Context) error { | ||
return nil // no-op as this is shared | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
var _ client.Client = &NamespacedClient{} | ||
|
||
type NamespacedClient struct { | ||
clusterName string | ||
client.Client | ||
} | ||
|
||
func (n *NamespacedClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { | ||
if key.Namespace != "default" { | ||
return apierrors.NewNotFound(schema.GroupResource{}, key.Name) | ||
} | ||
key.Namespace = n.clusterName | ||
if err := n.Client.Get(ctx, key, obj, opts...); err != nil { | ||
return err | ||
} | ||
obj.SetNamespace("default") | ||
return nil | ||
} | ||
|
||
func (n *NamespacedClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { | ||
var copts client.ListOptions | ||
for _, o := range opts { | ||
o.ApplyToList(&copts) | ||
} | ||
if copts.Namespace != "default" { | ||
return apierrors.NewNotFound(schema.GroupResource{}, copts.Namespace) | ||
} | ||
if err := n.Client.List(ctx, list, append(opts, client.InNamespace(n.clusterName))...); err != nil { | ||
return err | ||
} | ||
return meta.EachListItem(list, func(obj runtime.Object) error { | ||
obj.(client.Object).SetNamespace("default") | ||
return nil | ||
}) | ||
} | ||
|
||
func (n *NamespacedClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error { | ||
panic("implement me") | ||
} | ||
|
||
func (n *NamespacedClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error { | ||
panic("implement me") | ||
} | ||
|
||
func (n *NamespacedClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error { | ||
panic("implement me") | ||
} | ||
|
||
func (n *NamespacedClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error { | ||
panic("implement me") | ||
} | ||
|
||
func (n *NamespacedClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error { | ||
panic("implement me") | ||
} | ||
|
||
func (n *NamespacedClient) Status() client.SubResourceWriter { | ||
panic("implement me") | ||
} | ||
|
||
func (n *NamespacedClient) SubResource(subResource string) client.SubResourceClient { | ||
panic("implement me") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/client-go/tools/record" | ||
"sigs.k8s.io/controller-runtime/pkg/cache" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/cluster" | ||
) | ||
|
||
type NamespacedCluster struct { | ||
clusterName string | ||
cluster.Cluster | ||
} | ||
|
||
func (c *NamespacedCluster) Name() string { | ||
return c.clusterName | ||
} | ||
|
||
func (c *NamespacedCluster) GetCache() cache.Cache { | ||
return &NamespacedCache{clusterName: c.clusterName, Cache: c.Cluster.GetCache()} | ||
} | ||
|
||
func (c *NamespacedCluster) GetClient() client.Client { | ||
return &NamespacedClient{clusterName: c.clusterName, Client: c.Cluster.GetClient()} | ||
} | ||
|
||
func (c *NamespacedCluster) GetEventRecorderFor(name string) record.EventRecorder { | ||
panic("implement me") | ||
} | ||
|
||
func (c *NamespacedCluster) GetAPIReader() client.Reader { | ||
return c.GetClient() | ||
} | ||
|
||
func (c *NamespacedCluster) Start(ctx context.Context) error { | ||
return nil // no-op as this is shared | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
module sigs.k8s.io/controller-runtime/examples/fleet | ||
|
||
go 1.19 | ||
|
||
replace sigs.k8s.io/controller-runtime => ../.. | ||
|
||
require ( | ||
golang.org/x/sync v0.6.0 | ||
k8s.io/api v0.28.3 | ||
k8s.io/apimachinery v0.28.3 | ||
k8s.io/client-go v0.28.3 | ||
k8s.io/klog/v2 v2.110.1 | ||
sigs.k8s.io/controller-runtime v0.0.0-00010101000000-000000000000 | ||
) | ||
|
||
require ( | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.2.0 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/emicklei/go-restful/v3 v3.11.0 // indirect | ||
github.com/evanphx/json-patch/v5 v5.7.0 // indirect | ||
github.com/fsnotify/fsnotify v1.7.0 // indirect | ||
github.com/go-logr/logr v1.3.0 // indirect | ||
github.com/go-openapi/jsonpointer v0.19.6 // indirect | ||
github.com/go-openapi/jsonreference v0.20.2 // indirect | ||
github.com/go-openapi/swag v0.22.3 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect | ||
github.com/golang/protobuf v1.5.3 // indirect | ||
github.com/google/gnostic-models v0.6.8 // indirect | ||
github.com/google/go-cmp v0.6.0 // indirect | ||
github.com/google/gofuzz v1.2.0 // indirect | ||
github.com/google/uuid v1.3.0 // indirect | ||
github.com/imdario/mergo v0.3.6 // indirect | ||
github.com/josharian/intern v1.0.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/mailru/easyjson v0.7.7 // indirect | ||
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/prometheus/client_golang v1.17.0 // indirect | ||
github.com/prometheus/client_model v0.5.0 // indirect | ||
github.com/prometheus/common v0.44.0 // indirect | ||
github.com/prometheus/procfs v0.11.1 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect | ||
golang.org/x/net v0.17.0 // indirect | ||
golang.org/x/oauth2 v0.8.0 // indirect | ||
golang.org/x/sys v0.14.0 // indirect | ||
golang.org/x/term v0.13.0 // indirect | ||
golang.org/x/text v0.13.0 // indirect | ||
golang.org/x/time v0.3.0 // indirect | ||
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect | ||
google.golang.org/appengine v1.6.7 // indirect | ||
google.golang.org/protobuf v1.31.0 // indirect | ||
gopkg.in/inf.v0 v0.9.1 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
k8s.io/apiextensions-apiserver v0.28.3 // indirect | ||
k8s.io/component-base v0.28.3 // indirect | ||
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect | ||
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect | ||
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect | ||
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect | ||
sigs.k8s.io/yaml v1.4.0 // indirect | ||
) |
Oops, something went wrong.