Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ eks-test:
##@ Build

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

run: manifests generate generate-mocks fmt vet ## Run a controller from your host.
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/multicluster/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion pkg/cloudmap/aws_facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package cloudmap
import (
"context"

"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/version"
"github.com/aws/aws-sdk-go-v2/aws/middleware"

"github.com/aws/aws-sdk-go-v2/aws"
sd "github.com/aws/aws-sdk-go-v2/service/servicediscovery"
)
Expand Down Expand Up @@ -44,5 +47,9 @@ type awsFacade struct {

// NewAwsFacadeFromConfig creates a new AWS facade from an AWS client config.
func NewAwsFacadeFromConfig(cfg *aws.Config) AwsFacade {
return &awsFacade{sd.NewFromConfig(*cfg)}
sdClient := sd.NewFromConfig(*cfg, func(options *sd.Options) {
// Append User-Agent to all the request, the format is going to be aws-cloud-map-mcs-controller-for-k8s/0.0.0-abc
options.APIOptions = append(options.APIOptions, middleware.AddUserAgentKeyValue(version.GetUserAgentKey(), version.GetUserAgentValue()))
})
return &awsFacade{sdClient}
}
5 changes: 5 additions & 0 deletions pkg/cloudmap/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ func TestServiceDiscoveryClient_RegisterEndpoints(t *testing.T) {
model.EndpointHostnameAttr: test.Hostname,
model.EndpointNodeNameAttr: test.Nodename,
model.ServiceExportCreationAttr: strconv.FormatInt(test.SvcExportCreationTimestamp, 10),
model.K8sVersionAttr: test.PackageVersion,
}
attrs2 := map[string]string{
model.ClusterIdAttr: test.ClusterId1,
Expand All @@ -327,6 +328,7 @@ func TestServiceDiscoveryClient_RegisterEndpoints(t *testing.T) {
model.EndpointHostnameAttr: test.Hostname,
model.EndpointNodeNameAttr: test.Nodename,
model.ServiceExportCreationAttr: strconv.FormatInt(test.SvcExportCreationTimestamp, 10),
model.K8sVersionAttr: test.PackageVersion,
}

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

func getTestSdClient(t *testing.T) *testSdClient {
test.SetTestVersion()
mockController := gomock.NewController(t)
mockCache := cloudmapMock.NewMockServiceDiscoveryClientCache(mockController)
mockApi := cloudmapMock.NewMockServiceDiscoveryApi(mockController)
Expand Down Expand Up @@ -409,6 +412,7 @@ func getHttpInstanceSummaryForTest() []types.HttpInstanceSummary {
model.EndpointHostnameAttr: test.Hostname,
model.EndpointNodeNameAttr: test.Nodename,
model.ServiceExportCreationAttr: strconv.FormatInt(test.SvcExportCreationTimestamp, 10),
model.K8sVersionAttr: test.PackageVersion,
},
},
{
Expand All @@ -429,6 +433,7 @@ func getHttpInstanceSummaryForTest() []types.HttpInstanceSummary {
model.EndpointHostnameAttr: test.Hostname,
model.EndpointNodeNameAttr: test.Nodename,
model.ServiceExportCreationAttr: strconv.FormatInt(test.SvcExportCreationTimestamp, 10),
model.K8sVersionAttr: test.PackageVersion,
},
},
}
Expand Down
10 changes: 3 additions & 7 deletions pkg/controllers/multicluster/serviceexport_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,11 @@ func (r *ServiceExportReconciler) Reconcile(ctx context.Context, req ctrl.Reques
namespacedName := types.NamespacedName{Namespace: serviceExport.Namespace, Name: serviceExport.Name}
if err := r.Client.Get(ctx, namespacedName, &service); err != nil {
if errors.IsNotFound(err) {
r.Log.Info("no Service found, deleting the ServiceExport",
"Namespace", serviceExport.Namespace, "Name", serviceExport.Name)
r.Log.Info("no Service found, deleting the ServiceExport", "Namespace", serviceExport.Namespace, "Name", serviceExport.Name)
// Mark ServiceExport to be deleted, if the corresponding Service is not found
isServiceExportMarkedForDelete = true
} else {
r.Log.Error(err, "error fetching Service",
"Namespace", serviceExport.Namespace, "Name", serviceExport.Name)
r.Log.Error(err, "error fetching Service", "Namespace", serviceExport.Namespace, "Name", serviceExport.Name)
return ctrl.Result{}, nil
}
}
Expand Down Expand Up @@ -238,9 +236,7 @@ func (r *ServiceExportReconciler) extractEndpoints(ctx context.Context, svc *v1.
}

attributes := make(map[string]string)
if version.GetVersion() != "" {
attributes[model.K8sVersionAttr] = version.PackageName + " " + version.GetVersion()
}
attributes[model.K8sVersionAttr] = version.GetPackageVersion()

endpoints := make([]*model.Endpoint, 0)
for _, slice := range endpointSlices.Items {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ func getServiceExportScheme() *runtime.Scheme {
}

func getServiceExportReconciler(t *testing.T, mockClient *cloudmapMock.MockServiceDiscoveryClient, client client.Client) *ServiceExportReconciler {
test.SetTestVersion()
return &ServiceExportReconciler{
Client: client,
Log: common.NewLoggerWithLogr(testr.New(t)),
Expand Down
15 changes: 15 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,18 @@ func GetVersion() string {

return ""
}

func GetPackageVersion() string {
return PackageName + " " + GetVersion()
}

func GetUserAgentKey() string {
return PackageName
}

func GetUserAgentValue() string {
if GitVersion != "" {
return strings.TrimPrefix(GitVersion, "v")
}
return ""
}
11 changes: 9 additions & 2 deletions test/test-constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
"fmt"

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

aboutv1alpha1 "github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/apis/about/v1alpha1"
Expand Down Expand Up @@ -46,8 +47,14 @@ const (
SvcExportCreationTimestamp int64 = 1640995200000
Hostname = "host"
Nodename = "node"
PackageVersion = "aws-cloud-map-mcs-controller-for-k8s 0.0.1 (abcd)"
)

func SetTestVersion() {
version.GitVersion = "v0.0.1"
version.GitCommit = "abcd"
}

func GetTestHttpNamespace() *model.Namespace {
return &model.Namespace{
Id: HttpNsId,
Expand Down Expand Up @@ -111,7 +118,7 @@ func GetTestEndpoint1() *model.Endpoint {
ClusterSetId: ClusterSet,
ServiceType: model.ClusterSetIPType,
ServiceExportCreationTimestamp: SvcExportCreationTimestamp,
Attributes: make(map[string]string),
Attributes: map[string]string{model.K8sVersionAttr: PackageVersion},
}
}

Expand All @@ -137,7 +144,7 @@ func GetTestEndpoint2() *model.Endpoint {
ClusterSetId: ClusterSet,
ServiceType: model.ClusterSetIPType,
ServiceExportCreationTimestamp: SvcExportCreationTimestamp,
Attributes: make(map[string]string),
Attributes: map[string]string{model.K8sVersionAttr: PackageVersion},
}
}

Expand Down