Skip to content

Commit

Permalink
apply recommendations
Browse files Browse the repository at this point in the history
Signed-off-by: Benedikt Bongartz <[email protected]>
  • Loading branch information
frzifus committed Oct 14, 2024
1 parent 5773a44 commit f9df64a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
12 changes: 6 additions & 6 deletions pkg/sidecar/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,34 @@ func add(cfg config.Config, logger logr.Logger, otelcol v1beta1.OpenTelemetryCol
return pod, nil
}

func isOtelColContainer(c corev1.Container) bool { return c.Name == naming.Container() }

// remove the sidecar container from the given pod.
func remove(pod corev1.Pod) corev1.Pod {
if !existsIn(pod) {
return pod
}

fn := func(c corev1.Container) bool { return c.Name == naming.Container() }
pod.Spec.Containers = slices.DeleteFunc(pod.Spec.Containers, fn)
pod.Spec.Containers = slices.DeleteFunc(pod.Spec.Containers, isOtelColContainer)

if featuregate.EnableNativeSidecarContainers.IsEnabled() {
// NOTE: we also remove init containers (native sidecars) since k8s 1.28.
// This should have no side effects.
pod.Spec.InitContainers = slices.DeleteFunc(pod.Spec.InitContainers, fn)
pod.Spec.InitContainers = slices.DeleteFunc(pod.Spec.InitContainers, isOtelColContainer)
}
return pod
}

// existsIn checks whether a sidecar container exists in the given pod.
func existsIn(pod corev1.Pod) bool {
fn := func(c corev1.Container) bool { return c.Name == naming.Container() }
if slices.ContainsFunc(pod.Spec.Containers, fn) {
if slices.ContainsFunc(pod.Spec.Containers, isOtelColContainer) {
return true
}

if featuregate.EnableNativeSidecarContainers.IsEnabled() {
// NOTE: we also check init containers (native sidecars) since k8s 1.28.
// This should have no side effects.
if slices.ContainsFunc(pod.Spec.InitContainers, fn) {
if slices.ContainsFunc(pod.Spec.InitContainers, isOtelColContainer) {
return true
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/sidecar/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

var logger = logf.Log.WithName("unit-tests")

func sidecarFeatureGate(t *testing.T) {
func enableSidecarFeatureGate(t *testing.T) {
originalVal := featuregate.EnableNativeSidecarContainers.IsEnabled()
t.Logf("original is: %+v", originalVal)
require.NoError(t, colfeaturegate.GlobalRegistry().Set(featuregate.EnableNativeSidecarContainers.ID(), true))
Expand All @@ -42,7 +42,7 @@ func sidecarFeatureGate(t *testing.T) {
}

func TestAddNativeSidecar(t *testing.T) {
sidecarFeatureGate(t)
enableSidecarFeatureGate(t)
// prepare
pod := corev1.Pod{
Spec: corev1.PodSpec{
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestRemoveNonExistingSidecar(t *testing.T) {
}

func TestExistsIn(t *testing.T) {
sidecarFeatureGate(t)
enableSidecarFeatureGate(t)

for _, tt := range []struct {
desc string
Expand Down
1 change: 1 addition & 0 deletions tests/e2e-native-sidecar/00-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ spec:
- name: myapp
initContainers:
- name: otc-container
restartPolicy: Always
status:
containerStatuses:
- name: myapp
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e-native-sidecar/00-install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ metadata:
spec:
containers:
- name: myapp
image: jaegertracing/vertx-create-span:operator-e2e-tests
image: ghcr.io/open-telemetry/opentelemetry-operator/e2e-test-app-python:main
ports:
- containerPort: 8080
protocol: TCP

0 comments on commit f9df64a

Please sign in to comment.