Skip to content

Commit

Permalink
feat: add ports also to the container (#588)
Browse files Browse the repository at this point in the history
* feat: add ports also to the container

Signed-off-by: Smuu <18609909+Smuu@users.noreply.github.com>

* feat: also exposing ports in sidecars

Signed-off-by: Smuu <18609909+Smuu@users.noreply.github.com>

---------

Signed-off-by: Smuu <18609909+Smuu@users.noreply.github.com>
  • Loading branch information
smuu authored Dec 6, 2024
1 parent c435ce0 commit bf82cb3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/instance/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ func (e *execution) prepareReplicaSetConfig() k8s.ReplicaSetConfig {
StartupProbe: e.instance.monitoring.startupProbe,
Files: e.instance.storage.files,
SecurityContext: e.instance.security.prepareSecurityContext(),
TCPPorts: e.instance.network.portsTCP,
UDPPorts: e.instance.network.portsUDP,
}

sidecarConfigs := make([]k8s.ContainerConfig, 0)
Expand All @@ -403,6 +405,8 @@ func (e *execution) prepareReplicaSetConfig() k8s.ReplicaSetConfig {
StartupProbe: sidecar.Instance().monitoring.startupProbe,
Files: sidecar.Instance().storage.files,
SecurityContext: sidecar.Instance().security.prepareSecurityContext(),
TCPPorts: sidecar.Instance().network.portsTCP,
UDPPorts: sidecar.Instance().network.portsUDP,
})
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/k8s/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type ContainerConfig struct {
StartupProbe *v1.Probe // Startup probe for the container
Files []*File // Files to add to the Pod
SecurityContext *v1.SecurityContext // Security context for the container
TCPPorts []int // TCP ports to expose on the Pod
UDPPorts []int // UDP ports to expose on the Pod
}

type PodConfig struct {
Expand Down Expand Up @@ -521,6 +523,25 @@ func buildResources(memoryRequest, memoryLimit, cpuRequest resource.Quantity) v1
}
}

func buildPodPorts(tcpPorts, udpPorts []int) []v1.ContainerPort {
ports := make([]v1.ContainerPort, 0, len(tcpPorts)+len(udpPorts))
for _, port := range tcpPorts {
ports = append(ports, v1.ContainerPort{
Name: fmt.Sprintf("tcp-%d", port),
Protocol: v1.ProtocolTCP,
ContainerPort: int32(port),
})
}
for _, port := range udpPorts {
ports = append(ports, v1.ContainerPort{
Name: fmt.Sprintf("udp-%d", port),
Protocol: v1.ProtocolUDP,
ContainerPort: int32(port),
})
}
return ports
}

// prepareContainer creates a v1.Container from a given ContainerConfig.
func prepareContainer(config ContainerConfig) v1.Container {
return v1.Container{
Expand All @@ -532,6 +553,7 @@ func prepareContainer(config ContainerConfig) v1.Container {
Env: buildEnv(config.Env),
VolumeMounts: buildContainerVolumes(config.Name, config.Volumes, config.Files),
Resources: buildResources(config.MemoryRequest, config.MemoryLimit, config.CPURequest),
Ports: buildPodPorts(config.TCPPorts, config.UDPPorts),
LivenessProbe: config.LivenessProbe,
ReadinessProbe: config.ReadinessProbe,
StartupProbe: config.StartupProbe,
Expand Down

0 comments on commit bf82cb3

Please sign in to comment.