From 486f7334a3c86abf09335a0d4e2bce50358c8e9d Mon Sep 17 00:00:00 2001 From: Kubernetes Prow Robot Date: Mon, 11 Feb 2019 11:23:21 -0800 Subject: [PATCH] Merge pull request #1656 from flix-tech/asg-tags-resource-override Adding ability to override allocatable resources via ASG tags. --- .../cloudprovider/aws/aws_manager.go | 30 +++++++++++++++++++ .../cloudprovider/aws/aws_manager_test.go | 25 ++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/cluster-autoscaler/cloudprovider/aws/aws_manager.go b/cluster-autoscaler/cloudprovider/aws/aws_manager.go index b651ca05f165..7d7d199787e6 100644 --- a/cluster-autoscaler/cloudprovider/aws/aws_manager.go +++ b/cluster-autoscaler/cloudprovider/aws/aws_manager.go @@ -259,6 +259,14 @@ func (m *AwsManager) buildNodeFromTemplate(asg *asg, template *asgTemplate) (*ap } } + resourcesFromTags := extractAllocatableResourcesFromAsg(template.Tags) + if val, ok := resourcesFromTags["ephemeral-storage"]; ok { + node.Status.Capacity[apiv1.ResourceEphemeralStorage] = *val + } + + // TODO: use proper allocatable!! + node.Status.Allocatable = node.Status.Capacity + // NodeLabels node.Labels = cloudprovider.JoinStringMaps(node.Labels, extractLabelsFromAsg(template.Tags)) // GenericLabels @@ -412,6 +420,28 @@ func extractLabelsFromAsg(tags []*autoscaling.TagDescription) map[string]string return result } +func extractAllocatableResourcesFromAsg(tags []*autoscaling.TagDescription) map[string]*resource.Quantity { + result := make(map[string]*resource.Quantity) + + for _, tag := range tags { + k := *tag.Key + v := *tag.Value + splits := strings.Split(k, "k8s.io/cluster-autoscaler/node-template/resources/") + if len(splits) > 1 { + label := splits[1] + if label != "" { + quantity, err := resource.ParseQuantity(v) + if err != nil { + continue + } + result[label] = &quantity + } + } + } + + return result +} + func extractTaintsFromAsg(tags []*autoscaling.TagDescription) []apiv1.Taint { taints := make([]apiv1.Taint, 0) diff --git a/cluster-autoscaler/cloudprovider/aws/aws_manager_test.go b/cluster-autoscaler/cloudprovider/aws/aws_manager_test.go index 8512e875f82a..cf90ff19efb9 100644 --- a/cluster-autoscaler/cloudprovider/aws/aws_manager_test.go +++ b/cluster-autoscaler/cloudprovider/aws/aws_manager_test.go @@ -48,6 +48,31 @@ func TestBuildGenericLabels(t *testing.T) { assert.Equal(t, cloudprovider.DefaultOS, labels[kubeletapis.LabelOS]) } +func TestExtractAllocatableResourcesFromAsg(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 := extractAllocatableResourcesFromAsg(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()) +} + func TestExtractLabelsFromAsg(t *testing.T) { tags := []*autoscaling.TagDescription{ {