use step name in container name

This commit is contained in:
pat-s 2025-01-05 18:36:10 +01:00
parent 5082b618b0
commit 7700e0e6c8
No known key found for this signature in database
GPG key ID: 3C6318841EF78925
8 changed files with 34 additions and 33 deletions

View file

@ -70,7 +70,7 @@ func (e *docker) toConfig(step *types.Step, options BackendOptions) *container.C
}
func toContainerName(step *types.Step) string {
return "wp_" + step.UUID
return "wp_" + step.UUID[:5] + "-" + step.Name
}
// returns a container host configuration.

View file

@ -125,8 +125,8 @@ var (
)
func TestToContainerName(t *testing.T) {
assert.EqualValues(t, "wp_f51821af-4cb8-435e-a3c2-3a684185d828", toContainerName(testCmdStep))
assert.EqualValues(t, "wp_d841ee40-e66e-4275-bb3f-55bf89744b21", toContainerName(testPluginStep))
assert.EqualValues(t, "wp_f5182-hello", toContainerName(testCmdStep))
assert.EqualValues(t, "wp_d841e-lint", toContainerName(testPluginStep))
}
func TestStepToConfig(t *testing.T) {

View file

@ -77,7 +77,7 @@ func stepToPodName(step *types.Step) (name string, err error) {
}
func podName(step *types.Step) (string, error) {
return dnsName(podPrefix + step.UUID)
return dnsName(podPrefix + step.UUID[:5] + "-" + step.Name)
}
func podMeta(step *types.Step, config *config, options BackendOptions, podName string) (meta_v1.ObjectMeta, error) {

View file

@ -26,9 +26,9 @@ import (
)
func TestPodName(t *testing.T) {
name, err := podName(&types.Step{UUID: "01he8bebctabr3kgk0qj36d2me-0"})
name, err := podName(&types.Step{UUID: "01he8bebctabr3kgk0qj36d2me-0", Name: "go-test"})
assert.NoError(t, err)
assert.Equal(t, "wp-01he8bebctabr3kgk0qj36d2me-0", name)
assert.Equal(t, "wp-01he8-go-test", name)
_, err = podName(&types.Step{UUID: "01he8bebctabr3kgk0qj36d2me\\0a"})
assert.ErrorIs(t, err, ErrDNSPatternInvalid)
@ -40,19 +40,19 @@ func TestPodName(t *testing.T) {
func TestStepToPodName(t *testing.T) {
name, err := stepToPodName(&types.Step{UUID: "01he8bebctabr3kg", Name: "clone", Type: types.StepTypeClone})
assert.NoError(t, err)
assert.EqualValues(t, "wp-01he8bebctabr3kg", name)
assert.EqualValues(t, "wp-01he8-clone", name)
name, err = stepToPodName(&types.Step{UUID: "01he8bebctabr3kg", Name: "cache", Type: types.StepTypeCache})
assert.NoError(t, err)
assert.EqualValues(t, "wp-01he8bebctabr3kg", name)
assert.EqualValues(t, "wp-01he8-cache", name)
name, err = stepToPodName(&types.Step{UUID: "01he8bebctabr3kg", Name: "release", Type: types.StepTypePlugin})
assert.NoError(t, err)
assert.EqualValues(t, "wp-01he8bebctabr3kg", name)
assert.EqualValues(t, "wp-01he8-release", name)
name, err = stepToPodName(&types.Step{UUID: "01he8bebctabr3kg", Name: "prepare-env", Type: types.StepTypeCommands})
assert.NoError(t, err)
assert.EqualValues(t, "wp-01he8bebctabr3kg", name)
assert.EqualValues(t, "wp-01he8-prepare-env", name)
name, err = stepToPodName(&types.Step{UUID: "01he8bebctabr3kg", Name: "postgres", Type: types.StepTypeService})
assert.NoError(t, err)
assert.EqualValues(t, "wp-svc-01he8bebctabr3kg-postgres", name)
assert.EqualValues(t, "wp-svc-01he8-postgres", name)
}
func TestStepLabel(t *testing.T) {
@ -68,7 +68,7 @@ func TestTinyPod(t *testing.T) {
const expected = `
{
"metadata": {
"name": "wp-01he8bebctabr3kgk0qj36d2me-0",
"name": "wp-01he8-go-test",
"namespace": "woodpecker",
"creationTimestamp": null,
"labels": {
@ -86,7 +86,7 @@ func TestTinyPod(t *testing.T) {
],
"containers": [
{
"name": "wp-01he8bebctabr3kgk0qj36d2me-0",
"name": "wp-01he8-go-test",
"image": "gradle:8.4.0-jdk21",
"command": [
"/bin/sh",
@ -133,7 +133,7 @@ func TestTinyPod(t *testing.T) {
Environment: map[string]string{"CI": "woodpecker"},
}, &config{
Namespace: "woodpecker",
}, "wp-01he8bebctabr3kgk0qj36d2me-0", "linux/amd64", BackendOptions{})
}, "wp-01he8-go-test", "linux/amd64", BackendOptions{})
assert.NoError(t, err)
podJSON, err := json.Marshal(pod)
@ -147,7 +147,7 @@ func TestFullPod(t *testing.T) {
const expected = `
{
"metadata": {
"name": "wp-01he8bebctabr3kgk0qj36d2me-0",
"name": "wp-01he8-go-test",
"namespace": "woodpecker",
"creationTimestamp": null,
"labels": {
@ -171,7 +171,7 @@ func TestFullPod(t *testing.T) {
],
"containers": [
{
"name": "wp-01he8bebctabr3kgk0qj36d2me-0",
"name": "wp-01he8-go-test",
"image": "meltwater/drone-cache",
"command": [
"/bin/sh",
@ -256,7 +256,7 @@ func TestFullPod(t *testing.T) {
"name": "another-pull-secret"
},
{
"name": "wp-01he8bebctabr3kgk0qj36d2me-0"
"name": "wp-01he8-go-test"
}
],
"tolerations": [
@ -335,7 +335,7 @@ func TestFullPod(t *testing.T) {
PodAnnotationsAllowFromStep: true,
PodNodeSelector: map[string]string{"topology.kubernetes.io/region": "eu-central-1"},
SecurityContext: SecurityContextConfig{RunAsNonRoot: false},
}, "wp-01he8bebctabr3kgk0qj36d2me-0", "linux/amd64", BackendOptions{
}, "wp-01he8-go-test", "linux/amd64", BackendOptions{
Labels: map[string]string{"part-of": "woodpecker-ci"},
Annotations: map[string]string{"kubernetes.io/limit-ranger": "LimitRanger plugin set: cpu, memory request and limit for container"},
NodeSelector: map[string]string{"storage": "ssd"},
@ -366,7 +366,7 @@ func TestPodPrivilege(t *testing.T) {
}, &config{
Namespace: "woodpecker",
SecurityContext: SecurityContextConfig{RunAsNonRoot: globalRunAsRoot},
}, "wp-01he8bebctabr3kgk0qj36d2me-0", "linux/amd64", BackendOptions{
}, "wp-01he8-go-test", "linux/amd64", BackendOptions{
SecurityContext: &secCtx,
})
}
@ -443,7 +443,7 @@ func TestScratchPod(t *testing.T) {
const expected = `
{
"metadata": {
"name": "wp-01he8bebctabr3kgk0qj36d2me-0",
"name": "wp-01he8-go-test",
"namespace": "woodpecker",
"creationTimestamp": null,
"labels": {
@ -453,7 +453,7 @@ func TestScratchPod(t *testing.T) {
"spec": {
"containers": [
{
"name": "wp-01he8bebctabr3kgk0qj36d2me-0",
"name": "wp-01he8-go-test",
"image": "quay.io/curl/curl",
"command": [
"/usr/bin/curl",
@ -474,7 +474,7 @@ func TestScratchPod(t *testing.T) {
Entrypoint: []string{"/usr/bin/curl", "-v", "google.com"},
}, &config{
Namespace: "woodpecker",
}, "wp-01he8bebctabr3kgk0qj36d2me-0", "linux/amd64", BackendOptions{})
}, "wp-01he8-go-test", "linux/amd64", BackendOptions{})
assert.NoError(t, err)
podJSON, err := json.Marshal(pod)

View file

@ -208,7 +208,7 @@ func TestUsernameAndPasswordNeedsSecret(t *testing.T) {
func TestRegistrySecret(t *testing.T) {
const expected = `{
"metadata": {
"name": "wp-01he8bebctabr3kgk0qj36d2me-0",
"name": "wp-01he8-go-test",
"namespace": "woodpecker",
"creationTimestamp": null,
"labels": {

View file

@ -63,7 +63,7 @@ func mkService(step *types.Step, config *config) (*v1.Service, error) {
}
func serviceName(step *types.Step) (string, error) {
return dnsName(servicePrefix + step.UUID + "-" + step.Name)
return dnsName(servicePrefix + step.UUID[:5] + "-" + step.Name)
}
func servicePort(port types.Port) v1.ServicePort {

View file

@ -26,22 +26,22 @@ import (
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)
assert.Equal(t, "wp-svc-01he8-database", name)
name, err = serviceName(&types.Step{Name: "wp-01he8bebctabr3kgk0qj36d2me-0-services-0.woodpecker-runtime.svc.cluster.local", UUID: "01he8bebctabr3kgk0qj36d2me"})
name, err = serviceName(&types.Step{Name: "wp-01he8-clone-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)
assert.Equal(t, "wp-svc-01he8-wp-01he8-clone-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)
assert.Equal(t, "wp-svc-01he8-awesome-service", name)
}
func TestService(t *testing.T) {
expected := `
{
"metadata": {
"name": "wp-svc-01he8bebctabr3kgk0qj36d2me-0-bar",
"name": "wp-svc-01he8-bar",
"namespace": "foo",
"creationTimestamp": null
},
@ -66,7 +66,7 @@ func TestService(t *testing.T) {
}
],
"selector": {
"service": "wp-svc-01he8bebctabr3kgk0qj36d2me-0-bar"
"service": "wp-svc-01he8-bar"
},
"type": "ClusterIP"
},

View file

@ -16,6 +16,7 @@ package kubernetes
import (
"errors"
"fmt"
"os"
"regexp"
"strings"
@ -38,10 +39,10 @@ var (
func dnsName(i string) (string, error) {
res := strings.ToLower(strings.ReplaceAll(i, "_", "-"))
if found := dnsPattern.FindStringIndex(res); found == nil {
return "", ErrDNSPatternInvalid
found := dnsPattern.FindStringIndex(res)
if found == nil {
return "", fmt.Errorf("%w: found invalid characters '%v'", ErrDNSPatternInvalid, found)
}
return res, nil
}