-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
node_utils.go
35 lines (30 loc) · 1.01 KB
/
node_utils.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
package k8s
import (
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"regexp"
"strings"
)
var awsInstanceIDRegex = regexp.MustCompile("^i-[^/]*$")
// GetNodeCondition will get pointer to Node's existing condition.
// returns nil if no matching condition found.
func GetNodeCondition(node *corev1.Node, conditionType corev1.NodeConditionType) *corev1.NodeCondition {
for i := range node.Status.Conditions {
if node.Status.Conditions[i].Type == conditionType {
return &node.Status.Conditions[i]
}
}
return nil
}
func ExtractNodeInstanceID(node *corev1.Node) (string, error) {
providerID := node.Spec.ProviderID
if providerID == "" {
return "", errors.Errorf("providerID is not specified for node: %s", node.Name)
}
providerIDParts := strings.Split(providerID, "/")
instanceID := providerIDParts[len(providerIDParts)-1]
if !awsInstanceIDRegex.MatchString(instanceID) {
return "", errors.Errorf("providerID %s is invalid for EC2 instances, node: %s", providerID, node.Name)
}
return instanceID, nil
}