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

cache: golangci-lint fixes. #427

Merged
merged 3 commits into from
Dec 11, 2024
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
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
9 changes: 3 additions & 6 deletions pkg/resmgr/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ var _ = Describe("Cache", func() {
c, _, _ = makePopulatedCache(nriPods, nil)

for _, nriPod := range nriPods {
pod, err := c.InsertPod(nriPod)
pod := c.InsertPod(nriPod, nil)
Expect(pod).ToNot(BeNil())
Expect(err).To(BeNil())
}
})

Expand All @@ -61,9 +60,8 @@ var _ = Describe("Cache", func() {
c, _, _ = makePopulatedCache(nriPods, nil)

for _, nriPod := range nriPods {
pod, err := c.InsertPod(nriPod)
pod := c.InsertPod(nriPod, nil)
Expect(pod).ToNot(BeNil())
Expect(err).To(BeNil())

chk, ok := c.LookupPod(pod.GetID())
Expect(chk).ToNot(BeNil())
Expand Down Expand Up @@ -107,9 +105,8 @@ func makePopulatedCache(nriPods []*nri.PodSandbox, nriCtrs []*nri.Container) (ca
)

for _, nriPod := range nriPods {
pod, err := c.InsertPod(nriPod)
pod := c.InsertPod(nriPod, nil)
Expect(pod).ToNot(BeNil())
Expect(err).To(BeNil())
pods = append(pods, pod)
}
for _, nriCtr := range nriCtrs {
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
2 changes: 1 addition & 1 deletion pkg/resmgr/cache/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ var _ = Describe("Pod", func() {

_, pods, _ = makePopulatedCache(nriPods, nil)

Expect(pods[0].PrettyName()).To(Equal(pods[0].GetName()))
Expect(pods[0].PrettyName()).To(Equal(pods[0].GetNamespace() + "/" + pods[0].GetName()))
Expect(pods[1].PrettyName()).To(Equal(pods[1].GetNamespace() + "/" + pods[1].GetName()))
})
})
Expand Down
Loading
Loading