Skip to content

Commit fa5c20d

Browse files
sm43chmouel
authored andcommitted
Update sort package to latest & adds nil check for PRun before sorting
we use sort code from k8s repo, this updates the code to the latest master but keeps using integer.IntMin to continue using golang 1.20 & check pipelinrun is not nil and its name is not empty string then only add for sorting
1 parent edab9bf commit fa5c20d

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

pkg/pipelineascode/concurrency.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,19 @@ func (c *ConcurrencyManager) GetExecutionOrder() (string, []*v1.PipelineRun) {
4343
return "", nil
4444
}
4545

46+
if len(c.pipelineRuns) == 0 {
47+
return "", nil
48+
}
49+
4650
runtimeObjs := []runtime.Object{}
4751
for _, pr := range c.pipelineRuns {
48-
runtimeObjs = append(runtimeObjs, pr)
52+
if pr != nil && pr.Name != "" {
53+
runtimeObjs = append(runtimeObjs, pr)
54+
}
55+
}
56+
57+
if len(runtimeObjs) == 0 {
58+
return "", nil
4959
}
5060

5161
// sort runs by name

pkg/pipelineascode/concurrency_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,18 @@ func TestExecutionOrder(t *testing.T) {
2929
assert.Equal(t, order, "test/abc,test/def,test/mno,test/pqr")
3030
assert.Equal(t, len(runs), 4)
3131
}
32+
33+
func TestExecutionOrder_SinglePRun(t *testing.T) {
34+
cm := NewConcurrencyManager()
35+
36+
testNs := "test"
37+
abcPR := &tektonv1.PipelineRun{ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: testNs}}
38+
cm.Enable()
39+
40+
// add pipelineRuns in random order
41+
cm.AddPipelineRun(abcPR)
42+
43+
order, runs := cm.GetExecutionOrder()
44+
assert.Equal(t, order, "test/abc")
45+
assert.Equal(t, len(runs), 1)
46+
}

pkg/sort/runtime_sort.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
// The following code has been take from kubectl get command
1919
// instead of importing all the dependencies, copying only the required part
20-
// https://github.com/kubernetes/kubernetes/blob/8e48df135318026d5f8a972a96a2ff665889568a/staging/src/k8s.io/kubectl/pkg/cmd/get/sorter.go#L151
20+
// https://github.com/kubernetes/kubernetes/blob/20d0ab7ae808aaddb1556c3c38ca0607663c50ac/staging/src/k8s.io/kubectl/pkg/cmd/get/sorter.go#L150
2121

2222
// RuntimeSort is an implementation of the golang sort interface that knows how to sort
2323
// lists of runtime.Object.
@@ -56,7 +56,7 @@ func isLess(i, j reflect.Value) (bool, error) {
5656
return i.Float() < j.Float(), nil
5757
case reflect.String:
5858
return sortorder.NaturalLess(i.String(), j.String()), nil
59-
case reflect.Ptr:
59+
case reflect.Pointer:
6060
return isLess(i.Elem(), j.Elem())
6161
case reflect.Struct:
6262
// sort metav1.Time
@@ -210,6 +210,8 @@ func (r *RuntimeSort) Less(i, j int) bool {
210210
}
211211

212212
// OriginalPosition returns the starting (original) position of a particular index.
213+
// e.g. If OriginalPosition(0) returns 5 than the
214+
// item currently at position 0 was at position 5 in the original unsorted array.
213215
func (r *RuntimeSort) OriginalPosition(ix int) int {
214216
if ix < 0 || ix > len(r.origPosition) {
215217
return -1
@@ -227,5 +229,7 @@ func findJSONPathResults(parser *jsonpath.JSONPath, from runtime.Object) ([][]re
227229
// ByField sorts the runtime objects passed by the field.
228230
func ByField(field string, runTimeObj []runtime.Object) {
229231
sorter := NewRuntimeSort(field, runTimeObj)
230-
sort.Sort(sorter)
232+
if len(runTimeObj) > 0 {
233+
sort.Sort(sorter)
234+
}
231235
}

0 commit comments

Comments
 (0)