Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance reconcile of taskrun to avoid extra pod creation #2022

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func MakePod(images pipeline.Images, taskRun *v1alpha1.TaskRun, taskSpec v1alpha
*metav1.NewControllerRef(taskRun, groupVersionKind),
},
Annotations: podAnnotations,
Labels: makeLabels(taskRun),
Labels: MakeLabels(taskRun),
},
Spec: corev1.PodSpec{
RestartPolicy: corev1.RestartPolicyNever,
Expand All @@ -247,7 +247,7 @@ func MakePod(images pipeline.Images, taskRun *v1alpha1.TaskRun, taskSpec v1alpha
}

// makeLabels constructs the labels we will propagate from TaskRuns to Pods.
func makeLabels(s *v1alpha1.TaskRun) map[string]string {
func MakeLabels(s *v1alpha1.TaskRun) map[string]string {
labels := make(map[string]string, len(s.ObjectMeta.Labels)+1)
// NB: Set this *before* passing through TaskRun labels. If the TaskRun
// has a managed-by label, it should override this default.
Expand Down
2 changes: 1 addition & 1 deletion pkg/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ func TestMakeLabels(t *testing.T) {
"foo": "bar",
"hello": "world",
}
got := makeLabels(&v1alpha1.TaskRun{
got := MakeLabels(&v1alpha1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: taskRunName,
Labels: map[string]string{
Expand Down
5 changes: 3 additions & 2 deletions pkg/pod/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func MakeTaskRunStatus(tr v1alpha1.TaskRun, pod *corev1.Pod, taskSpec v1alpha1.T
}

func updateCompletedTaskRun(trs *v1alpha1.TaskRunStatus, pod *corev1.Pod) {
if didTaskRunFail(pod) {
if DidTaskRunFail(pod) {
msg := getFailureMessage(pod)
trs.SetCondition(&apis.Condition{
Type: apis.ConditionSucceeded,
Expand Down Expand Up @@ -231,7 +231,8 @@ func updateIncompleteTaskRun(trs *v1alpha1.TaskRunStatus, pod *corev1.Pod) {
}
}

func didTaskRunFail(pod *corev1.Pod) bool {
// DidTaskRunFail check the status of pod to decide if related taskrun is failed
func DidTaskRunFail(pod *corev1.Pod) bool {
f := pod.Status.Phase == corev1.PodFailed
for _, s := range pod.Status.ContainerStatuses {
if isContainerStep(s.Name) {
Expand Down
25 changes: 25 additions & 0 deletions pkg/reconciler/taskrun/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,22 @@ func (c *Reconciler) reconcile(ctx context.Context, tr *v1alpha1.TaskRun) error
c.Logger.Errorf("Error getting pod %q: %v", tr.Status.PodName, err)
return err
}
} else {
pos, err := c.KubeClientSet.CoreV1().Pods(tr.Namespace).List(metav1.ListOptions{
LabelSelector: getLabelSelector(tr),
})
if err != nil {
c.Logger.Errorf("Error listing pods: %v", err)
return err
}
for index := range pos.Items {
po := pos.Items[index]
if metav1.IsControlledBy(&po, tr) && !podconvert.DidTaskRunFail(&po) {
pod = &po
}
}
}

if pod == nil {
pod, err = c.createPod(tr, rtr)
if err != nil {
Expand Down Expand Up @@ -570,3 +585,13 @@ func resourceImplBinding(resources map[string]*v1alpha1.PipelineResource, images
}
return p, nil
}

// getLabelSelector get label of centain taskrun
func getLabelSelector(tr *v1alpha1.TaskRun) string {
labels := []string{}
labelsMap := podconvert.MakeLabels(tr)
for key, value := range labelsMap {
labels = append(labels, fmt.Sprintf("%s=%s", key, value))
}
return strings.Join(labels, ",")
}