Skip to content

Commit 70e24a4

Browse files
authored
Add User-Agent (#215)
* Append aws-cloud-map-mcs-controller-for-k8s specific User-Agent to AWS Cloudmap Requests. * Add K8S_CONTROLLER attributes unit tests * Running goimports and lint
1 parent cff581f commit 70e24a4

File tree

8 files changed

+43
-12
lines changed

8 files changed

+43
-12
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ eks-test:
122122
##@ Build
123123

124124
.DEFAULT: build
125-
build: manifests generate generate-mocks fmt vet lint ## Build manager binary.
125+
build: manifests generate generate-mocks fmt vet goimports lint ## Build manager binary.
126126
go build -ldflags="-s -w -X ${PKG}.GitVersion=${GIT_TAG} -X ${PKG}.GitCommit=${GIT_COMMIT}" -o bin/manager main.go
127127

128128
run: manifests generate generate-mocks fmt vet ## Run a controller from your host.

pkg/apis/multicluster/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cloudmap/aws_facade.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package cloudmap
33
import (
44
"context"
55

6+
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/version"
7+
"github.com/aws/aws-sdk-go-v2/aws/middleware"
8+
69
"github.com/aws/aws-sdk-go-v2/aws"
710
sd "github.com/aws/aws-sdk-go-v2/service/servicediscovery"
811
)
@@ -44,5 +47,9 @@ type awsFacade struct {
4447

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

pkg/cloudmap/client_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ func TestServiceDiscoveryClient_RegisterEndpoints(t *testing.T) {
310310
model.EndpointHostnameAttr: test.Hostname,
311311
model.EndpointNodeNameAttr: test.Nodename,
312312
model.ServiceExportCreationAttr: strconv.FormatInt(test.SvcExportCreationTimestamp, 10),
313+
model.K8sVersionAttr: test.PackageVersion,
313314
}
314315
attrs2 := map[string]string{
315316
model.ClusterIdAttr: test.ClusterId1,
@@ -327,6 +328,7 @@ func TestServiceDiscoveryClient_RegisterEndpoints(t *testing.T) {
327328
model.EndpointHostnameAttr: test.Hostname,
328329
model.EndpointNodeNameAttr: test.Nodename,
329330
model.ServiceExportCreationAttr: strconv.FormatInt(test.SvcExportCreationTimestamp, 10),
331+
model.K8sVersionAttr: test.PackageVersion,
330332
}
331333

332334
tc.mockApi.EXPECT().RegisterInstance(context.TODO(), test.SvcId, test.EndptId1, attrs1).
@@ -370,6 +372,7 @@ func TestServiceDiscoveryClient_DeleteEndpoints(t *testing.T) {
370372
}
371373

372374
func getTestSdClient(t *testing.T) *testSdClient {
375+
test.SetTestVersion()
373376
mockController := gomock.NewController(t)
374377
mockCache := cloudmapMock.NewMockServiceDiscoveryClientCache(mockController)
375378
mockApi := cloudmapMock.NewMockServiceDiscoveryApi(mockController)
@@ -409,6 +412,7 @@ func getHttpInstanceSummaryForTest() []types.HttpInstanceSummary {
409412
model.EndpointHostnameAttr: test.Hostname,
410413
model.EndpointNodeNameAttr: test.Nodename,
411414
model.ServiceExportCreationAttr: strconv.FormatInt(test.SvcExportCreationTimestamp, 10),
415+
model.K8sVersionAttr: test.PackageVersion,
412416
},
413417
},
414418
{
@@ -429,6 +433,7 @@ func getHttpInstanceSummaryForTest() []types.HttpInstanceSummary {
429433
model.EndpointHostnameAttr: test.Hostname,
430434
model.EndpointNodeNameAttr: test.Nodename,
431435
model.ServiceExportCreationAttr: strconv.FormatInt(test.SvcExportCreationTimestamp, 10),
436+
model.K8sVersionAttr: test.PackageVersion,
432437
},
433438
},
434439
}

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/controllers/multicluster/serviceexport_controller_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ func getServiceExportScheme() *runtime.Scheme {
205205
}
206206

207207
func getServiceExportReconciler(t *testing.T, mockClient *cloudmapMock.MockServiceDiscoveryClient, client client.Client) *ServiceExportReconciler {
208+
test.SetTestVersion()
208209
return &ServiceExportReconciler{
209210
Client: client,
210211
Log: common.NewLoggerWithLogr(testr.New(t)),

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+
}

test/test-constants.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package test
33
import (
44
"fmt"
55

6+
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/version"
67
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
78

89
aboutv1alpha1 "github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/apis/about/v1alpha1"
@@ -46,8 +47,14 @@ const (
4647
SvcExportCreationTimestamp int64 = 1640995200000
4748
Hostname = "host"
4849
Nodename = "node"
50+
PackageVersion = "aws-cloud-map-mcs-controller-for-k8s 0.0.1 (abcd)"
4951
)
5052

53+
func SetTestVersion() {
54+
version.GitVersion = "v0.0.1"
55+
version.GitCommit = "abcd"
56+
}
57+
5158
func GetTestHttpNamespace() *model.Namespace {
5259
return &model.Namespace{
5360
Id: HttpNsId,
@@ -111,7 +118,7 @@ func GetTestEndpoint1() *model.Endpoint {
111118
ClusterSetId: ClusterSet,
112119
ServiceType: model.ClusterSetIPType,
113120
ServiceExportCreationTimestamp: SvcExportCreationTimestamp,
114-
Attributes: make(map[string]string),
121+
Attributes: map[string]string{model.K8sVersionAttr: PackageVersion},
115122
}
116123
}
117124

@@ -137,7 +144,7 @@ func GetTestEndpoint2() *model.Endpoint {
137144
ClusterSetId: ClusterSet,
138145
ServiceType: model.ClusterSetIPType,
139146
ServiceExportCreationTimestamp: SvcExportCreationTimestamp,
140-
Attributes: make(map[string]string),
147+
Attributes: map[string]string{model.K8sVersionAttr: PackageVersion},
141148
}
142149
}
143150

0 commit comments

Comments
 (0)