Use toLabelValue instead of toDNSName in Kubernetes step label (#6890)

This commit is contained in:
Iván Valdés Castillo
2026-07-24 12:31:54 +02:00
committed by GitHub
parent 0229026e20
commit 16627d0374
3 changed files with 75 additions and 14 deletions
+2 -6
View File
@@ -140,11 +140,11 @@ func podLabels(step *types.Step, config *config, options BackendOptions, taskUUI
if isService(step) {
labels[ServiceLabel], _ = serviceName(step)
}
labels[StepLabelLegacy], err = stepLabel(step)
labels[StepLabelLegacy], err = toLabelValue(step.Name)
if err != nil {
return labels, err
}
labels[StepLabel], err = stepLabel(step)
labels[StepLabel], err = toLabelValue(step.Name)
if err != nil {
return labels, err
}
@@ -156,10 +156,6 @@ func podLabels(step *types.Step, config *config, options BackendOptions, taskUUI
return labels, nil
}
func stepLabel(step *types.Step) (string, error) {
return toDNSName(step.Name)
}
func podAnnotations(config *config, options BackendOptions) map[string]string {
annotations := make(map[string]string)
+71 -6
View File
@@ -16,6 +16,7 @@ package kubernetes
import (
"encoding/json"
"strings"
"testing"
"github.com/kinbiko/jsonassert"
@@ -115,14 +116,78 @@ func TestPodMeta(t *testing.T) {
assert.EqualValues(t, "", meta.Labels[ServiceLabel])
}
func TestStepLabel(t *testing.T) {
name, err := stepLabel(&types.Step{Name: "Build image"})
assert.NoError(t, err)
assert.EqualValues(t, "build-image", name)
// TestStepNameAsLabel verifies that step names are correctly converted to valid
// Kubernetes label values via toLabelValue (replaces the old stepLabel wrapper).
func TestStepNameAsLabel(t *testing.T) {
tests := []struct {
name string
stepName string
want string
}{
{
name: "spaces converted to dashes and lowercased",
stepName: "Build image",
want: "build-image",
},
{
name: "leading dot stripped",
stepName: ".build.image",
want: "build.image",
},
{
name: "simple lowercase name unchanged",
stepName: "test",
want: "test",
},
{
name: "underscores preserved",
stepName: "run_tests",
want: "run_tests",
},
{
name: "mixed special characters",
stepName: "Deploy (production)",
want: "deploy-production",
},
{
name: "consecutive spaces single dash",
stepName: "step name",
want: "step-name",
},
}
name, err = stepLabel(&types.Step{Name: ".build.image"})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := toLabelValue(tt.stepName)
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
// TestStepNameAsLabelInPod verifies that step labels in a pod use toLabelValue
// and produce valid values that Kubernetes accepts.
func TestStepNameAsLabelInPod(t *testing.T) {
step := &types.Step{
Name: "Build & Deploy (staging)",
Image: "alpine:latest",
UUID: "01he8bebctabr3kgk0qj36d2me-1",
WorkingDir: "/woodpecker/src",
Environment: map[string]string{},
}
meta, err := podMeta(step, &config{Namespace: "woodpecker"}, BackendOptions{}, "wp-01he8bebctabr3kgk0qj36d2me-1", taskUUID)
assert.NoError(t, err)
assert.EqualValues(t, "build.image", name)
assert.Equal(t, "build-deploy-staging", meta.Labels[StepLabel])
assert.Equal(t, "build-deploy-staging", meta.Labels[StepLabelLegacy])
}
// TestStepNameAsLabelLongName verifies that long step names are truncated
// to 63 characters (Kubernetes label value limit).
func TestStepNameAsLabelLongName(t *testing.T) {
longName := strings.Repeat("a", 100)
got, err := toLabelValue(longName)
assert.NoError(t, err)
assert.LessOrEqual(t, len(got), 63)
}
func TestPodHostnameSanitized(t *testing.T) {
+2 -2
View File
@@ -270,11 +270,11 @@ func registrySecretLabels(step *types.Step, config *config) (map[string]string,
if step.Type == types.StepTypeService {
labels[ServiceLabel], _ = serviceName(step)
}
labels[StepLabelLegacy], err = stepLabel(step)
labels[StepLabelLegacy], err = toLabelValue(step.Name)
if err != nil {
return labels, err
}
labels[StepLabel], err = stepLabel(step)
labels[StepLabel], err = toLabelValue(step.Name)
if err != nil {
return labels, err
}