Skip to content

Commit

Permalink
Update sort package to latest & adds nil check for PRun before sorting
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sm43 authored and chmouel committed May 29, 2024
1 parent edab9bf commit fa5c20d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
12 changes: 11 additions & 1 deletion pkg/pipelineascode/concurrency.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,19 @@ func (c *ConcurrencyManager) GetExecutionOrder() (string, []*v1.PipelineRun) {
return "", nil
}

if len(c.pipelineRuns) == 0 {
return "", nil
}

runtimeObjs := []runtime.Object{}
for _, pr := range c.pipelineRuns {
runtimeObjs = append(runtimeObjs, pr)
if pr != nil && pr.Name != "" {
runtimeObjs = append(runtimeObjs, pr)
}
}

if len(runtimeObjs) == 0 {
return "", nil
}

// sort runs by name
Expand Down
15 changes: 15 additions & 0 deletions pkg/pipelineascode/concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,18 @@ func TestExecutionOrder(t *testing.T) {
assert.Equal(t, order, "test/abc,test/def,test/mno,test/pqr")
assert.Equal(t, len(runs), 4)
}

func TestExecutionOrder_SinglePRun(t *testing.T) {
cm := NewConcurrencyManager()

testNs := "test"
abcPR := &tektonv1.PipelineRun{ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: testNs}}
cm.Enable()

// add pipelineRuns in random order
cm.AddPipelineRun(abcPR)

order, runs := cm.GetExecutionOrder()
assert.Equal(t, order, "test/abc")
assert.Equal(t, len(runs), 1)
}
10 changes: 7 additions & 3 deletions pkg/sort/runtime_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

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

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

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

0 comments on commit fa5c20d

Please sign in to comment.