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

Strip 'user_' prefix from CUR 2.0 resource tag labels (#4036)

Signed-off-by: Christian Petersen <Christian.Petersen2@ibm.com>
Christian Petersen 7 часов назад
Родитель
Сommit
a1b6790e87
2 измененных файлов с 25 добавлено и 4 удалено
  1. 23 3
      pkg/cloud/aws/athenaintegration.go
  2. 2 1
      pkg/cloud/aws/athenaintegration_test.go

+ 23 - 3
pkg/cloud/aws/athenaintegration.go

@@ -15,9 +15,12 @@ import (
 	"github.com/opencost/opencost/pkg/cloud"
 	"github.com/opencost/opencost/pkg/cloud"
 )
 )
 
 
-const LabelColumnPrefix = "resource_tags_user_"
-const AWSLabelColumnPrefix = "resource_tags_aws_"
+// Resource Tag Columns
 const AthenaResourceTagPrefix = "resource_tags_"
 const AthenaResourceTagPrefix = "resource_tags_"
+const AthenaResourceTagsUserPrefix = "user_"
+const AthenaResourceTagsAWSPrefix = "aws_"
+const LabelColumnPrefix = AthenaResourceTagPrefix + AthenaResourceTagsUserPrefix
+const AWSLabelColumnPrefix = AthenaResourceTagPrefix + AthenaResourceTagsAWSPrefix
 const AthenaResourceTagsColumn = "resource_tags"
 const AthenaResourceTagsColumn = "resource_tags"
 
 
 const AthenaResourceTagsCastToJsonColumn = "CAST(resource_tags AS JSON) as resource_tags"
 const AthenaResourceTagsCastToJsonColumn = "CAST(resource_tags AS JSON) as resource_tags"
@@ -433,10 +436,27 @@ func athenaRowToCloudCost(row types.Row, aqi AthenaQueryIndexes) (*opencost.Clou
 
 
 	if _, ok := aqi.ColumnIndexes[AthenaResourceTagsCastToJsonColumn]; ok {
 	if _, ok := aqi.ColumnIndexes[AthenaResourceTagsCastToJsonColumn]; ok {
 		resourceTags := GetAthenaRowValue(row, aqi.ColumnIndexes, AthenaResourceTagsCastToJsonColumn)
 		resourceTags := GetAthenaRowValue(row, aqi.ColumnIndexes, AthenaResourceTagsCastToJsonColumn)
-		err := json.Unmarshal([]byte(resourceTags), &labels)
+		rawTags := map[string]string{}
+		err := json.Unmarshal([]byte(resourceTags), &rawTags)
 		if err != nil {
 		if err != nil {
 			log.Errorf("athenaRowToCloudCost: error unmarshalling resource tags: %s", err.Error())
 			log.Errorf("athenaRowToCloudCost: error unmarshalling resource tags: %s", err.Error())
 		}
 		}
+		// aws tags keep their prefix
+		for tagKey, value := range rawTags {
+			if !strings.HasPrefix(tagKey, AthenaResourceTagsUserPrefix) && value != "" {
+				labels[tagKey] = value
+			}
+		}
+		// remove "user_" prefix, aws tags take precedence
+		for tagKey, value := range rawTags {
+			if !strings.HasPrefix(tagKey, AthenaResourceTagsUserPrefix) || value == "" {
+				continue
+			}
+			labelName := strings.TrimPrefix(tagKey, AthenaResourceTagsUserPrefix)
+			if _, exists := labels[labelName]; !exists {
+				labels[labelName] = value
+			}
+		}
 	}
 	}
 
 
 	invoiceEntityID := GetAthenaRowValue(row, aqi.ColumnIndexes, "bill_payer_account_id")
 	invoiceEntityID := GetAthenaRowValue(row, aqi.ColumnIndexes, "bill_payer_account_id")

+ 2 - 1
pkg/cloud/aws/athenaintegration_test.go

@@ -400,7 +400,7 @@ func Test_athenaRowToCloudCost(t *testing.T) {
 		},
 		},
 		{
 		{
 			name: "valid kubernetes with labels CUR 2.0",
 			name: "valid kubernetes with labels CUR 2.0",
-			row:  []string{"1", "2", "3", "4", "true", "2024-09-01 00:00:00.000", "resourceID", "payerAccountID", "usageAccountID", "productCode", "usageType", "regionCode", "availabilityZone", `{"test": "userTagTestValue", "aws_test": "awsTagTestValue"}`},
+			row:  []string{"1", "2", "3", "4", "true", "2024-09-01 00:00:00.000", "resourceID", "payerAccountID", "usageAccountID", "productCode", "usageType", "regionCode", "availabilityZone", `{"user_test": "userTagTestValue", "user_user_id": "u123", "user_empty": "", "user_aws_test": "userShadowsAws", "aws_test": "awsTagTestValue"}`},
 			aqi:  aqiCur20,
 			aqi:  aqiCur20,
 			want: &opencost.CloudCost{
 			want: &opencost.CloudCost{
 				Properties: &opencost.CloudCostProperties{
 				Properties: &opencost.CloudCostProperties{
@@ -416,6 +416,7 @@ func Test_athenaRowToCloudCost(t *testing.T) {
 					Category:          opencost.OtherCategory,
 					Category:          opencost.OtherCategory,
 					Labels: opencost.CloudCostLabels{
 					Labels: opencost.CloudCostLabels{
 						"test":     "userTagTestValue",
 						"test":     "userTagTestValue",
+						"user_id":  "u123",
 						"aws_test": "awsTagTestValue",
 						"aws_test": "awsTagTestValue",
 					},
 					},
 				},
 				},