Skip to content

Commit

Permalink
resmgr/cache: golangci-lint fixes.
Browse files Browse the repository at this point in the history
Signed-off-by: Krisztian Litkey <[email protected]>
  • Loading branch information
klihub authored and askervin committed Dec 11, 2024
1 parent 6562f54 commit ac591f1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 97 deletions.
68 changes: 38 additions & 30 deletions pkg/resmgr/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,9 @@ func (cch *cache) ResetActivePolicy() error {
func (cch *cache) InsertPod(nriPod *nri.PodSandbox, ch <-chan *podresapi.PodResources) Pod {
p := cch.createPod(nriPod, ch)
cch.Pods[nriPod.GetId()] = p
cch.Save()
if err := cch.Save(); err != nil {
log.Warnf("failed to save cache: %v", err)
}

return p
}
Expand All @@ -554,7 +556,9 @@ func (cch *cache) DeletePod(id string) Pod {
log.Debug("removing pod %s (%s)", p.PrettyName(), p.GetID())
delete(cch.Pods, id)

cch.Save()
if err := cch.Save(); err != nil {
log.Warnf("failed to save cache: %v", err)
}

return p
}
Expand All @@ -569,18 +573,18 @@ func (cch *cache) LookupPod(id string) (Pod, bool) {
func (cch *cache) InsertContainer(ctr *nri.Container) (Container, error) {
var err error

c := &container{
cache: cch,
}

c, err = cch.createContainer(ctr)
c, err := cch.createContainer(ctr)
if err != nil {
return nil, cacheError("failed to insert container %s: %v", c.GetID(), err)
}

cch.Containers[c.GetID()] = c
cch.createContainerDirectory(c.GetID())
cch.Save()
if err := cch.createContainerDirectory(c.GetID()); err != nil {
log.Warnf("failed to create container directory for %s: %v", c.GetID(), err)
}
if err := cch.Save(); err != nil {
log.Warnf("failed to save cache: %v", err)
}

return c, nil
}
Expand All @@ -593,10 +597,14 @@ func (cch *cache) DeleteContainer(id string) Container {
}

log.Debug("removing container %s", c.PrettyName())
cch.removeContainerDirectory(c.GetID())
if err := cch.removeContainerDirectory(c.GetID()); err != nil {
log.Warnf("failed to remove container directory for %s: %v", c.GetID(), err)
}
delete(cch.Containers, c.GetID())

cch.Save()
if err := cch.Save(); err != nil {
log.Warnf("failed to save cache: %v", err)
}

return c
}
Expand Down Expand Up @@ -624,7 +632,7 @@ func (cch *cache) LookupContainerByCgroup(path string) (Container, bool) {
continue
}

if strings.Index(path, c.GetID()) != -1 {
if strings.Contains(path, c.GetID()) {
return c, true
}
}
Expand Down Expand Up @@ -827,12 +835,12 @@ func (cch *cache) GetPolicyEntry(key string, ptr interface{}) bool {

// Marshal an opaque policy entry, special-casing cpusets and maps of cpusets.
func marshalEntry(obj interface{}) ([]byte, error) {
switch obj.(type) {
switch obj := obj.(type) {
case cpuset.CPUSet:
return []byte("\"" + obj.(cpuset.CPUSet).String() + "\""), nil
return []byte("\"" + obj.String() + "\""), nil
case map[string]cpuset.CPUSet:
dst := make(map[string]string)
for key, cset := range obj.(map[string]cpuset.CPUSet) {
for key, cset := range obj {
dst[key] = cset.String()
}
return json.Marshal(dst)
Expand All @@ -844,13 +852,13 @@ func marshalEntry(obj interface{}) ([]byte, error) {

// Unmarshal an opaque policy entry, special-casing cpusets and maps of cpusets.
func unmarshalEntry(data []byte, ptr interface{}) error {
switch ptr.(type) {
switch ptr := ptr.(type) {
case *cpuset.CPUSet:
cset, err := cpuset.Parse(string(data[1 : len(data)-1]))
if err != nil {
return err
}
*ptr.(*cpuset.CPUSet) = cset
*ptr = cset
return nil

case *map[string]cpuset.CPUSet:
Expand All @@ -868,7 +876,7 @@ func unmarshalEntry(data []byte, ptr interface{}) error {
dst[key] = cset
}

*ptr.(*map[string]cpuset.CPUSet) = dst
*ptr = dst
return nil

default:
Expand All @@ -884,32 +892,32 @@ func (cch *cache) cacheEntry(key string, ptr interface{}) error {
return nil
}

switch ptr.(type) {
switch ptr := ptr.(type) {
case *cpuset.CPUSet:
cch.policyData[key] = *ptr.(*cpuset.CPUSet)
cch.policyData[key] = *ptr
case *map[string]cpuset.CPUSet:
cch.policyData[key] = *ptr.(*map[string]cpuset.CPUSet)
cch.policyData[key] = *ptr
case *map[string]string:
cch.policyData[key] = *ptr.(*map[string]string)
cch.policyData[key] = *ptr

case *string:
cch.policyData[key] = *ptr.(*string)
cch.policyData[key] = *ptr
case *bool:
cch.policyData[key] = *ptr.(*bool)
cch.policyData[key] = *ptr

case *int32:
cch.policyData[key] = *ptr.(*int32)
cch.policyData[key] = *ptr
case *uint32:
cch.policyData[key] = *ptr.(*uint32)
cch.policyData[key] = *ptr
case *int64:
cch.policyData[key] = *ptr.(*int64)
cch.policyData[key] = *ptr
case *uint64:
cch.policyData[key] = *ptr.(*uint64)
cch.policyData[key] = *ptr

case *int:
cch.policyData[key] = *ptr.(*int)
cch.policyData[key] = *ptr
case *uint:
cch.policyData[key] = *ptr.(*uint)
cch.policyData[key] = *ptr

default:
return cacheError("can't handle policy data of type %T", ptr)
Expand Down
22 changes: 4 additions & 18 deletions pkg/resmgr/cache/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"path/filepath"
"regexp"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -272,11 +273,11 @@ func isReadOnlyDevice(rules []*nri.LinuxDeviceCgroup, d *nri.LinuxDevice) bool {
rType, rMajor, rMinor := r.Type, r.GetMajor().GetValue(), r.GetMinor().GetValue()
switch {
case rType == "" && rMajor == 0 && rMinor == 0:
if strings.IndexAny(r.Access, "w") > -1 {
if strings.Contains(r.Access, "w") {
readOnly = false
}
case d.Type == rType && d.Major == rMajor && d.Minor == rMinor:
if strings.IndexAny(r.Access, "w") > -1 {
if strings.Contains(r.Access, "w") {
readOnly = false
}
return readOnly
Expand Down Expand Up @@ -416,15 +417,11 @@ func (c *container) GetMounts() []*Mount {
var mounts []*Mount

for _, m := range c.Ctr.GetMounts() {
var options []string
for _, o := range m.Options {
options = append(options, o)
}
mounts = append(mounts, &Mount{
Destination: m.Destination,
Source: m.Source,
Type: m.Type,
Options: options,
Options: slices.Clone(m.Options),
})
}

Expand Down Expand Up @@ -890,17 +887,6 @@ func getTopologyHintsForDevice(devType string, major, minor int64, allowPathList
return topology.Hints{}
}

func getKubeletHint(cpus, mems string) (ret topology.Hints) {
if cpus != "" || mems != "" {
ret = topology.Hints{
topology.ProviderKubelet: topology.Hint{
Provider: topology.ProviderKubelet,
CPUs: cpus,
NUMAs: mems}}
}
return
}

func (c *container) GetAffinity() ([]*Affinity, error) {
pod, ok := c.GetPod()
if !ok {
Expand Down
40 changes: 19 additions & 21 deletions pkg/resmgr/cache/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"syscall"

"github.com/containers/nri-plugins/pkg/log"
Expand Down Expand Up @@ -799,24 +800,24 @@ func checkAllowedHints(annotations map[string]string, expectedHints int) bool {
ann := "allow" + "." + cache.TopologyHintsKey
allow, ok := ctr.GetEffectiveAnnotation(ann)
if !ok {
fmt.Errorf("unable to get annotation %s (%s)", ann, allow)
log.Get("cache").Errorf("unable to get annotation %s (%s)", ann, allow)
return false
}

if err := yaml.Unmarshal([]byte(allow), &pathList); err != nil {
fmt.Errorf("Error (%v) when trying to parse \"%s\"", err, allow)
log.Get("cache").Errorf("Error (%v) when trying to parse \"%s\"", err, allow)
return false
}

ann = "deny" + "." + cache.TopologyHintsKey
deny, ok := ctr.GetEffectiveAnnotation(ann)
if !ok {
fmt.Errorf("unable to get annotation %s (%s)", ann, deny)
log.Get("cache").Errorf("unable to get annotation %s (%s)", ann, deny)
return false
}

if err := yaml.Unmarshal([]byte(deny), &pathList); err != nil {
fmt.Errorf("Error (%v) when trying to parse \"%s\"", err, deny)
log.Get("cache").Errorf("Error (%v) when trying to parse \"%s\"", err, deny)
return false
}

Expand All @@ -840,6 +841,9 @@ func createSysFsDevice(devType string, major, minor int64) error {
}

realDevPath, err := filepath.EvalSymlinks(devPath)
if err != nil {
return err
}
if err := os.MkdirAll(testdataDir+"/"+realDevPath, 0770); err != nil {
return err
}
Expand All @@ -852,15 +856,19 @@ func createSysFsDevice(devType string, major, minor int64) error {
return err
}

f.Write([]byte(cpulist))
if _, err := f.Write([]byte(cpulist)); err != nil {
log.Get("cache").Errorf("unable to write to %s: %v", realDevPath+"/local_cpulist", err)
}
f.Close()

f, err = os.Create(realDevPath + "/numa_node")
if err != nil {
return err
}

f.Write([]byte(numanode))
if _, err := f.Write([]byte(numanode)); err != nil {
log.Get("cache").Errorf("unable to write to %s: %v", realDevPath+"/numa_node", err)
}
f.Close()

return nil
Expand Down Expand Up @@ -927,10 +935,7 @@ func WithCtrArgs(args []string) CtrOption {
nriCtr.Args = nil
return nil
}
nriCtr.Args = make([]string, len(args), len(args))
for i, a := range args {
nriCtr.Args[i] = a
}
nriCtr.Args = slices.Clone(args)
return nil
}
}
Expand All @@ -941,10 +946,7 @@ func WithCtrEnv(env []string) CtrOption {
nriCtr.Env = nil
return nil
}
nriCtr.Env = make([]string, len(env), len(env))
for i, e := range env {
nriCtr.Env[i] = e
}
nriCtr.Env = slices.Clone(env)
return nil
}
}
Expand All @@ -955,17 +957,13 @@ func WithCtrMounts(mounts []*nri.Mount) CtrOption {
nriCtr.Mounts = nil
return nil
}
nriCtr.Mounts = make([]*nri.Mount, len(mounts), len(mounts))
nriCtr.Mounts = make([]*nri.Mount, len(mounts))
for i, m := range mounts {
var options []string
for _, o := range m.Options {
options = append(options, o)
}
nriCtr.Mounts[i] = &nri.Mount{
Destination: m.Destination,
Source: m.Source,
Type: m.Type,
Options: options,
Options: slices.Clone(m.Options),
}
}
return nil
Expand All @@ -983,7 +981,7 @@ func WithCtrDevices(devices []*nri.LinuxDevice) CtrOption {
if nriCtr.Linux == nil {
nriCtr.Linux = &nri.LinuxContainer{}
}
nriCtr.Linux.Devices = make([]*nri.LinuxDevice, len(devices), len(devices))
nriCtr.Linux.Devices = make([]*nri.LinuxDevice, len(devices))
for i, d := range devices {
nriCtr.Linux.Devices[i] = &nri.LinuxDevice{
Path: d.Path,
Expand Down
2 changes: 1 addition & 1 deletion pkg/resmgr/cache/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (p *pod) setPodResources(podRes *podresapi.PodResources) {
func (p *pod) GetPodResources() *podresapi.PodResources {
if p.waitResCh != nil {
log.Debug("waiting for pod resources fetch to complete...")
_ = <-p.waitResCh
<-p.waitResCh
}
return p.PodResources
}
Expand Down
Loading

0 comments on commit ac591f1

Please sign in to comment.