From 19a57789ad22e0916940a6362fc682d253a73c85 Mon Sep 17 00:00:00 2001 From: Dipto Chakrabarty <45638240+DiptoChakrabarty@users.noreply.github.com> Date: Thu, 3 Mar 2022 06:42:46 +0530 Subject: [PATCH] Additional Lint Fixes in Codebase (#779) * fix comments and conditonals to fix lint issues * more linter fixes and spelling fixes * fix linter issues based on feedback --- pkg/apihelper/k8shelpers.go | 7 ++++++- pkg/apis/nfd/v1alpha1/rule_test.go | 4 ++-- pkg/nfd-client/base.go | 4 ++-- pkg/nfd-client/topology-updater/nfd-topology-updater.go | 4 ++-- pkg/nfd-master/nfd-master.go | 2 +- pkg/resourcemonitor/podresourcesscanner.go | 1 + pkg/resourcemonitor/types.go | 2 +- source/cpu/cpu.go | 1 + source/cpu/cstate_amd64.go | 4 ++-- source/cpu/power_amd64.go | 2 +- source/custom/rules/kconfig_rule.go | 1 + source/custom/rules/nodename_rule.go | 2 ++ source/fake/fake.go | 7 ++++++- source/local/local.go | 1 + source/memory/memory.go | 3 +++ source/pci/pci.go | 2 ++ 16 files changed, 34 insertions(+), 13 deletions(-) diff --git a/pkg/apihelper/k8shelpers.go b/pkg/apihelper/k8shelpers.go index 2a7aaf4be..823826330 100644 --- a/pkg/apihelper/k8shelpers.go +++ b/pkg/apihelper/k8shelpers.go @@ -29,11 +29,12 @@ import ( "k8s.io/client-go/tools/clientcmd" ) -// Implements APIHelpers +// K8sHelpers implements APIHelpers type K8sHelpers struct { Kubeconfig *restclient.Config } +// GetClient creates and returns a new clientset from given config func (h K8sHelpers) GetClient() (*k8sclient.Clientset, error) { clientset, err := k8sclient.NewForConfig(h.Kubeconfig) if err != nil { @@ -50,6 +51,7 @@ func (h K8sHelpers) GetTopologyClient() (*topologyclientset.Clientset, error) { return topologyClient, nil } +// GetNode retrieves one node object. func (h K8sHelpers) GetNode(cli *k8sclient.Clientset, nodeName string) (*api.Node, error) { // Get the node object using node name node, err := cli.CoreV1().Nodes().Get(context.TODO(), nodeName, meta_v1.GetOptions{}) @@ -60,10 +62,12 @@ func (h K8sHelpers) GetNode(cli *k8sclient.Clientset, nodeName string) (*api.Nod return node, nil } +// GetNodes retrieves all the node objects. func (h K8sHelpers) GetNodes(cli *k8sclient.Clientset) (*api.NodeList, error) { return cli.CoreV1().Nodes().List(context.TODO(), meta_v1.ListOptions{}) } +// UpdateNode sends updated node object to the apiserver func (h K8sHelpers) UpdateNode(c *k8sclient.Clientset, n *api.Node) error { // Send the updated node to the apiserver. _, err := c.CoreV1().Nodes().Update(context.TODO(), n, meta_v1.UpdateOptions{}) @@ -107,6 +111,7 @@ func (h K8sHelpers) GetPod(cli *k8sclient.Clientset, namespace string, podName s return pod, nil } +// GetKubeconfig returns the kubeconfig for the cluster func GetKubeconfig(path string) (*restclient.Config, error) { if path == "" { return restclient.InClusterConfig() diff --git a/pkg/apis/nfd/v1alpha1/rule_test.go b/pkg/apis/nfd/v1alpha1/rule_test.go index 7a87c5da1..c4be8050c 100644 --- a/pkg/apis/nfd/v1alpha1/rule_test.go +++ b/pkg/apis/nfd/v1alpha1/rule_test.go @@ -45,7 +45,7 @@ func TestRule(t *testing.T) { assert.Equal(t, r1.Labels, m.Labels, "empty matcher should have matched empty features") _, err = r2.Execute(f) - assert.Error(t, err, "matching agains a missing domain should have returned an error") + assert.Error(t, err, "matching against a missing domain should have returned an error") // Test empty domain d := feature.NewDomainFeatures() @@ -57,7 +57,7 @@ func TestRule(t *testing.T) { assert.Empty(t, r1.Vars, "vars should be empty") _, err = r2.Execute(f) - assert.Error(t, err, "matching agains a missing feature type should have returned an error") + assert.Error(t, err, "matching against a missing feature type should have returned an error") // Test empty feature sets d.Keys["kf-1"] = feature.NewKeyFeatures() diff --git a/pkg/nfd-client/base.go b/pkg/nfd-client/base.go index 704365ede..665bdd79c 100644 --- a/pkg/nfd-client/base.go +++ b/pkg/nfd-client/base.go @@ -64,7 +64,7 @@ func init() { // NodeName returns the name of the k8s node we're running on. func NodeName() string { return nodeName } -// Create new NfdWorker instance. +// NewNfdBaseClient creates a new NfdBaseClient instance. func NewNfdBaseClient(args *Args) (NfdBaseClient, error) { nfd := NfdBaseClient{args: *args} @@ -134,7 +134,7 @@ func (w *NfdBaseClient) Connect() error { return nil } -// disconnect closes the connection to NFD master +// Disconnect closes the connection to NFD master func (w *NfdBaseClient) Disconnect() { if w.clientConn != nil { klog.Infof("closing connection to nfd-master ...") diff --git a/pkg/nfd-client/topology-updater/nfd-topology-updater.go b/pkg/nfd-client/topology-updater/nfd-topology-updater.go index ad517cbac..2e43e5117 100644 --- a/pkg/nfd-client/topology-updater/nfd-topology-updater.go +++ b/pkg/nfd-client/topology-updater/nfd-topology-updater.go @@ -34,7 +34,7 @@ import ( "sigs.k8s.io/node-feature-discovery/pkg/version" ) -// Command line arguments +// Args are the command line arguments type Args struct { nfdclient.Args NoPublish bool @@ -61,7 +61,7 @@ type nfdTopologyUpdater struct { stop chan struct{} // channel for signaling stop } -// Create new NewTopologyUpdater instance. +// NewTopologyUpdater creates a new NfdTopologyUpdater instance. func NewTopologyUpdater(args Args, resourcemonitorArgs resourcemonitor.Args, policy string) (NfdTopologyUpdater, error) { base, err := nfdclient.NewNfdBaseClient(&args.Args) if err != nil { diff --git a/pkg/nfd-master/nfd-master.go b/pkg/nfd-master/nfd-master.go index 937e56afa..4f3b51ada 100644 --- a/pkg/nfd-master/nfd-master.go +++ b/pkg/nfd-master/nfd-master.go @@ -120,7 +120,7 @@ type nfdMaster struct { kubeconfig *restclient.Config } -// Create new NfdMaster server instance. +// NewNfdMaster creates a new NfdMaster server instance. func NewNfdMaster(args *Args) (NfdMaster, error) { nfd := &nfdMaster{args: *args, nodeName: os.Getenv("NODE_NAME"), diff --git a/pkg/resourcemonitor/podresourcesscanner.go b/pkg/resourcemonitor/podresourcesscanner.go index 2cef7dff3..4c8b4acc1 100644 --- a/pkg/resourcemonitor/podresourcesscanner.go +++ b/pkg/resourcemonitor/podresourcesscanner.go @@ -35,6 +35,7 @@ type PodResourcesScanner struct { apihelper apihelper.APIHelpers } +// NewPodResourcesScanner creates a new ResourcesScanner instance func NewPodResourcesScanner(namespace string, podResourceClient podresourcesapi.PodResourcesListerClient, kubeApihelper apihelper.APIHelpers) (ResourcesScanner, error) { resourcemonitorInstance := &PodResourcesScanner{ namespace: namespace, diff --git a/pkg/resourcemonitor/types.go b/pkg/resourcemonitor/types.go index e42209d44..b288ef8c7 100644 --- a/pkg/resourcemonitor/types.go +++ b/pkg/resourcemonitor/types.go @@ -57,7 +57,7 @@ type ResourcesScanner interface { Scan() ([]PodResources, error) } -// ResourceAggregator aggregates resource information based on the received data from underlying hardware and podresource API +// ResourcesAggregator aggregates resource information based on the received data from underlying hardware and podresource API type ResourcesAggregator interface { Aggregate(podResData []PodResources) topologyv1alpha1.ZoneList } diff --git a/source/cpu/cpu.go b/source/cpu/cpu.go index e5253b168..5c0fd8b85 100644 --- a/source/cpu/cpu.go +++ b/source/cpu/cpu.go @@ -46,6 +46,7 @@ type cpuidConfig struct { AttributeWhitelist []string `json:"attributeWhitelist,omitempty"` } +// Config holds configuration for the cpu source. type Config struct { Cpuid cpuidConfig `json:"cpuid,omitempty"` } diff --git a/source/cpu/cstate_amd64.go b/source/cpu/cstate_amd64.go index edd00d3b2..f894a07ee 100644 --- a/source/cpu/cstate_amd64.go +++ b/source/cpu/cstate_amd64.go @@ -63,9 +63,9 @@ func detectCstate() (map[string]string, error) { cstates, err := strconv.Atoi(strings.TrimSpace(string(data))) if err != nil { return cstate, fmt.Errorf("non-integer value of cstates: %w", err) - } else { - cstate["enabled"] = strconv.FormatBool(cstates > 0) } + cstate["enabled"] = strconv.FormatBool(cstates > 0) + return cstate, nil } diff --git a/source/cpu/power_amd64.go b/source/cpu/power_amd64.go index 16bd5a82f..23f050506 100644 --- a/source/cpu/power_amd64.go +++ b/source/cpu/power_amd64.go @@ -30,7 +30,7 @@ import ( ) const ( - // CPUID EAX input values + // LEAF_PROCESSOR_FREQUENCY_INFORMATION is the cpuid leaf to get processor frequency information LEAF_PROCESSOR_FREQUENCY_INFORMATION = 0x16 ) diff --git a/source/custom/rules/kconfig_rule.go b/source/custom/rules/kconfig_rule.go index a586f6193..2461c1907 100644 --- a/source/custom/rules/kconfig_rule.go +++ b/source/custom/rules/kconfig_rule.go @@ -28,6 +28,7 @@ type KconfigRule struct { nfdv1alpha1.MatchExpressionSet } +// Match compares the values of Kernel config provided and legacy config func (r *KconfigRule) Match() (bool, error) { options := kernel.GetLegacyKconfig() if options == nil { diff --git a/source/custom/rules/nodename_rule.go b/source/custom/rules/nodename_rule.go index 69eab4dad..639b4dac5 100644 --- a/source/custom/rules/nodename_rule.go +++ b/source/custom/rules/nodename_rule.go @@ -30,6 +30,7 @@ type NodenameRule struct { nfdv1alpha1.MatchExpression } +// Match checks if node name matches the rule. func (r *NodenameRule) Match() (bool, error) { nodeName, ok := source.GetFeatureSource("system").GetFeatures().Values[system.NameFeature].Elements["nodename"] if !ok || nodeName == "" { @@ -38,6 +39,7 @@ func (r *NodenameRule) Match() (bool, error) { return r.MatchExpression.Match(true, nodeName) } +// UnmarshalJSON unmarshals and validates the data provided func (r *NodenameRule) UnmarshalJSON(data []byte) error { if err := json.Unmarshal(data, &r.MatchExpression); err != nil { return err diff --git a/source/fake/fake.go b/source/fake/fake.go index 645abe6d0..cddc37551 100644 --- a/source/fake/fake.go +++ b/source/fake/fake.go @@ -27,11 +27,16 @@ import ( // Name of this feature source const Name = "fake" +// FlagFeature of this feature source const FlagFeature = "flag" + +// AttributeFeature of this feature source const AttributeFeature = "attribute" + +// InstanceFeature of this feature source const InstanceFeature = "instance" -// Configuration file options +// Config contains the configuration parameters of this source. type Config struct { Labels map[string]string `json:"labels"` FlagFeatures []string `json:"flagFeatures"` diff --git a/source/local/local.go b/source/local/local.go index 1829bdd55..9636d8b2b 100644 --- a/source/local/local.go +++ b/source/local/local.go @@ -35,6 +35,7 @@ import ( // Name of this feature source const Name = "local" +// LabelFeature of this feature source const LabelFeature = "label" // Config diff --git a/source/memory/memory.go b/source/memory/memory.go index 6cb817d14..4f2c45ac7 100644 --- a/source/memory/memory.go +++ b/source/memory/memory.go @@ -34,7 +34,10 @@ import ( // Name of this feature source const Name = "memory" +// NvFeature is the name of the feature set that holds all discovered NVDIMM devices. const NvFeature = "nv" + +// NumaFeature is the name of the feature set that holds all NUMA related features. const NumaFeature = "numa" // memorySource implements the FeatureSource and LabelSource interfaces. diff --git a/source/pci/pci.go b/source/pci/pci.go index 3980a63cd..b7a0f58c4 100644 --- a/source/pci/pci.go +++ b/source/pci/pci.go @@ -30,8 +30,10 @@ import ( // Name of this feature source const Name = "pci" +// DeviceFeature is the name of the feature set that holds all discovered PCI devices. const DeviceFeature = "device" +// Config holds the configuration parameters of this source. type Config struct { DeviceClassWhitelist []string `json:"deviceClassWhitelist,omitempty"` DeviceLabelFields []string `json:"deviceLabelFields,omitempty"`