Skip to content

Commit 59fb829

Browse files
authored
Implemented Target Allocator Container (aws#214)
1 parent 099460a commit 59fb829

39 files changed

+5681
-47
lines changed

Makefile

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ AUTO_INSTRUMENTATION_PYTHON_VERSION ?= "$(shell grep -v '\#' versions.txt | grep
88
AUTO_INSTRUMENTATION_DOTNET_VERSION ?= "$(shell grep -v '\#' versions.txt | grep aws-otel-dotnet-instrumentation | awk -F= '{print $$2}')"
99
DCGM_EXPORTER_VERSION ?= "$(shell grep -v '\#' versions.txt | grep dcgm-exporter | awk -F= '{print $$2}')"
1010
NEURON_MONITOR_VERSION ?= "$(shell grep -v '\#' versions.txt | grep neuron-monitor | awk -F= '{print $$2}')"
11-
11+
TARGET_ALLOCATOR_VERSION ?= $(shell grep -v '\#' versions.txt | grep target-allocator | awk -F= '{print $$2}')
1212
# Image URL to use all building/pushing image targets
1313
IMG_PREFIX ?= aws
1414
IMG_REPO ?= cloudwatch-agent-operator
1515
IMG ?= ${IMG_PREFIX}/${IMG_REPO}:${VERSION}
1616
ARCH ?= $(shell go env GOARCH)
1717

18+
TARGET_ALLOCATOR_IMG_REPO ?= target-allocator
19+
TARGET_ALLOCATOR_IMG ?= ${IMG_PREFIX}/${TARGET_ALLOCATOR_IMG_REPO}:${TARGET_ALLOCATOR_VERSION}
20+
1821
# Options for 'bundle-build'
1922
ifneq ($(origin CHANNELS), undefined)
2023
BUNDLE_CHANNELS := --channels=$(CHANNELS)
@@ -95,6 +98,10 @@ test: generate fmt vet envtest
9598
.PHONY: manager
9699
manager: generate fmt vet
97100
go build -o bin/manager main.go
101+
# Build target allocator binary
102+
.PHONY: targetallocator
103+
targetallocator:
104+
cd cmd/amazon-cloudwatch-agent-target-allocator && CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(ARCH) go build -installsuffix cgo -o bin/targetallocator_${ARCH} -ldflags "${LDFLAGS}" .
98105

99106
# Run against the configured Kubernetes cluster in ~/.kube/config
100107
.PHONY: run
@@ -161,6 +168,19 @@ container:
161168
container-push:
162169
docker push ${IMG}
163170

171+
.PHONY: container-target-allocator-push
172+
container-target-allocator-push:
173+
docker push ${TARGET_ALLOCATOR_IMG}
174+
175+
.PHONY: container-target-allocator
176+
container-target-allocator: GOOS = linux
177+
container-target-allocator: targetallocator
178+
docker buildx build --load --platform linux/${ARCH} -t ${TARGET_ALLOCATOR_IMG} cmd/amazon-cloudwatch-agent-target-allocator
179+
180+
.PHONY: ta-build-and-push
181+
ta-build-and-push: container-target-allocator
182+
ta-build-and-push: container-target-allocator-push
183+
164184
.PHONY: kustomize
165185
kustomize: ## Download kustomize locally if necessary.
166186
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Get CA certificates from the Alpine package repo
2+
FROM alpine:3.18 as certificates
3+
4+
RUN apk --no-cache add ca-certificates
5+
6+
# Start a new stage from scratch
7+
FROM scratch
8+
9+
ARG TARGETARCH
10+
11+
WORKDIR /root/
12+
13+
# Copy the certs
14+
COPY --from=certificates /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
15+
16+
# Copy binary built on the host
17+
COPY bin/targetallocator_${TARGETARCH} ./main
18+
19+
ENTRYPOINT ["./main"]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package allocation
5+
6+
import (
7+
"fmt"
8+
"strconv"
9+
10+
"github.com/prometheus/common/model"
11+
12+
"github.com/aws/amazon-cloudwatch-agent-operator/cmd/amazon-cloudwatch-agent-target-allocator/target"
13+
)
14+
15+
func colIndex(index, numCols int) int {
16+
if numCols == 0 {
17+
return -1
18+
}
19+
return index % numCols
20+
}
21+
22+
func MakeNNewTargets(n int, numCollectors int, startingIndex int) map[string]*target.Item {
23+
toReturn := map[string]*target.Item{}
24+
for i := startingIndex; i < n+startingIndex; i++ {
25+
collector := fmt.Sprintf("collector-%d", colIndex(i, numCollectors))
26+
label := model.LabelSet{
27+
"collector": model.LabelValue(collector),
28+
"i": model.LabelValue(strconv.Itoa(i)),
29+
"total": model.LabelValue(strconv.Itoa(n + startingIndex)),
30+
}
31+
newTarget := target.NewItem(fmt.Sprintf("test-job-%d", i), fmt.Sprintf("test-url-%d", i), label, collector)
32+
toReturn[newTarget.Hash()] = newTarget
33+
}
34+
return toReturn
35+
}
36+
37+
func MakeNCollectors(n int, startingIndex int) map[string]*Collector {
38+
toReturn := map[string]*Collector{}
39+
for i := startingIndex; i < n+startingIndex; i++ {
40+
collector := fmt.Sprintf("collector-%d", i)
41+
toReturn[collector] = &Collector{
42+
Name: collector,
43+
NumTargets: 0,
44+
}
45+
}
46+
return toReturn
47+
}
48+
49+
func MakeNNewTargetsWithEmptyCollectors(n int, startingIndex int) map[string]*target.Item {
50+
toReturn := map[string]*target.Item{}
51+
for i := startingIndex; i < n+startingIndex; i++ {
52+
label := model.LabelSet{
53+
"i": model.LabelValue(strconv.Itoa(i)),
54+
"total": model.LabelValue(strconv.Itoa(n + startingIndex)),
55+
}
56+
newTarget := target.NewItem(fmt.Sprintf("test-job-%d", i), fmt.Sprintf("test-url-%d", i), label, "")
57+
toReturn[newTarget.Hash()] = newTarget
58+
}
59+
return toReturn
60+
}

0 commit comments

Comments
 (0)