Skip to content

Commit b0066d8

Browse files
committed
test(module): add cleanup node labels hook
Signed-off-by: Pavel Tishkov <[email protected]>
1 parent 4e77008 commit b0066d8

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
// The purpose of this hook is to prevent already launched virt-handler pods from flapping, since the node group configuration virtualization-detect-kvm.sh will be responsible for installing the label virtualization.deckhouse.io/kvm-enabled.
18+
19+
package main
20+
21+
import (
22+
"context"
23+
"fmt"
24+
"strings"
25+
26+
"github.com/deckhouse/module-sdk/pkg"
27+
"github.com/deckhouse/module-sdk/pkg/app"
28+
"github.com/deckhouse/module-sdk/pkg/registry"
29+
"k8s.io/utils/ptr"
30+
31+
"hooks/pkg/common"
32+
33+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34+
)
35+
36+
const (
37+
nodesMetadataSnapshot = "virthandler-nodes"
38+
virtHandlerLabel = "kubevirt.internal.virtualization.deckhouse.io/schedulable"
39+
nodeJQFilter = ".metadata"
40+
)
41+
42+
var (
43+
labelPrefixesToRemove = []string{
44+
"cpu-feature.node.virtualization.deckhouse.io",
45+
"cpu-model-migration.node.virtualization.deckhouse.io",
46+
"cpu-model.node.virtualization.deckhouse.io",
47+
"cpu-timer.node.virtualization.deckhouse.io",
48+
"cpu-vendor.node.virtualization.deckhouse.io",
49+
"host-model-cpu.node.virtualization.deckhouse.io",
50+
"host-model-required-features.node.virtualization.deckhouse.io",
51+
"hyperv.node.virtualization.deckhouse.io",
52+
"kubevirt.internal.virtualization.deckhouse.io",
53+
"scheduling.node.virtualization.deckhouse.io",
54+
"virtualization.deckhouse.io",
55+
}
56+
)
57+
58+
var _ = registry.RegisterFunc(configDiscoveryService, handleVirtHandlerNodes)
59+
60+
var configDiscoveryService = &pkg.HookConfig{
61+
OnAfterDeleteHelm: &pkg.OrderedConfig{Order: 1},
62+
Kubernetes: []pkg.KubernetesConfig{
63+
{
64+
Name: nodesMetadataSnapshot,
65+
APIVersion: "v1",
66+
Kind: "Node",
67+
JqFilter: nodeJQFilter,
68+
LabelSelector: &metav1.LabelSelector{
69+
MatchExpressions: []metav1.LabelSelectorRequirement{
70+
{
71+
Key: virtHandlerLabel,
72+
Operator: metav1.LabelSelectorOpExists,
73+
},
74+
},
75+
},
76+
ExecuteHookOnSynchronization: ptr.To(false),
77+
ExecuteHookOnEvents: ptr.To(false),
78+
},
79+
},
80+
81+
Queue: fmt.Sprintf("modules/%s", common.MODULE_NAME),
82+
}
83+
84+
func handleVirtHandlerNodes(_ context.Context, input *pkg.HookInput) error {
85+
nodeMetadatas := input.Snapshots.Get(nodesMetadataSnapshot)
86+
if len(nodeMetadatas) == 0 {
87+
return nil
88+
}
89+
90+
for _, nodeMetadata := range nodeMetadatas {
91+
metadata := &metav1.ObjectMeta{}
92+
if err := nodeMetadata.UnmarshalTo(metadata); err != nil {
93+
input.Logger.Error(fmt.Sprintf("Failed to unmarshal node metadata %v", err))
94+
continue
95+
}
96+
97+
patches := []map[string]interface{}{}
98+
99+
for key, _ := range metadata.Labels {
100+
if strings.Contains(key, "virtualization.deckhouse.io") {
101+
patches = append(patches, map[string]interface{}{
102+
"op": "remove",
103+
"path": fmt.Sprintf("/metadata/labels/%s", jsonPatchEscape(key)),
104+
})
105+
}
106+
}
107+
108+
if len(patches) == 0 {
109+
continue
110+
}
111+
112+
input.PatchCollector.PatchWithJSON(patches, "v1", "Node", "", metadata.Name)
113+
}
114+
115+
return nil
116+
}
117+
118+
func jsonPatchEscape(s string) string {
119+
s = strings.ReplaceAll(s, "~", "~0")
120+
s = strings.ReplaceAll(s, "/", "~1")
121+
return s
122+
}
123+
124+
func main() {
125+
app.Run()
126+
}

images/hooks/werf.inc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ shell:
3535
- go build -ldflags="-s -w" -o /hooks/generate-secret-for-dvcr ./cmd/generate-secret-for-dvcr
3636
- go build -ldflags="-s -w" -o /hooks/discovery-clusterip-service-for-dvcr ./cmd/discovery-clusterip-service-for-dvcr
3737
- go build -ldflags="-s -w" -o /hooks/discovery-workload-nodes ./cmd/discovery-workload-nodes
38+
- go build -ldflags="-s -w" -o /hooks/cleanup-labels ./cmd/cleanup-labels

0 commit comments

Comments
 (0)