Skip to content

Commit

Permalink
feat: Add tests and add feature to update daemonset to have selector (o…
Browse files Browse the repository at this point in the history
…pen-telemetry#2665)

* feat: Allowing daemonset to have selectors

* linting
  • Loading branch information
rivToadd authored Feb 28, 2024
1 parent 874c72a commit 1e80370
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .chloggen/enable-selectors-daemonset-status.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: Allow for label selectors on the daemonset through update status

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

# (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:
5 changes: 4 additions & 1 deletion internal/status/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ func UpdateCollectorStatus(ctx context.Context, cli client.Client, changed *v1al
// 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.ModeSidecar {
changed.Status.Scale.Replicas = 0
changed.Status.Scale.Selector = ""
return nil
Expand Down Expand Up @@ -91,6 +93,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
192 changes: 192 additions & 0 deletions internal/status/collector/collector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

import (
"context"
"testing"

"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"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
)

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")
}

0 comments on commit 1e80370

Please sign in to comment.