|
| 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 | + input.Logger.Info("Start.") |
| 86 | + nodeMetadatas := input.Snapshots.Get(nodesMetadataSnapshot) |
| 87 | + if len(nodeMetadatas) == 0 { |
| 88 | + return nil |
| 89 | + } |
| 90 | + input.Logger.Info("Found %d nodes.", len(nodeMetadatas)) |
| 91 | + |
| 92 | + for _, nodeMetadata := range nodeMetadatas { |
| 93 | + metadata := &metav1.ObjectMeta{} |
| 94 | + if err := nodeMetadata.UnmarshalTo(metadata); err != nil { |
| 95 | + input.Logger.Error(fmt.Sprintf("Failed to unmarshal node metadata %v", err)) |
| 96 | + continue |
| 97 | + } |
| 98 | + |
| 99 | + patches := []map[string]interface{}{} |
| 100 | + |
| 101 | + for key, _ := range metadata.Labels { |
| 102 | + if strings.Contains(key, "virtualization.deckhouse.io") { |
| 103 | + patches = append(patches, map[string]interface{}{ |
| 104 | + "op": "remove", |
| 105 | + "path": fmt.Sprintf("/metadata/labels/%s", jsonPatchEscape(key)), |
| 106 | + }) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if len(patches) == 0 { |
| 111 | + input.Logger.Info("No labels found, nothing to do.") |
| 112 | + continue |
| 113 | + } else { |
| 114 | + input.Logger.Info(fmt.Sprintf("Removing %d labels from node %s", len(patches), metadata.Name)) |
| 115 | + } |
| 116 | + |
| 117 | + input.PatchCollector.PatchWithJSON(patches, "v1", "Node", "", metadata.Name) |
| 118 | + } |
| 119 | + input.Logger.Info("Done.") |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func jsonPatchEscape(s string) string { |
| 124 | + s = strings.ReplaceAll(s, "~", "~0") |
| 125 | + s = strings.ReplaceAll(s, "/", "~1") |
| 126 | + return s |
| 127 | +} |
| 128 | + |
| 129 | +func main() { |
| 130 | + app.Run() |
| 131 | +} |
0 commit comments