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

fix: can't run multiple services on k8s #3395

Merged
merged 5 commits into from
Feb 17, 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
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 @@
}

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

Check warning on line 93 in pipeline/backend/kubernetes/pod.go

View check run for this annotation

Codecov / codecov/patch

pipeline/backend/kubernetes/pod.go#L93

Added line #L93 was not covered by tests
}

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
},
6543 marked this conversation as resolved.
Show resolved Hide resolved
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