Skip to content

Commit

Permalink
Retrieve region/AZ from topology label
Browse files Browse the repository at this point in the history
Signed-off-by: torredil <[email protected]>
  • Loading branch information
torredil committed Aug 25, 2022
1 parent a6a7b09 commit 5012723
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
36 changes: 19 additions & 17 deletions pkg/cloud/metadata_k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,33 @@ func KubernetesAPIInstanceInfo(clientset kubernetes.Interface) (*Metadata, error
return nil, fmt.Errorf("node providerID empty, cannot parse")
}

awsRegionRegex := "(snow)|(([a-z]{2}(-gov)?)-(central|(north|south)?(east|west)?)-[0-9])"
awsAvailabilityZoneRegex := "(snow)|(([a-z]{2}(-gov)?)-(central|(north|south)?(east|west)?)-[0-9][a-z])"
awsInstanceIDRegex := "s\\.i-[a-z0-9]+|i-[a-z0-9]+$"

re := regexp.MustCompile(awsRegionRegex)
region := re.FindString(providerID)
if region == "" {
return nil, fmt.Errorf("did not find aws region in node providerID string")
}

re = regexp.MustCompile(awsAvailabilityZoneRegex)
availabilityZone := re.FindString(providerID)
if availabilityZone == "" {
return nil, fmt.Errorf("did not find aws availability zone in node providerID string")
}

re = regexp.MustCompile(awsInstanceIDRegex)
re := regexp.MustCompile(awsInstanceIDRegex)
instanceID := re.FindString(providerID)
if instanceID == "" {
return nil, fmt.Errorf("did not find aws instance ID in node providerID string")
}

var instanceType string
if it, ok := node.GetLabels()[corev1.LabelInstanceTypeStable]; ok {
instanceType = it
if val, ok := node.GetLabels()[corev1.LabelInstanceTypeStable]; ok {
instanceType = val
} else {
return nil, fmt.Errorf("could not retrieve instance type from topology label")
}

var region string
if val, ok := node.GetLabels()[corev1.LabelTopologyRegion]; ok {
region = val
} else {
return nil, fmt.Errorf("could not retrieve region from topology label")
}

var availabilityZone string
if val, ok := node.GetLabels()[corev1.LabelTopologyZone]; ok {
availabilityZone = val
} else {
return nil, fmt.Errorf("could not retrieve AZ from topology label")
}

instanceInfo := Metadata{
Expand Down
27 changes: 21 additions & 6 deletions pkg/cloud/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ func TestNewMetadataService(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"node.kubernetes.io/instance-type": stdInstanceType,
"topology.kubernetes.io/region": stdRegion,
"topology.kubernetes.io/zone": stdAvailabilityZone,
},
Name: nodeName,
},
Expand Down Expand Up @@ -179,38 +181,46 @@ func TestNewMetadataService(t *testing.T) {
nodeNameEnvVar: nodeName,
},
{
name: "failure: metadata not available, invalid region",
name: "failure: metadata not available, could not retrieve region",
ec2metadataAvailable: false,
expectedErr: fmt.Errorf("did not find aws region in node providerID string"),
expectedErr: fmt.Errorf("could not retrieve region from topology label"),
node: v1.Node{
TypeMeta: metav1.TypeMeta{
Kind: "Node",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"node.kubernetes.io/instance-type": stdInstanceType,
"topology.kubernetes.io/zone": stdAvailabilityZone,
},
Name: nodeName,
},
Spec: v1.NodeSpec{
ProviderID: "aws:///us-est-2b/i-abcdefgh123456789", // invalid region
ProviderID: "aws:///" + stdAvailabilityZone + "/" + stdInstanceID,
},
Status: v1.NodeStatus{},
},
nodeNameEnvVar: nodeName,
},
{
name: "failure: metadata not available, invalid az",
name: "failure: metadata not available, could not retrieve AZ",
ec2metadataAvailable: false,
expectedErr: fmt.Errorf("did not find aws availability zone in node providerID string"),
expectedErr: fmt.Errorf("could not retrieve AZ from topology label"),
node: v1.Node{
TypeMeta: metav1.TypeMeta{
Kind: "Node",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"node.kubernetes.io/instance-type": stdInstanceType,
"topology.kubernetes.io/region": stdRegion,
},
Name: nodeName,
},
Spec: v1.NodeSpec{
ProviderID: "aws:///us-west-21/i-abcdefgh123456789", // invalid AZ
ProviderID: "aws:///" + stdAvailabilityZone + "/" + stdInstanceID,
},
Status: v1.NodeStatus{},
},
Expand All @@ -226,6 +236,11 @@ func TestNewMetadataService(t *testing.T) {
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"node.kubernetes.io/instance-type": stdInstanceType,
"topology.kubernetes.io/region": stdRegion,
"topology.kubernetes.io/zone": stdAvailabilityZone,
},
Name: nodeName,
},
Spec: v1.NodeSpec{
Expand Down

0 comments on commit 5012723

Please sign in to comment.