Skip to content
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

update daemonset to have selector #2611

Closed
wants to merge 3 commits into from
Closed
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
16 changes: 16 additions & 0 deletions .chloggen/daemonset-vpa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
component: operator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Updates the Daemonset to allow for label selectors && fix casing for DisablePrometheusAnnotations

# One or more tracking issues related to the change
issues: [2605, 2608]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
2 changes: 1 addition & 1 deletion apis/v1alpha1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ type MetricsConfigSpec struct {
//
// +optional
// +kubebuilder:validation:Optional
DisablePrometheusAnnotations bool `json:"DisablePrometheusAnnotations,omitempty"`
DisablePrometheusAnnotations bool `json:"disablePrometheusAnnotations,omitempty"`
}

// ObservabilitySpec defines how telemetry data gets handled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3867,7 +3867,7 @@ spec:
metrics:
description: Metrics defines the metrics configuration for operands.
properties:
DisablePrometheusAnnotations:
disablePrometheusAnnotations:
description: DisablePrometheusAnnotations controls the automatic
addition of default Prometheus annotations ('prometheus.io/scrape',
'prometheus.io/port', and 'prometheus.io/path')
Expand Down Expand Up @@ -5211,7 +5211,7 @@ spec:
description: Metrics defines the metrics configuration for
operands.
properties:
DisablePrometheusAnnotations:
disablePrometheusAnnotations:
description: DisablePrometheusAnnotations controls the
automatic addition of default Prometheus annotations
('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3864,7 +3864,7 @@ spec:
metrics:
description: Metrics defines the metrics configuration for operands.
properties:
DisablePrometheusAnnotations:
disablePrometheusAnnotations:
description: DisablePrometheusAnnotations controls the automatic
addition of default Prometheus annotations ('prometheus.io/scrape',
'prometheus.io/port', and 'prometheus.io/path')
Expand Down Expand Up @@ -5208,7 +5208,7 @@ spec:
description: Metrics defines the metrics configuration for
operands.
properties:
DisablePrometheusAnnotations:
disablePrometheusAnnotations:
description: DisablePrometheusAnnotations controls the
automatic addition of default Prometheus annotations
('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')
Expand Down
4 changes: 2 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17812,7 +17812,7 @@ Metrics defines the metrics configuration for operands.
</tr>
</thead>
<tbody><tr>
<td><b>DisablePrometheusAnnotations</b></td>
<td><b>disablePrometheusAnnotations</b></td>
<td>boolean</td>
<td>
DisablePrometheusAnnotations controls the automatic addition of default Prometheus annotations ('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')<br/>
Expand Down Expand Up @@ -20322,7 +20322,7 @@ Metrics defines the metrics configuration for operands.
</tr>
</thead>
<tbody><tr>
<td><b>DisablePrometheusAnnotations</b></td>
<td><b>disablePrometheusAnnotations</b></td>
<td>boolean</td>
<td>
DisablePrometheusAnnotations controls the automatic addition of default Prometheus annotations ('prometheus.io/scrape', 'prometheus.io/port', and 'prometheus.io/path')<br/>
Expand Down
10 changes: 5 additions & 5 deletions internal/status/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,26 @@ import (

func UpdateCollectorStatus(ctx context.Context, cli client.Client, changed *v1alpha1.OpenTelemetryCollector) error {
if changed.Status.Version == "" {
// a version is not set, otherwise let the upgrade mechanism take care of it!
changed.Status.Version = version.OpenTelemetryCollector()
}

mode := changed.Spec.Mode
if mode != v1alpha1.ModeDeployment && mode != v1alpha1.ModeStatefulSet {

if mode != v1alpha1.ModeDeployment && mode != v1alpha1.ModeStatefulSet && mode != v1alpha1.ModeDaemonSet {
changed.Status.Scale.Replicas = 0
changed.Status.Scale.Selector = ""
return nil
}

name := naming.Collector(changed.Name)

// Set the scale selector
labels := manifestutils.Labels(changed.ObjectMeta, name, changed.Spec.Image, collector.ComponentOpenTelemetryCollector, []string{})
selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{MatchLabels: labels})
if err != nil {
return fmt.Errorf("failed to get selector for labelSelector: %w", err)
}
changed.Status.Scale.Selector = selector.String()

// Set the scale replicas
objKey := client.ObjectKey{
Namespace: changed.GetNamespace(),
Name: naming.Collector(changed.Name),
Expand All @@ -63,7 +62,7 @@ func UpdateCollectorStatus(ctx context.Context, cli client.Client, changed *v1al
var statusReplicas string
var statusImage string

switch mode { // nolint:exhaustive
switch mode {
case v1alpha1.ModeDeployment:
obj := &appsv1.Deployment{}
if err := cli.Get(ctx, objKey, obj); err != nil {
Expand Down Expand Up @@ -91,6 +90,7 @@ func UpdateCollectorStatus(ctx context.Context, cli client.Client, changed *v1al
}
statusImage = obj.Spec.Template.Spec.Containers[0].Image
}

changed.Status.Scale.Replicas = replicas
changed.Status.Image = statusImage
changed.Status.Scale.StatusReplicas = statusReplicas
Expand Down
177 changes: 177 additions & 0 deletions internal/status/collector/collector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package collector

import (
"context"
"testing"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestUpdateCollectorStatusUnsupported(t *testing.T) {
ctx := context.TODO()
cli := client.Client(fake.NewFakeClient())

changed := &v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "test-sidecar",
Namespace: "default",
},
Spec: v1alpha1.OpenTelemetryCollectorSpec{
Mode: v1alpha1.ModeSidecar,
},
}

err := UpdateCollectorStatus(ctx, cli, changed)
assert.NoError(t, err)

assert.Equal(t, int32(0), changed.Status.Scale.Replicas, "expected replicas to be 0")
assert.Equal(t, "", changed.Status.Scale.Selector, "expected selector to be empty")
}

func createMockKubernetesClientDeployment() client.Client {
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "test-deployment-collector",
Namespace: "default",
},
Status: appsv1.DeploymentStatus{
Replicas: 1,
ReadyReplicas: 1,
},
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "app",
Image: "app:latest",
},
},
},
},
},
}
return fake.NewClientBuilder().WithObjects(deployment).Build()
}

func TestUpdateCollectorStatusDeploymentMode(t *testing.T) {
ctx := context.TODO()
cli := createMockKubernetesClientDeployment()

changed := &v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "test-deployment",
Namespace: "default",
},
Spec: v1alpha1.OpenTelemetryCollectorSpec{
Mode: v1alpha1.ModeDeployment,
},
}

err := UpdateCollectorStatus(ctx, cli, changed)
assert.NoError(t, err)

assert.Equal(t, int32(1), changed.Status.Scale.Replicas, "expected replicas to be 1")
assert.Equal(t, "1/1", changed.Status.Scale.StatusReplicas, "expected status replicas to be 1/1")
assert.Equal(t, "app:latest", changed.Status.Image, "expected image to be app:latest")
}

func createMockKubernetesClientStatefulset() client.Client {
statefulset := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-statefulset-collector",
Namespace: "default",
},
Status: appsv1.StatefulSetStatus{
Replicas: 1,
ReadyReplicas: 1,
},
Spec: appsv1.StatefulSetSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "app",
Image: "app:latest",
},
},
},
},
},
}
return fake.NewClientBuilder().WithObjects(statefulset).Build()
}

func TestUpdateCollectorStatusStatefulset(t *testing.T) {
ctx := context.TODO()
cli := createMockKubernetesClientStatefulset()

changed := &v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "test-statefulset",
Namespace: "default",
},
Spec: v1alpha1.OpenTelemetryCollectorSpec{
Mode: v1alpha1.ModeStatefulSet,
},
}

err := UpdateCollectorStatus(ctx, cli, changed)
assert.NoError(t, err)

assert.Equal(t, int32(1), changed.Status.Scale.Replicas, "expected replicas to be 1")
assert.Equal(t, "1/1", changed.Status.Scale.StatusReplicas, "expected status replicas to be 1/1")
assert.Equal(t, "app:latest", changed.Status.Image, "expected image to be app:latest")
}

func createMockKubernetesClientDaemonset() client.Client {
daemonset := &appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: "test-daemonset-collector",
Namespace: "default",
},
Spec: appsv1.DaemonSetSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "app",
Image: "app:latest",
},
},
},
},
},
}
return fake.NewClientBuilder().WithObjects(daemonset).Build()
}

func TestUpdateCollectorStatusDaemonsetMode(t *testing.T) {
ctx := context.TODO()
cli := createMockKubernetesClientDaemonset()

changed := &v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "test-daemonset",
Namespace: "default",
Labels: map[string]string{
"customLabel": "customValue",
},
},
Spec: v1alpha1.OpenTelemetryCollectorSpec{
Mode: v1alpha1.ModeDaemonSet,
},
}

err := UpdateCollectorStatus(ctx, cli, changed)
assert.NoError(t, err)

assert.Contains(t, changed.Status.Scale.Selector, "customLabel=customValue", "expected selector to contain customlabel=customValue")
assert.Equal(t, "app:latest", changed.Status.Image, "expected image to be app:latest")
}