Skip to content

Commit bcbacb3

Browse files
Your Namebmiguel-teixeira
Your Name
authored andcommitted
feat/add support for hostPort in container
1 parent 2ddfe31 commit bcbacb3

27 files changed

+524
-207
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: enhancement
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: operator
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: "Add support for adding/extending otc-collector container ports."
9+
10+
# One or more tracking issues related to the change
11+
issues: [2763]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

apis/v1alpha1/collector_webhook_test.go

+47-29
Original file line numberDiff line numberDiff line change
@@ -475,15 +475,19 @@ func TestOTELColValidatingWebhook(t *testing.T) {
475475
thrift_http:
476476
endpoint: 0.0.0.0:15268
477477
`,
478-
Ports: []v1.ServicePort{
478+
Ports: []PortsSpec{
479479
{
480-
Name: "port1",
481-
Port: 5555,
480+
ServicePort: v1.ServicePort{
481+
Name: "port1",
482+
Port: 5555,
483+
},
482484
},
483485
{
484-
Name: "port2",
485-
Port: 5554,
486-
Protocol: v1.ProtocolUDP,
486+
ServicePort: v1.ServicePort{
487+
Name: "port2",
488+
Port: 5554,
489+
Protocol: v1.ProtocolUDP,
490+
},
487491
},
488492
},
489493
Autoscaler: &AutoscalerSpec{
@@ -533,15 +537,19 @@ func TestOTELColValidatingWebhook(t *testing.T) {
533537
thrift_http:
534538
endpoint: 0.0.0.0:15268
535539
`,
536-
Ports: []v1.ServicePort{
540+
Ports: []PortsSpec{
537541
{
538-
Name: "port1",
539-
Port: 5555,
542+
ServicePort: v1.ServicePort{
543+
Name: "port1",
544+
Port: 5555,
545+
},
540546
},
541547
{
542-
Name: "port2",
543-
Port: 5554,
544-
Protocol: v1.ProtocolUDP,
548+
ServicePort: v1.ServicePort{
549+
Name: "port2",
550+
Port: 5554,
551+
Protocol: v1.ProtocolUDP,
552+
},
545553
},
546554
},
547555
Autoscaler: &AutoscalerSpec{
@@ -601,15 +609,19 @@ func TestOTELColValidatingWebhook(t *testing.T) {
601609
thrift_http:
602610
endpoint: 0.0.0.0:15268
603611
`,
604-
Ports: []v1.ServicePort{
612+
Ports: []PortsSpec{
605613
{
606-
Name: "port1",
607-
Port: 5555,
614+
ServicePort: v1.ServicePort{
615+
Name: "port1",
616+
Port: 5555,
617+
},
608618
},
609619
{
610-
Name: "port2",
611-
Port: 5554,
612-
Protocol: v1.ProtocolUDP,
620+
ServicePort: v1.ServicePort{
621+
Name: "port2",
622+
Port: 5554,
623+
Protocol: v1.ProtocolUDP,
624+
},
613625
},
614626
},
615627
Autoscaler: &AutoscalerSpec{
@@ -687,12 +699,14 @@ func TestOTELColValidatingWebhook(t *testing.T) {
687699
name: "invalid port name",
688700
otelcol: OpenTelemetryCollector{
689701
Spec: OpenTelemetryCollectorSpec{
690-
Ports: []v1.ServicePort{
702+
Ports: []PortsSpec{
691703
{
692-
// this port name contains a non alphanumeric character, which is invalid.
693-
Name: "-test🦄port",
694-
Port: 12345,
695-
Protocol: v1.ProtocolTCP,
704+
ServicePort: v1.ServicePort{
705+
// this port name contains a non alphanumeric character, which is invalid.
706+
Name: "-test🦄port",
707+
Port: 12345,
708+
Protocol: v1.ProtocolTCP,
709+
},
696710
},
697711
},
698712
},
@@ -703,10 +717,12 @@ func TestOTELColValidatingWebhook(t *testing.T) {
703717
name: "invalid port name, too long",
704718
otelcol: OpenTelemetryCollector{
705719
Spec: OpenTelemetryCollectorSpec{
706-
Ports: []v1.ServicePort{
720+
Ports: []PortsSpec{
707721
{
708-
Name: "aaaabbbbccccdddd", // len: 16, too long
709-
Port: 5555,
722+
ServicePort: v1.ServicePort{
723+
Name: "aaaabbbbccccdddd", // len: 16, too long
724+
Port: 5555,
725+
},
710726
},
711727
},
712728
},
@@ -717,10 +733,12 @@ func TestOTELColValidatingWebhook(t *testing.T) {
717733
name: "invalid port num",
718734
otelcol: OpenTelemetryCollector{
719735
Spec: OpenTelemetryCollectorSpec{
720-
Ports: []v1.ServicePort{
736+
Ports: []PortsSpec{
721737
{
722-
Name: "aaaabbbbccccddd", // len: 15
723-
// no port set means it's 0, which is invalid
738+
ServicePort: v1.ServicePort{
739+
Name: "aaaabbbbccccddd", // len: 15
740+
// no port set means it's 0, which is invalid
741+
},
724742
},
725743
},
726744
},

apis/v1alpha1/convert.go

+43-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"gopkg.in/yaml.v3"
2222
appsv1 "k8s.io/api/apps/v1"
23+
v1 "k8s.io/api/core/v1"
2324
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425
"sigs.k8s.io/controller-runtime/pkg/conversion"
2526

@@ -96,7 +97,7 @@ func tov1beta1(in OpenTelemetryCollector) (v1beta1.OpenTelemetryCollector, error
9697
Image: copy.Spec.Image,
9798
ImagePullPolicy: copy.Spec.ImagePullPolicy,
9899
VolumeMounts: copy.Spec.VolumeMounts,
99-
Ports: copy.Spec.Ports,
100+
Ports: tov1beta1Ports(copy.Spec.Ports),
100101
Env: copy.Spec.Env,
101102
EnvFrom: copy.Spec.EnvFrom,
102103
VolumeClaimTemplates: copy.Spec.VolumeClaimTemplates,
@@ -144,6 +145,26 @@ func tov1beta1(in OpenTelemetryCollector) (v1beta1.OpenTelemetryCollector, error
144145
}, nil
145146
}
146147

148+
func tov1beta1Ports(in []PortsSpec) []v1beta1.PortsSpec {
149+
var ports []v1beta1.PortsSpec
150+
151+
for _, p := range in {
152+
ports = append(ports, v1beta1.PortsSpec{
153+
ServicePort: v1.ServicePort{
154+
Name: p.Name,
155+
Protocol: p.Protocol,
156+
AppProtocol: p.AppProtocol,
157+
Port: p.Port,
158+
TargetPort: p.TargetPort,
159+
NodePort: p.NodePort,
160+
},
161+
HostPort: p.HostPort,
162+
})
163+
}
164+
165+
return ports
166+
}
167+
147168
func tov1beta1TA(in OpenTelemetryTargetAllocator) v1beta1.TargetAllocatorEmbedded {
148169
return v1beta1.TargetAllocatorEmbedded{
149170
Replicas: in.Replicas,
@@ -249,6 +270,26 @@ func tov1beta1ConfigMaps(in []ConfigMapsSpec) []v1beta1.ConfigMapsSpec {
249270
return mapsSpecs
250271
}
251272

273+
func tov1alpha1Ports(in []v1beta1.PortsSpec) []PortsSpec {
274+
var ports []PortsSpec
275+
276+
for _, p := range in {
277+
ports = append(ports, PortsSpec{
278+
ServicePort: v1.ServicePort{
279+
Name: p.Name,
280+
Protocol: p.Protocol,
281+
AppProtocol: p.AppProtocol,
282+
Port: p.Port,
283+
TargetPort: p.TargetPort,
284+
NodePort: p.NodePort,
285+
},
286+
HostPort: p.HostPort,
287+
})
288+
}
289+
290+
return ports
291+
}
292+
252293
func tov1alpha1(in v1beta1.OpenTelemetryCollector) (*OpenTelemetryCollector, error) {
253294
copy := in.DeepCopy()
254295
configYaml, err := copy.Spec.Config.Yaml()
@@ -287,7 +328,7 @@ func tov1alpha1(in v1beta1.OpenTelemetryCollector) (*OpenTelemetryCollector, err
287328
ImagePullPolicy: copy.Spec.ImagePullPolicy,
288329
Config: configYaml,
289330
VolumeMounts: copy.Spec.VolumeMounts,
290-
Ports: copy.Spec.Ports,
331+
Ports: tov1alpha1Ports(copy.Spec.Ports),
291332
Env: copy.Spec.Env,
292333
EnvFrom: copy.Spec.EnvFrom,
293334
VolumeClaimTemplates: copy.Spec.VolumeClaimTemplates,

apis/v1alpha1/convert_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,12 @@ func Test_tov1beta1AndBack(t *testing.T) {
177177
Name: "aaa",
178178
},
179179
},
180-
Ports: []v1.ServicePort{
181-
{
180+
Ports: []PortsSpec{{
181+
ServicePort: v1.ServicePort{
182182
Name: "otlp",
183183
},
184-
},
184+
HostPort: 0,
185+
}},
185186
Env: []v1.EnvVar{
186187
{
187188
Name: "foo",

apis/v1alpha1/opentelemetrycollector_types.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ type OpenTelemetryCollectorSpec struct {
186186
// used to open additional ports that can't be inferred by the operator, like for custom receivers.
187187
// +optional
188188
// +listType=atomic
189-
Ports []v1.ServicePort `json:"ports,omitempty"`
189+
Ports []PortsSpec `json:"ports,omitempty"`
190190
// ENV vars to set on the OpenTelemetry Collector's Pods. These can then in certain cases be
191191
// consumed in the config file for the Collector.
192192
// +optional
@@ -291,6 +291,16 @@ type OpenTelemetryCollectorSpec struct {
291291
DeploymentUpdateStrategy appsv1.DeploymentStrategy `json:"deploymentUpdateStrategy,omitempty"`
292292
}
293293

294+
// PortsSpec defines the OpenTelemetryCollector's container/service ports additional specifications.
295+
type PortsSpec struct {
296+
// Allows defining which port to bind to the host in the Container.
297+
// +optional
298+
HostPort int32 `json:"hostPort,omitempty"`
299+
300+
// Maintain previous fields in new struct
301+
v1.ServicePort `json:",inline"`
302+
}
303+
294304
// OpenTelemetryTargetAllocator defines the configurations for the Prometheus target allocator.
295305
type OpenTelemetryTargetAllocator struct {
296306
// Replicas is the number of pod instances for the underlying TargetAllocator. This should only be set to a value

apis/v1alpha1/zz_generated.deepcopy.go

+17-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apis/v1beta1/common.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ type PodDisruptionBudgetSpec struct {
8484
MaxUnavailable *intstr.IntOrString `json:"maxUnavailable,omitempty"`
8585
}
8686

87+
// PortsSpec defines the OpenTelemetryCollector's container/service ports additional specifications.
88+
type PortsSpec struct {
89+
// Allows defining which port to bind to the host in the Container.
90+
// +optional
91+
HostPort int32 `json:"hostPort,omitempty"`
92+
93+
// Maintain previous fields in new struct
94+
v1.ServicePort `json:",inline"`
95+
}
96+
8797
type OpenTelemetryCommonFields struct {
8898
// ManagementState defines if the CR should be managed by the operator or not.
8999
// Default is managed.
@@ -151,12 +161,12 @@ type OpenTelemetryCommonFields struct {
151161
// +optional
152162
// +listType=atomic
153163
VolumeMounts []v1.VolumeMount `json:"volumeMounts,omitempty"`
154-
// Ports allows a set of ports to be exposed by the underlying v1.Service. By default, the operator
164+
// Ports allows a set of ports to be exposed by the underlying v1.Service & v1.ContainerPort. By default, the operator
155165
// will attempt to infer the required ports by parsing the .Spec.Config property but this property can be
156166
// used to open additional ports that can't be inferred by the operator, like for custom receivers.
157167
// +optional
158168
// +listType=atomic
159-
Ports []v1.ServicePort `json:"ports,omitempty"`
169+
Ports []PortsSpec `json:"ports,omitempty"`
160170
// Environment variables to set on the generated pods.
161171
// +optional
162172
Env []v1.EnvVar `json:"env,omitempty"`

apis/v1beta1/zz_generated.deepcopy.go

+17-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -6606,6 +6606,9 @@ spec:
66066606
properties:
66076607
appProtocol:
66086608
type: string
6609+
hostPort:
6610+
format: int32
6611+
type: integer
66096612
name:
66106613
type: string
66116614
nodePort:

config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -2300,6 +2300,9 @@ spec:
23002300
properties:
23012301
appProtocol:
23022302
type: string
2303+
hostPort:
2304+
format: int32
2305+
type: integer
23032306
name:
23042307
type: string
23052308
nodePort:
@@ -6592,6 +6595,9 @@ spec:
65926595
properties:
65936596
appProtocol:
65946597
type: string
6598+
hostPort:
6599+
format: int32
6600+
type: integer
65956601
name:
65966602
type: string
65976603
nodePort:

0 commit comments

Comments
 (0)