Просмотр исходного кода

Merge pull request #449 from kubecost/AjayTripathy-aws-transform

support aws transformation natively
Ajay Tripathy 6 лет назад
Родитель
Сommit
bc57a8cfb4
2 измененных файлов с 17 добавлено и 0 удалено
  1. 7 0
      pkg/cloud/csvprovider.go
  2. 10 0
      test/cloud_test.go

+ 7 - 0
pkg/cloud/csvprovider.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"fmt"
 	"io"
 	"io"
 	"os"
 	"os"
+	"regexp"
 	"strings"
 	"strings"
 	"sync"
 	"sync"
 	"time"
 	"time"
@@ -165,6 +166,12 @@ func (c *CSVProvider) NodePricing(key Key) (*Node, error) {
 func NodeValueFromMapField(m string, n *v1.Node) string {
 func NodeValueFromMapField(m string, n *v1.Node) string {
 	mf := strings.Split(m, ".")
 	mf := strings.Split(m, ".")
 	if len(mf) == 2 && mf[0] == "spec" && mf[1] == "providerID" {
 	if len(mf) == 2 && mf[0] == "spec" && mf[1] == "providerID" {
+		provIdRx := regexp.MustCompile("aws:///([^/]+)/([^/]+)") // It's of the form aws:///us-east-2a/i-0fea4fd46592d050b and we want i-0fea4fd46592d050b, if it exists
+		for matchNum, group := range provIdRx.FindStringSubmatch(n.Spec.ProviderID) {
+			if matchNum == 2 {
+				return group
+			}
+		}
 		return n.Spec.ProviderID
 		return n.Spec.ProviderID
 	} else if len(mf) > 1 && mf[0] == "metadata" {
 	} else if len(mf) > 1 && mf[0] == "metadata" {
 		if mf[1] == "name" {
 		if mf[1] == "name" {

+ 10 - 0
test/cloud_test.go

@@ -11,6 +11,16 @@ const(
 	nameMap = "metadata.name"
 	nameMap = "metadata.name"
 	labelMapFoo = "metadata.labels.foo"
 	labelMapFoo = "metadata.labels.foo"
 )
 )
+func TestTransformedValueFromMapField(t *testing.T) {
+	providerIDWant := "i-05445591e0d182d42"
+	n := &v1.Node{}
+	n.Spec.ProviderID = "aws:///us-east-1a/i-05445591e0d182d42"
+	got := cloud.NodeValueFromMapField(providerIDMap, n)
+	if got != providerIDWant {
+		t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant, got)
+	}
+}
+
 func TestNodeValueFromMapField(t *testing.T) {
 func TestNodeValueFromMapField(t *testing.T) {
 	providerIDWant := "providerid"
 	providerIDWant := "providerid"
 	nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
 	nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"