Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added protocol in port configuration #2993

Merged
merged 15 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/docs/20-usage/60-services.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ services:
image: redis
```

You can define a port and a protocol explicitly:

```yamlservices:
database:
image: mysql
ports:
- 3306

wireguard:
image: wg
ports:
- 51820/udp
```

## Configuration

Service containers generally expose environment variables to customize service startup such as default usernames, passwords and ports. Please see the official image documentation to learn more.
Expand Down
20 changes: 14 additions & 6 deletions pipeline/backend/kubernetes/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package kubernetes
import (
"context"
"fmt"
"strings"

"github.com/rs/zerolog/log"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
Expand All @@ -26,16 +27,12 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"
)

func mkService(namespace, name string, ports []uint16, selector map[string]string) (*v1.Service, error) {
func mkService(namespace, name string, ports []types.Port, selector map[string]string) (*v1.Service, error) {
log.Trace().Str("name", name).Interface("selector", selector).Interface("ports", ports).Msg("Creating service")

var svcPorts []v1.ServicePort
for _, port := range ports {
svcPorts = append(svcPorts, v1.ServicePort{
Name: fmt.Sprintf("port-%d", port),
Port: int32(port),
TargetPort: intstr.IntOrString{IntVal: int32(port)},
})
svcPorts = append(svcPorts, servicePort(port))
}

return &v1.Service{
Expand All @@ -55,6 +52,17 @@ func serviceName(step *types.Step) (string, error) {
return dnsName(step.Name)
}

func servicePort(port types.Port) v1.ServicePort {
portNumber := int32(port.Number)
portProtocol := strings.ToUpper(port.Protocol)
return v1.ServicePort{
Name: fmt.Sprintf("port-%d", portNumber),
Port: portNumber,
Protocol: v1.Protocol(portProtocol),
TargetPort: intstr.IntOrString{IntVal: portNumber},
}
}

func startService(ctx context.Context, engine *kube, step *types.Step) (*v1.Service, error) {
name, err := serviceName(step)
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions pipeline/backend/kubernetes/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ func TestService(t *testing.T) {
},
{
"name": "port-2",
"protocol": "TCP",
"port": 2,
"targetPort": 2
},
{
"name": "port-3",
"protocol": "UDP",
"port": 3,
"targetPort": 3
}
Expand All @@ -70,8 +72,12 @@ func TestService(t *testing.T) {
"loadBalancer": {}
}
}`

s, _ := mkService("foo", "bar", []uint16{1, 2, 3}, map[string]string{"step": "baz"})
ports := []types.Port{
{Number: 1},
{Number: 2, Protocol: "tcp"},
{Number: 3, Protocol: "udp"},
}
s, _ := mkService("foo", "bar", ports, map[string]string{"step": "baz"})
j, err := json.Marshal(s)
assert.NoError(t, err)
assert.JSONEq(t, expected, string(j))
Expand Down
5 changes: 5 additions & 0 deletions pipeline/backend/types/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type Network struct {
Name string `json:"name,omitempty"`
}

type Port struct {
Number uint16 `json:"number,omitempty"`
Protocol string `json:"protocol,omitempty"`
}

type HostAlias struct {
Name string `json:"name,omitempty"`
IP string `json:"ip,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion pipeline/backend/types/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Step struct {
NetworkMode string `json:"network_mode,omitempty"`
IpcMode string `json:"ipc_mode,omitempty"`
Sysctls map[string]string `json:"sysctls,omitempty"`
Ports []uint16 `json:"ports,omitempty"`
Ports []Port `json:"ports,omitempty"`
BackendOptions BackendOptions `json:"backend_options,omitempty"`
}

Expand Down
33 changes: 30 additions & 3 deletions pipeline/frontend/yaml/compiler/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"maps"
"path"
"strconv"
"strings"

"github.com/oklog/ulid/v2"
Expand Down Expand Up @@ -156,9 +157,13 @@ func (c *Compiler) createProcess(name string, container *yaml_types.Container, s
cpuSet = c.reslimit.CPUSet
}

var ports []uint16
for _, port := range container.Ports {
ports = append(ports, uint16(port))
var ports []backend_types.Port
for _, portDef := range container.Ports {
port, err := convertPort(portDef)
if err != nil {
return nil, err
}
ports = append(ports, port)
}

// at least one constraint contain status success, or all constraints have no status set
Expand Down Expand Up @@ -215,6 +220,28 @@ func (c *Compiler) stepWorkdir(container *yaml_types.Container) string {
return path.Join(c.base, c.path, container.Directory)
}

func convertPort(portDef string) (backend_types.Port, error) {
6543 marked this conversation as resolved.
Show resolved Hide resolved
var err error
var port backend_types.Port

portArr := strings.Split(portDef, "/")
6543 marked this conversation as resolved.
Show resolved Hide resolved

portNumber, err := strconv.ParseUint(portArr[0], 10, 16)
if err != nil {
return port, err
}

var proto string
if len(portArr) > 1 {
proto = portArr[1]
}

port.Number = uint16(portNumber)
port.Protocol = proto

return port, nil
}

func convertKubernetesBackendOptions(kubeOpt *yaml_types.KubernetesBackendOptions) backend_types.KubernetesBackendOptions {
resources := backend_types.Resources{
Limits: kubeOpt.Resources.Limits,
Expand Down
2 changes: 1 addition & 1 deletion pipeline/frontend/yaml/types/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type (
Settings map[string]any `yaml:"settings"`
Volumes Volumes `yaml:"volumes,omitempty"`
When constraint.When `yaml:"when,omitempty"`
Ports []base.StringOrInt `yaml:"ports,omitempty"`
Ports []string `yaml:"ports,omitempty"`

// Docker Specific
Privileged bool `yaml:"privileged,omitempty"`
Expand Down
4 changes: 3 additions & 1 deletion pipeline/frontend/yaml/types/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ settings:
baz: false
ports:
- 8080
- 4443/tcp
- 51820/udp
`)

func TestUnmarshalContainer(t *testing.T) {
Expand Down Expand Up @@ -129,7 +131,7 @@ func TestUnmarshalContainer(t *testing.T) {
"foo": "bar",
"baz": false,
},
Ports: []base.StringOrInt{8080},
Ports: []string{"8080", "4443/tcp", "51820/udp"},
}
got := Container{}
err := yaml.Unmarshal(containerYaml, &got)
Expand Down