Skip to content

Commit

Permalink
Declare ContainerPort for Target Allocator (open-telemetry#2313)
Browse files Browse the repository at this point in the history
* Declare ContainerPort for Target Allocator

* Use ContainerPort in Target Allocator service

* Changelog

* Fix tests

* Goimports

* Goimports
  • Loading branch information
cmergenthaler authored Nov 6, 2023
1 parent a61e474 commit 28bb371
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .chloggen/target-allocator-container-port.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: target allocator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Declare and use ContainerPort for Target Allocator

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

# (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:
8 changes: 8 additions & 0 deletions internal/manifests/targetallocator/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func Container(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelem
image = cfg.TargetAllocatorImage()
}

ports := make([]corev1.ContainerPort, 0)
ports = append(ports, corev1.ContainerPort{
Name: "http",
ContainerPort: 8080,
Protocol: corev1.ProtocolTCP,
})

volumeMounts := []corev1.VolumeMount{{
Name: naming.TAConfigMapVolume(),
MountPath: "/conf",
Expand Down Expand Up @@ -66,6 +73,7 @@ func Container(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelem
return corev1.Container{
Name: naming.TAContainer(),
Image: image,
Ports: ports,
Env: envVars,
VolumeMounts: volumeMounts,
Resources: otelcol.Spec.TargetAllocator.Resources,
Expand Down
35 changes: 35 additions & 0 deletions internal/manifests/targetallocator/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ func TestContainerWithImageOverridden(t *testing.T) {
assert.Equal(t, "overridden-image", c.Image)
}

func TestContainerPorts(t *testing.T) {
// prepare
otelcol := v1alpha1.OpenTelemetryCollector{
Spec: v1alpha1.OpenTelemetryCollectorSpec{
TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{
Enabled: true,
Image: "default-image",
},
},
}
cfg := config.New()

// test
c := Container(cfg, logger, otelcol)

// verify
assert.Len(t, c.Ports, 1)
assert.Equal(t, "http", c.Ports[0].Name)
assert.Equal(t, int32(8080), c.Ports[0].ContainerPort)
}

func TestContainerVolumes(t *testing.T) {
// prepare
otelcol := v1alpha1.OpenTelemetryCollector{
Expand Down Expand Up @@ -168,6 +189,13 @@ func TestContainerHasEnvVars(t *testing.T) {
SubPathExpr: "",
},
},
Ports: []corev1.ContainerPort{
{
Name: "http",
ContainerPort: 8080,
Protocol: corev1.ProtocolTCP,
},
},
}

// test
Expand Down Expand Up @@ -243,6 +271,13 @@ func TestContainerDoesNotOverrideEnvVars(t *testing.T) {
SubPathExpr: "",
},
},
Ports: []corev1.ContainerPort{
{
Name: "http",
ContainerPort: 8080,
Protocol: corev1.ProtocolTCP,
},
},
}

// test
Expand Down
2 changes: 1 addition & 1 deletion internal/manifests/targetallocator/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Service(params manifests.Params) *corev1.Service {
Ports: []corev1.ServicePort{{
Name: "targetallocation",
Port: 80,
TargetPort: intstr.FromInt(8080),
TargetPort: intstr.FromString("http"),
}},
},
}
Expand Down
45 changes: 45 additions & 0 deletions internal/manifests/targetallocator/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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 targetallocator

import (
"testing"

"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
)

func TestServicePorts(t *testing.T) {
otelcol := collectorInstance()
cfg := config.New()

params := manifests.Params{
OtelCol: otelcol,
Config: cfg,
Log: logger,
}

ports := []v1.ServicePort{{Name: "targetallocation", Port: 80, TargetPort: intstr.FromString("http")}}

s := Service(params)

assert.Equal(t, ports[0].Name, s.Spec.Ports[0].Name)
assert.Equal(t, ports[0].Port, s.Spec.Ports[0].Port)
assert.Equal(t, ports[0].TargetPort, s.Spec.Ports[0].TargetPort)
}

0 comments on commit 28bb371

Please sign in to comment.