-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sort the steps of taskrun based on the steps of task
- Loading branch information
1 parent
f4b73c8
commit 8552f8b
Showing
7 changed files
with
272 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
Copyright 2019 Knative Authors LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package status | ||
|
||
import ( | ||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
corev1 "k8s.io/api/core/v1" | ||
"sort" | ||
) | ||
|
||
|
||
// StepStateSorter implements a sorting mechanism to align the order of the steps in TaskRun | ||
// with the spec steps in Task. | ||
type StepStateSorter struct { | ||
taskRunSteps []v1alpha1.StepState | ||
mapForSort map[string]int | ||
} | ||
|
||
func (trt *StepStateSorter) Init(taskRunSteps []v1alpha1.StepState, taskSpecSteps []corev1.Container) { | ||
trt.taskRunSteps = taskRunSteps | ||
trt.mapForSort = trt.constructTaskStepsSorter(taskSpecSteps) | ||
} | ||
|
||
// constructTaskStepsSorter constructs a map matching the names of | ||
// the steps to their indices for a task. | ||
func (trt *StepStateSorter) constructTaskStepsSorter(taskSpecSteps []corev1.Container) map[string]int { | ||
sorter := make(map[string]int) | ||
for index, step := range taskSpecSteps { | ||
sorter[step.Name] = index | ||
} | ||
return sorter | ||
} | ||
|
||
// changeIndex sorts the steps of the task run, based on the | ||
// order of the steps in the task. Instead of changing the element with the one next to it, | ||
// we directly swap it with the desired index. | ||
func (trt *StepStateSorter) changeIndex(index int) { | ||
// Check if the current index is equal to the desired index. If they are equal, do not swap; if they | ||
// are not equal, swap index j with the desired index. | ||
desiredIndex, exist := trt.mapForSort[trt.taskRunSteps[index].Name] | ||
if exist && index != desiredIndex { | ||
trt.taskRunSteps[desiredIndex], trt.taskRunSteps[index] = trt.taskRunSteps[index], trt.taskRunSteps[desiredIndex] | ||
} | ||
} | ||
|
||
func (trt *StepStateSorter) Len() int { | ||
return len(trt.taskRunSteps) | ||
} | ||
|
||
func (trt *StepStateSorter) Swap(i, j int) { | ||
trt.changeIndex(j) | ||
// The index j is unable to reach the last index. | ||
// When i reaches the end of the array, we need to check whether the last one needs a swap. | ||
if (i == trt.Len() - 1 ) { | ||
trt.changeIndex(i) | ||
} | ||
} | ||
|
||
func (trt *StepStateSorter) Less(i, j int) bool { | ||
// Since the logic is complicated, we move it into the Swap function to decide whether | ||
// and how to change the index. We set it to true here in order to iterate all the | ||
// elements of the array in the Swap function. | ||
return true | ||
} | ||
|
||
func SortTaskRunStepOrder(taskRunSteps []v1alpha1.StepState, taskSpecSteps []corev1.Container) { | ||
trt := new(StepStateSorter) | ||
trt.Init(taskRunSteps, taskSpecSteps) | ||
sort.Sort(trt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Copyright 2019 Knative Authors LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package status | ||
|
||
import ( | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
tb "github.com/tektoncd/pipeline/test/builder" | ||
corev1 "k8s.io/api/core/v1" | ||
"testing" | ||
) | ||
|
||
func TestSortTaskRunStepOrder(t *testing.T) { | ||
task := tb.Task("failing-task", "default", tb.TaskSpec( | ||
tb.Step("hello", "busybox", | ||
tb.Command("/bin/sh"), tb.Args("-c", "echo hello"), | ||
), | ||
tb.Step("exit", "busybox", | ||
tb.Command("/bin/sh"), tb.Args("-c", "exit 1"), | ||
), | ||
tb.Step("world", "busybox", | ||
tb.Command("/bin/sh"), tb.Args("-c", "sleep 30s"), | ||
), | ||
)) | ||
|
||
taskRunStatusSteps := []v1alpha1.StepState{{ | ||
ContainerState: corev1.ContainerState{ | ||
Terminated: &corev1.ContainerStateTerminated{ | ||
ExitCode: 0, | ||
Reason: "Completed", | ||
}, | ||
}, | ||
Name: "world", | ||
|
||
}, { | ||
ContainerState: corev1.ContainerState{ | ||
Terminated: &corev1.ContainerStateTerminated{ | ||
ExitCode: 1, | ||
Reason: "Error", | ||
}, | ||
}, | ||
Name: "exit", | ||
}, { | ||
ContainerState: corev1.ContainerState{ | ||
Terminated: &corev1.ContainerStateTerminated{ | ||
ExitCode: 0, | ||
Reason: "Completed", | ||
}, | ||
}, | ||
Name: "hello", | ||
|
||
}, { | ||
|
||
ContainerState: corev1.ContainerState{ | ||
Terminated: &corev1.ContainerStateTerminated{ | ||
ExitCode: 0, | ||
Reason: "Completed", | ||
}, | ||
}, | ||
Name: "nop", | ||
}} | ||
|
||
SortTaskRunStepOrder(taskRunStatusSteps, task.Spec.Steps) | ||
actualStepOrder := []string{} | ||
for _, state := range(taskRunStatusSteps) { | ||
actualStepOrder = append(actualStepOrder, state.Name) | ||
} | ||
|
||
expectedStepOrder := []string{"hello", "exit", "world", "nop"} | ||
|
||
if d := cmp.Diff(actualStepOrder, expectedStepOrder); d != "" { | ||
t.Errorf("The status steps in TaksRun doesn't match the spec steps in Task, diff: %s", d) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters