Skip to content

Commit

Permalink
refactored src dir
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Kramarenko <[email protected]>
  • Loading branch information
ViktorKram committed Aug 6, 2024
1 parent 969dc81 commit 6aef89e
Show file tree
Hide file tree
Showing 10 changed files with 445 additions and 634 deletions.
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ issues:
- "should not use dot imports"
- "don't use an underscore in package name"
- "exported: .*"
- "could not import"

linters-settings:
gci:
Expand Down
2 changes: 1 addition & 1 deletion images/agent/src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module agent
go 1.22.2

require (
github.com/deckhouse/sds-node-configurator/api v0.0.0-20240709091744-c9d24f05db41
github.com/deckhouse/sds-node-configurator/api v0.0.0-20240805103635-969dc811217b
github.com/go-logr/logr v1.4.1
github.com/google/go-cmp v0.6.0
github.com/onsi/ginkgo/v2 v2.17.1
Expand Down
4 changes: 2 additions & 2 deletions images/agent/src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckhouse/sds-node-configurator/api v0.0.0-20240709091744-c9d24f05db41 h1:kfnAfII4E8yWkDZ4FJIPO9/OvXkMIQDPLB3zzNBo8Wg=
github.com/deckhouse/sds-node-configurator/api v0.0.0-20240709091744-c9d24f05db41/go.mod h1:H71+9G0Jr46Qs0BA3z3/xt0h9lbnJnCEYcaCJCWFBf0=
github.com/deckhouse/sds-node-configurator/api v0.0.0-20240805103635-969dc811217b h1:EYmHWTWcWMpyxJGZK05ZxlIFnh9s66DRrxLw/LNb/xw=
github.com/deckhouse/sds-node-configurator/api v0.0.0-20240805103635-969dc811217b/go.mod h1:H71+9G0Jr46Qs0BA3z3/xt0h9lbnJnCEYcaCJCWFBf0=
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/evanphx/json-patch v5.6.0+incompatible h1:jBYDEEiFBPxA0v50tFdvOzQQTCvpL6mnFh5mB2/l16U=
Expand Down
191 changes: 187 additions & 4 deletions images/agent/src/pkg/controller/block_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ limitations under the License.
package controller

import (
"agent/config"
"agent/internal"
"agent/pkg/cache"
"agent/pkg/logger"
"agent/pkg/monitoring"
"agent/pkg/utils"
"bytes"
"fmt"
"github.com/deckhouse/sds-node-configurator/api/v1alpha1"
errors2 "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"testing"

"k8s.io/apimachinery/pkg/api/resource"
Expand All @@ -30,6 +37,186 @@ import (
)

func TestBlockDeviceCtrl(t *testing.T) {
log, _ := logger.NewLogger("1")
cfg := config.Options{
NodeName: "test-node",
MachineId: "test-id",
}

t.Run("shouldDeleteBlockDevice", func(t *testing.T) {
t.Run("returns_true", func(t *testing.T) {
bd := v1alpha1.BlockDevice{
Status: v1alpha1.BlockDeviceStatus{
NodeName: cfg.NodeName,
Consumable: true,
},
}
actual := map[string]struct{}{}

assert.True(t, shouldDeleteBlockDevice(bd, actual, cfg.NodeName))
})

t.Run("returns_false_cause_of_dif_node", func(t *testing.T) {
bd := v1alpha1.BlockDevice{
Status: v1alpha1.BlockDeviceStatus{
NodeName: cfg.NodeName,
Consumable: true,
},
}
actual := map[string]struct{}{}

assert.False(t, shouldDeleteBlockDevice(bd, actual, "dif-node"))
})

t.Run("returns_false_cause_of_not_consumable", func(t *testing.T) {
bd := v1alpha1.BlockDevice{
Status: v1alpha1.BlockDeviceStatus{
NodeName: cfg.NodeName,
Consumable: false,
},
}
actual := map[string]struct{}{}

assert.False(t, shouldDeleteBlockDevice(bd, actual, cfg.NodeName))
})

t.Run("returns_false_cause_of_not_deprecated", func(t *testing.T) {
const name = "test"
bd := v1alpha1.BlockDevice{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Status: v1alpha1.BlockDeviceStatus{
NodeName: cfg.NodeName,
Consumable: true,
},
}
actual := map[string]struct{}{
name: {},
}

assert.False(t, shouldDeleteBlockDevice(bd, actual, cfg.NodeName))
})
})

t.Run("RemoveDeprecatedAPIDevices", func(t *testing.T) {
const (
goodName = "test-candidate1"
badName = "test-candidate2"
)
cl := NewFakeClient()
candidates := []internal.BlockDeviceCandidate{
{
NodeName: cfg.NodeName,
Consumable: false,
PVUuid: "142412421",
VGUuid: "123123123",
LvmVolumeGroupName: "test-lvg",
ActualVGNameOnTheNode: "test-vg",
Wwn: "12414212",
Serial: "1412412412412",
Path: "/dev/vdb",
Size: resource.MustParse("1G"),
Rota: false,
Model: "124124-adf",
Name: goodName,
HotPlug: false,
MachineId: "1245151241241",
},
}

bds := map[string]v1alpha1.BlockDevice{
goodName: {
ObjectMeta: metav1.ObjectMeta{
Name: goodName,
},
},
badName: {
ObjectMeta: metav1.ObjectMeta{
Name: badName,
},
Status: v1alpha1.BlockDeviceStatus{
Consumable: true,
NodeName: cfg.NodeName,
},
},
}

for _, bd := range bds {
err := cl.Create(ctx, &bd)
if err != nil {
t.Error(err)
}
}

defer func() {
for _, bd := range bds {
cl.Delete(ctx, &bd)
}
}()

for _, bd := range bds {
createdBd := &v1alpha1.BlockDevice{}
err := cl.Get(ctx, client.ObjectKey{
Name: bd.Name,
}, createdBd)
if err != nil {
t.Error(err)
}
assert.Equal(t, bd.Name, createdBd.Name)
}

RemoveDeprecatedAPIDevices(ctx, cl, *log, monitoring.GetMetrics(cfg.NodeName), candidates, bds, cfg.NodeName)

_, ok := bds[badName]
assert.False(t, ok)

deleted := &v1alpha1.BlockDevice{}
err := cl.Get(ctx, client.ObjectKey{
Name: badName,
}, deleted)
if assert.True(t, errors2.IsNotFound(err)) {
assert.Equal(t, "", deleted.Name)
}
})

t.Run("GetBlockDeviceCandidates", func(t *testing.T) {
devices := []internal.Device{
{
Name: "valid1",
Size: resource.MustParse("1G"),
Serial: "131412",
},
{
Name: "valid2",
Size: resource.MustParse("1G"),
Serial: "12412412",
},
{
Name: "valid3",
Size: resource.MustParse("1G"),
Serial: "4214215",
},
{
Name: "invalid",
FSType: "ext4",
Size: resource.MustParse("1G"),
},
}

sdsCache := cache.New()
sdsCache.StoreDevices(devices, bytes.Buffer{})

candidates := GetBlockDeviceCandidates(*log, cfg, sdsCache)

assert.Equal(t, 3, len(candidates))
for i := range candidates {
assert.Equal(t, devices[i].Name, candidates[i].Path)
assert.Equal(t, cfg.MachineId, candidates[i].MachineId)
assert.Equal(t, cfg.NodeName, candidates[i].NodeName)
}
})

t.Run("CheckConsumable", func(t *testing.T) {
t.Run("Good device returns true", func(t *testing.T) {
goodDevice := internal.Device{
Expand Down Expand Up @@ -205,10 +392,6 @@ func TestBlockDeviceCtrl(t *testing.T) {
})

t.Run("validateTestLSBLKOutput", func(t *testing.T) {
log, err := logger.NewLogger("1")
if err != nil {
t.Fatal(err)
}
testLsblkOutputBytes := []byte(testLsblkOutput)
devices, err := utils.UnmarshalDevices(testLsblkOutputBytes)
if assert.NoError(t, err) {
Expand Down
9 changes: 7 additions & 2 deletions images/agent/src/pkg/controller/controller_reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import (
"agent/pkg/controller"
"agent/pkg/monitoring"
"context"

"github.com/deckhouse/sds-node-configurator/api/v1alpha1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -138,7 +139,11 @@ var _ = Describe("Storage Controller", func() {
})

It("DeleteAPIBlockDevice", func() {
err := controller.DeleteAPIBlockDevice(ctx, cl, testMetrics, deviceName)
err := controller.DeleteAPIBlockDevice(ctx, cl, testMetrics, &v1alpha1.BlockDevice{
ObjectMeta: metav1.ObjectMeta{
Name: deviceName,
},
})
Expect(err).NotTo(HaveOccurred())

devices, err := controller.GetAPIBlockDevices(context.Background(), cl, testMetrics)
Expand Down
16 changes: 10 additions & 6 deletions images/agent/src/pkg/controller/lvm_logical_volume_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
)

var (
lvgName = "hdd-lvg-on-node-0"
poolName = "hhd-thin"
lvCount = 600
size, err = resource.ParseQuantity("1Gi")
lvgName = "hdd-lvg-on-node-0"
poolName = "hhd-thin"
lvCount = 600
size = "1Gi"

resizeOn = false

Expand Down Expand Up @@ -85,7 +85,9 @@ func BenchmarkRunThickLLVCreationSingleThread(b *testing.B) {
continue
}

llv.Spec.Size.Add(add)
lvsize := resource.MustParse(llv.Spec.Size)
lvsize.Add(add)
llv.Spec.Size = lvsize.String()
err = e2eCL.Update(ctx, &llv)
if err != nil {
b.Logf(err.Error())
Expand Down Expand Up @@ -150,7 +152,9 @@ func BenchmarkRunThinLLVCreationSingleThread(b *testing.B) {
continue
}

llv.Spec.Size.Add(add)
lvsize := resource.MustParse(llv.Spec.Size)
lvsize.Add(add)
llv.Spec.Size = lvsize.String()
err = e2eCL.Update(ctx, &llv)
if err != nil {
b.Logf(err.Error())
Expand Down
20 changes: 10 additions & 10 deletions images/agent/src/pkg/controller/lvm_logical_volume_watcher_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ func removeLLVFinalizersIfExist(

if removed {
log.Trace(fmt.Sprintf("[removeLLVFinalizersIfExist] removed finalizer %s from the LVMLogicalVolume %s", internal.SdsNodeConfiguratorFinalizer, llv.Name))
err := updateLVMLogicalVolume(ctx, metrics, cl, llv)
err := updateLVMLogicalVolumeSpec(ctx, metrics, cl, llv)
if err != nil {
log.Error(err, fmt.Sprintf("[updateLVMLogicalVolume] unable to update the LVMVolumeGroup %s", llv.Name))
log.Error(err, fmt.Sprintf("[updateLVMLogicalVolumeSpec] unable to update the LVMVolumeGroup %s", llv.Name))
return err
}
}
Expand Down Expand Up @@ -194,7 +194,7 @@ func addLLVFinalizerIfNotExist(ctx context.Context, cl client.Client, log logger
llv.Finalizers = append(llv.Finalizers, internal.SdsNodeConfiguratorFinalizer)

log.Trace(fmt.Sprintf("[addLLVFinalizerIfNotExist] added finalizer %s to the LVMLogicalVolume %s", internal.SdsNodeConfiguratorFinalizer, llv.Name))
err := updateLVMLogicalVolume(ctx, metrics, cl, llv)
err := updateLVMLogicalVolumeSpec(ctx, metrics, cl, llv)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -300,13 +300,13 @@ func validateLVMLogicalVolume(sdsCache *cache.Cache, llv *v1alpha1.LVMLogicalVol

// if a specified Thick LV name matches the existing Thin one
lv := sdsCache.FindLV(lvg.Spec.ActualVGNameOnTheNode, llv.Spec.ActualLVNameOnTheNode)
if lv != nil && len(lv.LVAttr) == 0 {
reason.WriteString(fmt.Sprintf("LV %s was found on the node, but can't be validated due to its attributes is empty string. ", lv.LVName))
}

if lv != nil {
if !checkIfLVBelongsToLLV(llv, lv) {
reason.WriteString(fmt.Sprintf("Specified LV %s is already created and it is doesnt match the one on the node.", lv.LVName))
if len(lv.LVAttr) == 0 {
reason.WriteString(fmt.Sprintf("LV %s was found on the node, but can't be validated due to its attributes is empty string. ", lv.LVName))
} else {
if !checkIfLVBelongsToLLV(llv, lv) {
reason.WriteString(fmt.Sprintf("Specified LV %s is already created and it is doesnt match the one on the node.", lv.LVName))
}
}
}

Expand Down Expand Up @@ -342,7 +342,7 @@ func updateLVMLogicalVolumePhaseIfNeeded(ctx context.Context, cl client.Client,
return nil
}

func updateLVMLogicalVolume(ctx context.Context, metrics monitoring.Metrics, cl client.Client, llv *v1alpha1.LVMLogicalVolume) error {
func updateLVMLogicalVolumeSpec(ctx context.Context, metrics monitoring.Metrics, cl client.Client, llv *v1alpha1.LVMLogicalVolume) error {
return cl.Update(ctx, llv)
}

Expand Down
Loading

0 comments on commit 6aef89e

Please sign in to comment.