Skip to content

Commit

Permalink
Merge pull request #1360 from torredil/fargate-affinity
Browse files Browse the repository at this point in the history
Add controller nodeAffinity to prefer EC2 over Fargate + Retrieve region/AZ from topology label in K8s
  • Loading branch information
k8s-ci-robot authored Sep 26, 2022
2 parents 03cde05 + 5012723 commit fcf2fa7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
11 changes: 10 additions & 1 deletion charts/aws-ebs-csi-driver/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,16 @@ fullnameOverride:
controller:
# If arbitrary args like "--aws-sdk-debug-log=true" need to be passed, use this value
additionalArgs: []
affinity: {}
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: eks.amazonaws.com/compute-type
operator: NotIn
values:
- fargate
# The default filesystem type of the volume to provision when fstype is unspecified in the StorageClass.
# If the default is not set and fstype is unset in the StorageClass, then no fstype will be set
defaultFsType: ext4
Expand Down
10 changes: 10 additions & 0 deletions deploy/kubernetes/base/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ spec:
kubernetes.io/os: linux
serviceAccountName: ebs-csi-controller-sa
priorityClassName: system-cluster-critical
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- preference:
matchExpressions:
- key: eks.amazonaws.com/compute-type
operator: NotIn
values:
- fargate
weight: 1
tolerations:
- key: CriticalAddonsOnly
operator: Exists
Expand Down
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 fcf2fa7

Please sign in to comment.