From e793d2f46f5f1aa39f1382fd6281b1f643789dd5 Mon Sep 17 00:00:00 2001 From: "elias.souza" Date: Fri, 16 Feb 2024 14:03:06 -0300 Subject: [PATCH 1/3] chore: fix duplicated services --- pipeline/backend/kubernetes/pod.go | 2 +- pipeline/backend/kubernetes/service.go | 2 +- pipeline/backend/kubernetes/service_test.go | 17 +++++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/pipeline/backend/kubernetes/pod.go b/pipeline/backend/kubernetes/pod.go index 698cdba57b6..70413951f58 100644 --- a/pipeline/backend/kubernetes/pod.go +++ b/pipeline/backend/kubernetes/pod.go @@ -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 diff --git a/pipeline/backend/kubernetes/service.go b/pipeline/backend/kubernetes/service.go index 6d3d54df02f..ef86f0ae7b9 100644 --- a/pipeline/backend/kubernetes/service.go +++ b/pipeline/backend/kubernetes/service.go @@ -62,7 +62,7 @@ func mkService(step *types.Step, config *config) (*v1.Service, error) { } func serviceName(step *types.Step) (string, error) { - return dnsName(step.Name) + return dnsName(step.Name + "-" + step.UUID) } func servicePort(port types.Port) v1.ServicePort { diff --git a/pipeline/backend/kubernetes/service_test.go b/pipeline/backend/kubernetes/service_test.go index 0380f110194..17b4922c511 100644 --- a/pipeline/backend/kubernetes/service_test.go +++ b/pipeline/backend/kubernetes/service_test.go @@ -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-0"}) assert.NoError(t, err) - assert.Equal(t, "database", name) + assert.Equal(t, "database-01he8bebctabr3kgk0qj36d2me-0", 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-0"}) assert.NoError(t, err) - assert.Equal(t, "wp-01he8bebctabr3kgk0qj36d2me-0-services-0.woodpecker-runtime.svc.cluster.local", name) + assert.Equal(t, "wp-01he8bebctabr3kgk0qj36d2me-0-services-0.woodpecker-runtime.svc.cluster.local-01he8bebctabr3kgk0qj36d2me-0", name) - name, err = serviceName(&types.Step{Name: "awesome_service"}) + name, err = serviceName(&types.Step{Name: "awesome_service", UUID: "01he8bebctabr3kgk0qj36d2me-0"}) assert.NoError(t, err) - assert.Equal(t, "awesome-service", name) + assert.Equal(t, "awesome-service-01he8bebctabr3kgk0qj36d2me-0", name) } func TestService(t *testing.T) { expected := ` { "metadata": { - "name": "bar", + "name": "bar-01he8bebctabr3kgk0qj36d2me-0", "namespace": "foo", "creationTimestamp": null }, @@ -66,7 +66,7 @@ func TestService(t *testing.T) { } ], "selector": { - "service": "bar" + "service": "bar-01he8bebctabr3kgk0qj36d2me-0" }, "type": "ClusterIP" }, @@ -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) From a2edd893b0f97f74488e84d5241c78f243780a6c Mon Sep 17 00:00:00 2001 From: "elias.souza" Date: Fri, 16 Feb 2024 15:17:29 -0300 Subject: [PATCH 2/3] chore: fix test pod --- pipeline/backend/kubernetes/pod_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pipeline/backend/kubernetes/pod_test.go b/pipeline/backend/kubernetes/pod_test.go index 207b050a0ff..b1aecf3abc3 100644 --- a/pipeline/backend/kubernetes/pod_test.go +++ b/pipeline/backend/kubernetes/pod_test.go @@ -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, "postgres-01he8bebctabr3kg", name) } func TestStepLabel(t *testing.T) { From b9e195ad3fc8e7ec22159f0ddc5ed318e546c423 Mon Sep 17 00:00:00 2001 From: "elias.souza" Date: Fri, 16 Feb 2024 18:20:09 -0300 Subject: [PATCH 3/3] chore: use wp-svc prefix for services pods --- pipeline/backend/kubernetes/pod_test.go | 2 +- pipeline/backend/kubernetes/service.go | 5 +++-- pipeline/backend/kubernetes/service_test.go | 16 ++++++++-------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/pipeline/backend/kubernetes/pod_test.go b/pipeline/backend/kubernetes/pod_test.go index b1aecf3abc3..167ba849e40 100644 --- a/pipeline/backend/kubernetes/pod_test.go +++ b/pipeline/backend/kubernetes/pod_test.go @@ -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-01he8bebctabr3kg", name) + assert.EqualValues(t, "wp-svc-01he8bebctabr3kg-postgres", name) } func TestStepLabel(t *testing.T) { diff --git a/pipeline/backend/kubernetes/service.go b/pipeline/backend/kubernetes/service.go index ef86f0ae7b9..48c8d3fdcb4 100644 --- a/pipeline/backend/kubernetes/service.go +++ b/pipeline/backend/kubernetes/service.go @@ -29,7 +29,8 @@ import ( ) const ( - ServiceLabel = "service" + ServiceLabel = "service" + servicePrefix = "wp-svc-" ) func mkService(step *types.Step, config *config) (*v1.Service, error) { @@ -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 + "-" + step.UUID) + return dnsName(servicePrefix + step.UUID + "-" + step.Name) } func servicePort(port types.Port) v1.ServicePort { diff --git a/pipeline/backend/kubernetes/service_test.go b/pipeline/backend/kubernetes/service_test.go index 17b4922c511..91ce54c1b26 100644 --- a/pipeline/backend/kubernetes/service_test.go +++ b/pipeline/backend/kubernetes/service_test.go @@ -24,24 +24,24 @@ import ( ) func TestServiceName(t *testing.T) { - name, err := serviceName(&types.Step{Name: "database", UUID: "01he8bebctabr3kgk0qj36d2me-0"}) + name, err := serviceName(&types.Step{Name: "database", UUID: "01he8bebctabr3kgk0qj36d2me"}) assert.NoError(t, err) - assert.Equal(t, "database-01he8bebctabr3kgk0qj36d2me-0", 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", UUID: "01he8bebctabr3kgk0qj36d2me-0"}) + 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-01he8bebctabr3kgk0qj36d2me-0", 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", UUID: "01he8bebctabr3kgk0qj36d2me-0"}) + name, err = serviceName(&types.Step{Name: "awesome_service", UUID: "01he8bebctabr3kgk0qj36d2me"}) assert.NoError(t, err) - assert.Equal(t, "awesome-service-01he8bebctabr3kgk0qj36d2me-0", name) + assert.Equal(t, "wp-svc-01he8bebctabr3kgk0qj36d2me-awesome-service", name) } func TestService(t *testing.T) { expected := ` { "metadata": { - "name": "bar-01he8bebctabr3kgk0qj36d2me-0", + "name": "wp-svc-01he8bebctabr3kgk0qj36d2me-0-bar", "namespace": "foo", "creationTimestamp": null }, @@ -66,7 +66,7 @@ func TestService(t *testing.T) { } ], "selector": { - "service": "bar-01he8bebctabr3kgk0qj36d2me-0" + "service": "wp-svc-01he8bebctabr3kgk0qj36d2me-0-bar" }, "type": "ClusterIP" },