-
Notifications
You must be signed in to change notification settings - Fork 156
/
kubevirt.go
81 lines (72 loc) · 2.7 KB
/
kubevirt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2023 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package base
import (
"os"
"regexp"
"strings"
uuid "github.com/satori/go.uuid"
)
const (
// EveVirtTypeFile contains the virtualization type, ie kvm, xen or kubevirt
EveVirtTypeFile = "/run/eve-hv-type"
// KubeAppNameMaxLen limits the length of the app name for Kubernetes.
// This also includes the appended UUID prefix.
KubeAppNameMaxLen = 32
// KubeAppNameUUIDSuffixLen : number of characters taken from the app UUID and appended
// to the app name for Kubernetes (to avoid name collisions between apps of the same
// DisplayName, see GetAppKubeName).
KubeAppNameUUIDSuffixLen = 5
// VMIPodNamePrefix : prefix added to name of every pod created to run VM.
VMIPodNamePrefix = "virt-launcher-"
// InstallOptionEtcdSizeGB grub option at install time. Size of etcd volume in GB.
InstallOptionEtcdSizeGB = "eve_install_kubevirt_etcd_sizeGB"
// DefaultEtcdSizeGB default for InstallOptionEtcdSizeGB
DefaultEtcdSizeGB uint32 = 10
// EtcdVolBlockSizeBytes is the block size for the etcd volume
EtcdVolBlockSizeBytes = uint64(4 * 1024)
)
// IsHVTypeKube - return true if the EVE image is kube cluster type.
func IsHVTypeKube() bool {
retbytes, err := os.ReadFile(EveVirtTypeFile)
if err != nil {
return false
}
if strings.Contains(string(retbytes), "kubevirt") {
return true
}
return false
}
var (
kubeNameForbiddenChars = regexp.MustCompile("[^a-zA-Z0-9-.]")
kubeNameSeparators = regexp.MustCompile("[.-]+")
)
// GetAppKubeName returns name of the application used inside Kubernetes (for Pod or VMI).
func GetAppKubeName(displayName string, uuid uuid.UUID) string {
appKubeName := displayName
// Replace underscores with dashes for Kubernetes
appKubeName = strings.ReplaceAll(appKubeName, "_", "-")
// Remove special characters using regular expressions
appKubeName = kubeNameForbiddenChars.ReplaceAllString(appKubeName, "")
// Reduce combinations like '-.-' or '.-.' to a single dash
appKubeName = kubeNameSeparators.ReplaceAllString(appKubeName, "-")
appKubeName = strings.ToLower(appKubeName)
const maxLen = KubeAppNameMaxLen - 1 - KubeAppNameUUIDSuffixLen
if len(appKubeName) > maxLen {
appKubeName = appKubeName[:maxLen]
}
return appKubeName + "-" + uuid.String()[:KubeAppNameUUIDSuffixLen]
}
// GetVMINameFromVirtLauncher : get VMI name from the corresponding Kubevirt
// launcher pod name.
func GetVMINameFromVirtLauncher(podName string) (vmiName string, isVirtLauncher bool) {
if !strings.HasPrefix(podName, VMIPodNamePrefix) {
return "", false
}
vmiName = strings.TrimPrefix(podName, VMIPodNamePrefix)
lastSep := strings.LastIndex(vmiName, "-")
if lastSep != -1 {
vmiName = vmiName[:lastSep]
}
return vmiName, true
}