Browse Source

add compat.go and use throughout

Ajay Tripathy 5 years ago
parent
commit
71a7580d22
5 changed files with 58 additions and 23 deletions
  1. 6 12
      pkg/cloud/awsprovider.go
  2. 4 3
      pkg/cloud/azureprovider.go
  3. 7 4
      pkg/cloud/gcpprovider.go
  4. 6 4
      pkg/costmodel/costmodel.go
  5. 35 0
      pkg/util/compat.go

+ 6 - 12
pkg/cloud/awsprovider.go

@@ -463,13 +463,9 @@ func (k *awsKey) ID() string {
 
 func (k *awsKey) Features() string {
 
-	instanceType := k.Labels[v1.LabelInstanceType]
-	var operatingSystem string
-	operatingSystem, ok := k.Labels[v1.LabelOSStable]
-	if !ok {
-		operatingSystem = k.Labels["beta.kubernetes.io/os"]
-	}
-	region := k.Labels[v1.LabelZoneRegion]
+	instanceType, _ := util.GetInstanceType(k.Labels)
+	operatingSystem, _ := util.GetOperatingSystem(k.Labels)
+	region, _ := util.GetRegion(k.Labels)
 
 	key := region + "," + instanceType + "," + operatingSystem
 	usageType := PreemptibleType
@@ -532,7 +528,7 @@ func (key *awsPVKey) Features() string {
 	// Storage class names are generally EBS volume types (gp2)
 	// Keys in Pricing are based on UsageTypes (EBS:VolumeType.gp2)
 	// Converts between the 2
-	region := key.Labels[v1.LabelZoneRegion]
+	region, _ := util.GetRegion(key.Labels)
 	//if region == "" {
 	//	region = "us-east-1"
 	//}
@@ -565,7 +561,7 @@ func (aws *AWS) ClusterManagementPricing() (string, float64, error) {
 	return aws.clusterProvisioner, aws.clusterManagementPrice, nil
 }
 
-// Use the pricing data from the current region. Fall back to
+// Use the pricing data from the current region. Fall back to using all region data if needed.
 func (aws *AWS) getRegionPricing(nodeList []*v1.Node) (*http.Response, string, error) {
 
 	pricingURL := "https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/"
@@ -575,10 +571,8 @@ func (aws *AWS) getRegionPricing(nodeList []*v1.Node) (*http.Response, string, e
 	for _, n := range nodeList {
 		labels := n.GetLabels()
 		currentNodeRegion := ""
-		if r, ok := labels[v1.LabelZoneRegion]; ok {
+		if r, ok := util.GetRegion(labels); ok {
 			currentNodeRegion = r
-		} else if r2, ok := labels["topology.kubernetes.io/region"]; ok { // Label as of 1.17
-			currentNodeRegion = r2
 		} else {
 			multiregion = true // We weren't able to detect the node's region, so pull all data.
 			break

+ 4 - 3
pkg/cloud/azureprovider.go

@@ -193,8 +193,9 @@ type azureKey struct {
 }
 
 func (k *azureKey) Features() string {
-	region := strings.ToLower(k.Labels[v1.LabelZoneRegion])
-	instance := k.Labels[v1.LabelInstanceType]
+	r, _ := util.GetRegion(k.Labels)
+	region := strings.ToLower(r)
+	instance, _ := util.GetInstanceType(k.Labels)
 	usageType := "ondemand"
 	return fmt.Sprintf("%s,%s,%s", region, instance, usageType)
 }
@@ -711,7 +712,7 @@ func (key *azurePvKey) Features() string {
 			storageClass = AzureFileStandardStorageClass
 		}
 	}
-	if region, ok := key.Labels[v1.LabelZoneRegion]; ok {
+	if region, ok := util.GetRegion(key.Labels); ok {
 		return region + "," + storageClass
 	}
 

+ 7 - 4
pkg/cloud/gcpprovider.go

@@ -1168,7 +1168,7 @@ func (gcp *GCP) ApplyReservedInstancePricing(nodes map[string]*Node) {
 			continue
 		}
 
-		nodeRegion, ok := kNode.Labels[v1.LabelZoneRegion]
+		nodeRegion, ok := util.GetRegion(kNode.Labels)
 		if !ok {
 			klog.V(4).Infof("[Reserved] Could not find node region")
 			continue
@@ -1322,7 +1322,8 @@ func (key *pvKey) Features() string {
 	} else if storageClass == "pd-standard" {
 		storageClass = "pdstandard"
 	}
-	return key.Labels[v1.LabelZoneRegion] + "," + storageClass
+	region, _ := util.GetRegion(key.Labels)
+	return region + "," + storageClass
 }
 
 type gcpKey struct {
@@ -1355,7 +1356,8 @@ func (gcp *gcpKey) GPUType() string {
 
 // GetKey maps node labels to information needed to retrieve pricing data
 func (gcp *gcpKey) Features() string {
-	instanceType := strings.ToLower(strings.Join(strings.Split(gcp.Labels[v1.LabelInstanceType], "-")[:2], ""))
+	it, _ := util.GetInstanceType(gcp.Labels)
+	instanceType := strings.ToLower(strings.Join(strings.Split(it, "-")[:2], ""))
 	if instanceType == "n1highmem" || instanceType == "n1highcpu" {
 		instanceType = "n1standard" // These are priced the same. TODO: support n1ultrahighmem
 	} else if instanceType == "n2highmem" || instanceType == "n2highcpu" {
@@ -1365,7 +1367,8 @@ func (gcp *gcpKey) Features() string {
 	} else if strings.HasPrefix(instanceType, "custom") {
 		instanceType = "custom" // The suffix of custom does not matter
 	}
-	region := strings.ToLower(gcp.Labels[v1.LabelZoneRegion])
+	r, _ := util.GetRegion(gcp.Labels)
+	region := strings.ToLower(r)
 	var usageType string
 
 	if t, ok := gcp.Labels["cloud.google.com/gke-preemptible"]; ok && t == "true" {

+ 6 - 4
pkg/costmodel/costmodel.go

@@ -796,7 +796,7 @@ func addPVData(cache clustercache.ClusterCache, pvClaimMapping map[string]*Persi
 	var defaultRegion string
 	nodeList := cache.GetAllNodes()
 	if len(nodeList) > 0 {
-		defaultRegion = nodeList[0].Labels[v1.LabelZoneRegion]
+		defaultRegion, _ = util.GetRegion(nodeList[0].Labels)
 	}
 
 	storageClasses := cache.GetAllStorageClasses()
@@ -818,7 +818,7 @@ func addPVData(cache clustercache.ClusterCache, pvClaimMapping map[string]*Persi
 			klog.V(4).Infof("Unable to find parameters for storage class \"%s\". Does pv \"%s\" have a storageClassName?", pv.Spec.StorageClassName, pv.Name)
 		}
 		var region string
-		if r, ok := pv.Labels[v1.LabelZoneRegion]; ok {
+		if r, ok := util.GetRegion(pv.Labels); ok {
 			region = r
 		} else {
 			region = defaultRegion
@@ -899,10 +899,12 @@ func (cm *CostModel) GetNodeCost(cp costAnalyzerCloud.Provider) (map[string]*cos
 		}
 		newCnode := *cnode
 		if newCnode.InstanceType == "" {
-			newCnode.InstanceType = n.Labels[v1.LabelInstanceType]
+			it, _ := util.GetInstanceType(n.Labels)
+			newCnode.InstanceType = it
 		}
 		if newCnode.Region == "" {
-			newCnode.Region = n.Labels[v1.LabelZoneRegion]
+			region, _ := util.GetRegion(n.Labels)
+			newCnode.Region = region
 		}
 		newCnode.ProviderID = n.Spec.ProviderID
 

+ 35 - 0
pkg/util/compat.go

@@ -0,0 +1,35 @@
+package util
+
+import (
+	v1 "k8s.io/api/core/v1"
+)
+
+func GetRegion(labels map[string]string) (string, bool) {
+	if _, ok := labels[v1.LabelZoneRegion]; ok {
+		return labels[v1.LabelZoneRegion], true
+	} else if _, ok := labels["topology.kubernetes.io/region"]; ok { // Label as of 1.17
+		return labels["topology.kubernetes.io/region"], true
+	} else {
+		return "", false
+	}
+}
+
+func GetInstanceType(labels map[string]string) (string, bool) {
+	if _, ok := labels[v1.LabelInstanceType]; ok {
+		return labels[v1.LabelInstanceType], true
+	} else if _, ok := labels["node.kubernetes.io/instance-type"]; ok {
+		return labels["node.kubernetes.io/instance-type"], true
+	} else {
+		return "", false
+	}
+}
+
+func GetOperatingSystem(labels map[string]string) (string, bool) {
+	if _, ok := labels[v1.LabelOSStable]; ok {
+		return labels[v1.LabelOSStable], true
+	} else if _, ok := labels["beta.kubernetes.io/os"]; ok {
+		return labels["beta.kubernetes.io/os"], true
+	} else {
+		return "", false
+	}
+}