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 all 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
2 changes: 1 addition & 1 deletion pipeline/backend/docker/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func TestToConfigFull(t *testing.T) {
Failure: "fail",
AuthConfig: backend.Auth{Username: "user", Password: "123456", Email: "user@example.com"},
NetworkMode: "bridge",
Ports: []uint16{21, 22},
Ports: []backend.Port{{Number: 21}, {Number: 22}},
})

assert.NotNil(t, conf)
Expand Down
16 changes: 16 additions & 0 deletions pipeline/backend/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func podContainer(step *types.Step, podName, goos string) (v1.Container, error)
}

container.Env = mapToEnvVars(step.Environment)
container.Ports = containerPorts(step.Ports)
container.SecurityContext = containerSecurityContext(step.BackendOptions.Kubernetes.SecurityContext, step.Privileged)

container.Resources, err = resourceRequirements(step.BackendOptions.Kubernetes.Resources)
Expand Down Expand Up @@ -198,6 +199,21 @@ func volumeMount(name, path string) v1.VolumeMount {
}
}

func containerPorts(ports []types.Port) []v1.ContainerPort {
containerPorts := make([]v1.ContainerPort, len(ports))
for i, port := range ports {
containerPorts[i] = containerPort(port)
}
return containerPorts
}

func containerPort(port types.Port) v1.ContainerPort {
return v1.ContainerPort{
ContainerPort: int32(port.Number),
Protocol: v1.Protocol(strings.ToUpper(port.Protocol)),
}
}

// Here is the service IPs (placed in /etc/hosts in the Pod)
func hostAliases(extraHosts []types.HostAlias) []v1.HostAlias {
hostAliases := []v1.HostAlias{}
Expand Down
19 changes: 19 additions & 0 deletions pipeline/backend/kubernetes/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,19 @@ func TestFullPod(t *testing.T) {
"echo $CI_SCRIPT | base64 -d | /bin/sh -e"
],
"workingDir": "/woodpecker/src",
"ports": [
{
"containerPort": 1234
},
{
"containerPort": 2345,
"protocol": "TCP"
},
{
"containerPort": 3456,
"protocol": "UDP"
}
],
"env": [
"<<UNORDERED>>",
{
Expand Down Expand Up @@ -269,6 +282,11 @@ func TestFullPod(t *testing.T) {
{Name: "cloudflare", IP: "1.1.1.1"},
{Name: "cf.v6", IP: "2606:4700:4700::64"},
}
ports := []types.Port{
{Number: 1234},
{Number: 2345, Protocol: "tcp"},
{Number: 3456, Protocol: "udp"},
}
secCtx := types.SecurityContext{
Privileged: newBool(true),
RunAsNonRoot: newBool(true),
Expand All @@ -294,6 +312,7 @@ func TestFullPod(t *testing.T) {
Volumes: []string{"woodpecker-cache:/woodpecker/src/cache"},
Environment: map[string]string{"CGO": "0"},
ExtraHosts: hostAliases,
Ports: ports,
BackendOptions: types.BackendOptions{
Kubernetes: types.KubernetesBackendOptions{
NodeSelector: map[string]string{"storage": "ssd"},
Expand Down
19 changes: 14 additions & 5 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"
v1 "k8s.io/api/core/v1"
Expand All @@ -43,13 +44,10 @@ func mkService(step *types.Step, namespace string) (*v1.Service, error) {

var svcPorts []v1.ServicePort
for _, port := range step.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))
}

log.Trace().Str("name", name).Interface("selector", selector).Interface("ports", svcPorts).Msg("creating service")
return &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand All @@ -67,6 +65,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) {
svc, err := mkService(step, engine.config.Namespace)
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 @@ -53,11 +53,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 @@ -71,10 +73,14 @@ func TestService(t *testing.T) {
"loadBalancer": {}
}
}`

ports := []types.Port{
{Number: 1},
{Number: 2, Protocol: "tcp"},
{Number: 3, Protocol: "udp"},
}
s, err := mkService(&types.Step{
Name: "bar",
Ports: []uint16{1, 2, 3},
Ports: ports,
}, "foo")
6543 marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(t, err)
j, err := json.Marshal(s)
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 @@ -45,7 +45,7 @@ type Step struct {
Failure string `json:"failure,omitempty"`
AuthConfig Auth `json:"auth_config,omitempty"`
NetworkMode string `json:"network_mode,omitempty"`
Ports []uint16 `json:"ports,omitempty"`
Ports []Port `json:"ports,omitempty"`
BackendOptions BackendOptions `json:"backend_options,omitempty"`
}

Expand Down
27 changes: 24 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 @@
"fmt"
"maps"
"path"
"strconv"
"strings"

"github.com/oklog/ulid/v2"
Expand Down Expand Up @@ -154,9 +155,13 @@
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)

Check warning on line 164 in pipeline/frontend/yaml/compiler/convert.go

View check run for this annotation

Codecov / codecov/patch

pipeline/frontend/yaml/compiler/convert.go#L160-L164

Added lines #L160 - L164 were not covered by tests
}

// at least one constraint contain status success, or all constraints have no status set
Expand Down Expand Up @@ -210,6 +215,22 @@
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

number, protocol, _ := strings.Cut(portDef, "/")
port.Protocol = protocol

portNumber, err := strconv.ParseUint(number, 10, 16)
if err != nil {
return port, err
}
port.Number = uint16(portNumber)

return port, nil
}

func convertKubernetesBackendOptions(kubeOpt *yaml_types.KubernetesBackendOptions) backend_types.KubernetesBackendOptions {
resources := backend_types.Resources{
Limits: kubeOpt.Resources.Limits,
Expand Down
60 changes: 60 additions & 0 deletions pipeline/frontend/yaml/compiler/convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2024 Woodpecker 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 compiler

import (
"testing"

"github.com/stretchr/testify/assert"
backend_types "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
)

func TestConvertPortNumber(t *testing.T) {
portDef := "1234"
actualPort, err := convertPort(portDef)
assert.NoError(t, err)
assert.Equal(t, backend_types.Port{
Number: 1234,
Protocol: "",
}, actualPort)
}

func TestConvertPortUdp(t *testing.T) {
portDef := "1234/udp"
actualPort, err := convertPort(portDef)
assert.NoError(t, err)
assert.Equal(t, backend_types.Port{
Number: 1234,
Protocol: "udp",
}, actualPort)
}

func TestConvertPortWrongOrder(t *testing.T) {
portDef := "tcp/1234"
_, err := convertPort(portDef)
assert.Error(t, err)
}

func TestConvertPortWrongDelimiter(t *testing.T) {
portDef := "1234|udp"
_, err := convertPort(portDef)
assert.Error(t, err)
}

func TestConvertPortWrong(t *testing.T) {
portDef := "http"
_, err := convertPort(portDef)
assert.Error(t, err)
}
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"`
DependsOn base.StringOrSlice `yaml:"depends_on,omitempty"`

// Docker Specific
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