diff --git a/.chloggen/target-allocator-container-port.yaml b/.chloggen/target-allocator-container-port.yaml new file mode 100644 index 0000000000..000f66233f --- /dev/null +++ b/.chloggen/target-allocator-container-port.yaml @@ -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: diff --git a/internal/manifests/targetallocator/container.go b/internal/manifests/targetallocator/container.go index 8c88b41ea3..6a6de7e5d8 100644 --- a/internal/manifests/targetallocator/container.go +++ b/internal/manifests/targetallocator/container.go @@ -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", @@ -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, diff --git a/internal/manifests/targetallocator/container_test.go b/internal/manifests/targetallocator/container_test.go index 1661e319a8..af0ad8478e 100644 --- a/internal/manifests/targetallocator/container_test.go +++ b/internal/manifests/targetallocator/container_test.go @@ -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{ @@ -168,6 +189,13 @@ func TestContainerHasEnvVars(t *testing.T) { SubPathExpr: "", }, }, + Ports: []corev1.ContainerPort{ + { + Name: "http", + ContainerPort: 8080, + Protocol: corev1.ProtocolTCP, + }, + }, } // test @@ -243,6 +271,13 @@ func TestContainerDoesNotOverrideEnvVars(t *testing.T) { SubPathExpr: "", }, }, + Ports: []corev1.ContainerPort{ + { + Name: "http", + ContainerPort: 8080, + Protocol: corev1.ProtocolTCP, + }, + }, } // test diff --git a/internal/manifests/targetallocator/service.go b/internal/manifests/targetallocator/service.go index 684b3ebcca..bb3079dda9 100644 --- a/internal/manifests/targetallocator/service.go +++ b/internal/manifests/targetallocator/service.go @@ -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"), }}, }, } diff --git a/internal/manifests/targetallocator/service_test.go b/internal/manifests/targetallocator/service_test.go new file mode 100644 index 0000000000..ad0676147b --- /dev/null +++ b/internal/manifests/targetallocator/service_test.go @@ -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) +}