Skip to content

Commit 632ead3

Browse files
chore(hooks): impl discovery clusterip service for dvcr in go (#1005)
Implement discovery clusterip service for dvcr hook in go --------- Signed-off-by: Yaroslav Borbat <[email protected]>
1 parent c0c39f2 commit 632ead3

File tree

5 files changed

+185
-187
lines changed

5 files changed

+185
-187
lines changed

hooks/discovery_clusterip_service_for_dvcr.py

Lines changed: 0 additions & 73 deletions
This file was deleted.

hooks/test_discovery_clusterip_service_for_dvcr.py

Lines changed: 0 additions & 114 deletions
This file was deleted.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Copyright 2025 Flant JSC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"context"
21+
"fmt"
22+
23+
"github.com/deckhouse/module-sdk/pkg"
24+
"github.com/deckhouse/module-sdk/pkg/app"
25+
"github.com/deckhouse/module-sdk/pkg/registry"
26+
"k8s.io/utils/ptr"
27+
28+
"hooks/pkg/common"
29+
)
30+
31+
const (
32+
discoveryService = "discovery-service"
33+
serviceName = "dvcr"
34+
35+
serviceIPValuePath = "virtualization.internal.dvcr.serviceIP"
36+
)
37+
38+
var _ = registry.RegisterFunc(configDiscoveryService, handleDiscoveryService)
39+
40+
var configDiscoveryService = &pkg.HookConfig{
41+
OnBeforeHelm: &pkg.OrderedConfig{Order: 5},
42+
Kubernetes: []pkg.KubernetesConfig{
43+
{
44+
Name: discoveryService,
45+
APIVersion: "v1",
46+
Kind: "Service",
47+
JqFilter: ".spec.clusterIP",
48+
49+
NameSelector: &pkg.NameSelector{
50+
MatchNames: []string{serviceName},
51+
},
52+
53+
NamespaceSelector: &pkg.NamespaceSelector{
54+
NameSelector: &pkg.NameSelector{
55+
MatchNames: []string{common.MODULE_NAMESPACE},
56+
},
57+
},
58+
59+
ExecuteHookOnSynchronization: ptr.To(false),
60+
},
61+
},
62+
63+
Queue: fmt.Sprintf("modules/%s", common.MODULE_NAME),
64+
}
65+
66+
func handleDiscoveryService(_ context.Context, input *pkg.HookInput) error {
67+
clusterIP := getClusterIP(input)
68+
69+
if clusterIP == "" {
70+
input.Logger.Info(fmt.Sprintf("ClusterIP of service dvcr not found. Delete value from %s", serviceIPValuePath))
71+
input.Values.Remove(serviceIPValuePath)
72+
return nil
73+
}
74+
75+
oldClusterIP := input.Values.Get(serviceIPValuePath).String()
76+
if clusterIP != oldClusterIP {
77+
input.Logger.Info(fmt.Sprintf("Set ip %s to %s", clusterIP, serviceIPValuePath))
78+
input.Values.Set(serviceIPValuePath, clusterIP)
79+
}
80+
return nil
81+
}
82+
83+
func getClusterIP(input *pkg.HookInput) string {
84+
snapshots := input.Snapshots.Get(discoveryService)
85+
if len(snapshots) > 0 {
86+
return snapshots[0].String()
87+
}
88+
return ""
89+
}
90+
91+
func main() {
92+
app.Run()
93+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Copyright 2025 Flant JSC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/deckhouse/deckhouse/pkg/log"
24+
"github.com/deckhouse/module-sdk/pkg"
25+
"github.com/deckhouse/module-sdk/testing/mock"
26+
. "github.com/onsi/ginkgo/v2"
27+
. "github.com/onsi/gomega"
28+
"github.com/tidwall/gjson"
29+
)
30+
31+
func TestDiscoveryClusterIPServiceForDVCR(t *testing.T) {
32+
RegisterFailHandler(Fail)
33+
RunSpecs(t, "DiscoveryClusterIPServiceForDVCR Suite")
34+
}
35+
36+
var _ = Describe("DiscoveryClusterIPServiceForDVCR", func() {
37+
var (
38+
dc *mock.DependencyContainerMock
39+
snapshots *mock.SnapshotsMock
40+
values *mock.PatchableValuesCollectorMock
41+
)
42+
43+
BeforeEach(func() {
44+
dc = mock.NewDependencyContainerMock(GinkgoT())
45+
snapshots = mock.NewSnapshotsMock(GinkgoT())
46+
values = mock.NewPatchableValuesCollectorMock(GinkgoT())
47+
})
48+
49+
AfterEach(func() {
50+
dc = nil
51+
snapshots = nil
52+
values = nil
53+
})
54+
55+
setSnapshots := func(snaps ...pkg.Snapshot) {
56+
snapshots.GetMock.When(discoveryService).Then(snaps)
57+
}
58+
59+
newSnapshot := func(clusterIP string) pkg.Snapshot {
60+
return mock.NewSnapshotMock(GinkgoT()).StringMock.Set(func() (s1 string) {
61+
return clusterIP
62+
})
63+
}
64+
65+
newInput := func() *pkg.HookInput {
66+
return &pkg.HookInput{
67+
Snapshots: snapshots,
68+
Values: values,
69+
DC: dc,
70+
Logger: log.NewNop(),
71+
}
72+
}
73+
74+
It("should set serviceIP to values", func() {
75+
setSnapshots(newSnapshot("10.0.0.1"))
76+
values.GetMock.When(serviceIPValuePath).Then(gjson.Result{Type: gjson.String, Str: ""})
77+
values.SetMock.Set(func(path string, v any) {
78+
Expect(path).To(Equal(serviceIPValuePath))
79+
Expect(v).To(Equal("10.0.0.1"))
80+
})
81+
Expect(handleDiscoveryService(context.Background(), newInput())).To(Succeed())
82+
})
83+
84+
It("Should delete serviceIP from values", func() {
85+
setSnapshots(newSnapshot(""))
86+
values.RemoveMock.Set(func(path string) {
87+
Expect(path).To(Equal(serviceIPValuePath))
88+
})
89+
Expect(handleDiscoveryService(context.Background(), newInput())).To(Succeed())
90+
})
91+
})

images/hooks/werf.inc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ shell:
3333
{{- end }}
3434
- go build -ldflags="-s -w" -o /hooks/prevent-default-vmclasses-deletion ./cmd/prevent-default-vmclasses-deletion
3535
- go build -ldflags="-s -w" -o /hooks/generate-secret-for-dvcr ./cmd/generate-secret-for-dvcr
36+
- go build -ldflags="-s -w" -o /hooks/discovery-clusterip-service-for-dvcr ./cmd/discovery-clusterip-service-for-dvcr

0 commit comments

Comments
 (0)