Skip to content

Commit

Permalink
Adding unittest for resource extraction from labels
Browse files Browse the repository at this point in the history
  • Loading branch information
Niels-Ole Kühl committed Feb 8, 2019
1 parent e628858 commit 934dca0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
12 changes: 6 additions & 6 deletions cluster-autoscaler/cloudprovider/aws/aws_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!!
Expand Down Expand Up @@ -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
Expand All @@ -318,7 +318,7 @@ func extractResourceAllocatableFromAsg(tags []*autoscaling.TagDescription) map[s
if err != nil {
continue
}
result[label] = quantity
result[label] = &quantity
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions cluster-autoscaler/cloudprovider/aws/aws_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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{
{
Expand Down

0 comments on commit 934dca0

Please sign in to comment.