Skip to content

Implemented TargetAllocator resource deployments. #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 105 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 96 commits
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
4b1eb31
Implemented TargetAllocator deployment and other dependencies from up…
musa-asad Aug 20, 2024
0e079bd
Added unit tests, organization, and relevant build files.
musa-asad Aug 20, 2024
ca26265
Fixed tests.
musa-asad Aug 20, 2024
3a871af
Update crds.
musa-asad Aug 20, 2024
0755622
Updated docs.
musa-asad Aug 20, 2024
a77533f
Reversed crd changes.
musa-asad Aug 20, 2024
1d8a923
Generated files.
musa-asad Aug 20, 2024
15a2e2b
Updated webhooks.
musa-asad Aug 20, 2024
2c7b788
Updated config_replace.go to include allocator logic.
musa-asad Aug 20, 2024
4202fae
Revert "Updated config_replace.go to include allocator logic."
musa-asad Aug 20, 2024
ea3c937
Updated doc and imports.
musa-asad Aug 20, 2024
0ef5529
Revert "Updated doc and imports."
musa-asad Aug 20, 2024
ec14fe5
Revert "Updated webhooks."
musa-asad Aug 20, 2024
d324ccf
Replica if enabled.
musa-asad Aug 20, 2024
2979609
Auto-generated.
musa-asad Aug 20, 2024
1235400
Added default config.
musa-asad Aug 20, 2024
db8c52f
Revert "Added default config."
musa-asad Aug 20, 2024
6da51f6
Removed errors for testing.
musa-asad Aug 21, 2024
36d1c6c
Revert "Removed errors for testing."
musa-asad Aug 21, 2024
3cdebd9
Use prometheusConfig.
musa-asad Aug 21, 2024
cfe2dbe
Fix version.
musa-asad Aug 21, 2024
c220dd5
Included validation in webhooks.
musa-asad Aug 21, 2024
561b2ff
Use prometheus config.
musa-asad Aug 21, 2024
60a2856
Updated webook test and added types to v1alpha2.
musa-asad Aug 21, 2024
f8720f4
Fixed test.
musa-asad Aug 21, 2024
75a0168
Removed extra comma.
musa-asad Aug 21, 2024
a1860bd
Auto-generated files.
musa-asad Aug 21, 2024
fe690fd
Removed pod mode validation.
musa-asad Aug 21, 2024
96080d2
Made prometheusConfig format specific to its use-case.
musa-asad Aug 22, 2024
9939f55
Use config instead of global for prometheusConfig yaml.
musa-asad Aug 22, 2024
fad09e9
Addressed some comments: yaml, target-allocator, and use consistent-h…
musa-asad Aug 29, 2024
1102dc1
Changed to TARGET_ALLOCATOR.
musa-asad Aug 29, 2024
ceaaa49
Removed unused escape 16326 logic.
musa-asad Aug 29, 2024
d77d772
Use Prometheus naming convention and fixed Makefile.
musa-asad Aug 29, 2024
d530489
Reverted yaml name.
musa-asad Aug 29, 2024
6002c81
Deploy Prometheus ConfigMap, Volume, and VolumeMount.
musa-asad Aug 30, 2024
2fffbdb
Spacing.
musa-asad Aug 30, 2024
e254d68
Spacing.
musa-asad Aug 30, 2024
15bfd18
Update E2E file name (#213)
harrryr Aug 30, 2024
95a68d1
Adding support for NodeJS auto instrumentation and integ tests (#220)
Paramadon Sep 3, 2024
9aabd53
Version bumps and updating release notes for v1.7.0 release (#221)
mitali-salvi Sep 3, 2024
681ec79
Added unit test for checking added volume.
musa-asad Sep 4, 2024
cff2d6e
Added configmap testing for prometheus.
musa-asad Sep 4, 2024
622570f
Enforce statefulset.
musa-asad Sep 4, 2024
c6a15f3
Implemented ReplacePrometheusConfig to allow for service discovery en…
musa-asad Sep 5, 2024
249c7f9
Fixed incorrect parsing.
musa-asad Sep 6, 2024
48099b4
Added testing for service discovery changes.
musa-asad Sep 8, 2024
ad907fd
Add testing for config_replace.go.
musa-asad Sep 8, 2024
85387a1
Fixing version.txt version of dotnet and nodejs (#228)
Paramadon Sep 9, 2024
25d547c
Fixing integration tests for EKS add-on with the change to separate C…
mitali-salvi Sep 9, 2024
af44378
Support configurable resources for NodeJS. (#225)
musa-asad Sep 9, 2024
a5c350e
Use target allocator service account name.
musa-asad Sep 9, 2024
cbf802f
Give a set name for the target allocator service account.
musa-asad Sep 9, 2024
8b9af0a
Fixed naming.
musa-asad Sep 9, 2024
e79ca42
Updated featuregate description.
musa-asad Sep 10, 2024
3e190b3
Fixed labels, comments, and naming conventions.
musa-asad Sep 10, 2024
f51816b
Change test failure to echo a warning when checking k8s version depen…
lisguo Sep 11, 2024
194b0b8
Re-introduce double dollar env substitution logic.
musa-asad Sep 12, 2024
12fcab9
Migrated prometheus type from string to PrometheusConfig struct.
musa-asad Sep 13, 2024
8a60455
Use yaml2 for configmap_test.go.
musa-asad Sep 13, 2024
28164a4
Add changes to v1alpha2.
musa-asad Sep 13, 2024
8516295
Implement marshaling and unmarshaling for AnyConfig.
musa-asad Sep 13, 2024
a8852ce
Add TA version to labels.
musa-asad Sep 13, 2024
a32bbc0
Open Application Signals ports on CWAgent only when enabled (#230)
Paramadon Sep 13, 2024
d84410b
Merge branch 'main' into target-allocator-v0.90.0
musa-asad Sep 18, 2024
55c57ff
Merge branch 'main' into merge-main
musa-asad Sep 18, 2024
8134bde
Merge branch 'target-allocator' into target-allocator-v0.90.0
musa-asad Sep 18, 2024
377104f
Supporting K8s 1.31 (#222)
mitali-salvi Sep 18, 2024
d124688
Merge branch 'aws:main' into target-allocator-v0.90.0
musa-asad Sep 19, 2024
646940a
Merge branch 'target-allocator' into target-allocator-v0.90.0
musa-asad Sep 19, 2024
130eee3
Fix version back.
musa-asad Sep 19, 2024
7512b4b
Don't unescape 93073 for prometheus config.
musa-asad Sep 22, 2024
87964bf
Fix test.
musa-asad Sep 23, 2024
0afedf1
Fix test.
musa-asad Sep 23, 2024
95d9c66
Removed double dollar to single dollar substitution logic.
musa-asad Sep 24, 2024
ce3edda
Removed more double dollar usages.
musa-asad Sep 24, 2024
b6faf59
Fix import.
musa-asad Sep 24, 2024
e07b0a6
Remove collector_id from httpsd logic.
musa-asad Sep 25, 2024
e432bd9
Remove unused proxy environmental variables.
musa-asad Sep 25, 2024
71d6405
Remove unused proxy tests.
musa-asad Sep 25, 2024
db066a0
Revert "Merge branch 'main' into target-allocator-v0.90.0"
musa-asad Sep 25, 2024
f4783e2
Revert "Merge branch 'target-allocator' into target-allocator-v0.90.0"
musa-asad Sep 25, 2024
923cb42
Revert "Merge branch 'aws:main' into target-allocator-v0.90.0"
musa-asad Sep 25, 2024
32e6e72
Revert "Merge branch 'target-allocator' into target-allocator-v0.90.0"
musa-asad Sep 25, 2024
cf4a6ad
Revert "Merge branch 'main' into merge-main"
musa-asad Sep 25, 2024
05fccd4
Revert last 5 commits.
musa-asad Sep 25, 2024
dab4047
Addressed comments.
musa-asad Sep 25, 2024
2df53f0
Merge remote-tracking branch 'upstream/target-allocator' into target-…
musa-asad Sep 25, 2024
0f3ca46
Updated configmap format.
musa-asad Sep 26, 2024
712bf31
Fixed issues.
musa-asad Sep 26, 2024
9a1c4e4
Made prometheus configmap compatible with current agent prometheus in…
musa-asad Sep 26, 2024
e0ddd4a
Made service name constant.
musa-asad Sep 26, 2024
0c6673e
Revert "Made service name constant."
musa-asad Sep 26, 2024
371812e
Make service name constant.
musa-asad Sep 26, 2024
a4e74da
Fix Makefile.
musa-asad Sep 26, 2024
557c263
Remove comment.
musa-asad Sep 27, 2024
9ec6ae7
Use ConfigMapEntry().
musa-asad Sep 27, 2024
f3446ec
Use function for service name.
musa-asad Sep 27, 2024
acd158f
Use constants isntead of ConfigMapEntry().
musa-asad Sep 27, 2024
4946b79
Use function for TAService().
musa-asad Sep 27, 2024
801e9eb
Use ConfigMapEntry() with fixed tests.
musa-asad Sep 27, 2024
2519ad1
Test ConfigMapEntry() for TA.
musa-asad Sep 27, 2024
deb08d5
Made target port 8080.
musa-asad Sep 27, 2024
ae90c31
Addressed nits.
musa-asad Sep 27, 2024
1986ad7
Fix imports.
musa-asad Oct 1, 2024
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
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ ARG AUTO_INSTRUMENTATION_DOTNET_VERSION
ARG AUTO_INSTRUMENTATION_NODEJS_VERSION
ARG DCMG_EXPORTER_VERSION
ARG NEURON_MONITOR_VERSION
ARG TARGET_ALLOCATOR_VERSION

# Build
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -ldflags="-X ${VERSION_PKG}.version=${VERSION} -X ${VERSION_PKG}.buildDate=${VERSION_DATE} -X ${VERSION_PKG}.agent=${AGENT_VERSION} -X ${VERSION_PKG}.autoInstrumentationJava=${AUTO_INSTRUMENTATION_JAVA_VERSION} -X ${VERSION_PKG}.autoInstrumentationPython=${AUTO_INSTRUMENTATION_PYTHON_VERSION} -X ${VERSION_PKG}.autoInstrumentationDotNet=${AUTO_INSTRUMENTATION_DOTNET_VERSION} -X ${VERSION_PKG}.autoInstrumentationNodeJS=${AUTO_INSTRUMENTATION_NODEJS_VERSION} -X ${VERSION_PKG}.dcgmExporter=${DCMG_EXPORTER_VERSION} -X ${VERSION_PKG}.neuronMonitor=${NEURON_MONITOR_VERSION}" -a -o manager main.go
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -ldflags="-X ${VERSION_PKG}.version=${VERSION} -X ${VERSION_PKG}.buildDate=${VERSION_DATE} -X ${VERSION_PKG}.agent=${AGENT_VERSION} -X ${VERSION_PKG}.autoInstrumentationJava=${AUTO_INSTRUMENTATION_JAVA_VERSION} -X ${VERSION_PKG}.autoInstrumentationPython=${AUTO_INSTRUMENTATION_PYTHON_VERSION} -X ${VERSION_PKG}.autoInstrumentationDotNet=${AUTO_INSTRUMENTATION_DOTNET_VERSION} -X ${VERSION_PKG}.autoInstrumentationNodeJS=${AUTO_INSTRUMENTATION_NODEJS_VERSION} -X ${VERSION_PKG}.dcgmExporter=${DCMG_EXPORTER_VERSION} -X ${VERSION_PKG}.neuronMonitor=${NEURON_MONITOR_VERSION} -X ${VERSION_PKG}.targetAllocator=${TARGET_ALLOCATOR_VERSION}" -a -o manager main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ AUTO_INSTRUMENTATION_DOTNET_VERSION ?= "$(shell grep -v '\#' versions.txt | grep
AUTO_INSTRUMENTATION_NODEJS_VERSION ?= "$(shell grep -v '\#' versions.txt | grep aws-otel-nodejs-instrumentation | awk -F= '{print $$2}')"
DCGM_EXPORTER_VERSION ?= "$(shell grep -v '\#' versions.txt | grep dcgm-exporter | awk -F= '{print $$2}')"
NEURON_MONITOR_VERSION ?= "$(shell grep -v '\#' versions.txt | grep neuron-monitor | awk -F= '{print $$2}')"
TARGET_ALLOCATOR_VERSION ?= $(shell grep -v '\#' versions.txt | grep target-allocator | awk -F= '{print $$2}')
TARGET_ALLOCATOR_VERSION ?= "$(shell grep -v '\#' versions.txt | grep target-allocator | awk -F= '{print $$2}')"

# Image URL to use all building/pushing image targets
IMG_PREFIX ?= aws
IMG_REPO ?= cloudwatch-agent-operator
Expand Down Expand Up @@ -162,7 +163,7 @@ generate: controller-gen api-docs
# buildx is used to ensure same results for arm based systems (m1/2 chips)
.PHONY: container
container:
docker buildx build --load --platform linux/${ARCH} -t ${IMG} --build-arg VERSION_PKG=${VERSION_PKG} --build-arg VERSION=${VERSION} --build-arg VERSION_DATE=${VERSION_DATE} --build-arg AGENT_VERSION=${AGENT_VERSION} --build-arg AUTO_INSTRUMENTATION_JAVA_VERSION=${AUTO_INSTRUMENTATION_JAVA_VERSION} --build-arg AUTO_INSTRUMENTATION_PYTHON_VERSION=${AUTO_INSTRUMENTATION_PYTHON_VERSION} --build-arg AUTO_INSTRUMENTATION_DOTNET_VERSION=${AUTO_INSTRUMENTATION_DOTNET_VERSION} --build-arg AUTO_INSTRUMENTATION_NODEJS_VERSION=${AUTO_INSTRUMENTATION_NODEJS_VERSION} --build-arg DCGM_EXPORTER_VERSION=${DCGM_EXPORTER_VERSION} --build-arg NEURON_MONITOR_VERSION=${NEURON_MONITOR_VERSION} .
docker buildx build --load --platform linux/${ARCH} -t ${IMG} --build-arg VERSION_PKG=${VERSION_PKG} --build-arg VERSION=${VERSION} --build-arg VERSION_DATE=${VERSION_DATE} --build-arg AGENT_VERSION=${AGENT_VERSION} --build-arg AUTO_INSTRUMENTATION_JAVA_VERSION=${AUTO_INSTRUMENTATION_JAVA_VERSION} --build-arg AUTO_INSTRUMENTATION_PYTHON_VERSION=${AUTO_INSTRUMENTATION_PYTHON_VERSION} --build-arg AUTO_INSTRUMENTATION_DOTNET_VERSION=${AUTO_INSTRUMENTATION_DOTNET_VERSION} --build-arg AUTO_INSTRUMENTATION_NODEJS_VERSION=${AUTO_INSTRUMENTATION_NODEJS_VERSION} --build-arg DCGM_EXPORTER_VERSION=${DCGM_EXPORTER_VERSION} --build-arg NEURON_MONITOR_VERSION=${NEURON_MONITOR_VERSION} --build-arg TARGET_ALLOCATOR_VERSION=${TARGET_ALLOCATOR_VERSION} .

# Push the container image, used only for local dev purposes
.PHONY: container-push
Expand Down
15 changes: 15 additions & 0 deletions apis/v1alpha1/allocation_strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package v1alpha1

type (
// AmazonCloudWatchAgentTargetAllocatorAllocationStrategy represent which strategy to distribute target to each collector
// +kubebuilder:validation:Enum=consistent-hashing
AmazonCloudWatchAgentTargetAllocatorAllocationStrategy string
)

const (
// AmazonCloudWatchAgentTargetAllocatorAllocationStrategyConsistentHashing targets will be consistently added to collectors, which allows a high-availability setup.
AmazonCloudWatchAgentTargetAllocatorAllocationStrategyConsistentHashing AmazonCloudWatchAgentTargetAllocatorAllocationStrategy = "consistent-hashing"
)
87 changes: 87 additions & 0 deletions apis/v1alpha1/amazoncloudwatchagent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ type AmazonCloudWatchAgentSpec struct {
// Collector and Target Allocator pods.
// +optional
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`
// TargetAllocator indicates a value which determines whether to spawn a target allocation resource or not.
// +optional
TargetAllocator AmazonCloudWatchAgentTargetAllocator `json:"targetAllocator,omitempty"`
// Mode represents how the collector should be deployed (deployment, daemonset, statefulset or sidecar)
// +optional
Mode Mode `json:"mode,omitempty"`
Expand All @@ -164,6 +167,9 @@ type AmazonCloudWatchAgentSpec struct {
// ImagePullPolicy indicates the pull policy to be used for retrieving the container image (Always, Never, IfNotPresent)
// +optional
ImagePullPolicy v1.PullPolicy `json:"imagePullPolicy,omitempty"`
// Prometheus is the raw YAML to be used as the collector's prometheus configuration.
// +optional
Prometheus PrometheusConfig `json:"prometheus,omitempty"`
// Config is the raw JSON to be used as the collector's configuration. Refer to the OpenTelemetry Collector documentation for details.
// +required
Config string `json:"config,omitempty"`
Expand Down Expand Up @@ -273,6 +279,87 @@ type AmazonCloudWatchAgentSpec struct {
UpdateStrategy appsv1.DaemonSetUpdateStrategy `json:"updateStrategy,omitempty"`
}

// AmazonCloudWatchAgentTargetAllocator defines the configurations for the Prometheus target allocator.
type AmazonCloudWatchAgentTargetAllocator struct {
// Replicas is the number of pod instances for the underlying TargetAllocator. This should only be set to a value
// other than 1 if a strategy that allows for high availability is chosen. Currently, the only allocation strategy
// that can be run in a high availability mode is consistent-hashing.
// +optional
Replicas *int32 `json:"replicas,omitempty"`
// NodeSelector to schedule OpenTelemetry TargetAllocator pods.
// +optional
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
// Resources to set on the OpenTelemetryTargetAllocator containers.
// +optional
Resources v1.ResourceRequirements `json:"resources,omitempty"`
// AllocationStrategy determines which strategy the target allocator should use for allocation.
// The current option is consistent-hashing.
// +optional
AllocationStrategy AmazonCloudWatchAgentTargetAllocatorAllocationStrategy `json:"allocationStrategy,omitempty"`
// FilterStrategy determines how to filter targets before allocating them among the collectors.
// The only current option is relabel-config (drops targets based on prom relabel_config).
// Filtering is disabled by default.
// +optional
FilterStrategy string `json:"filterStrategy,omitempty"`
// ServiceAccount indicates the name of an existing service account to use with this instance. When set,
// the operator will not automatically create a ServiceAccount for the TargetAllocator.
// +optional
ServiceAccount string `json:"serviceAccount,omitempty"`
// Image indicates the container image to use for the OpenTelemetry TargetAllocator.
// +optional
Image string `json:"image,omitempty"`
// Enabled indicates whether to use a target allocation mechanism for Prometheus targets or not.
// +optional
Enabled bool `json:"enabled,omitempty"`
// If specified, indicates the pod's scheduling constraints
// +optional
Affinity *v1.Affinity `json:"affinity,omitempty"`
// PrometheusCR defines the configuration for the retrieval of PrometheusOperator CRDs ( servicemonitor.monitoring.coreos.com/v1 and podmonitor.monitoring.coreos.com/v1 ) retrieval.
// All CR instances which the ServiceAccount has access to will be retrieved. This includes other namespaces.
// +optional
PrometheusCR AmazonCloudWatchAgentTargetAllocatorPrometheusCR `json:"prometheusCR,omitempty"`
// SecurityContext configures the container security context for
// the target-allocator.
// +optional
SecurityContext *v1.PodSecurityContext `json:"securityContext,omitempty"`
// TopologySpreadConstraints embedded kubernetes pod configuration option,
// controls how pods are spread across your cluster among failure-domains
// such as regions, zones, nodes, and other user-defined topology domains
// https://kubernetes.io/docs/concepts/workloads/pods/pod-topology-spread-constraints/
// +optional
TopologySpreadConstraints []v1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`
// Toleration embedded kubernetes pod configuration option,
// controls how pods can be scheduled with matching taints
// +optional
Tolerations []v1.Toleration `json:"tolerations,omitempty"`
// ENV vars to set on the OpenTelemetry TargetAllocator's Pods. These can then in certain cases be
// consumed in the config file for the TargetAllocator.
// +optional
Env []v1.EnvVar `json:"env,omitempty"`
}

type AmazonCloudWatchAgentTargetAllocatorPrometheusCR struct {
// Enabled indicates whether to use a PrometheusOperator custom resources as targets or not.
// +optional
Enabled bool `json:"enabled,omitempty"`
// Interval between consecutive scrapes. Equivalent to the same setting on the Prometheus CRD.
//
// Default: "30s"
// +kubebuilder:default:="30s"
// +kubebuilder:validation:Format:=duration
ScrapeInterval *metav1.Duration `json:"scrapeInterval,omitempty"`
// PodMonitors to be selected for target discovery.
// This is a map of {key,value} pairs. Each {key,value} in the map is going to exactly match a label in a
// PodMonitor's meta labels. The requirements are ANDed.
// +optional
PodMonitorSelector map[string]string `json:"podMonitorSelector,omitempty"`
// ServiceMonitors to be selected for target discovery.
// This is a map of {key,value} pairs. Each {key,value} in the map is going to exactly match a label in a
// ServiceMonitor's meta labels. The requirements are ANDed.
// +optional
ServiceMonitorSelector map[string]string `json:"serviceMonitorSelector,omitempty"`
}

// ScaleSubresourceStatus defines the observed state of the AmazonCloudWatchAgent's
// scale subresource.
type ScaleSubresourceStatus struct {
Expand Down
34 changes: 34 additions & 0 deletions apis/v1alpha1/collector_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"context"
"fmt"

"github.com/aws/amazon-cloudwatch-agent-operator/internal/manifests/collector/adapters"

ta "github.com/aws/amazon-cloudwatch-agent-operator/internal/manifests/targetallocator/adapters"
"github.com/aws/amazon-cloudwatch-agent-operator/pkg/featuregate"

"github.com/go-logr/logr"
autoscalingv2 "k8s.io/api/autoscaling/v2"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -87,6 +92,9 @@ func (c CollectorWebhook) defaulter(r *AmazonCloudWatchAgent) error {
if r.Spec.Replicas == nil {
r.Spec.Replicas = &one
}
if r.Spec.TargetAllocator.Enabled && r.Spec.TargetAllocator.Replicas == nil {
r.Spec.TargetAllocator.Replicas = &one
}

if r.Spec.MaxReplicas != nil || (r.Spec.Autoscaler != nil && r.Spec.Autoscaler.MaxReplicas != nil) {
if r.Spec.Autoscaler == nil {
Expand Down Expand Up @@ -163,6 +171,32 @@ func (c CollectorWebhook) validate(r *AmazonCloudWatchAgent) (admission.Warnings
return warnings, fmt.Errorf("the OpenTelemetry Collector mode is set to %s, which does not support the attribute 'AdditionalContainers'", r.Spec.Mode)
}

// validate target allocation
if r.Spec.TargetAllocator.Enabled && r.Spec.Mode != ModeStatefulSet {
return warnings, fmt.Errorf("the OpenTelemetry Collector mode is set to %s, which does not support the target allocation deployment", r.Spec.Mode)
}

// validate Prometheus config for target allocation
if r.Spec.TargetAllocator.Enabled {
promConfigYaml, err := r.Spec.Prometheus.Yaml()
if err != nil {
return warnings, fmt.Errorf("%s could not convert json to yaml", err)
}

promCfg, err := adapters.ConfigFromString(promConfigYaml)
if err != nil {
return warnings, fmt.Errorf("the OpenTelemetry Spec Prometheus configuration is incorrect, %w", err)
}
err = ta.ValidatePromConfig(promCfg, r.Spec.TargetAllocator.Enabled, featuregate.EnableTargetAllocatorRewrite.IsEnabled())
if err != nil {
return warnings, fmt.Errorf("the OpenTelemetry Spec Prometheus configuration is incorrect, %w", err)
}
err = ta.ValidateTargetAllocatorConfig(r.Spec.TargetAllocator.PrometheusCR.Enabled, promCfg)
if err != nil {
return warnings, fmt.Errorf("the OpenTelemetry Spec Prometheus configuration is incorrect, %w", err)
}
}

// validator port config
for _, p := range r.Spec.Ports {
nameErrs := validation.IsValidPortName(p.Name)
Expand Down
58 changes: 43 additions & 15 deletions apis/v1alpha1/collector_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"os"
"testing"

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"

"github.com/go-logr/logr"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -273,6 +276,7 @@ func TestOTELColDefaultingWebhook(t *testing.T) {
scheme: testScheme,
cfg: config.New(
config.WithCollectorImage("collector:v0.0.0"),
config.WithTargetAllocatorImage("ta:v0.0.0"),
),
}
ctx := context.Background()
Expand All @@ -283,6 +287,12 @@ func TestOTELColDefaultingWebhook(t *testing.T) {
}
}

var promCfgYaml = `config:
scrape_configs:
- job_name: otel-collector
scrape_interval: 10s
`

// TODO: a lot of these tests use .Spec.MaxReplicas and .Spec.MinReplicas. These fields are
// deprecated and moved to .Spec.Autoscaler. Fine to use these fields to test that old CRD is
// still supported but should eventually be updated.
Expand All @@ -294,6 +304,10 @@ func TestOTELColValidatingWebhook(t *testing.T) {
three := int32(3)
five := int32(5)

promCfg := PrometheusConfig{}
err := yaml.Unmarshal([]byte(promCfgYaml), &promCfg)
require.NoError(t, err)

tests := []struct { //nolint:govet
name string
otelcol AmazonCloudWatchAgent
Expand All @@ -313,21 +327,10 @@ func TestOTELColValidatingWebhook(t *testing.T) {
Replicas: &three,
MaxReplicas: &five,
UpgradeStrategy: "adhoc",
Config: `receivers:
examplereceiver:
endpoint: "0.0.0.0:12345"
examplereceiver/settings:
endpoint: "0.0.0.0:12346"
prometheus:
config:
scrape_configs:
- job_name: otel-collector
scrape_interval: 10s
jaeger/custom:
protocols:
thrift_http:
endpoint: 0.0.0.0:15268
`,
TargetAllocator: AmazonCloudWatchAgentTargetAllocator{
Enabled: true,
},
Prometheus: promCfg,
Ports: []v1.ServicePort{
{
Name: "port1",
Expand Down Expand Up @@ -373,6 +376,30 @@ func TestOTELColValidatingWebhook(t *testing.T) {
},
expectedErr: "does not support the attribute 'tolerations'",
},
{
name: "invalid mode with target allocator",
otelcol: AmazonCloudWatchAgent{
Spec: AmazonCloudWatchAgentSpec{
Mode: ModeDeployment,
TargetAllocator: AmazonCloudWatchAgentTargetAllocator{
Enabled: true,
},
},
},
expectedErr: "does not support the target allocation deployment",
},
{
name: "invalid target allocator config",
otelcol: AmazonCloudWatchAgent{
Spec: AmazonCloudWatchAgentSpec{
Mode: ModeStatefulSet,
TargetAllocator: AmazonCloudWatchAgentTargetAllocator{
Enabled: true,
},
},
},
expectedErr: "the OpenTelemetry Spec Prometheus configuration is incorrect",
},
{
name: "invalid port name",
otelcol: AmazonCloudWatchAgent{
Expand Down Expand Up @@ -755,6 +782,7 @@ func TestOTELColValidatingWebhook(t *testing.T) {
scheme: testScheme,
cfg: config.New(
config.WithCollectorImage("collector:v0.0.0"),
config.WithTargetAllocatorImage("ta:v0.0.0"),
),
}
ctx := context.Background()
Expand Down
Loading