woodpecker/pipeline/backend/kubernetes/service_test.go
Elias bffc9c8ff8
fix: can't run multiple services on k8s (#3395)
Fix Issue: https://github.com/woodpecker-ci/woodpecker/issues/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>
2024-02-17 12:30:06 +01:00

91 lines
2.5 KiB
Go

// Copyright 2022 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 kubernetes
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
)
func TestServiceName(t *testing.T) {
name, err := serviceName(&types.Step{Name: "database", UUID: "01he8bebctabr3kgk0qj36d2me"})
assert.NoError(t, err)
assert.Equal(t, "wp-svc-01he8bebctabr3kgk0qj36d2me-database", name)
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-svc-01he8bebctabr3kgk0qj36d2me-wp-01he8bebctabr3kgk0qj36d2me-0-services-0.woodpecker-runtime.svc.cluster.local", name)
name, err = serviceName(&types.Step{Name: "awesome_service", UUID: "01he8bebctabr3kgk0qj36d2me"})
assert.NoError(t, err)
assert.Equal(t, "wp-svc-01he8bebctabr3kgk0qj36d2me-awesome-service", name)
}
func TestService(t *testing.T) {
expected := `
{
"metadata": {
"name": "wp-svc-01he8bebctabr3kgk0qj36d2me-0-bar",
"namespace": "foo",
"creationTimestamp": null
},
"spec": {
"ports": [
{
"name": "port-1",
"port": 1,
"targetPort": 1
},
{
"name": "port-2",
"protocol": "TCP",
"port": 2,
"targetPort": 2
},
{
"name": "port-3",
"protocol": "UDP",
"port": 3,
"targetPort": 3
}
],
"selector": {
"service": "wp-svc-01he8bebctabr3kgk0qj36d2me-0-bar"
},
"type": "ClusterIP"
},
"status": {
"loadBalancer": {}
}
}`
ports := []types.Port{
{Number: 1},
{Number: 2, Protocol: "tcp"},
{Number: 3, Protocol: "udp"},
}
s, err := mkService(&types.Step{
Name: "bar",
UUID: "01he8bebctabr3kgk0qj36d2me-0",
Ports: ports,
}, &config{Namespace: "foo"})
assert.NoError(t, err)
j, err := json.Marshal(s)
assert.NoError(t, err)
assert.JSONEq(t, expected, string(j))
}