diff --git a/charts/_crds/cluster.clusterpedia.io_clustersyncresources.yaml b/charts/_crds/cluster.clusterpedia.io_clustersyncresources.yaml new file mode 100644 index 000000000..dc50f8dd2 --- /dev/null +++ b/charts/_crds/cluster.clusterpedia.io_clustersyncresources.yaml @@ -0,0 +1,60 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.9.0 + creationTimestamp: null + name: clustersyncresources.cluster.clusterpedia.io +spec: + group: cluster.clusterpedia.io + names: + kind: ClusterSyncResources + listKind: ClusterSyncResourcesList + plural: clustersyncresources + singular: clustersyncresources + scope: Cluster + versions: + - name: v1alpha2 + schema: + openAPIV3Schema: + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + properties: + syncResources: + items: + properties: + group: + type: string + resources: + items: + type: string + minItems: 1 + type: array + versions: + items: + type: string + type: array + required: + - group + - resources + type: object + type: array + required: + - syncResources + type: object + type: object + served: true + storage: true diff --git a/charts/_crds/cluster.clusterpedia.io_pediaclusters.yaml b/charts/_crds/cluster.clusterpedia.io_pediaclusters.yaml index d0748553d..8cbe71445 100644 --- a/charts/_crds/cluster.clusterpedia.io_pediaclusters.yaml +++ b/charts/_crds/cluster.clusterpedia.io_pediaclusters.yaml @@ -78,6 +78,8 @@ spec: - resources type: object type: array + syncResourcesRefName: + type: string tokenData: format: byte type: string diff --git a/examples/clustersyncresources.yaml b/examples/clustersyncresources.yaml new file mode 100644 index 000000000..87b02acfa --- /dev/null +++ b/examples/clustersyncresources.yaml @@ -0,0 +1,11 @@ +apiVersion: cluster.clusterpedia.io/v1alpha2 +kind: ClusterSyncResources +metadata: + name: cluster-sync-resources-example +spec: + syncResources: + - group: "" + resources: + - pods + versions: + - v1 diff --git a/examples/pediacluster.yaml b/examples/pediacluster.yaml index 6f4ebb00b..87f385d23 100644 --- a/examples/pediacluster.yaml +++ b/examples/pediacluster.yaml @@ -9,9 +9,9 @@ spec: certData: keyData: syncResources: - - group: apps - resources: - - deployments - - group: "" - resources: - - pods + - group: apps + resources: + - deployments + - group: "" + resources: + - pods diff --git a/hack/update-codegen.sh b/hack/update-codegen.sh index cf2aba123..c439af99c 100755 --- a/hack/update-codegen.sh +++ b/hack/update-codegen.sh @@ -52,13 +52,15 @@ client-gen \ --input-base="github.com/clusterpedia-io/api" \ --input="cluster/v1alpha2" \ --output-package="github.com/clusterpedia-io/clusterpedia/pkg/generated/clientset" \ - --clientset-name="versioned" + --clientset-name="versioned" \ + --plural-exceptions="ClusterSyncResources:ClusterSyncResources" echo "Generating with lister-gen" lister-gen \ --go-header-file="hack/boilerplate.go.txt" \ --input-dirs="github.com/clusterpedia-io/api/cluster/v1alpha2" \ - --output-package="github.com/clusterpedia-io/clusterpedia/pkg/generated/listers" + --output-package="github.com/clusterpedia-io/clusterpedia/pkg/generated/listers" \ + --plural-exceptions="ClusterSyncResources:ClusterSyncResources" echo "Generating with informer-gen" informer-gen \ @@ -67,7 +69,8 @@ informer-gen \ --output-package="github.com/clusterpedia-io/clusterpedia/pkg/generated/informers" \ --versioned-clientset-package="github.com/clusterpedia-io/clusterpedia/pkg/generated/clientset/versioned" \ --listers-package="github.com/clusterpedia-io/clusterpedia/pkg/generated/listers" \ - --output-package="github.com/clusterpedia-io/clusterpedia/pkg/generated/informers" + --output-package="github.com/clusterpedia-io/clusterpedia/pkg/generated/informers" \ + --plural-exceptions="ClusterSyncResources:ClusterSyncResources" #echo "Generating with openapi-gen" #openapi-gen \ diff --git a/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/cluster_client.go b/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/cluster_client.go index 36c502e4e..cfc34c4b4 100644 --- a/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/cluster_client.go +++ b/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/cluster_client.go @@ -12,6 +12,7 @@ import ( type ClusterV1alpha2Interface interface { RESTClient() rest.Interface + ClusterSyncResourcesGetter PediaClustersGetter } @@ -20,6 +21,10 @@ type ClusterV1alpha2Client struct { restClient rest.Interface } +func (c *ClusterV1alpha2Client) ClusterSyncResources() ClusterSyncResourcesInterface { + return newClusterSyncResources(c) +} + func (c *ClusterV1alpha2Client) PediaClusters() PediaClusterInterface { return newPediaClusters(c) } diff --git a/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/clustersyncresources.go b/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/clustersyncresources.go new file mode 100644 index 000000000..a8fa13fd2 --- /dev/null +++ b/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/clustersyncresources.go @@ -0,0 +1,152 @@ +// Code generated by client-gen. DO NOT EDIT. + +package v1alpha2 + +import ( + "context" + "time" + + v1alpha2 "github.com/clusterpedia-io/api/cluster/v1alpha2" + scheme "github.com/clusterpedia-io/clusterpedia/pkg/generated/clientset/versioned/scheme" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + types "k8s.io/apimachinery/pkg/types" + watch "k8s.io/apimachinery/pkg/watch" + rest "k8s.io/client-go/rest" +) + +// ClusterSyncResourcesGetter has a method to return a ClusterSyncResourcesInterface. +// A group's client should implement this interface. +type ClusterSyncResourcesGetter interface { + ClusterSyncResources() ClusterSyncResourcesInterface +} + +// ClusterSyncResourcesInterface has methods to work with ClusterSyncResources resources. +type ClusterSyncResourcesInterface interface { + Create(ctx context.Context, clusterSyncResources *v1alpha2.ClusterSyncResources, opts v1.CreateOptions) (*v1alpha2.ClusterSyncResources, error) + Update(ctx context.Context, clusterSyncResources *v1alpha2.ClusterSyncResources, opts v1.UpdateOptions) (*v1alpha2.ClusterSyncResources, error) + Delete(ctx context.Context, name string, opts v1.DeleteOptions) error + DeleteCollection(ctx context.Context, opts v1.DeleteOptions, listOpts v1.ListOptions) error + Get(ctx context.Context, name string, opts v1.GetOptions) (*v1alpha2.ClusterSyncResources, error) + List(ctx context.Context, opts v1.ListOptions) (*v1alpha2.ClusterSyncResourcesList, error) + Watch(ctx context.Context, opts v1.ListOptions) (watch.Interface, error) + Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts v1.PatchOptions, subresources ...string) (result *v1alpha2.ClusterSyncResources, err error) + ClusterSyncResourcesExpansion +} + +// clusterSyncResources implements ClusterSyncResourcesInterface +type clusterSyncResources struct { + client rest.Interface +} + +// newClusterSyncResources returns a ClusterSyncResources +func newClusterSyncResources(c *ClusterV1alpha2Client) *clusterSyncResources { + return &clusterSyncResources{ + client: c.RESTClient(), + } +} + +// Get takes name of the clusterSyncResources, and returns the corresponding clusterSyncResources object, and an error if there is any. +func (c *clusterSyncResources) Get(ctx context.Context, name string, options v1.GetOptions) (result *v1alpha2.ClusterSyncResources, err error) { + result = &v1alpha2.ClusterSyncResources{} + err = c.client.Get(). + Resource("clustersyncresources"). + Name(name). + VersionedParams(&options, scheme.ParameterCodec). + Do(ctx). + Into(result) + return +} + +// List takes label and field selectors, and returns the list of ClusterSyncResources that match those selectors. +func (c *clusterSyncResources) List(ctx context.Context, opts v1.ListOptions) (result *v1alpha2.ClusterSyncResourcesList, err error) { + var timeout time.Duration + if opts.TimeoutSeconds != nil { + timeout = time.Duration(*opts.TimeoutSeconds) * time.Second + } + result = &v1alpha2.ClusterSyncResourcesList{} + err = c.client.Get(). + Resource("clustersyncresources"). + VersionedParams(&opts, scheme.ParameterCodec). + Timeout(timeout). + Do(ctx). + Into(result) + return +} + +// Watch returns a watch.Interface that watches the requested clusterSyncResources. +func (c *clusterSyncResources) Watch(ctx context.Context, opts v1.ListOptions) (watch.Interface, error) { + var timeout time.Duration + if opts.TimeoutSeconds != nil { + timeout = time.Duration(*opts.TimeoutSeconds) * time.Second + } + opts.Watch = true + return c.client.Get(). + Resource("clustersyncresources"). + VersionedParams(&opts, scheme.ParameterCodec). + Timeout(timeout). + Watch(ctx) +} + +// Create takes the representation of a clusterSyncResources and creates it. Returns the server's representation of the clusterSyncResources, and an error, if there is any. +func (c *clusterSyncResources) Create(ctx context.Context, clusterSyncResources *v1alpha2.ClusterSyncResources, opts v1.CreateOptions) (result *v1alpha2.ClusterSyncResources, err error) { + result = &v1alpha2.ClusterSyncResources{} + err = c.client.Post(). + Resource("clustersyncresources"). + VersionedParams(&opts, scheme.ParameterCodec). + Body(clusterSyncResources). + Do(ctx). + Into(result) + return +} + +// Update takes the representation of a clusterSyncResources and updates it. Returns the server's representation of the clusterSyncResources, and an error, if there is any. +func (c *clusterSyncResources) Update(ctx context.Context, clusterSyncResources *v1alpha2.ClusterSyncResources, opts v1.UpdateOptions) (result *v1alpha2.ClusterSyncResources, err error) { + result = &v1alpha2.ClusterSyncResources{} + err = c.client.Put(). + Resource("clustersyncresources"). + Name(clusterSyncResources.Name). + VersionedParams(&opts, scheme.ParameterCodec). + Body(clusterSyncResources). + Do(ctx). + Into(result) + return +} + +// Delete takes name of the clusterSyncResources and deletes it. Returns an error if one occurs. +func (c *clusterSyncResources) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error { + return c.client.Delete(). + Resource("clustersyncresources"). + Name(name). + Body(&opts). + Do(ctx). + Error() +} + +// DeleteCollection deletes a collection of objects. +func (c *clusterSyncResources) DeleteCollection(ctx context.Context, opts v1.DeleteOptions, listOpts v1.ListOptions) error { + var timeout time.Duration + if listOpts.TimeoutSeconds != nil { + timeout = time.Duration(*listOpts.TimeoutSeconds) * time.Second + } + return c.client.Delete(). + Resource("clustersyncresources"). + VersionedParams(&listOpts, scheme.ParameterCodec). + Timeout(timeout). + Body(&opts). + Do(ctx). + Error() +} + +// Patch applies the patch and returns the patched clusterSyncResources. +func (c *clusterSyncResources) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts v1.PatchOptions, subresources ...string) (result *v1alpha2.ClusterSyncResources, err error) { + result = &v1alpha2.ClusterSyncResources{} + err = c.client.Patch(pt). + Resource("clustersyncresources"). + Name(name). + SubResource(subresources...). + VersionedParams(&opts, scheme.ParameterCodec). + Body(data). + Do(ctx). + Into(result) + return +} diff --git a/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/fake/fake_cluster_client.go b/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/fake/fake_cluster_client.go index 00e38561d..3de30531a 100644 --- a/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/fake/fake_cluster_client.go +++ b/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/fake/fake_cluster_client.go @@ -12,6 +12,10 @@ type FakeClusterV1alpha2 struct { *testing.Fake } +func (c *FakeClusterV1alpha2) ClusterSyncResources() v1alpha2.ClusterSyncResourcesInterface { + return &FakeClusterSyncResources{c} +} + func (c *FakeClusterV1alpha2) PediaClusters() v1alpha2.PediaClusterInterface { return &FakePediaClusters{c} } diff --git a/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/fake/fake_clustersyncresources.go b/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/fake/fake_clustersyncresources.go new file mode 100644 index 000000000..1c5717609 --- /dev/null +++ b/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/fake/fake_clustersyncresources.go @@ -0,0 +1,106 @@ +// Code generated by client-gen. DO NOT EDIT. + +package fake + +import ( + "context" + + v1alpha2 "github.com/clusterpedia-io/api/cluster/v1alpha2" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + labels "k8s.io/apimachinery/pkg/labels" + schema "k8s.io/apimachinery/pkg/runtime/schema" + types "k8s.io/apimachinery/pkg/types" + watch "k8s.io/apimachinery/pkg/watch" + testing "k8s.io/client-go/testing" +) + +// FakeClusterSyncResources implements ClusterSyncResourcesInterface +type FakeClusterSyncResources struct { + Fake *FakeClusterV1alpha2 +} + +var clustersyncresourcesResource = schema.GroupVersionResource{Group: "cluster.clusterpedia.io", Version: "v1alpha2", Resource: "clustersyncresources"} + +var clustersyncresourcesKind = schema.GroupVersionKind{Group: "cluster.clusterpedia.io", Version: "v1alpha2", Kind: "ClusterSyncResources"} + +// Get takes name of the clusterSyncResources, and returns the corresponding clusterSyncResources object, and an error if there is any. +func (c *FakeClusterSyncResources) Get(ctx context.Context, name string, options v1.GetOptions) (result *v1alpha2.ClusterSyncResources, err error) { + obj, err := c.Fake. + Invokes(testing.NewRootGetAction(clustersyncresourcesResource, name), &v1alpha2.ClusterSyncResources{}) + if obj == nil { + return nil, err + } + return obj.(*v1alpha2.ClusterSyncResources), err +} + +// List takes label and field selectors, and returns the list of ClusterSyncResources that match those selectors. +func (c *FakeClusterSyncResources) List(ctx context.Context, opts v1.ListOptions) (result *v1alpha2.ClusterSyncResourcesList, err error) { + obj, err := c.Fake. + Invokes(testing.NewRootListAction(clustersyncresourcesResource, clustersyncresourcesKind, opts), &v1alpha2.ClusterSyncResourcesList{}) + if obj == nil { + return nil, err + } + + label, _, _ := testing.ExtractFromListOptions(opts) + if label == nil { + label = labels.Everything() + } + list := &v1alpha2.ClusterSyncResourcesList{ListMeta: obj.(*v1alpha2.ClusterSyncResourcesList).ListMeta} + for _, item := range obj.(*v1alpha2.ClusterSyncResourcesList).Items { + if label.Matches(labels.Set(item.Labels)) { + list.Items = append(list.Items, item) + } + } + return list, err +} + +// Watch returns a watch.Interface that watches the requested clusterSyncResources. +func (c *FakeClusterSyncResources) Watch(ctx context.Context, opts v1.ListOptions) (watch.Interface, error) { + return c.Fake. + InvokesWatch(testing.NewRootWatchAction(clustersyncresourcesResource, opts)) +} + +// Create takes the representation of a clusterSyncResources and creates it. Returns the server's representation of the clusterSyncResources, and an error, if there is any. +func (c *FakeClusterSyncResources) Create(ctx context.Context, clusterSyncResources *v1alpha2.ClusterSyncResources, opts v1.CreateOptions) (result *v1alpha2.ClusterSyncResources, err error) { + obj, err := c.Fake. + Invokes(testing.NewRootCreateAction(clustersyncresourcesResource, clusterSyncResources), &v1alpha2.ClusterSyncResources{}) + if obj == nil { + return nil, err + } + return obj.(*v1alpha2.ClusterSyncResources), err +} + +// Update takes the representation of a clusterSyncResources and updates it. Returns the server's representation of the clusterSyncResources, and an error, if there is any. +func (c *FakeClusterSyncResources) Update(ctx context.Context, clusterSyncResources *v1alpha2.ClusterSyncResources, opts v1.UpdateOptions) (result *v1alpha2.ClusterSyncResources, err error) { + obj, err := c.Fake. + Invokes(testing.NewRootUpdateAction(clustersyncresourcesResource, clusterSyncResources), &v1alpha2.ClusterSyncResources{}) + if obj == nil { + return nil, err + } + return obj.(*v1alpha2.ClusterSyncResources), err +} + +// Delete takes name of the clusterSyncResources and deletes it. Returns an error if one occurs. +func (c *FakeClusterSyncResources) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error { + _, err := c.Fake. + Invokes(testing.NewRootDeleteActionWithOptions(clustersyncresourcesResource, name, opts), &v1alpha2.ClusterSyncResources{}) + return err +} + +// DeleteCollection deletes a collection of objects. +func (c *FakeClusterSyncResources) DeleteCollection(ctx context.Context, opts v1.DeleteOptions, listOpts v1.ListOptions) error { + action := testing.NewRootDeleteCollectionAction(clustersyncresourcesResource, listOpts) + + _, err := c.Fake.Invokes(action, &v1alpha2.ClusterSyncResourcesList{}) + return err +} + +// Patch applies the patch and returns the patched clusterSyncResources. +func (c *FakeClusterSyncResources) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts v1.PatchOptions, subresources ...string) (result *v1alpha2.ClusterSyncResources, err error) { + obj, err := c.Fake. + Invokes(testing.NewRootPatchSubresourceAction(clustersyncresourcesResource, name, pt, data, subresources...), &v1alpha2.ClusterSyncResources{}) + if obj == nil { + return nil, err + } + return obj.(*v1alpha2.ClusterSyncResources), err +} diff --git a/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/generated_expansion.go b/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/generated_expansion.go index 2680156ca..25b7539ad 100644 --- a/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/generated_expansion.go +++ b/pkg/generated/clientset/versioned/typed/cluster/v1alpha2/generated_expansion.go @@ -2,4 +2,6 @@ package v1alpha2 +type ClusterSyncResourcesExpansion interface{} + type PediaClusterExpansion interface{} diff --git a/pkg/generated/informers/externalversions/cluster/v1alpha2/clustersyncresources.go b/pkg/generated/informers/externalversions/cluster/v1alpha2/clustersyncresources.go new file mode 100644 index 000000000..26d135924 --- /dev/null +++ b/pkg/generated/informers/externalversions/cluster/v1alpha2/clustersyncresources.go @@ -0,0 +1,73 @@ +// Code generated by informer-gen. DO NOT EDIT. + +package v1alpha2 + +import ( + "context" + time "time" + + clusterv1alpha2 "github.com/clusterpedia-io/api/cluster/v1alpha2" + versioned "github.com/clusterpedia-io/clusterpedia/pkg/generated/clientset/versioned" + internalinterfaces "github.com/clusterpedia-io/clusterpedia/pkg/generated/informers/externalversions/internalinterfaces" + v1alpha2 "github.com/clusterpedia-io/clusterpedia/pkg/generated/listers/cluster/v1alpha2" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + runtime "k8s.io/apimachinery/pkg/runtime" + watch "k8s.io/apimachinery/pkg/watch" + cache "k8s.io/client-go/tools/cache" +) + +// ClusterSyncResourcesInformer provides access to a shared informer and lister for +// ClusterSyncResources. +type ClusterSyncResourcesInformer interface { + Informer() cache.SharedIndexInformer + Lister() v1alpha2.ClusterSyncResourcesLister +} + +type clusterSyncResourcesInformer struct { + factory internalinterfaces.SharedInformerFactory + tweakListOptions internalinterfaces.TweakListOptionsFunc +} + +// NewClusterSyncResourcesInformer constructs a new informer for ClusterSyncResources type. +// Always prefer using an informer factory to get a shared informer instead of getting an independent +// one. This reduces memory footprint and number of connections to the server. +func NewClusterSyncResourcesInformer(client versioned.Interface, resyncPeriod time.Duration, indexers cache.Indexers) cache.SharedIndexInformer { + return NewFilteredClusterSyncResourcesInformer(client, resyncPeriod, indexers, nil) +} + +// NewFilteredClusterSyncResourcesInformer constructs a new informer for ClusterSyncResources type. +// Always prefer using an informer factory to get a shared informer instead of getting an independent +// one. This reduces memory footprint and number of connections to the server. +func NewFilteredClusterSyncResourcesInformer(client versioned.Interface, resyncPeriod time.Duration, indexers cache.Indexers, tweakListOptions internalinterfaces.TweakListOptionsFunc) cache.SharedIndexInformer { + return cache.NewSharedIndexInformer( + &cache.ListWatch{ + ListFunc: func(options v1.ListOptions) (runtime.Object, error) { + if tweakListOptions != nil { + tweakListOptions(&options) + } + return client.ClusterV1alpha2().ClusterSyncResources().List(context.TODO(), options) + }, + WatchFunc: func(options v1.ListOptions) (watch.Interface, error) { + if tweakListOptions != nil { + tweakListOptions(&options) + } + return client.ClusterV1alpha2().ClusterSyncResources().Watch(context.TODO(), options) + }, + }, + &clusterv1alpha2.ClusterSyncResources{}, + resyncPeriod, + indexers, + ) +} + +func (f *clusterSyncResourcesInformer) defaultInformer(client versioned.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer { + return NewFilteredClusterSyncResourcesInformer(client, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions) +} + +func (f *clusterSyncResourcesInformer) Informer() cache.SharedIndexInformer { + return f.factory.InformerFor(&clusterv1alpha2.ClusterSyncResources{}, f.defaultInformer) +} + +func (f *clusterSyncResourcesInformer) Lister() v1alpha2.ClusterSyncResourcesLister { + return v1alpha2.NewClusterSyncResourcesLister(f.Informer().GetIndexer()) +} diff --git a/pkg/generated/informers/externalversions/cluster/v1alpha2/interface.go b/pkg/generated/informers/externalversions/cluster/v1alpha2/interface.go index 35e631a5d..6c3448622 100644 --- a/pkg/generated/informers/externalversions/cluster/v1alpha2/interface.go +++ b/pkg/generated/informers/externalversions/cluster/v1alpha2/interface.go @@ -8,6 +8,8 @@ import ( // Interface provides access to all the informers in this group version. type Interface interface { + // ClusterSyncResources returns a ClusterSyncResourcesInformer. + ClusterSyncResources() ClusterSyncResourcesInformer // PediaClusters returns a PediaClusterInformer. PediaClusters() PediaClusterInformer } @@ -23,6 +25,11 @@ func New(f internalinterfaces.SharedInformerFactory, namespace string, tweakList return &version{factory: f, namespace: namespace, tweakListOptions: tweakListOptions} } +// ClusterSyncResources returns a ClusterSyncResourcesInformer. +func (v *version) ClusterSyncResources() ClusterSyncResourcesInformer { + return &clusterSyncResourcesInformer{factory: v.factory, tweakListOptions: v.tweakListOptions} +} + // PediaClusters returns a PediaClusterInformer. func (v *version) PediaClusters() PediaClusterInformer { return &pediaClusterInformer{factory: v.factory, tweakListOptions: v.tweakListOptions} diff --git a/pkg/generated/informers/externalversions/generic.go b/pkg/generated/informers/externalversions/generic.go index 5dcff00ca..714e89313 100644 --- a/pkg/generated/informers/externalversions/generic.go +++ b/pkg/generated/informers/externalversions/generic.go @@ -37,6 +37,8 @@ func (f *genericInformer) Lister() cache.GenericLister { func (f *sharedInformerFactory) ForResource(resource schema.GroupVersionResource) (GenericInformer, error) { switch resource { // Group=cluster.clusterpedia.io, Version=v1alpha2 + case v1alpha2.SchemeGroupVersion.WithResource("clustersyncresources"): + return &genericInformer{resource: resource.GroupResource(), informer: f.Cluster().V1alpha2().ClusterSyncResources().Informer()}, nil case v1alpha2.SchemeGroupVersion.WithResource("pediaclusters"): return &genericInformer{resource: resource.GroupResource(), informer: f.Cluster().V1alpha2().PediaClusters().Informer()}, nil diff --git a/pkg/generated/listers/cluster/v1alpha2/clustersyncresources.go b/pkg/generated/listers/cluster/v1alpha2/clustersyncresources.go new file mode 100644 index 000000000..69ffe1ba4 --- /dev/null +++ b/pkg/generated/listers/cluster/v1alpha2/clustersyncresources.go @@ -0,0 +1,52 @@ +// Code generated by lister-gen. DO NOT EDIT. + +package v1alpha2 + +import ( + v1alpha2 "github.com/clusterpedia-io/api/cluster/v1alpha2" + "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/client-go/tools/cache" +) + +// ClusterSyncResourcesLister helps list ClusterSyncResources. +// All objects returned here must be treated as read-only. +type ClusterSyncResourcesLister interface { + // List lists all ClusterSyncResources in the indexer. + // Objects returned here must be treated as read-only. + List(selector labels.Selector) (ret []*v1alpha2.ClusterSyncResources, err error) + // Get retrieves the ClusterSyncResources from the index for a given name. + // Objects returned here must be treated as read-only. + Get(name string) (*v1alpha2.ClusterSyncResources, error) + ClusterSyncResourcesListerExpansion +} + +// clusterSyncResourcesLister implements the ClusterSyncResourcesLister interface. +type clusterSyncResourcesLister struct { + indexer cache.Indexer +} + +// NewClusterSyncResourcesLister returns a new ClusterSyncResourcesLister. +func NewClusterSyncResourcesLister(indexer cache.Indexer) ClusterSyncResourcesLister { + return &clusterSyncResourcesLister{indexer: indexer} +} + +// List lists all ClusterSyncResources in the indexer. +func (s *clusterSyncResourcesLister) List(selector labels.Selector) (ret []*v1alpha2.ClusterSyncResources, err error) { + err = cache.ListAll(s.indexer, selector, func(m interface{}) { + ret = append(ret, m.(*v1alpha2.ClusterSyncResources)) + }) + return ret, err +} + +// Get retrieves the ClusterSyncResources from the index for a given name. +func (s *clusterSyncResourcesLister) Get(name string) (*v1alpha2.ClusterSyncResources, error) { + obj, exists, err := s.indexer.GetByKey(name) + if err != nil { + return nil, err + } + if !exists { + return nil, errors.NewNotFound(v1alpha2.Resource("clustersyncresources"), name) + } + return obj.(*v1alpha2.ClusterSyncResources), nil +} diff --git a/pkg/generated/listers/cluster/v1alpha2/expansion_generated.go b/pkg/generated/listers/cluster/v1alpha2/expansion_generated.go index da93dcaa0..fc389aa96 100644 --- a/pkg/generated/listers/cluster/v1alpha2/expansion_generated.go +++ b/pkg/generated/listers/cluster/v1alpha2/expansion_generated.go @@ -2,6 +2,10 @@ package v1alpha2 +// ClusterSyncResourcesListerExpansion allows custom methods to be added to +// ClusterSyncResourcesLister. +type ClusterSyncResourcesListerExpansion interface{} + // PediaClusterListerExpansion allows custom methods to be added to // PediaClusterLister. type PediaClusterListerExpansion interface{} diff --git a/pkg/synchromanager/clustersynchro_manager.go b/pkg/synchromanager/clustersynchro_manager.go index 63c9cfa70..57cfcb10d 100644 --- a/pkg/synchromanager/clustersynchro_manager.go +++ b/pkg/synchromanager/clustersynchro_manager.go @@ -11,6 +11,7 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/util/wait" clientset "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -43,10 +44,11 @@ type Manager struct { clusterpediaclient crdclientset.Interface informerFactory externalversions.SharedInformerFactory - queue workqueue.RateLimitingInterface - storage storage.StorageFactory - clusterlister clusterlister.PediaClusterLister - clusterInformer cache.SharedIndexInformer + queue workqueue.RateLimitingInterface + storage storage.StorageFactory + clusterlister clusterlister.PediaClusterLister + clusterSyncResourcesLister clusterlister.ClusterSyncResourcesLister + clusterInformer cache.SharedIndexInformer synchrolock sync.RWMutex synchros map[string]*clustersynchro.ClusterSynchro @@ -56,15 +58,17 @@ type Manager struct { func NewManager(kubeclient clientset.Interface, client crdclientset.Interface, storage storage.StorageFactory) *Manager { factory := externalversions.NewSharedInformerFactory(client, 0) clusterinformer := factory.Cluster().V1alpha2().PediaClusters() + clusterSyncResourcesInformer := factory.Cluster().V1alpha2().ClusterSyncResources() manager := &Manager{ informerFactory: factory, kubeclient: kubeclient, clusterpediaclient: client, - storage: storage, - clusterlister: clusterinformer.Lister(), - clusterInformer: clusterinformer.Informer(), + storage: storage, + clusterlister: clusterinformer.Lister(), + clusterInformer: clusterinformer.Informer(), + clusterSyncResourcesLister: clusterSyncResourcesInformer.Lister(), queue: workqueue.NewRateLimitingQueue( workqueue.NewItemExponentialFailureRateLimiter(2*time.Second, 5*time.Second), ), @@ -80,6 +84,14 @@ func NewManager(kubeclient clientset.Interface, client crdclientset.Interface, s }, ) + clusterSyncResourcesInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ + AddFunc: manager.handleClusterSyncResources, + UpdateFunc: func(oldObj, newObj interface{}) { + manager.handleClusterSyncResources(newObj) + }, + DeleteFunc: manager.handleClusterSyncResources, + }) + return manager } @@ -281,8 +293,22 @@ func (manager *Manager) reconcileCluster(cluster *clusterv1alpha2.PediaCluster) } manager.UpdateClusterSynchroCondition(cluster.Name, clusterv1alpha2.RunningConditionReason, "", metav1.ConditionTrue) + syncResources := cluster.Spec.SyncResources + if refName := cluster.Spec.SyncResourcesRefName; refName != "" { + ref, err := manager.clusterSyncResourcesLister.Get(refName) + if err != nil { + if !apierrors.IsNotFound(err) { + klog.ErrorS(err, "Failed to get SyncResourcesRef of cluster", "cluster", cluster.Name, "SyncResourcesRef", refName) + return err + } + // TODO: Use more obvious method to let users know. + klog.Warningf("Failed to get SyncResourcesRef of cluster %s: %v", cluster.Name, err) + } else { + syncResources = append(syncResources, ref.Spec.SyncResources...) + } + } - synchro.SetResources(cluster.Spec.SyncResources, cluster.Spec.SyncAllCustomResources) + synchro.SetResources(syncResources, cluster.Spec.SyncAllCustomResources) manager.synchrolock.Lock() manager.synchros[cluster.Name] = synchro @@ -356,6 +382,25 @@ func (manager *Manager) UpdateClusterStatus(ctx context.Context, name string, st }) } +func (manager *Manager) handleClusterSyncResources(obj interface{}) { + // ClusterSyncResources is cluster scoped resource, key is name + refName, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) + if err != nil { + klog.ErrorS(err, "handle clustersyncresources failed") + return + } + clusters, err := manager.clusterlister.List(labels.Everything()) + if err != nil { + klog.ErrorS(err, "list clusters failed while handling clustersyncresources", "clustersyncresources", refName) + return + } + for _, cluster := range clusters { + if cluster.Spec.SyncResourcesRefName == refName { + manager.enqueue(cluster) + } + } +} + func buildClusterConfig(cluster *clusterv1alpha2.PediaCluster) (*rest.Config, error) { if len(cluster.Spec.Kubeconfig) != 0 { clientconfig, err := clientcmd.NewClientConfigFromBytes(cluster.Spec.Kubeconfig) diff --git a/staging/src/github.com/clusterpedia-io/api/cluster/v1alpha2/register.go b/staging/src/github.com/clusterpedia-io/api/cluster/v1alpha2/register.go index 75fc6233a..c5498b7d2 100644 --- a/staging/src/github.com/clusterpedia-io/api/cluster/v1alpha2/register.go +++ b/staging/src/github.com/clusterpedia-io/api/cluster/v1alpha2/register.go @@ -42,6 +42,8 @@ func addKnownTypes(scheme *runtime.Scheme) error { scheme.AddKnownTypes(SchemeGroupVersion, &PediaCluster{}, &PediaClusterList{}, + &ClusterSyncResources{}, + &ClusterSyncResourcesList{}, ) // AddToGroupVersion allows the serialization of client types like ListOptions. v1.AddToGroupVersion(scheme, SchemeGroupVersion) diff --git a/staging/src/github.com/clusterpedia-io/api/cluster/v1alpha2/types.go b/staging/src/github.com/clusterpedia-io/api/cluster/v1alpha2/types.go index 9675af3b4..38b0fe97b 100644 --- a/staging/src/github.com/clusterpedia-io/api/cluster/v1alpha2/types.go +++ b/staging/src/github.com/clusterpedia-io/api/cluster/v1alpha2/types.go @@ -70,6 +70,9 @@ type ClusterSpec struct { // +optional SyncAllCustomResources bool `json:"syncAllCustomResources,omitempty"` + + // +optional + SyncResourcesRefName string `json:"syncResourcesRefName,omitempty"` } type ClusterGroupResources struct { @@ -194,3 +197,31 @@ type PediaClusterList struct { Items []PediaCluster `json:"items"` } + +// +genclient +// +genclient:nonNamespaced +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object +// +kubebuilder:resource:scope="Cluster" +type ClusterSyncResources struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + // +optional + Spec ClusterSyncResourcesSpec `json:"spec,omitempty"` +} + +type ClusterSyncResourcesSpec struct { + // +required + SyncResources []ClusterGroupResources `json:"syncResources"` +} + +// +kubebuilder:object:root=true +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object +type ClusterSyncResourcesList struct { + metav1.TypeMeta `json:",inline"` + + // +optional + metav1.ListMeta `json:"metadata,omitempty"` + + Items []ClusterSyncResources `json:"items"` +} diff --git a/staging/src/github.com/clusterpedia-io/api/cluster/v1alpha2/zz_generated.deepcopy.go b/staging/src/github.com/clusterpedia-io/api/cluster/v1alpha2/zz_generated.deepcopy.go index 03fdb34e3..b2183dbca 100644 --- a/staging/src/github.com/clusterpedia-io/api/cluster/v1alpha2/zz_generated.deepcopy.go +++ b/staging/src/github.com/clusterpedia-io/api/cluster/v1alpha2/zz_generated.deepcopy.go @@ -177,6 +177,89 @@ func (in *ClusterStatus) DeepCopy() *ClusterStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ClusterSyncResources) DeepCopyInto(out *ClusterSyncResources) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + in.Spec.DeepCopyInto(&out.Spec) + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterSyncResources. +func (in *ClusterSyncResources) DeepCopy() *ClusterSyncResources { + if in == nil { + return nil + } + out := new(ClusterSyncResources) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ClusterSyncResources) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ClusterSyncResourcesList) DeepCopyInto(out *ClusterSyncResourcesList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]ClusterSyncResources, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterSyncResourcesList. +func (in *ClusterSyncResourcesList) DeepCopy() *ClusterSyncResourcesList { + if in == nil { + return nil + } + out := new(ClusterSyncResourcesList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *ClusterSyncResourcesList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ClusterSyncResourcesSpec) DeepCopyInto(out *ClusterSyncResourcesSpec) { + *out = *in + if in.SyncResources != nil { + in, out := &in.SyncResources, &out.SyncResources + *out = make([]ClusterGroupResources, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterSyncResourcesSpec. +func (in *ClusterSyncResourcesSpec) DeepCopy() *ClusterSyncResourcesSpec { + if in == nil { + return nil + } + out := new(ClusterSyncResourcesSpec) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *PediaCluster) DeepCopyInto(out *PediaCluster) { *out = *in