Skip to content

Commit 59b0922

Browse files
authored
Extract controller build version using ldflags and store it in Cloud Map (#16)
1 parent f4df306 commit 59b0922

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ COPY . ./
1111
RUN go mod download
1212

1313
# Build
14-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go
14+
ENV PKG=github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/version
15+
RUN GIT_TAG=$(git describe --tags --dirty --always) && \
16+
GIT_COMMIT=$(git describe --dirty --always) && \
17+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build \
18+
-ldflags="-s -w -X ${PKG}.GitVersion=${GIT_TAG} -X ${PKG}.GitCommit=${GIT_COMMIT}" -a -o manager main.go
1519

1620
# Use distroless as minimal base image to package the manager binary
1721
# Refer to https://github.com/GoogleContainerTools/distroless for more details

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
GIT_COMMIT:=$(shell git describe --dirty --always)
2+
GIT_TAG:=$(shell git describe --dirty --always --tags)
3+
PKG:=github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/version
14

25
# Image URL to use all building/pushing image targets
36
IMG ?= controller:latest
@@ -70,7 +73,7 @@ e2e-test: manifests kustomize kubetest2 fmt vet
7073
##@ Build
7174

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

7578
run: manifests generate generate-mocks fmt vet ## Run a controller from your host.
7679
go run ./main.go

main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ package main
1919
import (
2020
"context"
2121
"flag"
22+
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/cloudmap"
23+
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/version"
2224
"github.com/aws/aws-sdk-go-v2/config"
2325
"os"
2426

25-
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/cloudmap"
26-
2727
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2828
// to ensure that exec-entrypoint and run can make use of them.
2929
_ "k8s.io/client-go/plugin/pkg/client/auth"
@@ -69,6 +69,9 @@ func main() {
6969

7070
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
7171

72+
v := version.GetVersion()
73+
setupLog.Info("starting AWS Cloud Map MCS Controller for K8s", "version", v)
74+
7275
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
7376
Scheme: scheme,
7477
MetricsBindAddress: metricsAddr,

pkg/controllers/serviceexport_controller.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/cloudmap"
2323
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/model"
24+
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/version"
2425
v1 "k8s.io/api/core/v1"
2526
discovery "k8s.io/api/discovery/v1beta1"
2627
"k8s.io/apimachinery/pkg/types"
@@ -198,12 +199,17 @@ func (r *ServiceExportReconciler) extractEndpoints(ctx context.Context, svc *v1.
198199
for _, port := range slice.Ports {
199200
for _, ep := range slice.Endpoints {
200201
for _, IP := range ep.Addresses {
202+
attributes := make(map[string]string, 0)
203+
if version.GetVersion() != "" {
204+
attributes["K8S_CONTROLLER"] = version.PackageName + " " + version.GetVersion()
205+
}
206+
// TODO extract attributes - pod, node and other useful details if possible
207+
201208
result = append(result, &model.Endpoint{
202209
Id: model.EndpointIdFromIPAddress(IP),
203210
IP: IP,
204211
Port: *port.Port,
205-
Attributes: make(map[string]string, 0),
206-
// TODO extract attributes - pod, node and other useful details if possible
212+
Attributes: attributes,
207213
})
208214
}
209215
}

pkg/version/version.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// Build information obtained with the help of -ldflags
9+
var (
10+
GitVersion string
11+
GitCommit string
12+
PackageName = "aws-cloud-map-mcs-controller-for-k8s"
13+
)
14+
15+
// GetVersion figures out the version information
16+
// based on variables set by -ldflags.
17+
func GetVersion() string {
18+
// only set the appVersion if -ldflags was used
19+
if GitCommit != "" {
20+
return fmt.Sprintf("%s (%s)", strings.TrimPrefix(GitVersion, "v"), GitCommit)
21+
}
22+
23+
return ""
24+
}

0 commit comments

Comments
 (0)