Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove use of ioutil deprecated functions #491

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/webhook/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"

"github.com/fsnotify/fsnotify"
Expand Down Expand Up @@ -55,7 +55,7 @@
func serve(w http.ResponseWriter, r *http.Request, admit admitHandler) {
var body []byte
if r.Body != nil {
if data, err := ioutil.ReadAll(r.Body); err == nil {
if data, err := io.ReadAll(r.Body); err == nil {
body = data
}
}
Expand All @@ -67,7 +67,7 @@
return
}

glog.V(2).Info(fmt.Sprintf("handling request: %s", body))

Check failure

Code scanning / CodeQL

Log entries created from user input High

This log entry depends on a
user-provided value
.

deserializer := webhook.Codecs.UniversalDeserializer()
obj, gvk, err := deserializer.Decode(body, nil, nil)
Expand All @@ -83,7 +83,7 @@
case v1.SchemeGroupVersion.WithKind("AdmissionReview"):
requestedAdmissionReview, ok := obj.(*v1.AdmissionReview)
if !ok {
glog.Errorf("Expected v1.AdmissionReview but got: %T", obj)

Check failure

Code scanning / CodeQL

Log entries created from user input High

This log entry depends on a
user-provided value
.
return
}
responseAdmissionReview := &v1.AdmissionReview{}
Expand Down
9 changes: 4 additions & 5 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
Expand Down Expand Up @@ -1056,7 +1055,7 @@ func tryCreateSwitchdevUdevRule(nodeState *sriovnetworkv1.SriovNetworkNodeState)
}
}

oldContent, err := ioutil.ReadFile(filePath)
oldContent, err := os.ReadFile(filePath)
// if oldContent = newContent, don't do anything
if err == nil && newContent == string(oldContent) {
return nil
Expand All @@ -1069,7 +1068,7 @@ func tryCreateSwitchdevUdevRule(nodeState *sriovnetworkv1.SriovNetworkNodeState)

// if the file does not exist or if oldContent != newContent
// write to file and create it if it doesn't exist
err = ioutil.WriteFile(filePath, []byte(newContent), 0664)
err = os.WriteFile(filePath, []byte(newContent), 0664)
if err != nil {
glog.Errorf("tryCreateSwitchdevUdevRule(): fail to write file: %v", err)
return err
Expand Down Expand Up @@ -1116,7 +1115,7 @@ func tryCreateNMUdevRule() error {
// add NM udev rules for renaming VF rep
newContent = newContent + "SUBSYSTEM==\"net\", ACTION==\"add|move\", ATTRS{phys_switch_id}!=\"\", ATTR{phys_port_name}==\"pf*vf*\", ENV{NM_UNMANAGED}=\"1\"\n"

oldContent, err := ioutil.ReadFile(filePath)
oldContent, err := os.ReadFile(filePath)
// if oldContent = newContent, don't do anything
if err == nil && newContent == string(oldContent) {
return nil
Expand All @@ -1135,7 +1134,7 @@ func tryCreateNMUdevRule() error {

// if the file does not exist or if oldContent != newContent
// write to file and create it if it doesn't exist
err = ioutil.WriteFile(filePath, []byte(newContent), 0666)
err = os.WriteFile(filePath, []byte(newContent), 0666)
if err != nil {
glog.Errorf("tryCreateNMUdevRule(): fail to write file: %v", err)
return err
Expand Down
4 changes: 2 additions & 2 deletions pkg/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package daemon
import (
"context"
"flag"
"io/ioutil"
"os"
"path"
"testing"

Expand Down Expand Up @@ -321,7 +321,7 @@ func updateSriovNetworkNodeState(c snclientset.Interface, nodeState *sriovnetwor

func assertFileContents(path, contents string) {
Eventually(func() (string, error) {
ret, err := ioutil.ReadFile(path)
ret, err := os.ReadFile(path)
return string(ret), err
}, "10s").WithOffset(1).Should(Equal(contents))
}
9 changes: 4 additions & 5 deletions pkg/plugins/k8s/k8s_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package k8s

import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
Expand Down Expand Up @@ -346,7 +345,7 @@ func (p *K8sPlugin) getSwitchDevSystemServices() []*service.Service {
}

func (p *K8sPlugin) isSwitchdevScriptNeedUpdate(scriptObj *service.ScriptManifestFile) (needUpdate bool, err error) {
data, err := ioutil.ReadFile(path.Join(chroot, scriptObj.Path))
data, err := os.ReadFile(path.Join(chroot, scriptObj.Path))
if err != nil {
if !os.IsNotExist(err) {
return false, err
Expand Down Expand Up @@ -457,23 +456,23 @@ func (p *K8sPlugin) updateSwitchdevService() error {
}

if p.updateTarget.switchdevBeforeNMRunScript {
err := ioutil.WriteFile(path.Join(chroot, p.switchdevBeforeNMRunScript.Path),
err := os.WriteFile(path.Join(chroot, p.switchdevBeforeNMRunScript.Path),
[]byte(p.switchdevBeforeNMRunScript.Contents.Inline), 0755)
if err != nil {
return err
}
}

if p.updateTarget.switchdevAfterNMRunScript {
err := ioutil.WriteFile(path.Join(chroot, p.switchdevAfterNMRunScript.Path),
err := os.WriteFile(path.Join(chroot, p.switchdevAfterNMRunScript.Path),
[]byte(p.switchdevAfterNMRunScript.Contents.Inline), 0755)
if err != nil {
return err
}
}

if p.updateTarget.switchdevUdevScript {
err := ioutil.WriteFile(path.Join(chroot, p.switchdevUdevScript.Path),
err := os.WriteFile(path.Join(chroot, p.switchdevUdevScript.Path),
[]byte(p.switchdevUdevScript.Contents.Inline), 0755)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -120,7 +119,7 @@ func renderTemplate(path string, d *RenderData) (*bytes.Buffer, error) {
tmpl.Funcs(template.FuncMap{"getOr": getOr, "isSet": isSet})
tmpl.Funcs(sprig.TxtFuncMap())

source, err := ioutil.ReadFile(path)
source, err := os.ReadFile(path)
if err != nil {
return nil, errors.Wrapf(err, "failed to read manifest %s", path)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/service/service_manager.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package service

import (
"io/ioutil"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -43,7 +42,7 @@ func (sm *serviceManager) IsServiceExist(servicePath string) (bool, error) {

// ReadService read service from given path
func (sm *serviceManager) ReadService(servicePath string) (*Service, error) {
data, err := ioutil.ReadFile(path.Join(sm.chroot, servicePath))
data, err := os.ReadFile(path.Join(sm.chroot, servicePath))
if err != nil {
return nil, err
}
Expand All @@ -58,7 +57,7 @@ func (sm *serviceManager) ReadService(servicePath string) (*Service, error) {
// EnableService creates service file and enables it with systemctl enable
func (sm *serviceManager) EnableService(service *Service) error {
// Write service file
err := ioutil.WriteFile(path.Join(sm.chroot, service.Path), []byte(service.Content), 0644)
err := os.WriteFile(path.Join(sm.chroot, service.Path), []byte(service.Content), 0644)
if err != nil {
return err
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/service/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package service

import (
"io/ioutil"
"io"
"os"
"strings"

"github.com/coreos/go-systemd/v22/unit"
Expand Down Expand Up @@ -55,7 +56,7 @@ OUTER:
newServiceOptions = append(newServiceOptions, opt)
}

data, err := ioutil.ReadAll(unit.Serialize(newServiceOptions))
data, err := io.ReadAll(unit.Serialize(newServiceOptions))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -84,7 +85,7 @@ OUTER:
serviceOptions = append(serviceOptions, appendOpt)
}

data, err := ioutil.ReadAll(unit.Serialize(serviceOptions))
data, err := io.ReadAll(unit.Serialize(serviceOptions))
if err != nil {
return nil, err
}
Expand All @@ -98,7 +99,7 @@ OUTER:

// ReadServiceInjectionManifestFile reads service injection file
func ReadServiceInjectionManifestFile(path string) (*Service, error) {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand All @@ -117,7 +118,7 @@ func ReadServiceInjectionManifestFile(path string) (*Service, error) {

// ReadServiceManifestFile reads service file
func ReadServiceManifestFile(path string) (*Service, error) {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand All @@ -136,7 +137,7 @@ func ReadServiceManifestFile(path string) (*Service, error) {

// ReadScriptManifestFile reads script file
func ReadScriptManifestFile(path string) (*ScriptManifestFile, error) {
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand Down
15 changes: 7 additions & 8 deletions pkg/systemd/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package systemd
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -56,7 +55,7 @@ type SriovResult struct {
}

func ReadConfFile() (spec *SriovConfig, err error) {
rawConfig, err := ioutil.ReadFile(SriovSystemdConfigPath)
rawConfig, err := os.ReadFile(SriovSystemdConfigPath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -101,7 +100,7 @@ func WriteConfFile(newState *sriovnetworkv1.SriovNetworkNodeState, unsupportedNi
}
}

oldContent, err := ioutil.ReadFile(SriovHostSystemdConfigPath)
oldContent, err := os.ReadFile(SriovHostSystemdConfigPath)
if err != nil {
glog.Errorf("WriteConfFile(): fail to read file: %v", err)
return false, err
Expand All @@ -128,7 +127,7 @@ func WriteConfFile(newState *sriovnetworkv1.SriovNetworkNodeState, unsupportedNi
glog.V(2).Infof("WriteConfFile(): previews configuration is not equal: old config:\n%s\nnew config:\n%s\n", string(oldContent), string(newContent))

glog.V(2).Infof("WriteConfFile(): write '%s' to %s", newContent, SriovHostSystemdConfigPath)
err = ioutil.WriteFile(SriovHostSystemdConfigPath, newContent, 0644)
err = os.WriteFile(SriovHostSystemdConfigPath, newContent, 0644)
if err != nil {
glog.Errorf("WriteConfFile(): fail to write file: %v", err)
return false, err
Expand Down Expand Up @@ -167,7 +166,7 @@ func WriteSriovResult(result *SriovResult) error {
}

glog.V(2).Infof("WriteSriovResult(): write '%s' to %s", string(out), SriovSystemdResultPath)
err = ioutil.WriteFile(SriovSystemdResultPath, out, 0644)
err = os.WriteFile(SriovSystemdResultPath, out, 0644)
if err != nil {
glog.Errorf("WriteSriovResult(): failed to write sriov result file on path %s: %v", SriovSystemdResultPath, err)
return err
Expand All @@ -188,7 +187,7 @@ func ReadSriovResult() (*SriovResult, error) {
}
}

rawConfig, err := ioutil.ReadFile(SriovHostSystemdResultPath)
rawConfig, err := os.ReadFile(SriovHostSystemdResultPath)
if err != nil {
glog.Errorf("ReadSriovResult(): failed to read sriov result file on path %s: %v", SriovHostSystemdResultPath, err)
return nil, err
Expand Down Expand Up @@ -224,7 +223,7 @@ func WriteSriovSupportedNics() error {
rawNicList = append(rawNicList, []byte(fmt.Sprintf("%s\n", line))...)
}

err = ioutil.WriteFile(sriovHostSystemdSupportedNicPath, rawNicList, 0644)
err = os.WriteFile(sriovHostSystemdSupportedNicPath, rawNicList, 0644)
if err != nil {
glog.Errorf("WriteSriovSupportedNics(): failed to write sriov supporter nics ids file on path %s: %v", sriovHostSystemdSupportedNicPath, err)
return err
Expand All @@ -245,7 +244,7 @@ func ReadSriovSupportedNics() ([]string, error) {
}
}

rawConfig, err := ioutil.ReadFile(sriovSystemdSupportedNicPath)
rawConfig, err := os.ReadFile(sriovSystemdSupportedNicPath)
if err != nil {
glog.Errorf("ReadSriovSupportedNics(): failed to read sriov supporter nics file on path %s: %v", sriovSystemdSupportedNicPath, err)
return nil, err
Expand Down
13 changes: 6 additions & 7 deletions pkg/utils/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand All @@ -24,7 +23,7 @@ func Unbind(pciAddr string) error {
}

filePath := filepath.Join(sysBusPciDrivers, driver, "unbind")
err := ioutil.WriteFile(filePath, []byte(pciAddr), os.ModeAppend)
err := os.WriteFile(filePath, []byte(pciAddr), os.ModeAppend)
if err != nil {
glog.Errorf("Unbind(): fail to unbind driver for device %s. %s", pciAddr, err)
return err
Expand All @@ -49,13 +48,13 @@ func BindDpdkDriver(pciAddr, driver string) error {
}

driverOverridePath := filepath.Join(sysBusPciDevices, pciAddr, "driver_override")
err := ioutil.WriteFile(driverOverridePath, []byte(driver), os.ModeAppend)
err := os.WriteFile(driverOverridePath, []byte(driver), os.ModeAppend)
if err != nil {
glog.Errorf("BindDpdkDriver(): fail to write driver_override for device %s %s", driver, err)
return err
}
bindPath := filepath.Join(sysBusPciDrivers, driver, "bind")
err = ioutil.WriteFile(bindPath, []byte(pciAddr), os.ModeAppend)
err = os.WriteFile(bindPath, []byte(pciAddr), os.ModeAppend)
if err != nil {
glog.Errorf("BindDpdkDriver(): fail to bind driver for device %s: %s", pciAddr, err)
_, err := os.Readlink(filepath.Join(sysBusPciDevices, pciAddr, "iommu_group"))
Expand All @@ -65,7 +64,7 @@ func BindDpdkDriver(pciAddr, driver string) error {
}
return err
}
err = ioutil.WriteFile(driverOverridePath, []byte(""), os.ModeAppend)
err = os.WriteFile(driverOverridePath, []byte(""), os.ModeAppend)
if err != nil {
glog.Errorf("BindDpdkDriver(): fail to clear driver_override for device %s: %s", pciAddr, err)
return err
Expand All @@ -90,12 +89,12 @@ func BindDefaultDriver(pciAddr string) error {
}

driverOverridePath := filepath.Join(sysBusPciDevices, pciAddr, "driver_override")
err := ioutil.WriteFile(driverOverridePath, []byte("\x00"), os.ModeAppend)
err := os.WriteFile(driverOverridePath, []byte("\x00"), os.ModeAppend)
if err != nil {
glog.Errorf("BindDefaultDriver(): fail to write driver_override for device %s: %s", pciAddr, err)
return err
}
err = ioutil.WriteFile(sysBusPciDriversProbe, []byte(pciAddr), os.ModeAppend)
err = os.WriteFile(sysBusPciDriversProbe, []byte(pciAddr), os.ModeAppend)
if err != nil {
glog.Errorf("BindDefaultDriver(): fail to bind driver for device %s: %s", pciAddr, err)
return err
Expand Down
5 changes: 2 additions & 3 deletions pkg/utils/sriov.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/golang/glog"
Expand Down Expand Up @@ -125,7 +124,7 @@ func WriteSwitchdevConfFile(newState *sriovnetworkv1.SriovNetworkNodeState) (upd
return
}
}
oldContent, err := ioutil.ReadFile(SriovHostSwitchDevConfPath)
oldContent, err := os.ReadFile(SriovHostSwitchDevConfPath)
if err != nil {
glog.Errorf("WriteSwitchdevConfFile(): fail to read file: %v", err)
return
Expand All @@ -145,7 +144,7 @@ func WriteSwitchdevConfFile(newState *sriovnetworkv1.SriovNetworkNodeState) (upd
}
update = true
glog.V(2).Infof("WriteSwitchdevConfFile(): write '%s' to switchdev.conf", newContent)
err = ioutil.WriteFile(SriovHostSwitchDevConfPath, newContent, 0644)
err = os.WriteFile(SriovHostSwitchDevConfPath, newContent, 0644)
if err != nil {
glog.Errorf("WriteSwitchdevConfFile(): fail to write file: %v", err)
return
Expand Down
Loading
Loading