diff --git a/images/hooks/cmd/discovery-workload-nodes/main.go b/images/hooks/cmd/discovery-virthandler-nodes/main.go similarity index 75% rename from images/hooks/cmd/discovery-workload-nodes/main.go rename to images/hooks/cmd/discovery-virthandler-nodes/main.go index 928c0f9f81..8f845c6de3 100644 --- a/images/hooks/cmd/discovery-workload-nodes/main.go +++ b/images/hooks/cmd/discovery-virthandler-nodes/main.go @@ -31,26 +31,27 @@ import ( ) const ( - discoveryNodesSnapshot = "discovery-nodes" - nodeLabel = "kubevirt.internal.virtualization.deckhouse.io/schedulable" - nodeLabelValue = "true" + nodesSnapshot = "discovery-nodes" + virtHandlerLabel = "kubevirt.internal.virtualization.deckhouse.io/schedulable" + virtHandlerLabelValue = "true" + nodeJQFilter = ".metadata.name" virtHandlerNodeCountPath = "virtualization.internal.virtHandler.nodeCount" ) -var _ = registry.RegisterFunc(configDiscoveryService, handleDiscoveryNodes) +var _ = registry.RegisterFunc(configDiscoveryService, handleDiscoveryVirtHandlerNodes) var configDiscoveryService = &pkg.HookConfig{ OnBeforeHelm: &pkg.OrderedConfig{Order: 5}, Kubernetes: []pkg.KubernetesConfig{ { - Name: discoveryNodesSnapshot, + Name: nodesSnapshot, APIVersion: "v1", Kind: "Node", - JqFilter: ".metadata.name", + JqFilter: nodeJQFilter, LabelSelector: &metav1.LabelSelector{ MatchLabels: map[string]string{ - nodeLabel: nodeLabelValue, + virtHandlerLabel: virtHandlerLabelValue, }, }, ExecuteHookOnSynchronization: ptr.To(false), @@ -60,8 +61,8 @@ var configDiscoveryService = &pkg.HookConfig{ Queue: fmt.Sprintf("modules/%s", common.MODULE_NAME), } -func handleDiscoveryNodes(_ context.Context, input *pkg.HookInput) error { - nodeCount := len(input.Snapshots.Get(discoveryNodesSnapshot)) +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 new file mode 100644 index 0000000000..48a03754d1 --- /dev/null +++ b/images/hooks/cmd/discovery-virthandler-nodes/main_test.go @@ -0,0 +1,78 @@ +/* +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" + "testing" + + "github.com/deckhouse/module-sdk/pkg" + "github.com/deckhouse/module-sdk/testing/mock" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestDiscoveryVirthandlerNodes(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Discovery virt-handler nodes Suite") +} + +var _ = Describe("Discovery virt-handler nodes", func() { + var ( + snapshots *mock.SnapshotsMock + values *mock.PatchableValuesCollectorMock + input *pkg.HookInput + ) + + BeforeEach(func() { + snapshots = mock.NewSnapshotsMock(GinkgoT()) + values = mock.NewPatchableValuesCollectorMock(GinkgoT()) + + input = &pkg.HookInput{ + Values: values, + Snapshots: snapshots, + } + }) + + Context("Empty cluster", func() { + It("Hook must execute successfully", func() { + snapshots.GetMock.When(nodesSnapshot).Then( + []pkg.Snapshot{}, + ) + values.SetMock.When(virtHandlerNodeCountPath, 0) + err := handleDiscoveryVirtHandlerNodes(context.Background(), input) + Expect(err).ShouldNot(HaveOccurred()) + }) + }) + + Context("Two nodes should be discovered.", func() { + It("Hook must execute successfully", func() { + + snapshots.GetMock.When(nodesSnapshot).Then( + []pkg.Snapshot{ + mock.NewSnapshotMock(GinkgoT()).StringMock.Return("n1"), + mock.NewSnapshotMock(GinkgoT()).StringMock.Return("n2"), + }, + ) + + values.SetMock.When(virtHandlerNodeCountPath, 2) + err := handleDiscoveryVirtHandlerNodes(context.Background(), input) + Expect(err).ShouldNot(HaveOccurred()) + }) + }) + +}) diff --git a/images/hooks/go.mod b/images/hooks/go.mod index ba3d8425a9..dbdb04cb05 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 @@ -60,6 +61,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 +96,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 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