Skip to content

Commit

Permalink
[controller] Refactor code on linter issues and test improvements. (#81)
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Kramarenko <[email protected]>
  • Loading branch information
ViktorKram committed Sep 17, 2024
1 parent a86aa91 commit 3ac594f
Show file tree
Hide file tree
Showing 47 changed files with 2,381 additions and 2,452 deletions.
6 changes: 5 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ issues:
- "should not use dot imports"
- "don't use an underscore in package name"
- "exported: .*"
- "could not import"

linters-settings:
gci:
sections:
- standard
- default
- prefix(agent)
- prefix(sds-health-watcher-controller)
- prefix(sds-utils-installer)
errcheck:
ignore: fmt:.*,[rR]ead|[wW]rite|[cC]lose,io:Copy

Expand All @@ -24,7 +28,7 @@ linters:
- gci
- gocritic
- gofmt
- goimports
# - goimports
- gosimple
- govet
- ineffassign
Expand Down
23 changes: 12 additions & 11 deletions images/agent/src/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,12 @@ limitations under the License.
package main

import (
"agent/config"
"agent/pkg/cache"
"agent/pkg/controller"
"agent/pkg/kubutils"
"agent/pkg/logger"
"agent/pkg/monitoring"
"agent/pkg/scanner"
"context"
"fmt"
"github.com/deckhouse/sds-node-configurator/api/v1alpha1"
"os"
goruntime "runtime"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"

"github.com/deckhouse/sds-node-configurator/api/v1alpha1"
v1 "k8s.io/api/core/v1"
sv1 "k8s.io/api/storage/v1"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand All @@ -39,6 +31,15 @@ import (
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"

"agent/config"
"agent/pkg/cache"
"agent/pkg/controller"
"agent/pkg/kubutils"
"agent/pkg/logger"
"agent/pkg/monitoring"
"agent/pkg/scanner"
)

var (
Expand All @@ -61,7 +62,7 @@ func main() {

log, err := logger.NewLogger(cfgParams.Loglevel)
if err != nil {
fmt.Println(fmt.Sprintf("unable to create NewLogger, err: %v", err))
fmt.Printf("unable to create NewLogger, err: %v\n", err)
os.Exit(1)
}

Expand All @@ -71,7 +72,7 @@ func main() {
log.Info("[main] CfgParams has been successfully created")
log.Info(fmt.Sprintf("[main] %s = %s", config.LogLevel, cfgParams.Loglevel))
log.Info(fmt.Sprintf("[main] %s = %s", config.NodeName, cfgParams.NodeName))
log.Info(fmt.Sprintf("[main] %s = %s", config.MachineID, cfgParams.MachineId))
log.Info(fmt.Sprintf("[main] %s = %s", config.MachineID, cfgParams.MachineID))
log.Info(fmt.Sprintf("[main] %s = %s", config.ScanInterval, cfgParams.BlockDeviceScanIntervalSec.String()))
log.Info(fmt.Sprintf("[main] %s = %s", config.ThrottleInterval, cfgParams.ThrottleIntervalSec.String()))
log.Info(fmt.Sprintf("[main] %s = %s", config.CmdDeadlineDuration, cfgParams.CmdDeadlineDurationSec.String()))
Expand Down
14 changes: 7 additions & 7 deletions images/agent/src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ limitations under the License.
package config

import (
"agent/internal"
"agent/pkg/logger"
"bytes"
"fmt"
"os"
"os/exec"

"strconv"
"strings"
"time"

"agent/internal"
"agent/pkg/logger"
)

const (
Expand All @@ -42,7 +42,7 @@ const (
)

type Options struct {
MachineId string
MachineID string
NodeName string
Loglevel logger.Verbosity
MetricsPort string
Expand All @@ -69,11 +69,11 @@ func NewConfig() (*Options, error) {
opts.Loglevel = logger.Verbosity(loglevel)
}

machId, err := getMachineId()
machID, err := getMachineID()
if err != nil {
return nil, fmt.Errorf("[NewConfig] unable to get %s, error: %w", MachineID, err)
}
opts.MachineId = machId
opts.MachineID = machID

opts.MetricsPort = os.Getenv(MetricsPort)
if opts.MetricsPort == "" {
Expand Down Expand Up @@ -127,7 +127,7 @@ func NewConfig() (*Options, error) {
return &opts, nil
}

func getMachineId() (string, error) {
func getMachineID() (string, error) {
id := os.Getenv(MachineID)
if id == "" {
args := []string{"-m", "-u", "-i", "-n", "-p", "-t", "1", "cat", "/etc/machine-id"}
Expand Down
27 changes: 15 additions & 12 deletions images/agent/src/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ package config

import (
"fmt"
"github.com/stretchr/testify/assert"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewConfig(t *testing.T) {
t.Run("AllValuesSet_ReturnsNoError", func(t *testing.T) {
expNodeName := "test-node"
expMetricsPort := ":0000"
expMachineId := "test-id"
expMachineID := "test-id"

err := os.Setenv(NodeName, expNodeName)
if err != nil {
Expand All @@ -37,20 +38,23 @@ func TestNewConfig(t *testing.T) {
if err != nil {
t.Error(err)
}
err = os.Setenv(MachineID, expMachineId)
err = os.Setenv(MachineID, expMachineID)
if err != nil {
t.Error(err)
}
defer os.Clearenv()

opts, err := NewConfig()

if assert.NoError(t, err) {
assert.Equal(t, expNodeName, opts.NodeName)
assert.Equal(t, expMetricsPort, opts.MetricsPort)
assert.Equal(t, expMachineId, opts.MachineId)
assert.Equal(t, expMachineID, opts.MachineID)
}
})

t.Run("NodeNameNotSet_ReturnsError", func(t *testing.T) {
machineIdFile := "./host-root/etc/machine-id"
machineIDFile := "./host-root/etc/machine-id"
expMetricsPort := ":0000"
expErrorMsg := fmt.Sprintf("[NewConfig] required %s env variable is not specified", NodeName)

Expand All @@ -65,7 +69,7 @@ func TestNewConfig(t *testing.T) {
t.Error(err)
}

file, err := os.Create(machineIdFile)
file, err := os.Create(machineIDFile)
if err != nil {
t.Error(err)
}
Expand All @@ -85,11 +89,11 @@ func TestNewConfig(t *testing.T) {
assert.EqualError(t, err, expErrorMsg)
})

t.Run("MachineIdNotSet_ReturnsError", func(t *testing.T) {
t.Run("MachineIDNotSet_ReturnsError", func(t *testing.T) {
expMetricsPort := ":0000"
expNodeName := "test-node"
expErrorMsg := fmt.Sprintf("[NewConfig] unable to get %s, error: %s",
MachineID, "open /host-root/etc/machine-id: no such file or directory")
MachineID, "fork/exec /opt/deckhouse/sds/bin/nsenter.static: no such file or directory")

err := os.Setenv(MetricsPort, expMetricsPort)
if err != nil {
Expand All @@ -108,13 +112,13 @@ func TestNewConfig(t *testing.T) {
t.Run("MetricsPortNotSet_ReturnsDefaultPort", func(t *testing.T) {
expNodeName := "test-node"
expMetricsPort := ":4202"
expMachineId := "test-id"
expMachineID := "test-id"

err := os.Setenv(NodeName, expNodeName)
if err != nil {
t.Error(err)
}
err = os.Setenv(MachineID, expMachineId)
err = os.Setenv(MachineID, expMachineID)
if err != nil {
t.Error(err)
}
Expand All @@ -126,8 +130,7 @@ func TestNewConfig(t *testing.T) {
if assert.NoError(t, err) {
assert.Equal(t, expNodeName, opts.NodeName)
assert.Equal(t, expMetricsPort, opts.MetricsPort)
assert.Equal(t, expMachineId, opts.MachineId)
assert.Equal(t, expMachineID, opts.MachineID)
}
})

}
61 changes: 31 additions & 30 deletions images/agent/src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,67 @@ go 1.22.2

require (
github.com/deckhouse/sds-node-configurator/api v0.0.0-20240805103635-969dc811217b
github.com/go-logr/logr v1.4.1
github.com/go-logr/logr v1.4.2
github.com/google/go-cmp v0.6.0
github.com/onsi/ginkgo/v2 v2.17.1
github.com/onsi/gomega v1.32.0
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
github.com/pilebones/go-udev v0.9.0
github.com/prometheus/client_golang v1.18.0
github.com/stretchr/testify v1.8.4
k8s.io/api v0.30.2
k8s.io/apiextensions-apiserver v0.30.1
k8s.io/apimachinery v0.30.2
k8s.io/client-go v0.30.1
k8s.io/klog/v2 v2.120.1
k8s.io/utils v0.0.0-20231127182322-b307cd553661
sigs.k8s.io/controller-runtime v0.18.4
github.com/prometheus/client_golang v1.19.1
github.com/stretchr/testify v1.9.0
k8s.io/api v0.31.0
k8s.io/apiextensions-apiserver v0.31.0
k8s.io/apimachinery v0.31.0
k8s.io/client-go v0.31.0
k8s.io/klog/v2 v2.130.1
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8
sigs.k8s.io/controller-runtime v0.19.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/google/pprof v0.0.0-20240525223248-4bfdf5a9a2af // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/exp v0.0.0-20231127185646-65229373498e // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.18.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 3ac594f

Please sign in to comment.