From 176ba091f20ff189e8dfedc4b0e607e66317f759 Mon Sep 17 00:00:00 2001 From: Brandon Wagner Date: Fri, 9 Apr 2021 12:45:53 -0500 Subject: [PATCH] add user-agent string (#352) * add user-agent string * include the makefile * change useragent to karpenter.sh * inline withUserAgent * set useragent default to unspecified --- Makefile | 7 ++++--- pkg/cloudprovider/aws/factory.go | 12 +++++++++++- pkg/utils/project/project.go | 6 ++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 32fd4ebeb13c..13861baaa400 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,10 @@ -GOFLAGS ?= "-tags=${CLOUD_PROVIDER}" - RELEASE_REPO ?= public.ecr.aws/b6u6q9h4 -RELEASE_VERSION ?= v0.2.0 +RELEASE_VERSION ?= $(shell git describe --tags --always --dirty) RELEASE_MANIFEST = releases/${CLOUD_PROVIDER}/manifest.yaml +## Inject the app version into project.Version +LDFLAGS ?= "-ldflags=-X=github.com/awslabs/karpenter/pkg/utils/project.Version=${RELEASE_VERSION}" +GOFLAGS ?= "-tags=${CLOUD_PROVIDER} ${LDFLAGS}" WITH_GOFLAGS = GOFLAGS=${GOFLAGS} WITH_RELEASE_REPO = KO_DOCKER_REPO=${RELEASE_REPO} diff --git a/pkg/cloudprovider/aws/factory.go b/pkg/cloudprovider/aws/factory.go index f727b6ba44d1..a782d724d574 100644 --- a/pkg/cloudprovider/aws/factory.go +++ b/pkg/cloudprovider/aws/factory.go @@ -13,11 +13,13 @@ limitations under the License. package aws import ( + "fmt" "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/ec2metadata" "github.com/aws/aws-sdk-go/aws/endpoints" + "github.com/aws/aws-sdk-go/aws/request" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/iam" @@ -26,6 +28,7 @@ import ( "github.com/awslabs/karpenter/pkg/cloudprovider" "github.com/awslabs/karpenter/pkg/packing" "github.com/awslabs/karpenter/pkg/utils/log" + "github.com/awslabs/karpenter/pkg/utils/project" "github.com/patrickmn/go-cache" ) @@ -50,7 +53,7 @@ type Factory struct { } func NewFactory(options cloudprovider.Options) *Factory { - sess := withRegion(session.Must(session.NewSession(&aws.Config{STSRegionalEndpoint: endpoints.RegionalSTSEndpoint}))) + sess := withUserAgent(withRegion(session.Must(session.NewSession(&aws.Config{STSRegionalEndpoint: endpoints.RegionalSTSEndpoint})))) ec2api := ec2.New(sess) subnetProvider := &SubnetProvider{ ec2api: ec2api, @@ -101,3 +104,10 @@ func withRegion(sess *session.Session) *session.Session { sess.Config.Region = aws.String(region) return sess } + +// withUserAgent adds a karpenter specific user-agent string to AWS session +func withUserAgent(sess *session.Session) *session.Session { + userAgent := fmt.Sprintf("karpenter.sh-%s", project.Version) + sess.Handlers.Build.PushBack(request.MakeAddToUserAgentFreeFormHandler(userAgent)) + return sess +} diff --git a/pkg/utils/project/project.go b/pkg/utils/project/project.go index 90f9466d1487..045547a21fde 100644 --- a/pkg/utils/project/project.go +++ b/pkg/utils/project/project.go @@ -19,6 +19,12 @@ import ( "runtime" ) +var ( + // Version is the karpenter app version injected during compilation + // when using the Makefile + Version = "unspecified" +) + func RelativeToRoot(path string) string { _, file, _, _ := runtime.Caller(0) manifestsRoot := filepath.Join(filepath.Dir(file), "..", "..", "..")