From 934dca0abb33ca55a4d9f13a2c68c284d7922f50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels-Ole=20K=C3=BChl?= Date: Fri, 8 Feb 2019 14:24:12 +0100 Subject: [PATCH] Adding unittest for resource extraction from labels --- .../cloudprovider/aws/aws_manager.go | 12 ++++----- .../cloudprovider/aws/aws_manager_test.go | 27 +++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/cluster-autoscaler/cloudprovider/aws/aws_manager.go b/cluster-autoscaler/cloudprovider/aws/aws_manager.go index 07e7be1361b9..c44b5316814f 100644 --- a/cluster-autoscaler/cloudprovider/aws/aws_manager.go +++ b/cluster-autoscaler/cloudprovider/aws/aws_manager.go @@ -249,13 +249,13 @@ func (m *AwsManager) buildNodeFromTemplate(asg *asg, template *asgTemplate) (*ap resourcesFromTags := extractResourceAllocatableFromAsg(template.Tags) if val, ok := resourcesFromTags["cpu"]; ok { - node.Status.Capacity[apiv1.ResourceCPU] = val + node.Status.Capacity[apiv1.ResourceCPU] = *val } if val, ok := resourcesFromTags["memory"]; ok { - node.Status.Capacity[apiv1.ResourceMemory] = val + node.Status.Capacity[apiv1.ResourceMemory] = *val } if val, ok := resourcesFromTags["ephemeral-storage"]; ok { - node.Status.Capacity[apiv1.ResourceEphemeralStorage] = val + node.Status.Capacity[apiv1.ResourceEphemeralStorage] = *val } // TODO: use proper allocatable!! @@ -304,8 +304,8 @@ func extractLabelsFromAsg(tags []*autoscaling.TagDescription) map[string]string return result } -func extractResourceAllocatableFromAsg(tags []*autoscaling.TagDescription) map[string]resource.Quantity { - result := make(map[string]resource.Quantity) +func extractResourceAllocatableFromAsg(tags []*autoscaling.TagDescription) map[string]*resource.Quantity { + result := make(map[string]*resource.Quantity) for _, tag := range tags { k := *tag.Key @@ -318,7 +318,7 @@ func extractResourceAllocatableFromAsg(tags []*autoscaling.TagDescription) map[s if err != nil { continue } - result[label] = quantity + result[label] = &quantity } } } diff --git a/cluster-autoscaler/cloudprovider/aws/aws_manager_test.go b/cluster-autoscaler/cloudprovider/aws/aws_manager_test.go index e1f0ca488f4a..8fa676fa0c50 100644 --- a/cluster-autoscaler/cloudprovider/aws/aws_manager_test.go +++ b/cluster-autoscaler/cloudprovider/aws/aws_manager_test.go @@ -33,6 +33,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" apiv1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" "k8s.io/autoscaler/cluster-autoscaler/cloudprovider" kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis" ) @@ -82,6 +83,32 @@ func TestBuildGenericLabels(t *testing.T) { assert.Equal(t, cloudprovider.DefaultOS, labels[kubeletapis.LabelOS]) } +func TestExtractResourceAllocatableFromAsg(t *testing.T) { + tags := []*autoscaling.TagDescription{ + { + Key: aws.String("k8s.io/cluster-autoscaler/node-template/resources/cpu"), + Value: aws.String("100m"), + }, + { + Key: aws.String("k8s.io/cluster-autoscaler/node-template/resources/memory"), + Value: aws.String("100M"), + }, + { + Key: aws.String("k8s.io/cluster-autoscaler/node-template/resources/ephemeral-storage"), + Value: aws.String("20G"), + }, + } + + labels := extractResourceAllocatableFromAsg(tags) + + assert.Equal(t, resource.NewMilliQuantity(100, resource.DecimalSI).String(), labels["cpu"].String()) + expectedMemory := resource.MustParse("100M") + assert.Equal(t, (&expectedMemory).String(), labels["memory"].String()) + expectedEphemeralStorage := resource.MustParse("20G") + assert.Equal(t, (&expectedEphemeralStorage).String(), labels["ephemeral-storage"].String()) + //assert.Equal(t, "bar", labels["foo"]) +} + func TestExtractLabelsFromAsg(t *testing.T) { tags := []*autoscaling.TagDescription{ {