From f202a503ffa57eeab7390591f7cfa520461132de Mon Sep 17 00:00:00 2001 From: Pavel Tishkov Date: Thu, 12 Jun 2025 11:26:03 +0300 Subject: [PATCH 1/4] test(module): add test for discovery nodes hook Signed-off-by: Pavel Tishkov --- .../cmd/discovery-virthandler-nodes/main.go | 75 +++++++++ .../discovery-virthandler-nodes/main_test.go | 157 ++++++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 images/hooks/cmd/discovery-virthandler-nodes/main.go create mode 100644 images/hooks/cmd/discovery-virthandler-nodes/main_test.go diff --git a/images/hooks/cmd/discovery-virthandler-nodes/main.go b/images/hooks/cmd/discovery-virthandler-nodes/main.go new file mode 100644 index 0000000000..cde452243f --- /dev/null +++ b/images/hooks/cmd/discovery-virthandler-nodes/main.go @@ -0,0 +1,75 @@ +/* +Copyright 2025 Flant JSC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "context" + "fmt" + + "github.com/deckhouse/module-sdk/pkg" + "github.com/deckhouse/module-sdk/pkg/app" + "github.com/deckhouse/module-sdk/pkg/registry" + "k8s.io/utils/ptr" + + "hooks/pkg/common" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +const ( + nodesSnapshot = "discovery-nodes" + virtHandlerLabel = "kubevirt.internal.virtualization.deckhouse.io/schedulable" + virtHandlerLabelValue = "true" + nodeJQFilter = ".metadata.name" + + virtHandlerNodeCountPath = "virtualization.internal.virtHandler.nodeCount" +) + +var _ = registry.RegisterFunc(configDiscoveryService, handleDiscoveryVirtHandkerNodes) + +var configDiscoveryService = &pkg.HookConfig{ + OnBeforeHelm: &pkg.OrderedConfig{Order: 5}, + Kubernetes: []pkg.KubernetesConfig{ + { + Name: nodesSnapshot, + APIVersion: "v1", + Kind: "Node", + JqFilter: nodeJQFilter, + LabelSelector: &metav1.LabelSelector{ + MatchLabels: map[string]string{ + virtHandlerLabel: virtHandlerLabelValue, + }, + }, + ExecuteHookOnSynchronization: ptr.To(false), + }, + }, + + Queue: fmt.Sprintf("modules/%s", common.MODULE_NAME), +} + +func handleDiscoveryVirtHandkerNodes(_ context.Context, input *pkg.HookInput) error { + nodeCount := len(input.Snapshots.Get(nodesSnapshot)) + if nodeCount == 0 { + nodeCount = 1 + } + input.Values.Set(virtHandlerNodeCountPath, nodeCount) + return nil +} + +func main() { + app.Run() +} diff --git a/images/hooks/cmd/discovery-virthandler-nodes/main_test.go b/images/hooks/cmd/discovery-virthandler-nodes/main_test.go new file mode 100644 index 0000000000..6904a124dd --- /dev/null +++ b/images/hooks/cmd/discovery-virthandler-nodes/main_test.go @@ -0,0 +1,157 @@ +/* +Copyright 2024 Flant JSC +Licensed under the Deckhouse Platform Enterprise Edition (EE) license. See https://github.com/deckhouse/deckhouse/blob/main/ee/LICENSE +*/ + +package main + +import ( + "bytes" + "context" + "encoding/json" + "os" + "testing" + "time" + + "github.com/deckhouse/deckhouse/pkg/log" + "github.com/deckhouse/module-sdk/pkg" + "github.com/deckhouse/module-sdk/pkg/jq" + "github.com/deckhouse/module-sdk/testing/mock" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/yaml" +) + +func TestDiscoveryVirthandlerNodes(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Discovery virt-handler nodes Suite") +} + +var _ = Describe("Discovery virt-handler nodes", func() { + err := os.Setenv("D8_IS_TESTS_ENVIRONMENT", "true") + Expect(err).ShouldNot(HaveOccurred()) + + const ( + node1YAML = ` +--- +apiVersion: v1 +kind: Node +metadata: + labels: + kubevirt.internal.virtualization.deckhouse.io/schedulable: "true" + name: node1 +` + + node2YAML = ` +--- +apiVersion: v1 +kind: Node +metadata: + labels: + kubevirt.internal.virtualization.deckhouse.io/schedulable: "true" + name: node2 +` + ) + + var ( + snapshots *mock.SnapshotsMock + values *mock.PatchableValuesCollectorMock + patchCollector *mock.PatchCollectorMock + input *pkg.HookInput + buf *bytes.Buffer + ) + + filterResultNode1, err := nodeYamlToSnapshot(node1YAML) + if err != nil { + Expect(err).ShouldNot(HaveOccurred()) + } + + filterResultNode2, err := nodeYamlToSnapshot(node2YAML) + if err != nil { + Expect(err).ShouldNot(HaveOccurred()) + } + + BeforeEach(func() { + snapshots = mock.NewSnapshotsMock(GinkgoT()) + values = mock.NewPatchableValuesCollectorMock(GinkgoT()) + patchCollector = mock.NewPatchCollectorMock(GinkgoT()) + + buf = bytes.NewBuffer([]byte{}) + + input = &pkg.HookInput{ + Values: values, + Snapshots: snapshots, + Logger: log.NewLogger(log.Options{ + Level: log.LevelDebug.Level(), + Output: buf, + TimeFunc: func(_ time.Time) time.Time { + parsedTime, err := time.Parse(time.DateTime, "2006-01-02 15:04:05") + Expect(err).ShouldNot(HaveOccurred()) + return parsedTime + }, + }), + PatchCollector: patchCollector, + } + }) + + Context("Empty cluster", func() { + It("Hook must execute successfully", func() { + snapshots.GetMock.When(nodesSnapshot).Then( + []pkg.Snapshot{}, + ) + values.SetMock.When(virtHandlerNodeCountPath, 1) + err := handleDiscoveryVirtHandkerNodes(context.Background(), input) + Expect(err).ShouldNot(HaveOccurred()) + }) + }) + + Context("Four nodes but only two should be patched.", func() { + It("Hook must execute successfully", func() { + + snapshots.GetMock.When(nodesSnapshot).Then( + []pkg.Snapshot{ + mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(getNodeSnapshot(filterResultNode1)), + mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(getNodeSnapshot(filterResultNode2)), + }, + ) + + values.SetMock.When(virtHandlerNodeCountPath, 2) + err := handleDiscoveryVirtHandkerNodes(context.Background(), input) + Expect(err).ShouldNot(HaveOccurred()) + }) + }) + +}) + +func nodeYamlToSnapshot(manifest string) (string, error) { + node := new(v1.Node) + err := yaml.Unmarshal([]byte(manifest), node) + if err != nil { + return "", err + } + + query, err := jq.NewQuery(nodeJQFilter) + if err != nil { + return "", err + } + + filterResult, err := query.FilterObject(context.Background(), node) + if err != nil { + return "", err + } + + return filterResult.String(), nil +} + +func getNodeSnapshot(nodeManifest string) func(v any) (err error) { + return func(v any) (err error) { + rt := v.(*metav1.ObjectMeta) + if err := json.Unmarshal([]byte(nodeManifest), rt); err != nil { + return err + } + + return nil + } +} From bc07eddaf4397a838a6e927f738087b82b53abd4 Mon Sep 17 00:00:00 2001 From: Pavel Tishkov Date: Thu, 12 Jun 2025 11:27:17 +0300 Subject: [PATCH 2/4] test(module): add test for discovery nodes hook Signed-off-by: Pavel Tishkov --- .../discovery-virthandler-nodes/main_test.go | 15 +++- .../cmd/discovery-workload-nodes/main.go | 71 ------------------- images/hooks/go.mod | 7 +- images/hooks/go.sum | 3 + images/hooks/werf.inc.yaml | 2 +- 5 files changed, 23 insertions(+), 75 deletions(-) delete mode 100644 images/hooks/cmd/discovery-workload-nodes/main.go diff --git a/images/hooks/cmd/discovery-virthandler-nodes/main_test.go b/images/hooks/cmd/discovery-virthandler-nodes/main_test.go index 6904a124dd..0c253ca97c 100644 --- a/images/hooks/cmd/discovery-virthandler-nodes/main_test.go +++ b/images/hooks/cmd/discovery-virthandler-nodes/main_test.go @@ -1,6 +1,17 @@ /* -Copyright 2024 Flant JSC -Licensed under the Deckhouse Platform Enterprise Edition (EE) license. See https://github.com/deckhouse/deckhouse/blob/main/ee/LICENSE +Copyright 2025 Flant JSC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ package main diff --git a/images/hooks/cmd/discovery-workload-nodes/main.go b/images/hooks/cmd/discovery-workload-nodes/main.go deleted file mode 100644 index 928c0f9f81..0000000000 --- a/images/hooks/cmd/discovery-workload-nodes/main.go +++ /dev/null @@ -1,71 +0,0 @@ -/* -Copyright 2025 Flant JSC - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "context" - "fmt" - - "github.com/deckhouse/module-sdk/pkg" - "github.com/deckhouse/module-sdk/pkg/app" - "github.com/deckhouse/module-sdk/pkg/registry" - "k8s.io/utils/ptr" - - "hooks/pkg/common" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -const ( - discoveryNodesSnapshot = "discovery-nodes" - nodeLabel = "kubevirt.internal.virtualization.deckhouse.io/schedulable" - nodeLabelValue = "true" - - virtHandlerNodeCountPath = "virtualization.internal.virtHandler.nodeCount" -) - -var _ = registry.RegisterFunc(configDiscoveryService, handleDiscoveryNodes) - -var configDiscoveryService = &pkg.HookConfig{ - OnBeforeHelm: &pkg.OrderedConfig{Order: 5}, - Kubernetes: []pkg.KubernetesConfig{ - { - Name: discoveryNodesSnapshot, - APIVersion: "v1", - Kind: "Node", - JqFilter: ".metadata.name", - LabelSelector: &metav1.LabelSelector{ - MatchLabels: map[string]string{ - nodeLabel: nodeLabelValue, - }, - }, - ExecuteHookOnSynchronization: ptr.To(false), - }, - }, - - Queue: fmt.Sprintf("modules/%s", common.MODULE_NAME), -} - -func handleDiscoveryNodes(_ context.Context, input *pkg.HookInput) error { - nodeCount := len(input.Snapshots.Get(discoveryNodesSnapshot)) - input.Values.Set(virtHandlerNodeCountPath, nodeCount) - return nil -} - -func main() { - app.Run() -} diff --git a/images/hooks/go.mod b/images/hooks/go.mod index ba3d8425a9..81e36babe8 100644 --- a/images/hooks/go.mod +++ b/images/hooks/go.mod @@ -8,6 +8,7 @@ require ( github.com/deckhouse/deckhouse/pkg/log v0.0.0-20250424095005-9ab587d01d7a github.com/deckhouse/module-sdk v0.2.2 github.com/deckhouse/virtualization/api v0.15.0 + github.com/onsi/ginkgo v1.16.4 github.com/onsi/ginkgo/v2 v2.17.1 github.com/onsi/gomega v1.32.0 github.com/tidwall/gjson v1.14.4 @@ -15,6 +16,7 @@ require ( k8s.io/api v0.30.11 k8s.io/apimachinery v0.30.11 k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 + sigs.k8s.io/yaml v1.4.0 ) require ( @@ -50,6 +52,8 @@ require ( github.com/google/uuid v1.6.0 // indirect github.com/imdario/mergo v0.3.16 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/itchyny/gojq v0.12.17 // indirect + github.com/itchyny/timefmt-go v0.1.6 // indirect github.com/jmoiron/sqlx v1.3.5 // indirect github.com/jonboulle/clockwork v0.4.0 // indirect github.com/josharian/intern v1.0.0 // indirect @@ -60,6 +64,7 @@ require ( 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/nxadm/tail v1.4.8 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.1.0 // indirect github.com/openshift/api v0.0.0-20230503133300-8bbcb7ca7183 // indirect @@ -94,6 +99,7 @@ require ( gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect google.golang.org/protobuf v1.35.1 // indirect gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/apiextensions-apiserver v0.30.11 // indirect @@ -106,7 +112,6 @@ require ( sigs.k8s.io/controller-runtime v0.18.7 // indirect sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect - sigs.k8s.io/yaml v1.4.0 // indirect ) replace golang.org/x/net => golang.org/x/net v0.40.0 // CVE-2025-22870, CVE-2025-22872 diff --git a/images/hooks/go.sum b/images/hooks/go.sum index 12b10b7d21..dac713fe3f 100644 --- a/images/hooks/go.sum +++ b/images/hooks/go.sum @@ -204,11 +204,13 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8m github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= +github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc= github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0= github.com/onsi/ginkgo/v2 v2.0.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c= github.com/onsi/ginkgo/v2 v2.17.1 h1:V++EzdbhI4ZV4ev0UTIj0PzhzOcReJFyJaLjtSF55M8= @@ -468,6 +470,7 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/images/hooks/werf.inc.yaml b/images/hooks/werf.inc.yaml index 5d13dd4b33..ab12b8e87c 100644 --- a/images/hooks/werf.inc.yaml +++ b/images/hooks/werf.inc.yaml @@ -34,4 +34,4 @@ shell: - go build -ldflags="-s -w" -o /hooks/prevent-default-vmclasses-deletion ./cmd/prevent-default-vmclasses-deletion - go build -ldflags="-s -w" -o /hooks/generate-secret-for-dvcr ./cmd/generate-secret-for-dvcr - go build -ldflags="-s -w" -o /hooks/discovery-clusterip-service-for-dvcr ./cmd/discovery-clusterip-service-for-dvcr - - go build -ldflags="-s -w" -o /hooks/discovery-workload-nodes ./cmd/discovery-workload-nodes + - go build -ldflags="-s -w" -o /hooks/discovery-virthandler-nodes ./cmd/discovery-virthandler-nodes From 612fc1c6db336d496b1bf6232d84d52053d18968 Mon Sep 17 00:00:00 2001 From: Pavel Tishkov Date: Fri, 13 Jun 2025 17:09:14 +0300 Subject: [PATCH 3/4] fix tests Signed-off-by: Pavel Tishkov --- .../cmd/discovery-virthandler-nodes/main.go | 3 - .../discovery-virthandler-nodes/main_test.go | 76 +------------------ images/hooks/go.mod | 4 +- 3 files changed, 5 insertions(+), 78 deletions(-) diff --git a/images/hooks/cmd/discovery-virthandler-nodes/main.go b/images/hooks/cmd/discovery-virthandler-nodes/main.go index cde452243f..8900027953 100644 --- a/images/hooks/cmd/discovery-virthandler-nodes/main.go +++ b/images/hooks/cmd/discovery-virthandler-nodes/main.go @@ -63,9 +63,6 @@ var configDiscoveryService = &pkg.HookConfig{ func handleDiscoveryVirtHandkerNodes(_ context.Context, input *pkg.HookInput) error { nodeCount := len(input.Snapshots.Get(nodesSnapshot)) - if nodeCount == 0 { - nodeCount = 1 - } input.Values.Set(virtHandlerNodeCountPath, nodeCount) return nil } diff --git a/images/hooks/cmd/discovery-virthandler-nodes/main_test.go b/images/hooks/cmd/discovery-virthandler-nodes/main_test.go index 0c253ca97c..e00466d245 100644 --- a/images/hooks/cmd/discovery-virthandler-nodes/main_test.go +++ b/images/hooks/cmd/discovery-virthandler-nodes/main_test.go @@ -19,20 +19,15 @@ package main import ( "bytes" "context" - "encoding/json" "os" "testing" "time" "github.com/deckhouse/deckhouse/pkg/log" "github.com/deckhouse/module-sdk/pkg" - "github.com/deckhouse/module-sdk/pkg/jq" "github.com/deckhouse/module-sdk/testing/mock" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - v1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/yaml" ) func TestDiscoveryVirthandlerNodes(t *testing.T) { @@ -44,28 +39,6 @@ var _ = Describe("Discovery virt-handler nodes", func() { err := os.Setenv("D8_IS_TESTS_ENVIRONMENT", "true") Expect(err).ShouldNot(HaveOccurred()) - const ( - node1YAML = ` ---- -apiVersion: v1 -kind: Node -metadata: - labels: - kubevirt.internal.virtualization.deckhouse.io/schedulable: "true" - name: node1 -` - - node2YAML = ` ---- -apiVersion: v1 -kind: Node -metadata: - labels: - kubevirt.internal.virtualization.deckhouse.io/schedulable: "true" - name: node2 -` - ) - var ( snapshots *mock.SnapshotsMock values *mock.PatchableValuesCollectorMock @@ -74,16 +47,6 @@ metadata: buf *bytes.Buffer ) - filterResultNode1, err := nodeYamlToSnapshot(node1YAML) - if err != nil { - Expect(err).ShouldNot(HaveOccurred()) - } - - filterResultNode2, err := nodeYamlToSnapshot(node2YAML) - if err != nil { - Expect(err).ShouldNot(HaveOccurred()) - } - BeforeEach(func() { snapshots = mock.NewSnapshotsMock(GinkgoT()) values = mock.NewPatchableValuesCollectorMock(GinkgoT()) @@ -112,19 +75,19 @@ metadata: snapshots.GetMock.When(nodesSnapshot).Then( []pkg.Snapshot{}, ) - values.SetMock.When(virtHandlerNodeCountPath, 1) + values.SetMock.When(virtHandlerNodeCountPath, 0) err := handleDiscoveryVirtHandkerNodes(context.Background(), input) Expect(err).ShouldNot(HaveOccurred()) }) }) - Context("Four nodes but only two should be patched.", func() { + Context("Two nodes should be discovered.", func() { It("Hook must execute successfully", func() { snapshots.GetMock.When(nodesSnapshot).Then( []pkg.Snapshot{ - mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(getNodeSnapshot(filterResultNode1)), - mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(getNodeSnapshot(filterResultNode2)), + mock.NewSnapshotMock(GinkgoT()).StringMock.Return("n1"), + mock.NewSnapshotMock(GinkgoT()).StringMock.Return("n2"), }, ) @@ -135,34 +98,3 @@ metadata: }) }) - -func nodeYamlToSnapshot(manifest string) (string, error) { - node := new(v1.Node) - err := yaml.Unmarshal([]byte(manifest), node) - if err != nil { - return "", err - } - - query, err := jq.NewQuery(nodeJQFilter) - if err != nil { - return "", err - } - - filterResult, err := query.FilterObject(context.Background(), node) - if err != nil { - return "", err - } - - return filterResult.String(), nil -} - -func getNodeSnapshot(nodeManifest string) func(v any) (err error) { - return func(v any) (err error) { - rt := v.(*metav1.ObjectMeta) - if err := json.Unmarshal([]byte(nodeManifest), rt); err != nil { - return err - } - - return nil - } -} diff --git a/images/hooks/go.mod b/images/hooks/go.mod index 81e36babe8..dbdb04cb05 100644 --- a/images/hooks/go.mod +++ b/images/hooks/go.mod @@ -16,7 +16,6 @@ require ( k8s.io/api v0.30.11 k8s.io/apimachinery v0.30.11 k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 - sigs.k8s.io/yaml v1.4.0 ) require ( @@ -52,8 +51,6 @@ require ( github.com/google/uuid v1.6.0 // indirect github.com/imdario/mergo v0.3.16 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/itchyny/gojq v0.12.17 // indirect - github.com/itchyny/timefmt-go v0.1.6 // indirect github.com/jmoiron/sqlx v1.3.5 // indirect github.com/jonboulle/clockwork v0.4.0 // indirect github.com/josharian/intern v1.0.0 // indirect @@ -112,6 +109,7 @@ require ( sigs.k8s.io/controller-runtime v0.18.7 // indirect sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect + sigs.k8s.io/yaml v1.4.0 // indirect ) replace golang.org/x/net => golang.org/x/net v0.40.0 // CVE-2025-22870, CVE-2025-22872 From 6f21a2b3bb59ea4a5cb4891309e4bb99c6e3891f Mon Sep 17 00:00:00 2001 From: Pavel Tishkov Date: Sat, 14 Jun 2025 16:38:54 +0300 Subject: [PATCH 4/4] refactor Signed-off-by: Pavel Tishkov --- .../cmd/discovery-virthandler-nodes/main.go | 4 +-- .../discovery-virthandler-nodes/main_test.go | 34 ++++--------------- 2 files changed, 8 insertions(+), 30 deletions(-) diff --git a/images/hooks/cmd/discovery-virthandler-nodes/main.go b/images/hooks/cmd/discovery-virthandler-nodes/main.go index 8900027953..8f845c6de3 100644 --- a/images/hooks/cmd/discovery-virthandler-nodes/main.go +++ b/images/hooks/cmd/discovery-virthandler-nodes/main.go @@ -39,7 +39,7 @@ const ( virtHandlerNodeCountPath = "virtualization.internal.virtHandler.nodeCount" ) -var _ = registry.RegisterFunc(configDiscoveryService, handleDiscoveryVirtHandkerNodes) +var _ = registry.RegisterFunc(configDiscoveryService, handleDiscoveryVirtHandlerNodes) var configDiscoveryService = &pkg.HookConfig{ OnBeforeHelm: &pkg.OrderedConfig{Order: 5}, @@ -61,7 +61,7 @@ var configDiscoveryService = &pkg.HookConfig{ Queue: fmt.Sprintf("modules/%s", common.MODULE_NAME), } -func handleDiscoveryVirtHandkerNodes(_ context.Context, input *pkg.HookInput) error { +func handleDiscoveryVirtHandlerNodes(_ context.Context, input *pkg.HookInput) error { nodeCount := len(input.Snapshots.Get(nodesSnapshot)) input.Values.Set(virtHandlerNodeCountPath, nodeCount) return nil diff --git a/images/hooks/cmd/discovery-virthandler-nodes/main_test.go b/images/hooks/cmd/discovery-virthandler-nodes/main_test.go index e00466d245..48a03754d1 100644 --- a/images/hooks/cmd/discovery-virthandler-nodes/main_test.go +++ b/images/hooks/cmd/discovery-virthandler-nodes/main_test.go @@ -17,16 +17,12 @@ limitations under the License. package main import ( - "bytes" "context" - "os" "testing" - "time" - "github.com/deckhouse/deckhouse/pkg/log" "github.com/deckhouse/module-sdk/pkg" "github.com/deckhouse/module-sdk/testing/mock" - . "github.com/onsi/ginkgo" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" ) @@ -36,37 +32,19 @@ func TestDiscoveryVirthandlerNodes(t *testing.T) { } var _ = Describe("Discovery virt-handler nodes", func() { - err := os.Setenv("D8_IS_TESTS_ENVIRONMENT", "true") - Expect(err).ShouldNot(HaveOccurred()) - var ( - snapshots *mock.SnapshotsMock - values *mock.PatchableValuesCollectorMock - patchCollector *mock.PatchCollectorMock - input *pkg.HookInput - buf *bytes.Buffer + snapshots *mock.SnapshotsMock + values *mock.PatchableValuesCollectorMock + input *pkg.HookInput ) BeforeEach(func() { snapshots = mock.NewSnapshotsMock(GinkgoT()) values = mock.NewPatchableValuesCollectorMock(GinkgoT()) - patchCollector = mock.NewPatchCollectorMock(GinkgoT()) - - buf = bytes.NewBuffer([]byte{}) input = &pkg.HookInput{ Values: values, Snapshots: snapshots, - Logger: log.NewLogger(log.Options{ - Level: log.LevelDebug.Level(), - Output: buf, - TimeFunc: func(_ time.Time) time.Time { - parsedTime, err := time.Parse(time.DateTime, "2006-01-02 15:04:05") - Expect(err).ShouldNot(HaveOccurred()) - return parsedTime - }, - }), - PatchCollector: patchCollector, } }) @@ -76,7 +54,7 @@ var _ = Describe("Discovery virt-handler nodes", func() { []pkg.Snapshot{}, ) values.SetMock.When(virtHandlerNodeCountPath, 0) - err := handleDiscoveryVirtHandkerNodes(context.Background(), input) + err := handleDiscoveryVirtHandlerNodes(context.Background(), input) Expect(err).ShouldNot(HaveOccurred()) }) }) @@ -92,7 +70,7 @@ var _ = Describe("Discovery virt-handler nodes", func() { ) values.SetMock.When(virtHandlerNodeCountPath, 2) - err := handleDiscoveryVirtHandkerNodes(context.Background(), input) + err := handleDiscoveryVirtHandlerNodes(context.Background(), input) Expect(err).ShouldNot(HaveOccurred()) }) })