From 63bde529ca63e0d9a9109a2a99a0021c1857a557 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakivskyy Date: Mon, 2 Sep 2024 19:30:44 +0300 Subject: [PATCH] Add CRD for nfd-worker config Signed-off-by: Oleg Zhurakivskyy --- .../nfd/v1alpha1/fake/fake_nfd_client.go | 4 + .../nfd/v1alpha1/fake/fake_workerconfig.go | 129 +++++++++++++ .../typed/nfd/v1alpha1/generated_expansion.go | 2 + .../typed/nfd/v1alpha1/nfd_client.go | 5 + .../typed/nfd/v1alpha1/workerconfig.go | 178 ++++++++++++++++++ .../informers/externalversions/generic.go | 2 + .../nfd/v1alpha1/interface.go | 7 + .../nfd/v1alpha1/workerconfig.go | 90 +++++++++ .../nfd/v1alpha1/expansion_generated.go | 8 + .../listers/nfd/v1alpha1/workerconfig.go | 99 ++++++++++ api/nfd/v1alpha1/types.go | 28 +++ api/nfd/v1alpha1/zz_generated.deepcopy.go | 83 ++++++++ deployment/base/nfd-crds/nfd-api-crds.yaml | 54 ++++++ .../crds/nfd-api-crds.yaml | 54 ++++++ pkg/nfd-worker/nfd-api-controller.go | 120 ++++++++++++ pkg/nfd-worker/nfd-worker.go | 22 +++ 16 files changed, 885 insertions(+) create mode 100644 api/generated/clientset/versioned/typed/nfd/v1alpha1/fake/fake_workerconfig.go create mode 100644 api/generated/clientset/versioned/typed/nfd/v1alpha1/workerconfig.go create mode 100644 api/generated/informers/externalversions/nfd/v1alpha1/workerconfig.go create mode 100644 api/generated/listers/nfd/v1alpha1/workerconfig.go create mode 100644 pkg/nfd-worker/nfd-api-controller.go diff --git a/api/generated/clientset/versioned/typed/nfd/v1alpha1/fake/fake_nfd_client.go b/api/generated/clientset/versioned/typed/nfd/v1alpha1/fake/fake_nfd_client.go index 282a6e717b..f3fe39e779 100644 --- a/api/generated/clientset/versioned/typed/nfd/v1alpha1/fake/fake_nfd_client.go +++ b/api/generated/clientset/versioned/typed/nfd/v1alpha1/fake/fake_nfd_client.go @@ -40,6 +40,10 @@ func (c *FakeNfdV1alpha1) NodeFeatureRules() v1alpha1.NodeFeatureRuleInterface { return &FakeNodeFeatureRules{c} } +func (c *FakeNfdV1alpha1) WorkerConfigs(namespace string) v1alpha1.WorkerConfigInterface { + return &FakeWorkerConfigs{c, namespace} +} + // RESTClient returns a RESTClient that is used to communicate // with API server by this client implementation. func (c *FakeNfdV1alpha1) RESTClient() rest.Interface { diff --git a/api/generated/clientset/versioned/typed/nfd/v1alpha1/fake/fake_workerconfig.go b/api/generated/clientset/versioned/typed/nfd/v1alpha1/fake/fake_workerconfig.go new file mode 100644 index 0000000000..05dd90335d --- /dev/null +++ b/api/generated/clientset/versioned/typed/nfd/v1alpha1/fake/fake_workerconfig.go @@ -0,0 +1,129 @@ +/* +Copyright 2024 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. +*/ + +// Code generated by client-gen. DO NOT EDIT. + +package fake + +import ( + "context" + + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + labels "k8s.io/apimachinery/pkg/labels" + types "k8s.io/apimachinery/pkg/types" + watch "k8s.io/apimachinery/pkg/watch" + testing "k8s.io/client-go/testing" + v1alpha1 "sigs.k8s.io/node-feature-discovery/api/nfd/v1alpha1" +) + +// FakeWorkerConfigs implements WorkerConfigInterface +type FakeWorkerConfigs struct { + Fake *FakeNfdV1alpha1 + ns string +} + +var workerconfigsResource = v1alpha1.SchemeGroupVersion.WithResource("workerconfigs") + +var workerconfigsKind = v1alpha1.SchemeGroupVersion.WithKind("WorkerConfig") + +// Get takes name of the workerConfig, and returns the corresponding workerConfig object, and an error if there is any. +func (c *FakeWorkerConfigs) Get(ctx context.Context, name string, options v1.GetOptions) (result *v1alpha1.WorkerConfig, err error) { + obj, err := c.Fake. + Invokes(testing.NewGetAction(workerconfigsResource, c.ns, name), &v1alpha1.WorkerConfig{}) + + if obj == nil { + return nil, err + } + return obj.(*v1alpha1.WorkerConfig), err +} + +// List takes label and field selectors, and returns the list of WorkerConfigs that match those selectors. +func (c *FakeWorkerConfigs) List(ctx context.Context, opts v1.ListOptions) (result *v1alpha1.WorkerConfigList, err error) { + obj, err := c.Fake. + Invokes(testing.NewListAction(workerconfigsResource, workerconfigsKind, c.ns, opts), &v1alpha1.WorkerConfigList{}) + + if obj == nil { + return nil, err + } + + label, _, _ := testing.ExtractFromListOptions(opts) + if label == nil { + label = labels.Everything() + } + list := &v1alpha1.WorkerConfigList{ListMeta: obj.(*v1alpha1.WorkerConfigList).ListMeta} + for _, item := range obj.(*v1alpha1.WorkerConfigList).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 workerConfigs. +func (c *FakeWorkerConfigs) Watch(ctx context.Context, opts v1.ListOptions) (watch.Interface, error) { + return c.Fake. + InvokesWatch(testing.NewWatchAction(workerconfigsResource, c.ns, opts)) + +} + +// Create takes the representation of a workerConfig and creates it. Returns the server's representation of the workerConfig, and an error, if there is any. +func (c *FakeWorkerConfigs) Create(ctx context.Context, workerConfig *v1alpha1.WorkerConfig, opts v1.CreateOptions) (result *v1alpha1.WorkerConfig, err error) { + obj, err := c.Fake. + Invokes(testing.NewCreateAction(workerconfigsResource, c.ns, workerConfig), &v1alpha1.WorkerConfig{}) + + if obj == nil { + return nil, err + } + return obj.(*v1alpha1.WorkerConfig), err +} + +// Update takes the representation of a workerConfig and updates it. Returns the server's representation of the workerConfig, and an error, if there is any. +func (c *FakeWorkerConfigs) Update(ctx context.Context, workerConfig *v1alpha1.WorkerConfig, opts v1.UpdateOptions) (result *v1alpha1.WorkerConfig, err error) { + obj, err := c.Fake. + Invokes(testing.NewUpdateAction(workerconfigsResource, c.ns, workerConfig), &v1alpha1.WorkerConfig{}) + + if obj == nil { + return nil, err + } + return obj.(*v1alpha1.WorkerConfig), err +} + +// Delete takes name of the workerConfig and deletes it. Returns an error if one occurs. +func (c *FakeWorkerConfigs) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error { + _, err := c.Fake. + Invokes(testing.NewDeleteActionWithOptions(workerconfigsResource, c.ns, name, opts), &v1alpha1.WorkerConfig{}) + + return err +} + +// DeleteCollection deletes a collection of objects. +func (c *FakeWorkerConfigs) DeleteCollection(ctx context.Context, opts v1.DeleteOptions, listOpts v1.ListOptions) error { + action := testing.NewDeleteCollectionAction(workerconfigsResource, c.ns, listOpts) + + _, err := c.Fake.Invokes(action, &v1alpha1.WorkerConfigList{}) + return err +} + +// Patch applies the patch and returns the patched workerConfig. +func (c *FakeWorkerConfigs) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts v1.PatchOptions, subresources ...string) (result *v1alpha1.WorkerConfig, err error) { + obj, err := c.Fake. + Invokes(testing.NewPatchSubresourceAction(workerconfigsResource, c.ns, name, pt, data, subresources...), &v1alpha1.WorkerConfig{}) + + if obj == nil { + return nil, err + } + return obj.(*v1alpha1.WorkerConfig), err +} diff --git a/api/generated/clientset/versioned/typed/nfd/v1alpha1/generated_expansion.go b/api/generated/clientset/versioned/typed/nfd/v1alpha1/generated_expansion.go index 02d3e35186..4d887962d9 100644 --- a/api/generated/clientset/versioned/typed/nfd/v1alpha1/generated_expansion.go +++ b/api/generated/clientset/versioned/typed/nfd/v1alpha1/generated_expansion.go @@ -23,3 +23,5 @@ type NodeFeatureExpansion interface{} type NodeFeatureGroupExpansion interface{} type NodeFeatureRuleExpansion interface{} + +type WorkerConfigExpansion interface{} diff --git a/api/generated/clientset/versioned/typed/nfd/v1alpha1/nfd_client.go b/api/generated/clientset/versioned/typed/nfd/v1alpha1/nfd_client.go index 37a6f340aa..074bec1521 100644 --- a/api/generated/clientset/versioned/typed/nfd/v1alpha1/nfd_client.go +++ b/api/generated/clientset/versioned/typed/nfd/v1alpha1/nfd_client.go @@ -31,6 +31,7 @@ type NfdV1alpha1Interface interface { NodeFeaturesGetter NodeFeatureGroupsGetter NodeFeatureRulesGetter + WorkerConfigsGetter } // NfdV1alpha1Client is used to interact with features provided by the nfd.k8s-sigs.io group. @@ -50,6 +51,10 @@ func (c *NfdV1alpha1Client) NodeFeatureRules() NodeFeatureRuleInterface { return newNodeFeatureRules(c) } +func (c *NfdV1alpha1Client) WorkerConfigs(namespace string) WorkerConfigInterface { + return newWorkerConfigs(c, namespace) +} + // NewForConfig creates a new NfdV1alpha1Client for the given config. // NewForConfig is equivalent to NewForConfigAndClient(c, httpClient), // where httpClient was generated with rest.HTTPClientFor(c). diff --git a/api/generated/clientset/versioned/typed/nfd/v1alpha1/workerconfig.go b/api/generated/clientset/versioned/typed/nfd/v1alpha1/workerconfig.go new file mode 100644 index 0000000000..6ef14a1f77 --- /dev/null +++ b/api/generated/clientset/versioned/typed/nfd/v1alpha1/workerconfig.go @@ -0,0 +1,178 @@ +/* +Copyright 2024 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. +*/ + +// Code generated by client-gen. DO NOT EDIT. + +package v1alpha1 + +import ( + "context" + "time" + + 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" + scheme "sigs.k8s.io/node-feature-discovery/api/generated/clientset/versioned/scheme" + v1alpha1 "sigs.k8s.io/node-feature-discovery/api/nfd/v1alpha1" +) + +// WorkerConfigsGetter has a method to return a WorkerConfigInterface. +// A group's client should implement this interface. +type WorkerConfigsGetter interface { + WorkerConfigs(namespace string) WorkerConfigInterface +} + +// WorkerConfigInterface has methods to work with WorkerConfig resources. +type WorkerConfigInterface interface { + Create(ctx context.Context, workerConfig *v1alpha1.WorkerConfig, opts v1.CreateOptions) (*v1alpha1.WorkerConfig, error) + Update(ctx context.Context, workerConfig *v1alpha1.WorkerConfig, opts v1.UpdateOptions) (*v1alpha1.WorkerConfig, 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) (*v1alpha1.WorkerConfig, error) + List(ctx context.Context, opts v1.ListOptions) (*v1alpha1.WorkerConfigList, 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 *v1alpha1.WorkerConfig, err error) + WorkerConfigExpansion +} + +// workerConfigs implements WorkerConfigInterface +type workerConfigs struct { + client rest.Interface + ns string +} + +// newWorkerConfigs returns a WorkerConfigs +func newWorkerConfigs(c *NfdV1alpha1Client, namespace string) *workerConfigs { + return &workerConfigs{ + client: c.RESTClient(), + ns: namespace, + } +} + +// Get takes name of the workerConfig, and returns the corresponding workerConfig object, and an error if there is any. +func (c *workerConfigs) Get(ctx context.Context, name string, options v1.GetOptions) (result *v1alpha1.WorkerConfig, err error) { + result = &v1alpha1.WorkerConfig{} + err = c.client.Get(). + Namespace(c.ns). + Resource("workerconfigs"). + Name(name). + VersionedParams(&options, scheme.ParameterCodec). + Do(ctx). + Into(result) + return +} + +// List takes label and field selectors, and returns the list of WorkerConfigs that match those selectors. +func (c *workerConfigs) List(ctx context.Context, opts v1.ListOptions) (result *v1alpha1.WorkerConfigList, err error) { + var timeout time.Duration + if opts.TimeoutSeconds != nil { + timeout = time.Duration(*opts.TimeoutSeconds) * time.Second + } + result = &v1alpha1.WorkerConfigList{} + err = c.client.Get(). + Namespace(c.ns). + Resource("workerconfigs"). + VersionedParams(&opts, scheme.ParameterCodec). + Timeout(timeout). + Do(ctx). + Into(result) + return +} + +// Watch returns a watch.Interface that watches the requested workerConfigs. +func (c *workerConfigs) 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(). + Namespace(c.ns). + Resource("workerconfigs"). + VersionedParams(&opts, scheme.ParameterCodec). + Timeout(timeout). + Watch(ctx) +} + +// Create takes the representation of a workerConfig and creates it. Returns the server's representation of the workerConfig, and an error, if there is any. +func (c *workerConfigs) Create(ctx context.Context, workerConfig *v1alpha1.WorkerConfig, opts v1.CreateOptions) (result *v1alpha1.WorkerConfig, err error) { + result = &v1alpha1.WorkerConfig{} + err = c.client.Post(). + Namespace(c.ns). + Resource("workerconfigs"). + VersionedParams(&opts, scheme.ParameterCodec). + Body(workerConfig). + Do(ctx). + Into(result) + return +} + +// Update takes the representation of a workerConfig and updates it. Returns the server's representation of the workerConfig, and an error, if there is any. +func (c *workerConfigs) Update(ctx context.Context, workerConfig *v1alpha1.WorkerConfig, opts v1.UpdateOptions) (result *v1alpha1.WorkerConfig, err error) { + result = &v1alpha1.WorkerConfig{} + err = c.client.Put(). + Namespace(c.ns). + Resource("workerconfigs"). + Name(workerConfig.Name). + VersionedParams(&opts, scheme.ParameterCodec). + Body(workerConfig). + Do(ctx). + Into(result) + return +} + +// Delete takes name of the workerConfig and deletes it. Returns an error if one occurs. +func (c *workerConfigs) Delete(ctx context.Context, name string, opts v1.DeleteOptions) error { + return c.client.Delete(). + Namespace(c.ns). + Resource("workerconfigs"). + Name(name). + Body(&opts). + Do(ctx). + Error() +} + +// DeleteCollection deletes a collection of objects. +func (c *workerConfigs) 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(). + Namespace(c.ns). + Resource("workerconfigs"). + VersionedParams(&listOpts, scheme.ParameterCodec). + Timeout(timeout). + Body(&opts). + Do(ctx). + Error() +} + +// Patch applies the patch and returns the patched workerConfig. +func (c *workerConfigs) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts v1.PatchOptions, subresources ...string) (result *v1alpha1.WorkerConfig, err error) { + result = &v1alpha1.WorkerConfig{} + err = c.client.Patch(pt). + Namespace(c.ns). + Resource("workerconfigs"). + Name(name). + SubResource(subresources...). + VersionedParams(&opts, scheme.ParameterCodec). + Body(data). + Do(ctx). + Into(result) + return +} diff --git a/api/generated/informers/externalversions/generic.go b/api/generated/informers/externalversions/generic.go index 86577fff52..74b8b45375 100644 --- a/api/generated/informers/externalversions/generic.go +++ b/api/generated/informers/externalversions/generic.go @@ -59,6 +59,8 @@ func (f *sharedInformerFactory) ForResource(resource schema.GroupVersionResource return &genericInformer{resource: resource.GroupResource(), informer: f.Nfd().V1alpha1().NodeFeatureGroups().Informer()}, nil case v1alpha1.SchemeGroupVersion.WithResource("nodefeaturerules"): return &genericInformer{resource: resource.GroupResource(), informer: f.Nfd().V1alpha1().NodeFeatureRules().Informer()}, nil + case v1alpha1.SchemeGroupVersion.WithResource("workerconfigs"): + return &genericInformer{resource: resource.GroupResource(), informer: f.Nfd().V1alpha1().WorkerConfigs().Informer()}, nil } diff --git a/api/generated/informers/externalversions/nfd/v1alpha1/interface.go b/api/generated/informers/externalversions/nfd/v1alpha1/interface.go index ea861a472f..1b0d7107c9 100644 --- a/api/generated/informers/externalversions/nfd/v1alpha1/interface.go +++ b/api/generated/informers/externalversions/nfd/v1alpha1/interface.go @@ -30,6 +30,8 @@ type Interface interface { NodeFeatureGroups() NodeFeatureGroupInformer // NodeFeatureRules returns a NodeFeatureRuleInformer. NodeFeatureRules() NodeFeatureRuleInformer + // WorkerConfigs returns a WorkerConfigInformer. + WorkerConfigs() WorkerConfigInformer } type version struct { @@ -57,3 +59,8 @@ func (v *version) NodeFeatureGroups() NodeFeatureGroupInformer { func (v *version) NodeFeatureRules() NodeFeatureRuleInformer { return &nodeFeatureRuleInformer{factory: v.factory, tweakListOptions: v.tweakListOptions} } + +// WorkerConfigs returns a WorkerConfigInformer. +func (v *version) WorkerConfigs() WorkerConfigInformer { + return &workerConfigInformer{factory: v.factory, namespace: v.namespace, tweakListOptions: v.tweakListOptions} +} diff --git a/api/generated/informers/externalversions/nfd/v1alpha1/workerconfig.go b/api/generated/informers/externalversions/nfd/v1alpha1/workerconfig.go new file mode 100644 index 0000000000..3346110cc7 --- /dev/null +++ b/api/generated/informers/externalversions/nfd/v1alpha1/workerconfig.go @@ -0,0 +1,90 @@ +/* +Copyright 2024 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. +*/ + +// Code generated by informer-gen. DO NOT EDIT. + +package v1alpha1 + +import ( + "context" + time "time" + + 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" + versioned "sigs.k8s.io/node-feature-discovery/api/generated/clientset/versioned" + internalinterfaces "sigs.k8s.io/node-feature-discovery/api/generated/informers/externalversions/internalinterfaces" + v1alpha1 "sigs.k8s.io/node-feature-discovery/api/generated/listers/nfd/v1alpha1" + nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/api/nfd/v1alpha1" +) + +// WorkerConfigInformer provides access to a shared informer and lister for +// WorkerConfigs. +type WorkerConfigInformer interface { + Informer() cache.SharedIndexInformer + Lister() v1alpha1.WorkerConfigLister +} + +type workerConfigInformer struct { + factory internalinterfaces.SharedInformerFactory + tweakListOptions internalinterfaces.TweakListOptionsFunc + namespace string +} + +// NewWorkerConfigInformer constructs a new informer for WorkerConfig 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 NewWorkerConfigInformer(client versioned.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers) cache.SharedIndexInformer { + return NewFilteredWorkerConfigInformer(client, namespace, resyncPeriod, indexers, nil) +} + +// NewFilteredWorkerConfigInformer constructs a new informer for WorkerConfig 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 NewFilteredWorkerConfigInformer(client versioned.Interface, namespace string, 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.NfdV1alpha1().WorkerConfigs(namespace).List(context.TODO(), options) + }, + WatchFunc: func(options v1.ListOptions) (watch.Interface, error) { + if tweakListOptions != nil { + tweakListOptions(&options) + } + return client.NfdV1alpha1().WorkerConfigs(namespace).Watch(context.TODO(), options) + }, + }, + &nfdv1alpha1.WorkerConfig{}, + resyncPeriod, + indexers, + ) +} + +func (f *workerConfigInformer) defaultInformer(client versioned.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer { + return NewFilteredWorkerConfigInformer(client, f.namespace, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions) +} + +func (f *workerConfigInformer) Informer() cache.SharedIndexInformer { + return f.factory.InformerFor(&nfdv1alpha1.WorkerConfig{}, f.defaultInformer) +} + +func (f *workerConfigInformer) Lister() v1alpha1.WorkerConfigLister { + return v1alpha1.NewWorkerConfigLister(f.Informer().GetIndexer()) +} diff --git a/api/generated/listers/nfd/v1alpha1/expansion_generated.go b/api/generated/listers/nfd/v1alpha1/expansion_generated.go index 5890048ee1..7f7156e041 100644 --- a/api/generated/listers/nfd/v1alpha1/expansion_generated.go +++ b/api/generated/listers/nfd/v1alpha1/expansion_generated.go @@ -37,3 +37,11 @@ type NodeFeatureGroupNamespaceListerExpansion interface{} // NodeFeatureRuleListerExpansion allows custom methods to be added to // NodeFeatureRuleLister. type NodeFeatureRuleListerExpansion interface{} + +// WorkerConfigListerExpansion allows custom methods to be added to +// WorkerConfigLister. +type WorkerConfigListerExpansion interface{} + +// WorkerConfigNamespaceListerExpansion allows custom methods to be added to +// WorkerConfigNamespaceLister. +type WorkerConfigNamespaceListerExpansion interface{} diff --git a/api/generated/listers/nfd/v1alpha1/workerconfig.go b/api/generated/listers/nfd/v1alpha1/workerconfig.go new file mode 100644 index 0000000000..a15e12eaab --- /dev/null +++ b/api/generated/listers/nfd/v1alpha1/workerconfig.go @@ -0,0 +1,99 @@ +/* +Copyright 2024 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. +*/ + +// Code generated by lister-gen. DO NOT EDIT. + +package v1alpha1 + +import ( + "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/client-go/tools/cache" + v1alpha1 "sigs.k8s.io/node-feature-discovery/api/nfd/v1alpha1" +) + +// WorkerConfigLister helps list WorkerConfigs. +// All objects returned here must be treated as read-only. +type WorkerConfigLister interface { + // List lists all WorkerConfigs in the indexer. + // Objects returned here must be treated as read-only. + List(selector labels.Selector) (ret []*v1alpha1.WorkerConfig, err error) + // WorkerConfigs returns an object that can list and get WorkerConfigs. + WorkerConfigs(namespace string) WorkerConfigNamespaceLister + WorkerConfigListerExpansion +} + +// workerConfigLister implements the WorkerConfigLister interface. +type workerConfigLister struct { + indexer cache.Indexer +} + +// NewWorkerConfigLister returns a new WorkerConfigLister. +func NewWorkerConfigLister(indexer cache.Indexer) WorkerConfigLister { + return &workerConfigLister{indexer: indexer} +} + +// List lists all WorkerConfigs in the indexer. +func (s *workerConfigLister) List(selector labels.Selector) (ret []*v1alpha1.WorkerConfig, err error) { + err = cache.ListAll(s.indexer, selector, func(m interface{}) { + ret = append(ret, m.(*v1alpha1.WorkerConfig)) + }) + return ret, err +} + +// WorkerConfigs returns an object that can list and get WorkerConfigs. +func (s *workerConfigLister) WorkerConfigs(namespace string) WorkerConfigNamespaceLister { + return workerConfigNamespaceLister{indexer: s.indexer, namespace: namespace} +} + +// WorkerConfigNamespaceLister helps list and get WorkerConfigs. +// All objects returned here must be treated as read-only. +type WorkerConfigNamespaceLister interface { + // List lists all WorkerConfigs in the indexer for a given namespace. + // Objects returned here must be treated as read-only. + List(selector labels.Selector) (ret []*v1alpha1.WorkerConfig, err error) + // Get retrieves the WorkerConfig from the indexer for a given namespace and name. + // Objects returned here must be treated as read-only. + Get(name string) (*v1alpha1.WorkerConfig, error) + WorkerConfigNamespaceListerExpansion +} + +// workerConfigNamespaceLister implements the WorkerConfigNamespaceLister +// interface. +type workerConfigNamespaceLister struct { + indexer cache.Indexer + namespace string +} + +// List lists all WorkerConfigs in the indexer for a given namespace. +func (s workerConfigNamespaceLister) List(selector labels.Selector) (ret []*v1alpha1.WorkerConfig, err error) { + err = cache.ListAllByNamespace(s.indexer, s.namespace, selector, func(m interface{}) { + ret = append(ret, m.(*v1alpha1.WorkerConfig)) + }) + return ret, err +} + +// Get retrieves the WorkerConfig from the indexer for a given namespace and name. +func (s workerConfigNamespaceLister) Get(name string) (*v1alpha1.WorkerConfig, error) { + obj, exists, err := s.indexer.GetByKey(s.namespace + "/" + name) + if err != nil { + return nil, err + } + if !exists { + return nil, errors.NewNotFound(v1alpha1.Resource("workerconfig"), name) + } + return obj.(*v1alpha1.WorkerConfig), nil +} diff --git a/api/nfd/v1alpha1/types.go b/api/nfd/v1alpha1/types.go index c89efd8185..ab102c66f6 100644 --- a/api/nfd/v1alpha1/types.go +++ b/api/nfd/v1alpha1/types.go @@ -21,6 +21,34 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) +// WorkerConfig resource holds the configuration for NFD worker +// +kubebuilder:object:root=true +// +genclient +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object +type WorkerConfig struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + // Specification of the WorkerConfig, containing configuration data + Spec WorkerConfigSpec `json:"spec"` +} + +// WorkerConfigSpec holds configuration data +type WorkerConfigSpec struct { + Data map[string]string `json:"data"` +} + +// WorkerConfigList contains a list of WorkerConfig objects. +// +kubebuilder:object:root=true +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object +type WorkerConfigList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata"` + + // List of WorkerConfigs + Items []WorkerConfig `json:"items"` +} + // NodeFeatureList contains a list of NodeFeature objects. // +kubebuilder:object:root=true // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object diff --git a/api/nfd/v1alpha1/zz_generated.deepcopy.go b/api/nfd/v1alpha1/zz_generated.deepcopy.go index 491d9866c4..190059300b 100644 --- a/api/nfd/v1alpha1/zz_generated.deepcopy.go +++ b/api/nfd/v1alpha1/zz_generated.deepcopy.go @@ -709,3 +709,86 @@ func (in *Rule) DeepCopy() *Rule { in.DeepCopyInto(out) return out } + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *WorkerConfig) DeepCopyInto(out *WorkerConfig) { + *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 WorkerConfig. +func (in *WorkerConfig) DeepCopy() *WorkerConfig { + if in == nil { + return nil + } + out := new(WorkerConfig) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *WorkerConfig) 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 *WorkerConfigList) DeepCopyInto(out *WorkerConfigList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]WorkerConfig, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new WorkerConfigList. +func (in *WorkerConfigList) DeepCopy() *WorkerConfigList { + if in == nil { + return nil + } + out := new(WorkerConfigList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *WorkerConfigList) 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 *WorkerConfigSpec) DeepCopyInto(out *WorkerConfigSpec) { + *out = *in + if in.Data != nil { + in, out := &in.Data, &out.Data + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new WorkerConfigSpec. +func (in *WorkerConfigSpec) DeepCopy() *WorkerConfigSpec { + if in == nil { + return nil + } + out := new(WorkerConfigSpec) + in.DeepCopyInto(out) + return out +} diff --git a/deployment/base/nfd-crds/nfd-api-crds.yaml b/deployment/base/nfd-crds/nfd-api-crds.yaml index 0a73c5dcae..1cb138a7a3 100644 --- a/deployment/base/nfd-crds/nfd-api-crds.yaml +++ b/deployment/base/nfd-crds/nfd-api-crds.yaml @@ -708,3 +708,57 @@ spec: type: object served: true storage: true +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.14.0 + name: workerconfigs.nfd.k8s-sigs.io +spec: + group: nfd.k8s-sigs.io + names: + kind: WorkerConfig + listKind: WorkerConfigList + plural: workerconfigs + singular: workerconfig + scope: Namespaced + versions: + - name: v1alpha1 + schema: + openAPIV3Schema: + description: WorkerConfig resource holds the configuration for NFD worker + 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: + description: Specification of the WorkerConfig, containing configuration + data + properties: + data: + additionalProperties: + type: string + type: object + required: + - data + type: object + required: + - spec + type: object + served: true + storage: true diff --git a/deployment/helm/node-feature-discovery/crds/nfd-api-crds.yaml b/deployment/helm/node-feature-discovery/crds/nfd-api-crds.yaml index 0a73c5dcae..1cb138a7a3 100644 --- a/deployment/helm/node-feature-discovery/crds/nfd-api-crds.yaml +++ b/deployment/helm/node-feature-discovery/crds/nfd-api-crds.yaml @@ -708,3 +708,57 @@ spec: type: object served: true storage: true +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.14.0 + name: workerconfigs.nfd.k8s-sigs.io +spec: + group: nfd.k8s-sigs.io + names: + kind: WorkerConfig + listKind: WorkerConfigList + plural: workerconfigs + singular: workerconfig + scope: Namespaced + versions: + - name: v1alpha1 + schema: + openAPIV3Schema: + description: WorkerConfig resource holds the configuration for NFD worker + 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: + description: Specification of the WorkerConfig, containing configuration + data + properties: + data: + additionalProperties: + type: string + type: object + required: + - data + type: object + required: + - spec + type: object + served: true + storage: true diff --git a/pkg/nfd-worker/nfd-api-controller.go b/pkg/nfd-worker/nfd-api-controller.go new file mode 100644 index 0000000000..24966785cf --- /dev/null +++ b/pkg/nfd-worker/nfd-api-controller.go @@ -0,0 +1,120 @@ +/* +Copyright 2024 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 nfdworker + +import ( + "fmt" + "time" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + utilruntime "k8s.io/apimachinery/pkg/util/runtime" + restclient "k8s.io/client-go/rest" + "k8s.io/client-go/tools/cache" + "k8s.io/klog/v2" + + nfdclientset "sigs.k8s.io/node-feature-discovery/api/generated/clientset/versioned" + nfdscheme "sigs.k8s.io/node-feature-discovery/api/generated/clientset/versioned/scheme" + nfdinformers "sigs.k8s.io/node-feature-discovery/api/generated/informers/externalversions" + nfdinformersv1alpha1 "sigs.k8s.io/node-feature-discovery/api/generated/informers/externalversions/nfd/v1alpha1" + nfdlisters "sigs.k8s.io/node-feature-discovery/api/generated/listers/nfd/v1alpha1" + nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/api/nfd/v1alpha1" + "sigs.k8s.io/node-feature-discovery/pkg/utils" +) + +type nfdController struct { + configLister nfdlisters.WorkerConfigLister + stopChan chan struct{} + updateConfigChan chan map[string]string +} + +type nfdApiControllerOptions struct { + ResyncPeriod time.Duration +} + +func init() { + utilruntime.Must(nfdv1alpha1.AddToScheme(nfdscheme.Scheme)) +} + +func newNfdController(config *restclient.Config, nfdApiControllerOptions nfdApiControllerOptions) (*nfdController, error) { + c := &nfdController{ + stopChan: make(chan struct{}), + updateConfigChan: make(chan map[string]string), + } + + nfdClient := nfdclientset.NewForConfigOrDie(config) + + klog.V(2).InfoS("initializing new NFD API controller", "options", utils.DelayedDumper(nfdApiControllerOptions)) + + informerFactory := nfdinformers.NewSharedInformerFactory(nfdClient, nfdApiControllerOptions.ResyncPeriod) + + // Add informer for NodeFeature objects + tweakListOpts := func(opts *metav1.ListOptions) { + // Tweak list opts on initial sync to avoid timeouts on the apiserver. + // NodeFeature objects are huge and the Kubernetes apiserver + // (v1.30) experiences http handler timeouts when the resource + // version is set to some non-empty value (TODO: find out why). + if opts.ResourceVersion == "0" { + opts.ResourceVersion = "" + } + } + configInformer := nfdinformersv1alpha1.New(informerFactory, "", tweakListOpts).WorkerConfigs() + if _, err := configInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ + AddFunc: func(obj interface{}) { + cfg := obj.(*nfdv1alpha1.WorkerConfig) + klog.V(2).InfoS("WorkerConfig added", "workerconfig", klog.KObj(cfg)) + c.updateConfiguration(cfg) + }, + UpdateFunc: func(oldObj, newObj interface{}) { + cfg := newObj.(*nfdv1alpha1.WorkerConfig) + klog.V(2).InfoS("WorkerConfig updated", "workerconfig", klog.KObj(cfg)) + c.updateConfiguration(cfg) + }, + DeleteFunc: func(obj interface{}) { + cfg := obj.(*nfdv1alpha1.WorkerConfig) + klog.V(2).InfoS("WorkerConfig deleted", "workerconfig", klog.KObj(cfg)) + c.updateConfiguration(cfg) + }, + }); err != nil { + return nil, err + } + c.configLister = configInformer.Lister() + + // Start informers + informerFactory.Start(c.stopChan) + now := time.Now() + ret := informerFactory.WaitForCacheSync(c.stopChan) + for res, ok := range ret { + if !ok { + return nil, fmt.Errorf("informer cache failed to sync resource %s", res) + } + } + + klog.InfoS("informer caches synced", "duration", time.Since(now)) + + return c, nil +} + +func (c *nfdController) stop() { + close(c.stopChan) +} + +func (c *nfdController) updateConfiguration(cfg *nfdv1alpha1.WorkerConfig) { + select { + case c.updateConfigChan <- cfg.Spec.Data: + case <-c.stopChan: + } +} diff --git a/pkg/nfd-worker/nfd-worker.go b/pkg/nfd-worker/nfd-worker.go index 4e4c3d09ed..9e6348a8da 100644 --- a/pkg/nfd-worker/nfd-worker.go +++ b/pkg/nfd-worker/nfd-worker.go @@ -122,6 +122,8 @@ type ConfigOverrideArgs struct { } type nfdWorker struct { + *nfdController + args Args certWatch *utils.FsWatcher clientConn *grpc.ClientConn @@ -309,6 +311,8 @@ func (w *nfdWorker) Run() error { return err } + w.startNfdApiController() + defer w.grpcDisconnect() // Create ticker for feature discovery and run feature discovery once before the loop. @@ -387,6 +391,9 @@ func (w *nfdWorker) Run() error { return err } + case <-w.nfdController.updateConfigChan: + klog.InfoS("reloading configuration") + case <-w.certWatch.Events: klog.InfoS("TLS certificate update, renewing connection to nfd-master") w.grpcDisconnect() @@ -819,6 +826,21 @@ func (m *nfdWorker) updateNodeFeatureObject(labels Labels) error { return nil } +func (m *nfdWorker) startNfdApiController() error { + kubeconfig, err := utils.GetKubeconfig(m.args.Kubeconfig) + if err != nil { + return err + } + klog.InfoS("starting the nfd api controller") + m.nfdController, err = newNfdController(kubeconfig, nfdApiControllerOptions{ + //ResyncPeriod: m.config.ResyncPeriod.Duration, + }) + if err != nil { + return fmt.Errorf("failed to initialize CRD controller: %w", err) + } + return nil +} + // getNfdClient returns the clientset for using the nfd CRD api func (m *nfdWorker) getNfdClient() (nfdclient.Interface, error) { if m.nfdClient != nil {