encode and decode published ports using container labels

This commit is contained in:
Pavel Sviderski
2024-12-10 19:21:52 +10:00
parent 4de7ecd747
commit 0652123bc7
3 changed files with 69 additions and 2 deletions
+2 -2
View File
@@ -27,9 +27,9 @@ type PortSpec struct {
PublishedPort uint16
// ContainerPort is the port inside the container that the service listens on.
ContainerPort uint16
// Protocol specifies the network protocol. Default is ProtocolHTTPS if Hostname is set, ProtocolTCP otherwise.
// Protocol specifies the network protocol.
Protocol string
// Mode specifies how the port is published. Default is PortModeIngress.
// Mode specifies how the port is published.
Mode string
}
+12
View File
@@ -238,6 +238,18 @@ func (cli *Client) runContainer(
config.Labels[api.LabelServiceMode] = api.ServiceModeGlobal
}
if len(spec.Ports) > 0 {
encodedPorts := make([]string, len(spec.Ports))
for i, p := range spec.Ports {
encodedPorts[i], err = p.String()
if err != nil {
return resp, fmt.Errorf("encode service port spec: %w", err)
}
}
config.Labels[api.LabelServicePorts] = strings.Join(encodedPorts, ",")
}
hostConfig := &container.HostConfig{
Init: spec.Container.Init,
}