1
0
Fork 0
mirror of https://github.com/kubernetes-sigs/node-feature-discovery.git synced 2024-12-14 11:57:51 +00:00

Drop deprecated io/ioutil package

Makes golanci-lint happy.
This commit is contained in:
Markus Lehtonen 2022-09-08 13:23:49 +03:00
parent 98228d2069
commit 12e859d50c
21 changed files with 52 additions and 63 deletions

View file

@ -17,7 +17,7 @@ limitations under the License.
package kubeconf
import (
"io/ioutil"
"os"
"github.com/ghodss/yaml"
@ -26,7 +26,7 @@ import (
// GetKubeletConfigFromLocalFile returns KubeletConfiguration loaded from the node local config
func GetKubeletConfigFromLocalFile(kubeletConfigPath string) (*kubeletconfigv1beta1.KubeletConfiguration, error) {
kubeletBytes, err := ioutil.ReadFile(kubeletConfigPath)
kubeletBytes, err := os.ReadFile(kubeletConfigPath)
if err != nil {
return nil, err
}

View file

@ -20,7 +20,6 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"os"
"time"
@ -105,7 +104,7 @@ func (w *NfdBaseClient) Connect() error {
return fmt.Errorf("failed to load client certificate: %v", err)
}
// Load CA cert for server cert verification
caCert, err := ioutil.ReadFile(w.args.CaFile)
caCert, err := os.ReadFile(w.args.CaFile)
if err != nil {
return fmt.Errorf("failed to read root certificate file: %v", err)
}

View file

@ -17,7 +17,6 @@ limitations under the License.
package worker
import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
@ -129,7 +128,7 @@ func TestConfigParse(t *testing.T) {
})
})
// Create a temporary config file
f, err := ioutil.TempFile("", "nfd-test-")
f, err := os.CreateTemp("", "nfd-test-")
defer os.Remove(f.Name())
So(err, ShouldBeNil)
_, err = f.WriteString(`
@ -197,7 +196,7 @@ sources:
func TestDynamicConfig(t *testing.T) {
Convey("When running nfd-worker", t, func() {
tmpDir, err := ioutil.TempDir("", "*.nfd-test")
tmpDir, err := os.MkdirTemp("", "*.nfd-test")
So(err, ShouldBeNil)
defer os.RemoveAll(tmpDir)

View file

@ -19,7 +19,6 @@ package worker
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
@ -401,7 +400,7 @@ func (w *nfdWorker) configure(filepath string, overrides string) error {
// Try to read and parse config file
if filepath != "" {
data, err := ioutil.ReadFile(filepath)
data, err := os.ReadFile(filepath)
if err != nil {
if os.IsNotExist(err) {
klog.Infof("config file %q not found, using defaults", filepath)

View file

@ -18,7 +18,7 @@ package utils
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
@ -44,7 +44,7 @@ type MemoryResourceInfo map[v1.ResourceName]int64
// GetNumaMemoryResources returns total amount of memory and hugepages under NUMA nodes
func GetNumaMemoryResources() (NumaMemoryResources, error) {
nodes, err := ioutil.ReadDir(sysBusNodeBasepath)
nodes, err := os.ReadDir(sysBusNodeBasepath)
if err != nil {
return nil, err
}
@ -82,7 +82,7 @@ func GetNumaMemoryResources() (NumaMemoryResources, error) {
}
func getHugepagesBytes(path string) (MemoryResourceInfo, error) {
entries, err := ioutil.ReadDir(path)
entries, err := os.ReadDir(path)
if err != nil {
return nil, err
}
@ -101,7 +101,7 @@ func getHugepagesBytes(path string) (MemoryResourceInfo, error) {
return nil, err
}
data, err := ioutil.ReadFile(filepath.Join(path, entry.Name(), "nr_hugepages"))
data, err := os.ReadFile(filepath.Join(path, entry.Name(), "nr_hugepages"))
if err != nil {
return nil, err
}
@ -120,7 +120,7 @@ func getHugepagesBytes(path string) (MemoryResourceInfo, error) {
}
func readTotalMemoryFromMeminfo(path string) (int64, error) {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return -1, err
}

View file

@ -20,7 +20,7 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"os"
"sync"
)
@ -50,7 +50,7 @@ func (c *TlsConfig) UpdateConfig(certFile, keyFile, caFile string) error {
return fmt.Errorf("failed to load server certificate: %v", err)
}
// Load CA cert for client cert verification
caCert, err := ioutil.ReadFile(caFile)
caCert, err := os.ReadFile(caFile)
if err != nil {
return fmt.Errorf("failed to read root certificate file: %v", err)
}

View file

@ -17,7 +17,7 @@ limitations under the License.
package cpu
import (
"io/ioutil"
"os"
"strconv"
"k8s.io/klog/v2"
@ -282,14 +282,14 @@ func discoverTopology() map[string]string {
// Check if any (online) CPUs have thread siblings
func haveThreadSiblings() (bool, error) {
files, err := ioutil.ReadDir(source.SysfsDir.Path("bus/cpu/devices"))
files, err := os.ReadDir(source.SysfsDir.Path("bus/cpu/devices"))
if err != nil {
return false, err
}
for _, file := range files {
// Try to read siblings from topology
siblings, err := ioutil.ReadFile(source.SysfsDir.Path("bus/cpu/devices", file.Name(), "topology/thread_siblings_list"))
siblings, err := os.ReadFile(source.SysfsDir.Path("bus/cpu/devices", file.Name(), "topology/thread_siblings_list"))
if err != nil {
return false, err
}

View file

@ -18,7 +18,6 @@ package cpu
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
@ -45,7 +44,7 @@ func detectCstate() (map[string]string, error) {
}
// When the intel_idle driver is in use (default), check setting of max_cstates
driver, err := ioutil.ReadFile(filepath.Join(cpuidleDir, "current_driver"))
driver, err := os.ReadFile(filepath.Join(cpuidleDir, "current_driver"))
if err != nil {
return cstate, fmt.Errorf("cannot get driver for cpuidle: %w", err)
}
@ -56,7 +55,7 @@ func detectCstate() (map[string]string, error) {
return cstate, nil
}
data, err := ioutil.ReadFile(source.SysfsDir.Path("module/intel_idle/parameters/max_cstate"))
data, err := os.ReadFile(source.SysfsDir.Path("module/intel_idle/parameters/max_cstate"))
if err != nil {
return cstate, fmt.Errorf("cannot determine cstate from max_cstates: %w", err)
}

View file

@ -18,7 +18,6 @@ package cpu
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
@ -52,7 +51,7 @@ func discoverSSTBF() (bool, error) {
nominalBaseFrequency := int(freqInfo.EAX)
// Loop over all CPUs in the system
files, err := ioutil.ReadDir(source.SysfsDir.Path("bus/cpu/devices"))
files, err := os.ReadDir(source.SysfsDir.Path("bus/cpu/devices"))
if err != nil {
return false, err
@ -60,7 +59,7 @@ func discoverSSTBF() (bool, error) {
for _, file := range files {
// Try to read effective base frequency of each cpu in the system
filePath := source.SysfsDir.Path("bus/cpu/devices", file.Name(), "cpufreq/base_frequency")
data, err := ioutil.ReadFile(filePath)
data, err := os.ReadFile(filePath)
if os.IsNotExist(err) {
// Ignore missing file and continue to check other CPUs
continue

View file

@ -18,7 +18,6 @@ package cpu
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -42,7 +41,7 @@ func detectPstate() (map[string]string, error) {
}
// Get global pstate status
data, err := ioutil.ReadFile(filepath.Join(pstateDir, "status"))
data, err := os.ReadFile(filepath.Join(pstateDir, "status"))
if err != nil {
return nil, fmt.Errorf("could not read pstate status: %w", err)
}
@ -55,7 +54,7 @@ func detectPstate() (map[string]string, error) {
features := map[string]string{"status": status}
// Check turbo boost
bytes, err := ioutil.ReadFile(filepath.Join(pstateDir, "no_turbo"))
bytes, err := os.ReadFile(filepath.Join(pstateDir, "no_turbo"))
if err != nil {
klog.Errorf("can't detect whether turbo boost is enabled: %s", err.Error())
} else {
@ -72,7 +71,7 @@ func detectPstate() (map[string]string, error) {
// Determine scaling governor that is being used
cpufreqDir := filepath.Join(sysfsBase, "cpufreq")
policies, err := ioutil.ReadDir(cpufreqDir)
policies, err := os.ReadDir(cpufreqDir)
if err != nil {
klog.Errorf("failed to read cpufreq directory: %s", err.Error())
return features, nil
@ -81,7 +80,7 @@ func detectPstate() (map[string]string, error) {
scaling := ""
for _, policy := range policies {
// Ensure at least one cpu is using this policy
cpus, err := ioutil.ReadFile(filepath.Join(cpufreqDir, policy.Name(), "affected_cpus"))
cpus, err := os.ReadFile(filepath.Join(cpufreqDir, policy.Name(), "affected_cpus"))
if err != nil {
klog.Errorf("could not read cpufreq policy %s affected_cpus", policy.Name())
continue
@ -91,7 +90,7 @@ func detectPstate() (map[string]string, error) {
continue
}
data, err := ioutil.ReadFile(filepath.Join(cpufreqDir, policy.Name(), "scaling_governor"))
data, err := os.ReadFile(filepath.Join(cpufreqDir, policy.Name(), "scaling_governor"))
if err != nil {
klog.Errorf("could not read cpufreq policy %s scaling_governor", policy.Name())
continue

View file

@ -17,7 +17,6 @@ limitations under the License.
package custom
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -41,7 +40,7 @@ func readDir(dirName string, recursive bool) []CustomRule {
features := make([]CustomRule, 0)
klog.V(1).Infof("getting files in %s", dirName)
files, err := ioutil.ReadDir(dirName)
files, err := os.ReadDir(dirName)
if err != nil {
if os.IsNotExist(err) {
klog.V(1).Infof("custom config directory %q does not exist", dirName)
@ -69,7 +68,7 @@ func readDir(dirName string, recursive bool) []CustomRule {
}
klog.V(2).Infof("processing file %q", fileName)
bytes, err := ioutil.ReadFile(fileName)
bytes, err := os.ReadFile(fileName)
if err != nil {
klog.Errorf("could not read custom config file %q, %v", fileName, err)
continue

View file

@ -20,7 +20,7 @@ import (
"bytes"
"compress/gzip"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"strings"
@ -44,7 +44,7 @@ func readKconfigGzip(filename string) ([]byte, error) {
}
defer r.Close()
return ioutil.ReadAll(r)
return io.ReadAll(r)
}
// parseKconfig reads Linux kernel configuration and returns all set options
@ -88,7 +88,7 @@ func parseKconfig(configPath string) (realKconfig, legacyKconfig map[string]stri
break
}
} else {
if raw, err = ioutil.ReadFile(path); err == nil {
if raw, err = os.ReadFile(path); err == nil {
break
}
}

View file

@ -18,14 +18,14 @@ package kernel
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
const kmodProcfsPath = "/proc/modules"
func getLoadedModules() ([]string, error) {
out, err := ioutil.ReadFile(kmodProcfsPath)
out, err := os.ReadFile(kmodProcfsPath)
if err != nil {
return nil, fmt.Errorf("failed to read file %s: %s", kmodProcfsPath, err.Error())
}

View file

@ -18,7 +18,6 @@ package kernel
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
@ -40,7 +39,7 @@ func SelinuxEnabled() (bool, error) {
return false, nil
}
status, err := ioutil.ReadFile(filepath.Join(selinuxBase, "enforce"))
status, err := os.ReadFile(filepath.Join(selinuxBase, "enforce"))
if err != nil {
return false, fmt.Errorf("failed to detect the status of selinux: %w", err)
}

View file

@ -17,7 +17,7 @@ limitations under the License.
package kernel
import (
"io/ioutil"
"os"
"regexp"
"strings"
)
@ -53,7 +53,7 @@ func parseVersion() (map[string]string, error) {
}
func getVersion() (string, error) {
unameRaw, err := ioutil.ReadFile("/proc/sys/kernel/osrelease")
unameRaw, err := os.ReadFile("/proc/sys/kernel/osrelease")
if err != nil {
return "", err
}

View file

@ -19,7 +19,6 @@ package local
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -135,7 +134,7 @@ func parseFeatures(lines [][]byte) map[string]string {
func getFeaturesFromHooks() (map[string]string, error) {
features := make(map[string]string)
files, err := ioutil.ReadDir(hookDir)
files, err := os.ReadDir(hookDir)
if err != nil {
if os.IsNotExist(err) {
klog.Infof("hook directory %v does not exist", hookDir)
@ -212,7 +211,7 @@ func runHook(file string) ([][]byte, error) {
func getFeaturesFromFiles() (map[string]string, error) {
features := make(map[string]string)
files, err := ioutil.ReadDir(featureFilesDir)
files, err := os.ReadDir(featureFilesDir)
if err != nil {
if os.IsNotExist(err) {
klog.Infof("features directory %v does not exist", featureFilesDir)
@ -256,7 +255,7 @@ func getFileContent(fileName string) ([][]byte, error) {
}
if filestat.Mode().IsRegular() {
fileContent, err := ioutil.ReadFile(path)
fileContent, err := os.ReadFile(path)
// Do not return any lines if an error occurred
if err != nil {

View file

@ -18,7 +18,6 @@ package memory
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
@ -117,7 +116,7 @@ func (s *memorySource) GetFeatures() *feature.DomainFeatures {
func detectNuma() (map[string]string, error) {
sysfsBasePath := source.SysfsDir.Path("bus/node/devices")
nodes, err := ioutil.ReadDir(sysfsBasePath)
nodes, err := os.ReadDir(sysfsBasePath)
if err != nil {
return nil, fmt.Errorf("failed to list numa nodes: %w", err)
}
@ -133,7 +132,7 @@ func detectNv() ([]feature.InstanceFeature, error) {
sysfsBasePath := source.SysfsDir.Path("bus/nd/devices")
info := make([]feature.InstanceFeature, 0)
devices, err := ioutil.ReadDir(sysfsBasePath)
devices, err := os.ReadDir(sysfsBasePath)
if os.IsNotExist(err) {
klog.V(1).Info("No NVDIMM devices present")
return info, nil
@ -156,7 +155,7 @@ var ndDevAttrs = []string{"devtype", "mode"}
func readNdDeviceInfo(path string) feature.InstanceFeature {
attrs := map[string]string{"name": filepath.Base(path)}
for _, attrName := range ndDevAttrs {
data, err := ioutil.ReadFile(filepath.Join(path, attrName))
data, err := os.ReadFile(filepath.Join(path, attrName))
if err != nil {
klog.V(3).Infof("failed to read nd device attribute %s: %w", attrName, err)
continue

View file

@ -18,7 +18,6 @@ package network
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
@ -115,7 +114,7 @@ func (s *networkSource) GetFeatures() *feature.DomainFeatures {
func detectNetDevices() ([]feature.InstanceFeature, error) {
sysfsBasePath := source.SysfsDir.Path(sysfsBaseDir)
ifaces, err := ioutil.ReadDir(sysfsBasePath)
ifaces, err := os.ReadDir(sysfsBasePath)
if err != nil {
return nil, fmt.Errorf("failed to list network interfaces: %w", err)
}
@ -137,7 +136,7 @@ func detectNetDevices() ([]feature.InstanceFeature, error) {
func readIfaceInfo(path string) feature.InstanceFeature {
attrs := map[string]string{"name": filepath.Base(path)}
for _, attrName := range ifaceAttrs {
data, err := ioutil.ReadFile(filepath.Join(path, attrName))
data, err := os.ReadFile(filepath.Join(path, attrName))
if err != nil {
if !os.IsNotExist(err) {
klog.Errorf("failed to read net iface attribute %s: %v", attrName, err)
@ -148,7 +147,7 @@ func readIfaceInfo(path string) feature.InstanceFeature {
}
for _, attrName := range devAttrs {
data, err := ioutil.ReadFile(filepath.Join(path, "device", attrName))
data, err := os.ReadFile(filepath.Join(path, "device", attrName))
if err != nil {
if !os.IsNotExist(err) {
klog.Errorf("failed to read net device attribute %s: %v", attrName, err)

View file

@ -18,7 +18,7 @@ package pci
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -34,7 +34,7 @@ var optionalDevAttrs = []string{"sriov_totalvfs", "iommu_group/type", "iommu/int
// Read a single PCI device attribute
// A PCI attribute in this context, maps to the corresponding sysfs file
func readSinglePciAttribute(devPath string, attrName string) (string, error) {
data, err := ioutil.ReadFile(filepath.Join(devPath, attrName))
data, err := os.ReadFile(filepath.Join(devPath, attrName))
if err != nil {
return "", fmt.Errorf("failed to read device attribute %s: %v", attrName, err)
}
@ -73,7 +73,7 @@ func readPciDevInfo(devPath string) (*feature.InstanceFeature, error) {
func detectPci() ([]feature.InstanceFeature, error) {
sysfsBasePath := source.SysfsDir.Path("bus/pci/devices")
devices, err := ioutil.ReadDir(sysfsBasePath)
devices, err := os.ReadDir(sysfsBasePath)
if err != nil {
return nil, err
}

View file

@ -18,7 +18,7 @@ package storage
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -96,7 +96,7 @@ func (s *storageSource) GetFeatures() *feature.DomainFeatures {
func detectBlock() ([]feature.InstanceFeature, error) {
sysfsBasePath := source.SysfsDir.Path("block")
blockdevices, err := ioutil.ReadDir(sysfsBasePath)
blockdevices, err := os.ReadDir(sysfsBasePath)
if err != nil {
return nil, fmt.Errorf("failed to list block devices: %w", err)
}
@ -113,7 +113,7 @@ func detectBlock() ([]feature.InstanceFeature, error) {
func readBlockDevQueueInfo(path string) *feature.InstanceFeature {
attrs := map[string]string{"name": filepath.Base(path)}
for _, attrName := range queueAttrs {
data, err := ioutil.ReadFile(filepath.Join(path, "queue", attrName))
data, err := os.ReadFile(filepath.Join(path, "queue", attrName))
if err != nil {
klog.V(3).Infof("failed to read block device queue attribute %s: %w", attrName, err)
continue

View file

@ -18,7 +18,7 @@ package usb
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
@ -40,7 +40,7 @@ var devAttrFileMap = map[string]string{
}
func readSingleUsbSysfsAttribute(path string) (string, error) {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("failed to read device attribute %s: %v", filepath.Base(path), err)
}