-
Notifications
You must be signed in to change notification settings - Fork 273
/
labels.go
84 lines (73 loc) 路 3.71 KB
/
labels.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
82
83
84
package framework
import (
"encoding/json"
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/constants"
"github.com/aws/eks-anywhere/pkg/logger"
)
const LabelPrefix = "eksa.e2e"
func ValidateControlPlaneLabels(controlPlane v1alpha1.ControlPlaneConfiguration, node corev1.Node) error {
logger.V(4).Info("Validating control plane labels")
return validateLabels(controlPlane.Labels, node)
}
// ValidateControlPlaneFailureDomainLabels validate if Cloudstack provider replaces ds.meta_data.failuredomain with proper failuredomain name
// in control plane node label 'cluster.x-k8s.io/failure-domain'.
func ValidateControlPlaneFailureDomainLabels(controlPlane v1alpha1.ControlPlaneConfiguration, node corev1.Node) error {
if controlPlane.MachineGroupRef.Kind == "CloudStackMachineConfig" {
logger.V(4).Info("Validating control plane node failuredomain label")
return validateFailureDomainLabel(controlPlane.Labels, node)
}
return fmt.Errorf("ds.meta_data.failuredomain placeholder in node label is currently only supported in CloudStack provider")
}
func ValidateWorkerNodeLabels(w v1alpha1.WorkerNodeGroupConfiguration, node corev1.Node) error {
logger.V(4).Info("Validating worker node labels", "worker node group", w.Name)
return validateLabels(w.Labels, node)
}
// ValidateWorkerNodeFailureDomainLabels validate if Cloudstack provider replaces ds.meta_data.failuredomain with proper failuredomain name
// in worker group node label 'cluster.x-k8s.io/failure-domain'.
func ValidateWorkerNodeFailureDomainLabels(w v1alpha1.WorkerNodeGroupConfiguration, node corev1.Node) error {
if w.MachineGroupRef.Kind == v1alpha1.CloudStackMachineConfigKind {
logger.V(4).Info("Validating worker node failuredomain label", "worker node group", w.Name)
return validateFailureDomainLabel(w.Labels, node)
}
return fmt.Errorf("ds.meta_data.failuredomain placeholder in node label is currently only supported in CloudStack provider")
}
func validateLabels(expectedLabels map[string]string, node corev1.Node) error {
actualLabels := retrieveTestNodeLabels(node.Labels)
expectedBytes, _ := json.Marshal(expectedLabels)
actualBytes, _ := json.Marshal(actualLabels)
if !v1alpha1.MapEqual(expectedLabels, actualLabels) {
return fmt.Errorf("labels on node %v and corresponding configuration do not match; configured labels: %v; node labels: %v",
node.Name, string(expectedBytes), string(actualBytes))
}
logger.V(4).Info("expected labels from cluster spec configuration are present on the corresponding node", "node", node.Name, "node labels", string(actualBytes), "configuration labels", string(expectedBytes))
return nil
}
func retrieveTestNodeLabels(nodeLabels map[string]string) map[string]string {
labels := map[string]string{}
for key, val := range nodeLabels {
if strings.HasPrefix(key, LabelPrefix) {
labels[key] = val
}
}
return labels
}
func validateFailureDomainLabel(expectedLabels map[string]string, node corev1.Node) error {
if failuredomainSpecified, ok := expectedLabels[constants.FailureDomainLabelName]; ok {
if failuredomain, exist := node.Labels[constants.FailureDomainLabelName]; exist {
logger.V(4).Info("node label: ", constants.FailureDomainLabelName, failuredomain)
if failuredomainSpecified == constants.CloudstackFailureDomainPlaceholder && failuredomain == failuredomainSpecified {
return fmt.Errorf("value %s of label %s on node %s is not replaced with a failurdomain name by CloudStack provider",
constants.CloudstackFailureDomainPlaceholder,
constants.FailureDomainLabelName,
node.Name)
}
} else {
return fmt.Errorf("expected labels %s not found on node %s", constants.FailureDomainLabelName, node.Name)
}
}
return nil
}