Skip to content

Commit 80c64e8

Browse files
committed
Append aws-cloud-map-mcs-controller-for-k8s specific User-Agent to AWS Cloudmap Requests.
1 parent 4d38220 commit 80c64e8

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

pkg/cloudmap/aws_facade.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package cloudmap
22

33
import (
44
"context"
5+
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/version"
6+
"github.com/aws/aws-sdk-go-v2/aws/middleware"
57

68
"github.com/aws/aws-sdk-go-v2/aws"
79
sd "github.com/aws/aws-sdk-go-v2/service/servicediscovery"
@@ -44,5 +46,9 @@ type awsFacade struct {
4446

4547
// NewAwsFacadeFromConfig creates a new AWS facade from an AWS client config.
4648
func NewAwsFacadeFromConfig(cfg *aws.Config) AwsFacade {
47-
return &awsFacade{sd.NewFromConfig(*cfg)}
49+
sdClient := sd.NewFromConfig(*cfg, func(options *sd.Options) {
50+
// Append User-Agent to all the request, the format is going to be aws-cloud-map-mcs-controller-for-k8s/0.0.0-abc
51+
options.APIOptions = append(options.APIOptions, middleware.AddUserAgentKeyValue(version.GetUserAgentKey(), version.GetUserAgentValue()))
52+
})
53+
return &awsFacade{sdClient}
4854
}

pkg/controllers/multicluster/serviceexport_controller.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,11 @@ func (r *ServiceExportReconciler) Reconcile(ctx context.Context, req ctrl.Reques
7171
namespacedName := types.NamespacedName{Namespace: serviceExport.Namespace, Name: serviceExport.Name}
7272
if err := r.Client.Get(ctx, namespacedName, &service); err != nil {
7373
if errors.IsNotFound(err) {
74-
r.Log.Info("no Service found, deleting the ServiceExport",
75-
"Namespace", serviceExport.Namespace, "Name", serviceExport.Name)
74+
r.Log.Info("no Service found, deleting the ServiceExport", "Namespace", serviceExport.Namespace, "Name", serviceExport.Name)
7675
// Mark ServiceExport to be deleted, if the corresponding Service is not found
7776
isServiceExportMarkedForDelete = true
7877
} else {
79-
r.Log.Error(err, "error fetching Service",
80-
"Namespace", serviceExport.Namespace, "Name", serviceExport.Name)
78+
r.Log.Error(err, "error fetching Service", "Namespace", serviceExport.Namespace, "Name", serviceExport.Name)
8179
return ctrl.Result{}, nil
8280
}
8381
}
@@ -238,9 +236,7 @@ func (r *ServiceExportReconciler) extractEndpoints(ctx context.Context, svc *v1.
238236
}
239237

240238
attributes := make(map[string]string)
241-
if version.GetVersion() != "" {
242-
attributes[model.K8sVersionAttr] = version.PackageName + " " + version.GetVersion()
243-
}
239+
attributes[model.K8sVersionAttr] = version.GetPackageVersion()
244240

245241
endpoints := make([]*model.Endpoint, 0)
246242
for _, slice := range endpointSlices.Items {

pkg/version/version.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,18 @@ func GetVersion() string {
2222

2323
return ""
2424
}
25+
26+
func GetPackageVersion() string {
27+
return PackageName + " " + GetVersion()
28+
}
29+
30+
func GetUserAgentKey() string {
31+
return PackageName
32+
}
33+
34+
func GetUserAgentValue() string {
35+
if GitVersion != "" {
36+
return strings.TrimPrefix(GitVersion, "v")
37+
}
38+
return ""
39+
}

0 commit comments

Comments
 (0)