Skip to content

Commit 4d50541

Browse files
committed
fix: pipeline names easily exceed character limit
1 parent 265d27b commit 4d50541

File tree

3 files changed

+54
-43
lines changed

3 files changed

+54
-43
lines changed

pkg/pipelines/tekton/gitlab_int_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,9 @@ func awaitBuildCompletion(t *testing.T, name, ns string) <-chan struct{} {
562562
t.Fatal(err)
563563
}
564564

565+
// Watch for TaskRuns with the build task label and function name label
565566
listOpts := metav1.ListOptions{
566-
LabelSelector: "tekton.dev/pipelineTask=build",
567+
LabelSelector: fmt.Sprintf("tekton.dev/pipelineTask=build,function.knative.dev/name=%s", name),
567568
Watch: true,
568569
}
569570
w, err := clis.Tekton.TektonV1().TaskRuns(ns).Watch(context.Background(), listOpts)
@@ -580,9 +581,7 @@ func awaitBuildCompletion(t *testing.T, name, ns string) <-chan struct{} {
580581
if !ok {
581582
continue
582583
}
583-
if !strings.HasPrefix(taskRun.Name, name) {
584-
continue
585-
}
584+
// No longer need to check name prefix since we're filtering by label
586585
for _, condition := range taskRun.Status.Conditions {
587586
if condition.Type == apis.ConditionSucceeded && condition.IsTrue() {
588587
ch <- struct{}{}

pkg/pipelines/tekton/resources.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package tekton
22

33
import (
44
"context"
5+
"crypto/sha256"
6+
"encoding/hex"
57
"errors"
68
"fmt"
79

@@ -57,7 +59,12 @@ func getPipelineName(f fn.Function) string {
5759
} else {
5860
source = "git"
5961
}
60-
return fmt.Sprintf("%s-%s-%s-pipeline", f.Name, f.Build.Builder, source)
62+
63+
// Kubernetes resource names must be <= 63 characters (RFC 1123)
64+
fullIdentifier := fmt.Sprintf("%s-%s-%s", f.Name, f.Build.Builder, source)
65+
hash := sha256.Sum256([]byte(fullIdentifier))
66+
shortHash := hex.EncodeToString(hash[:4]) // Use first 8 characters
67+
return fmt.Sprintf("func-%s-%s-%s", shortHash, f.Build.Builder, source)
6168
}
6269

6370
func getPipelineRunGenerateName(f fn.Function) string {

test/oncluster/tekton.go

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,43 @@ package oncluster
33
import (
44
"context"
55
"fmt"
6-
"strings"
76
"testing"
87

98
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
109
"knative.dev/func/pkg/k8s"
1110
"knative.dev/func/pkg/pipelines/tekton"
1211
)
1312

14-
// TektonPipelineExists verifies pipeline with a given prefix exists on cluster
15-
func TektonPipelineExists(t *testing.T, pipelinePrefix string) bool {
13+
// TektonPipelineExists verifies pipeline with a given function name exists on cluster
14+
func TektonPipelineExists(t *testing.T, functionName string) bool {
1615
ns, _, _ := k8s.GetClientConfig().Namespace()
1716
client, _ := tekton.NewTektonClient(ns)
18-
pipelines, err := client.Pipelines(ns).List(context.Background(), v1.ListOptions{})
17+
// Look for pipelines with the function.knative.dev/name label matching the function name
18+
pipelines, err := client.Pipelines(ns).List(context.Background(), v1.ListOptions{
19+
LabelSelector: "function.knative.dev/name=" + functionName,
20+
})
1921
if err != nil {
2022
t.Error(err.Error())
23+
return false
2124
}
22-
for _, pipeline := range pipelines.Items {
23-
if strings.HasPrefix(pipeline.Name, pipelinePrefix) && strings.HasSuffix(pipeline.Name, "-pipeline") {
24-
return true
25-
}
26-
}
27-
return false
25+
// If any pipeline exists with this label, it means the pipeline was created for this function
26+
return len(pipelines.Items) > 0
2827
}
2928

30-
// TektonPipelineRunExists verifies pipelinerun with a given prefix exists on cluster
31-
func TektonPipelineRunExists(t *testing.T, pipelineRunPrefix string) bool {
29+
// TektonPipelineRunExists verifies pipelinerun with a given function name exists on cluster
30+
func TektonPipelineRunExists(t *testing.T, functionName string) bool {
3231
ns, _, _ := k8s.GetClientConfig().Namespace()
3332
client, _ := tekton.NewTektonClient(ns)
34-
pipelineRuns, err := client.PipelineRuns(ns).List(context.Background(), v1.ListOptions{})
33+
// Look for pipeline runs with the function.knative.dev/name label matching the function name
34+
pipelineRuns, err := client.PipelineRuns(ns).List(context.Background(), v1.ListOptions{
35+
LabelSelector: "function.knative.dev/name=" + functionName,
36+
})
3537
if err != nil {
3638
t.Error(err.Error())
39+
return false
3740
}
38-
for _, run := range pipelineRuns.Items {
39-
if strings.HasPrefix(run.Name, pipelineRunPrefix) {
40-
return true
41-
}
42-
}
43-
return false
41+
// If any pipeline run exists with this label, it means a pipeline run was created for this function
42+
return len(pipelineRuns.Items) > 0
4443
}
4544

4645
type PipelineRunSummary struct {
@@ -67,33 +66,39 @@ func (p *PipelineRunSummary) IsSucceed() bool {
6766

6867
// TektonPipelineLastRunSummary gather information about a pipeline run such as
6968
// list of tasks executed and status of each task execution. It is meant to be used on assertions
70-
func TektonPipelineLastRunSummary(t *testing.T, pipelinePrefix string) *PipelineRunSummary {
69+
func TektonPipelineLastRunSummary(t *testing.T, functionName string) *PipelineRunSummary {
7170
ns, _, _ := k8s.GetClientConfig().Namespace()
7271
client, _ := tekton.NewTektonClient(ns)
73-
pipelineRuns, err := client.PipelineRuns(ns).List(context.Background(), v1.ListOptions{})
72+
// Look for pipeline runs with the function.knative.dev/name label matching the function name
73+
pipelineRuns, err := client.PipelineRuns(ns).List(context.Background(), v1.ListOptions{
74+
LabelSelector: "function.knative.dev/name=" + functionName,
75+
})
7476
if err != nil {
7577
t.Error(err.Error())
78+
return &PipelineRunSummary{}
7679
}
80+
7781
lr := PipelineRunSummary{}
78-
for _, run := range pipelineRuns.Items {
79-
if strings.HasPrefix(run.Name, pipelinePrefix) {
80-
lr.PipelineRunName = run.Name
81-
if len(run.Status.Conditions) > 0 {
82-
lr.PipelineRunStatus = run.Status.Conditions[0].Reason
82+
// Get the most recent pipeline run (they're sorted by creation time)
83+
if len(pipelineRuns.Items) > 0 {
84+
// Take the first one (most recent) from the filtered list
85+
run := pipelineRuns.Items[0]
86+
lr.PipelineRunName = run.Name
87+
if len(run.Status.Conditions) > 0 {
88+
lr.PipelineRunStatus = run.Status.Conditions[0].Reason
89+
}
90+
lr.TasksRunSummary = []PipelineTaskRunSummary{}
91+
for _, ref := range run.Status.ChildReferences {
92+
r := PipelineTaskRunSummary{}
93+
r.TaskName = ref.PipelineTaskName
94+
taskRun, err := client.TaskRuns(ns).Get(context.Background(), ref.Name, v1.GetOptions{})
95+
if err != nil {
96+
t.Error(err.Error())
8397
}
84-
lr.TasksRunSummary = []PipelineTaskRunSummary{}
85-
for _, ref := range run.Status.ChildReferences {
86-
r := PipelineTaskRunSummary{}
87-
r.TaskName = ref.PipelineTaskName
88-
taskRun, err := client.TaskRuns(ns).Get(context.Background(), ref.Name, v1.GetOptions{})
89-
if err != nil {
90-
t.Error(err.Error())
91-
}
92-
if len(taskRun.Status.Conditions) > 0 {
93-
r.TaskStatus = taskRun.Status.Conditions[0].Reason
94-
}
95-
lr.TasksRunSummary = append(lr.TasksRunSummary, r)
98+
if len(taskRun.Status.Conditions) > 0 {
99+
r.TaskStatus = taskRun.Status.Conditions[0].Reason
96100
}
101+
lr.TasksRunSummary = append(lr.TasksRunSummary, r)
97102
}
98103
}
99104
return &lr

0 commit comments

Comments
 (0)