Skip to content

Commit

Permalink
final refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Kramarenko <[email protected]>
  • Loading branch information
ViktorKram committed Aug 8, 2024
1 parent c81388e commit 19aeb2e
Show file tree
Hide file tree
Showing 31 changed files with 1,530 additions and 1,416 deletions.
16 changes: 8 additions & 8 deletions images/agent/src/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@ limitations under the License.
package main

import (
"context"
"fmt"
"os"
goruntime "runtime"

"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"

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 +38,7 @@ 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"
)

var (
Expand All @@ -61,7 +61,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 +71,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
24 changes: 14 additions & 10 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,7 +89,7 @@ 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",
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,7 +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)
}
})
}
8 changes: 4 additions & 4 deletions images/agent/src/internal/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type BlockDeviceCandidate struct {
PkName string
Type string
FSType string
MachineId string
MachineID string
PartUUID string
}

Expand All @@ -54,7 +54,7 @@ type LVMVolumeGroupCandidate struct {
StatusThinPools []LVMVGStatusThinPool
VGSize resource.Quantity
VGFree resource.Quantity
VGUuid string
VGUUID string
Nodes map[string][]LVMVGDevice
}

Expand All @@ -71,7 +71,7 @@ type LVMVGDevice struct {
Path string
PVSize resource.Quantity
DevSize resource.Quantity
PVUuid string
PVUUID string
BlockDevice string
}

Expand Down Expand Up @@ -127,7 +127,7 @@ type VGData struct {
VGShared string `json:"vg_shared"`
VGSize resource.Quantity `json:"vg_size"`
VGTags string `json:"vg_tags"`
VGUuid string `json:"vg_uuid"`
VGUUID string `json:"vg_uuid"`
}

type LVReport struct {
Expand Down
5 changes: 3 additions & 2 deletions images/agent/src/pkg/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cache

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

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

type Cache struct {
Expand Down
5 changes: 3 additions & 2 deletions images/agent/src/pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cache

import (
"agent/internal"
"bytes"
"github.com/stretchr/testify/assert"
"testing"

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

func TestCache(t *testing.T) {
Expand Down
Loading

0 comments on commit 19aeb2e

Please sign in to comment.