Skip to content

Commit 265acbb

Browse files
authored
[controller] Refactor code on linter issues and test improvements. (#81)
Signed-off-by: Viktor Kramarenko <[email protected]>
1 parent b001b5a commit 265acbb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2381
-2452
lines changed

.golangci.yaml

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ issues:
77
- "should not use dot imports"
88
- "don't use an underscore in package name"
99
- "exported: .*"
10+
- "could not import"
1011

1112
linters-settings:
1213
gci:
1314
sections:
1415
- standard
1516
- default
17+
- prefix(agent)
18+
- prefix(sds-health-watcher-controller)
19+
- prefix(sds-utils-installer)
1620
errcheck:
1721
ignore: fmt:.*,[rR]ead|[wW]rite|[cC]lose,io:Copy
1822

@@ -24,7 +28,7 @@ linters:
2428
- gci
2529
- gocritic
2630
- gofmt
27-
- goimports
31+
# - goimports
2832
- gosimple
2933
- govet
3034
- ineffassign

images/agent/src/cmd/main.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,12 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
"agent/config"
21-
"agent/pkg/cache"
22-
"agent/pkg/controller"
23-
"agent/pkg/kubutils"
24-
"agent/pkg/logger"
25-
"agent/pkg/monitoring"
26-
"agent/pkg/scanner"
2720
"context"
2821
"fmt"
29-
"github.com/deckhouse/sds-node-configurator/api/v1alpha1"
3022
"os"
3123
goruntime "runtime"
32-
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
3324

25+
"github.com/deckhouse/sds-node-configurator/api/v1alpha1"
3426
v1 "k8s.io/api/core/v1"
3527
sv1 "k8s.io/api/storage/v1"
3628
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -39,6 +31,15 @@ import (
3931
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
4032
"sigs.k8s.io/controller-runtime/pkg/healthz"
4133
"sigs.k8s.io/controller-runtime/pkg/manager"
34+
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
35+
36+
"agent/config"
37+
"agent/pkg/cache"
38+
"agent/pkg/controller"
39+
"agent/pkg/kubutils"
40+
"agent/pkg/logger"
41+
"agent/pkg/monitoring"
42+
"agent/pkg/scanner"
4243
)
4344

4445
var (
@@ -61,7 +62,7 @@ func main() {
6162

6263
log, err := logger.NewLogger(cfgParams.Loglevel)
6364
if err != nil {
64-
fmt.Println(fmt.Sprintf("unable to create NewLogger, err: %v", err))
65+
fmt.Printf("unable to create NewLogger, err: %v\n", err)
6566
os.Exit(1)
6667
}
6768

@@ -71,7 +72,7 @@ func main() {
7172
log.Info("[main] CfgParams has been successfully created")
7273
log.Info(fmt.Sprintf("[main] %s = %s", config.LogLevel, cfgParams.Loglevel))
7374
log.Info(fmt.Sprintf("[main] %s = %s", config.NodeName, cfgParams.NodeName))
74-
log.Info(fmt.Sprintf("[main] %s = %s", config.MachineID, cfgParams.MachineId))
75+
log.Info(fmt.Sprintf("[main] %s = %s", config.MachineID, cfgParams.MachineID))
7576
log.Info(fmt.Sprintf("[main] %s = %s", config.ScanInterval, cfgParams.BlockDeviceScanIntervalSec.String()))
7677
log.Info(fmt.Sprintf("[main] %s = %s", config.ThrottleInterval, cfgParams.ThrottleIntervalSec.String()))
7778
log.Info(fmt.Sprintf("[main] %s = %s", config.CmdDeadlineDuration, cfgParams.CmdDeadlineDurationSec.String()))

images/agent/src/config/config.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ limitations under the License.
1717
package config
1818

1919
import (
20-
"agent/internal"
21-
"agent/pkg/logger"
2220
"bytes"
2321
"fmt"
2422
"os"
2523
"os/exec"
26-
2724
"strconv"
2825
"strings"
2926
"time"
27+
28+
"agent/internal"
29+
"agent/pkg/logger"
3030
)
3131

3232
const (
@@ -42,7 +42,7 @@ const (
4242
)
4343

4444
type Options struct {
45-
MachineId string
45+
MachineID string
4646
NodeName string
4747
Loglevel logger.Verbosity
4848
MetricsPort string
@@ -69,11 +69,11 @@ func NewConfig() (*Options, error) {
6969
opts.Loglevel = logger.Verbosity(loglevel)
7070
}
7171

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

7878
opts.MetricsPort = os.Getenv(MetricsPort)
7979
if opts.MetricsPort == "" {
@@ -127,7 +127,7 @@ func NewConfig() (*Options, error) {
127127
return &opts, nil
128128
}
129129

130-
func getMachineId() (string, error) {
130+
func getMachineID() (string, error) {
131131
id := os.Getenv(MachineID)
132132
if id == "" {
133133
args := []string{"-m", "-u", "-i", "-n", "-p", "-t", "1", "cat", "/etc/machine-id"}

images/agent/src/config/config_test.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ package config
1818

1919
import (
2020
"fmt"
21-
"github.com/stretchr/testify/assert"
2221
"os"
2322
"testing"
23+
24+
"github.com/stretchr/testify/assert"
2425
)
2526

2627
func TestNewConfig(t *testing.T) {
2728
t.Run("AllValuesSet_ReturnsNoError", func(t *testing.T) {
2829
expNodeName := "test-node"
2930
expMetricsPort := ":0000"
30-
expMachineId := "test-id"
31+
expMachineID := "test-id"
3132

3233
err := os.Setenv(NodeName, expNodeName)
3334
if err != nil {
@@ -37,20 +38,23 @@ func TestNewConfig(t *testing.T) {
3738
if err != nil {
3839
t.Error(err)
3940
}
40-
err = os.Setenv(MachineID, expMachineId)
41+
err = os.Setenv(MachineID, expMachineID)
42+
if err != nil {
43+
t.Error(err)
44+
}
4145
defer os.Clearenv()
4246

4347
opts, err := NewConfig()
4448

4549
if assert.NoError(t, err) {
4650
assert.Equal(t, expNodeName, opts.NodeName)
4751
assert.Equal(t, expMetricsPort, opts.MetricsPort)
48-
assert.Equal(t, expMachineId, opts.MachineId)
52+
assert.Equal(t, expMachineID, opts.MachineID)
4953
}
5054
})
5155

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

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

68-
file, err := os.Create(machineIdFile)
72+
file, err := os.Create(machineIDFile)
6973
if err != nil {
7074
t.Error(err)
7175
}
@@ -85,11 +89,11 @@ func TestNewConfig(t *testing.T) {
8589
assert.EqualError(t, err, expErrorMsg)
8690
})
8791

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

9498
err := os.Setenv(MetricsPort, expMetricsPort)
9599
if err != nil {
@@ -108,13 +112,13 @@ func TestNewConfig(t *testing.T) {
108112
t.Run("MetricsPortNotSet_ReturnsDefaultPort", func(t *testing.T) {
109113
expNodeName := "test-node"
110114
expMetricsPort := ":4202"
111-
expMachineId := "test-id"
115+
expMachineID := "test-id"
112116

113117
err := os.Setenv(NodeName, expNodeName)
114118
if err != nil {
115119
t.Error(err)
116120
}
117-
err = os.Setenv(MachineID, expMachineId)
121+
err = os.Setenv(MachineID, expMachineID)
118122
if err != nil {
119123
t.Error(err)
120124
}
@@ -126,8 +130,7 @@ func TestNewConfig(t *testing.T) {
126130
if assert.NoError(t, err) {
127131
assert.Equal(t, expNodeName, opts.NodeName)
128132
assert.Equal(t, expMetricsPort, opts.MetricsPort)
129-
assert.Equal(t, expMachineId, opts.MachineId)
133+
assert.Equal(t, expMachineID, opts.MachineID)
130134
}
131135
})
132-
133136
}

images/agent/src/go.mod

+31-30
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,67 @@ go 1.22.2
44

55
require (
66
github.com/deckhouse/sds-node-configurator/api v0.0.0-20240805103635-969dc811217b
7-
github.com/go-logr/logr v1.4.1
7+
github.com/go-logr/logr v1.4.2
88
github.com/google/go-cmp v0.6.0
9-
github.com/onsi/ginkgo/v2 v2.17.1
10-
github.com/onsi/gomega v1.32.0
9+
github.com/onsi/ginkgo/v2 v2.19.0
10+
github.com/onsi/gomega v1.33.1
1111
github.com/pilebones/go-udev v0.9.0
12-
github.com/prometheus/client_golang v1.18.0
13-
github.com/stretchr/testify v1.8.4
14-
k8s.io/api v0.30.2
15-
k8s.io/apiextensions-apiserver v0.30.1
16-
k8s.io/apimachinery v0.30.2
17-
k8s.io/client-go v0.30.1
18-
k8s.io/klog/v2 v2.120.1
19-
k8s.io/utils v0.0.0-20231127182322-b307cd553661
20-
sigs.k8s.io/controller-runtime v0.18.4
12+
github.com/prometheus/client_golang v1.19.1
13+
github.com/stretchr/testify v1.9.0
14+
k8s.io/api v0.31.0
15+
k8s.io/apiextensions-apiserver v0.31.0
16+
k8s.io/apimachinery v0.31.0
17+
k8s.io/client-go v0.31.0
18+
k8s.io/klog/v2 v2.130.1
19+
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8
20+
sigs.k8s.io/controller-runtime v0.19.0
2121
)
2222

2323
require (
2424
github.com/beorn7/perks v1.0.1 // indirect
25-
github.com/cespare/xxhash/v2 v2.2.0 // indirect
26-
github.com/davecgh/go-spew v1.1.1 // indirect
25+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
26+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2727
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
2828
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
2929
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
3030
github.com/fsnotify/fsnotify v1.7.0 // indirect
31+
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
3132
github.com/go-openapi/jsonpointer v0.20.0 // indirect
3233
github.com/go-openapi/jsonreference v0.20.2 // indirect
3334
github.com/go-openapi/swag v0.22.4 // indirect
34-
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
35+
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
3536
github.com/gogo/protobuf v1.3.2 // indirect
3637
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
3738
github.com/golang/protobuf v1.5.4 // indirect
3839
github.com/google/gnostic-models v0.6.8 // indirect
3940
github.com/google/gofuzz v1.2.0 // indirect
40-
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
41-
github.com/google/uuid v1.4.0 // indirect
41+
github.com/google/pprof v0.0.0-20240525223248-4bfdf5a9a2af // indirect
42+
github.com/google/uuid v1.6.0 // indirect
4243
github.com/imdario/mergo v0.3.16 // indirect
4344
github.com/josharian/intern v1.0.0 // indirect
4445
github.com/json-iterator/go v1.1.12 // indirect
4546
github.com/mailru/easyjson v0.7.7 // indirect
46-
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
4747
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4848
github.com/modern-go/reflect2 v1.0.2 // indirect
4949
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
5050
github.com/pkg/errors v0.9.1 // indirect
51-
github.com/pmezard/go-difflib v1.0.0 // indirect
52-
github.com/prometheus/client_model v0.5.0 // indirect
53-
github.com/prometheus/common v0.45.0 // indirect
54-
github.com/prometheus/procfs v0.12.0 // indirect
51+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
52+
github.com/prometheus/client_model v0.6.1 // indirect
53+
github.com/prometheus/common v0.55.0 // indirect
54+
github.com/prometheus/procfs v0.15.1 // indirect
5555
github.com/spf13/pflag v1.0.5 // indirect
56+
github.com/x448/float16 v0.8.4 // indirect
5657
golang.org/x/exp v0.0.0-20231127185646-65229373498e // indirect
57-
golang.org/x/net v0.23.0 // indirect
58-
golang.org/x/oauth2 v0.15.0 // indirect
59-
golang.org/x/sys v0.18.0 // indirect
60-
golang.org/x/term v0.18.0 // indirect
61-
golang.org/x/text v0.14.0 // indirect
58+
golang.org/x/net v0.26.0 // indirect
59+
golang.org/x/oauth2 v0.21.0 // indirect
60+
golang.org/x/sys v0.21.0 // indirect
61+
golang.org/x/term v0.21.0 // indirect
62+
golang.org/x/text v0.16.0 // indirect
6263
golang.org/x/time v0.5.0 // indirect
63-
golang.org/x/tools v0.18.0 // indirect
64+
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
6465
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
65-
google.golang.org/appengine v1.6.8 // indirect
66-
google.golang.org/protobuf v1.33.0 // indirect
66+
google.golang.org/protobuf v1.34.2 // indirect
67+
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
6768
gopkg.in/inf.v0 v0.9.1 // indirect
6869
gopkg.in/yaml.v2 v2.4.0 // indirect
6970
gopkg.in/yaml.v3 v3.0.1 // indirect

0 commit comments

Comments
 (0)