Skip to content

Commit

Permalink
fix: can't run multiple services on k8s (#3395)
Browse files Browse the repository at this point in the history
Fix Issue: #3288

The way the pod service starts up makes it impossible to run two or more
pipelines at the same time when we have a service section.

The idea is to set the name of the service in the same way we did for
the pod name.

Pipeline: 

```yaml

services:
  mydb:
    image: mysql
    environment:
      - MYSQL_DATABASE=test
      - MYSQL_ROOT_PASSWORD=example
    ports:
      - 3306/tcp
steps:
  get-version:
    image: ubuntu
    commands:
      - ( apt update && apt dist-upgrade -y && apt install -y mysql-client 2>&1 )> /dev/null
      - sleep 30s # need to wait for mysql-server init
      - echo 'SHOW VARIABLES LIKE "version"' | mysql -uroot -hmydb test -pexample
```

Running more than one pipeline result:


![image](https://github.com/woodpecker-ci/woodpecker/assets/22245125/e512309f-0d1e-4125-bab9-2357a710fedd)

---------

Co-authored-by: elias.souza <elias.souza@quintoandar.com.br>
  • Loading branch information
eliasscosta and eliasscosta authored Feb 17, 2024
1 parent b7891b9 commit bffc9c8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pipeline/backend/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func podMeta(step *types.Step, config *config, options BackendOptions, podName s
}

if step.Type == types.StepTypeService {
meta.Labels[ServiceLabel] = step.Name
meta.Labels[ServiceLabel], _ = serviceName(step)
}

meta.Annotations = config.PodAnnotations
Expand Down
2 changes: 1 addition & 1 deletion pipeline/backend/kubernetes/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestStepToPodName(t *testing.T) {
assert.EqualValues(t, "wp-01he8bebctabr3kg", name)
name, err = stepToPodName(&types.Step{UUID: "01he8bebctabr3kg", Name: "postgres", Type: types.StepTypeService})
assert.NoError(t, err)
assert.EqualValues(t, "postgres", name)
assert.EqualValues(t, "wp-svc-01he8bebctabr3kg-postgres", name)
}

func TestStepLabel(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions pipeline/backend/kubernetes/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import (
)

const (
ServiceLabel = "service"
ServiceLabel = "service"
servicePrefix = "wp-svc-"
)

func mkService(step *types.Step, config *config) (*v1.Service, error) {
Expand Down Expand Up @@ -62,7 +63,7 @@ func mkService(step *types.Step, config *config) (*v1.Service, error) {
}

func serviceName(step *types.Step) (string, error) {
return dnsName(step.Name)
return dnsName(servicePrefix + step.UUID + "-" + step.Name)
}

func servicePort(port types.Port) v1.ServicePort {
Expand Down
17 changes: 9 additions & 8 deletions pipeline/backend/kubernetes/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@ import (
)

func TestServiceName(t *testing.T) {
name, err := serviceName(&types.Step{Name: "database"})
name, err := serviceName(&types.Step{Name: "database", UUID: "01he8bebctabr3kgk0qj36d2me"})
assert.NoError(t, err)
assert.Equal(t, "database", name)
assert.Equal(t, "wp-svc-01he8bebctabr3kgk0qj36d2me-database", name)

name, err = serviceName(&types.Step{Name: "wp-01he8bebctabr3kgk0qj36d2me-0-services-0.woodpecker-runtime.svc.cluster.local"})
name, err = serviceName(&types.Step{Name: "wp-01he8bebctabr3kgk0qj36d2me-0-services-0.woodpecker-runtime.svc.cluster.local", UUID: "01he8bebctabr3kgk0qj36d2me"})
assert.NoError(t, err)
assert.Equal(t, "wp-01he8bebctabr3kgk0qj36d2me-0-services-0.woodpecker-runtime.svc.cluster.local", name)
assert.Equal(t, "wp-svc-01he8bebctabr3kgk0qj36d2me-wp-01he8bebctabr3kgk0qj36d2me-0-services-0.woodpecker-runtime.svc.cluster.local", name)

name, err = serviceName(&types.Step{Name: "awesome_service"})
name, err = serviceName(&types.Step{Name: "awesome_service", UUID: "01he8bebctabr3kgk0qj36d2me"})
assert.NoError(t, err)
assert.Equal(t, "awesome-service", name)
assert.Equal(t, "wp-svc-01he8bebctabr3kgk0qj36d2me-awesome-service", name)
}

func TestService(t *testing.T) {
expected := `
{
"metadata": {
"name": "bar",
"name": "wp-svc-01he8bebctabr3kgk0qj36d2me-0-bar",
"namespace": "foo",
"creationTimestamp": null
},
Expand All @@ -66,7 +66,7 @@ func TestService(t *testing.T) {
}
],
"selector": {
"service": "bar"
"service": "wp-svc-01he8bebctabr3kgk0qj36d2me-0-bar"
},
"type": "ClusterIP"
},
Expand All @@ -81,6 +81,7 @@ func TestService(t *testing.T) {
}
s, err := mkService(&types.Step{
Name: "bar",
UUID: "01he8bebctabr3kgk0qj36d2me-0",
Ports: ports,
}, &config{Namespace: "foo"})
assert.NoError(t, err)
Expand Down

0 comments on commit bffc9c8

Please sign in to comment.