Skip to content

Commit

Permalink
Merge branch 'main' into fixes/golangci-lint/topology-aware
Browse files Browse the repository at this point in the history
  • Loading branch information
klihub authored Dec 11, 2024
2 parents 8eed4bb + 8ab768c commit 5d20f3e
Show file tree
Hide file tree
Showing 50 changed files with 222 additions and 306 deletions.
3 changes: 1 addition & 2 deletions cmd/plugins/memory-qos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (

type plugin struct {
stub stub.Stub
mask stub.EventMask
config *pluginConfig
}

Expand Down Expand Up @@ -216,7 +215,7 @@ func (p *plugin) CreateContainer(ctx context.Context, pod *api.PodSandbox, ctr *
return nil, nil, errWithContext
}
class = value
case sliceContains(p.config.UnifiedAnnotations, annPrefix) == true:
case sliceContains(p.config.UnifiedAnnotations, annPrefix):
unified[annPrefix] = value
log.Tracef("applying unified annotation %q resulted in unified=%v", annPrefix, unified)
default:
Expand Down
14 changes: 9 additions & 5 deletions cmd/plugins/memtierd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (

type plugin struct {
stub stub.Stub
mask stub.EventMask
config *pluginConfig
cgroupsDir string
ctrMemtierdEnv map[string]*memtierdEnv
Expand Down Expand Up @@ -73,7 +72,6 @@ type qosClass struct {
}

type memtierdEnv struct {
pid int
ctrDir string
configFile string
outputFile string
Expand Down Expand Up @@ -314,7 +312,11 @@ func (p *plugin) StopContainer(ctx context.Context, pod *api.PodSandbox, ctr *ap
log.Debugf("StopContainer: killing memtierd of %s (pid: %d) failed: %s", ppName, pid, err)
}
// Close files, read exit status (leave no zombie processes behind)
go mtdEnv.cmd.Wait()
go func() {
if err := mtdEnv.cmd.Wait(); err != nil {
log.Errorf("StopContainer: waiting for memtierd of %s (pid: %d) failed: %s", ppName, pid, err)
}
}()
}

log.Tracef("StopContainer: removing memtierd run directory %s", mtdEnv.ctrDir)
Expand Down Expand Up @@ -438,9 +440,11 @@ func (me *memtierdEnv) startMemtierd() error {
return fmt.Errorf("failed to start command %s: %q", cmd, err)
}
if cmd.Process != nil {
os.WriteFile(me.pidFile,
if err := os.WriteFile(me.pidFile,
[]byte(fmt.Sprintf("%d\n", cmd.Process.Pid)),
0400)
0400); err != nil {
log.Warnf("failed to write PID file %q: %s", me.pidFile, err)
}
}
me.cmd = cmd
return nil
Expand Down
7 changes: 0 additions & 7 deletions cmd/plugins/template/policy/template-policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,10 @@ func (p *policy) ExportResourceData(c cache.Container) map[string]string {
return nil
}

// Initialize or reinitialize the policy.
func (p *policy) initialize() error {
return nil
}

type NoMetrics struct{}

func (*NoMetrics) Describe(chan<- *prometheus.Desc) {
return
}

func (*NoMetrics) Collect(chan<- prometheus.Metric) {
return
}
6 changes: 3 additions & 3 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ func (a *Agent) Start(notifyFn NotifyFn) error {
break
}
if e.Type == watch.Added || e.Type == watch.Modified {
group, _ := e.Object.(*corev1.Node).Labels[a.groupLabel]
group := e.Object.(*corev1.Node).Labels[a.groupLabel]
if group == "" {
for _, l := range deprecatedGroupLabels {
group, _ = e.Object.(*corev1.Node).Labels[l]
group = e.Object.(*corev1.Node).Labels[l]
if group != "" {
log.Warnf("Using DEPRECATED config group label %q", l)
log.Warnf("Please switch to using label %q instead", a.groupLabel)
Expand Down Expand Up @@ -259,7 +259,7 @@ func (a *Agent) Stop() {

if a.stopC != nil {
close(a.stopC)
_ = <-a.doneC
<-a.doneC
a.stopC = nil
}
}
Expand Down
16 changes: 1 addition & 15 deletions pkg/agent/podresapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ package podresapi
import (
"context"
"fmt"
"net"
"strings"
"time"

logger "github.com/containers/nri-plugins/pkg/log"
"google.golang.org/grpc"
Expand All @@ -43,7 +41,6 @@ const (
// these constants were obtained from NFD sources, cross-checked against
// https://github.com/kubernetes/kubernetes/blob/release-1.31/test/e2e_node/util.go#L83
defaultSocketPath = "/var/lib/kubelet/pod-resources/kubelet.sock"
timeout = 10 * time.Second
maxSize = 1024 * 1024 * 16
)

Expand Down Expand Up @@ -77,20 +74,9 @@ func NewClient(options ...ClientOption) (*Client, error) {
}

if c.conn == nil {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

dialer := func(ctx context.Context, addr string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, "unix", addr)
}

conn, err := grpc.DialContext(
ctx,
c.socketPath,
conn, err := grpc.NewClient("unix://"+c.socketPath,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(dialer),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxSize)),
grpc.WithBlock(),
)

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/watch/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (w *FileWatch) Stop() {

if w.stopC != nil {
close(w.stopC)
_ = <-w.doneC
<-w.doneC
w.stopC = nil
}
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/agent/watch/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type ObjectWatch struct {
name string
resultC chan Event
wif watch.Interface
pending *Event
reopenC <-chan time.Time
failing bool

Expand Down Expand Up @@ -73,7 +72,7 @@ func (w *ObjectWatch) Stop() {

if w.stopC != nil {
close(w.stopC)
_ = <-w.doneC
<-w.doneC
w.stopC = nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/config/v1alpha1/resmgr/control/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (
// +k8s:deepcopy-gen=true
type Config struct {
// +optional
CPU *cpu.Config `json:"cpu",omitempty"`
CPU *cpu.Config `json:"cpu,omitempty"`
}
4 changes: 2 additions & 2 deletions pkg/apis/resmgr/v1alpha1/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ func (e *Expression) Validate() error {
return exprError("invalid expression, '%s' requires a single value", e.Op)
}
case Exists, NotExist:
if e.Values != nil && len(e.Values) != 0 {
if len(e.Values) != 0 {
return exprError("invalid expression, '%s' does not take any values", e.Op)
}

case In, NotIn:
case MatchesAny, MatchesNone:

case AlwaysTrue:
if e.Values != nil && len(e.Values) != 0 {
if len(e.Values) != 0 {
return exprError("invalid expression, '%s' does not take any values", e.Op)
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/cgroups/cgroupblkio.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package cgroups
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -341,7 +340,7 @@ var currentPlatform platformInterface = defaultPlatform{}

// readFromFile returns file contents as a string.
func (dpm defaultPlatform) readFromFile(filename string) (string, error) {
content, err := ioutil.ReadFile(filename)
content, err := os.ReadFile(filename)
return string(content), err
}

Expand All @@ -352,6 +351,6 @@ func (dpm defaultPlatform) writeToFile(filename string, content string) error {
return err
}
defer f.Close()
_, err = fmt.Fprintf(f, content)
_, err = fmt.Fprintf(f, "%s", content)
return err
}
4 changes: 2 additions & 2 deletions pkg/cgroups/cgroupstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package cgroups

import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -89,7 +89,7 @@ type GlobalNumaStats struct {

func readCgroupFileLines(filePath string) ([]string, error) {

f, err := ioutil.ReadFile(filePath)
f, err := os.ReadFile(filePath)

if err != nil {
return nil, err
Expand Down
5 changes: 4 additions & 1 deletion pkg/cgroupstats/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func walkCgroups() []string {
containerDirs := []string{}

cpuset := filepath.Join(cgroupRoot, "cpuset")
filepath.Walk(filepath.Join(cpuset, kubepodsDir),
err := filepath.Walk(filepath.Join(cpuset, kubepodsDir),
func(path string, info os.FileInfo, err error) error {
if err != nil {
if os.IsNotExist(err) {
Expand Down Expand Up @@ -315,6 +315,9 @@ func walkCgroups() []string {

return nil
})
if err != nil {
log.Warnf("cgroupfs walk failed: %v", err)
}

return containerDirs
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/cpuallocator/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ type allocatorHelper struct {
prefer CPUPriority // CPU priority to prefer
cnt int // number of CPUs to allocate
result cpuset.CPUSet // set of CPUs allocated

pkgs []sysfs.CPUPackage // physical CPU packages, sorted by preference
cpus []sysfs.CPU // CPU cores, sorted by preference
}

// CPUAllocator is an interface for a generic CPU allocator
Expand Down Expand Up @@ -942,7 +939,6 @@ func (a *allocatorHelper) takeCacheGroups() {
a.result = result
a.from = from
a.cnt = 0
return
}

// Allocate full idle CPU cores.
Expand Down
7 changes: 3 additions & 4 deletions pkg/cpuallocator/cpuallocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package cpuallocator

import (
"io/ioutil"
"os"
"path"
"testing"
Expand All @@ -30,7 +29,7 @@ import (

func TestAllocatorHelper(t *testing.T) {
// Create tmpdir and decompress testdata there
tmpdir, err := ioutil.TempDir("", "nri-resource-policy-test-")
tmpdir, err := os.MkdirTemp("", "nri-resource-policy-test-")
if err != nil {
t.Fatalf("failed to create tmpdir: %v", err)
}
Expand Down Expand Up @@ -108,7 +107,7 @@ func TestClusteredAllocation(t *testing.T) {
}

// Create tmpdir and decompress testdata there
tmpdir, err := ioutil.TempDir("", "nri-resource-policy-test-")
tmpdir, err := os.MkdirTemp("", "nri-resource-policy-test-")
if err != nil {
t.Fatalf("failed to create tmpdir: %v", err)
}
Expand Down Expand Up @@ -324,7 +323,7 @@ func TestClusteredCoreKindAllocation(t *testing.T) {
}

// Create tmpdir and decompress testdata there
tmpdir, err := ioutil.TempDir("", "nri-resource-policy-test-")
tmpdir, err := os.MkdirTemp("", "nri-resource-policy-test-")
if err != nil {
t.Fatalf("failed to create tmpdir: %v", err)
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/healthz/healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,20 @@ func serve(w http.ResponseWriter, req *http.Request) {
status, details := check()
if status == Healthy {
w.WriteHeader(200)
w.Write([]byte("ok"))
_, err := w.Write([]byte("ok"))
if err != nil {
log.Errorf("failed to write response: %v", err)
}
} else {
errors := ""
for _, err := range details {
errors += fmt.Sprintf("%v\n", err)
}
w.WriteHeader(500)
w.Write([]byte(errors))
_, err := w.Write([]byte(errors))
if err != nil {
log.Errorf("failed to write response: %v", err)
}
}
}

Expand Down
13 changes: 10 additions & 3 deletions pkg/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ func (s *Server) Start(addr string) error {
s.server.Addr = ln.Addr().String()
}

go s.server.Serve(ln)
go func() {
err := s.server.Serve(ln)
if err != nil && err != http.ErrServerClosed {
log.Warn("HTTP server exited with error: %v", err)
}
}()

return nil
}
Expand Down Expand Up @@ -200,8 +205,10 @@ func (s *Server) Shutdown(wait bool) {
close(sync)
})
}
s.server.Shutdown(context.Background())
_ = <-sync
if err := s.server.Shutdown(context.Background()); err != nil && err != http.ErrServerClosed {
log.Warnf("failed to shutdown server: %v", err)
}
<-sync

s.server = nil
}
Expand Down
10 changes: 2 additions & 8 deletions pkg/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package http

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"
)
Expand Down Expand Up @@ -48,12 +48,6 @@ func TestStartStop(t *testing.T) {
srv.Stop()
}

type urlTest struct {
pattern string
response string
fallback string
}

func checkURL(t *testing.T, srv *Server, path, response string, status int) {
url := "http://" + srv.GetAddress() + path

Expand All @@ -66,7 +60,7 @@ func checkURL(t *testing.T, srv *Server, path, response string, status int) {
t.Errorf("http.Get(%s) status %d, expected %d", url, res.StatusCode, status)
}

txt, err := ioutil.ReadAll(res.Body)
txt, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("http.Get(%s) failed to read response: %v", url, err)
}
Expand Down
Loading

0 comments on commit 5d20f3e

Please sign in to comment.