Skip to content

Commit

Permalink
add containerstate and containername for sidecars
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhelfand committed Feb 21, 2020
1 parent bb1d197 commit 787b30b
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 33 deletions.
10 changes: 6 additions & 4 deletions pkg/apis/pipeline/v1alpha2/taskrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,20 @@ func (tr *TaskRunStatus) SetCondition(newCond *apis.Condition) {
}
}

// StepState reports the results of running a step in the Task.
// StepState reports the results of running a step in a Task.
type StepState struct {
corev1.ContainerState
Name string `json:"name,omitempty"`
ContainerName string `json:"container,omitempty"`
ImageID string `json:"imageID,omitempty"`
}

// SidecarState reports the results of sidecar in the Task.
// SidecarState reports the results of running a sidecar in a Task.
type SidecarState struct {
Name string `json:"name,omitempty"`
ImageID string `json:"imageID,omitempty"`
corev1.ContainerState
Name string `json:"name,omitempty"`
ContainerName string `json:"container,omitempty"`
ImageID string `json:"imageID,omitempty"`
}

// CloudEventDelivery is the target of a cloud event along with the state of
Expand Down
5 changes: 4 additions & 1 deletion pkg/apis/pipeline/v1alpha2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pkg/pod/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ func MakeTaskRunStatus(tr v1alpha1.TaskRun, pod *corev1.Pod, taskSpec v1alpha1.T
})
} else if isContainerSidecar(s.Name) {
trs.Sidecars = append(trs.Sidecars, v1alpha1.SidecarState{
Name: trimSidecarPrefix(s.Name),
ImageID: s.ImageID,
ContainerState: *s.State.DeepCopy(),
Name: trimSidecarPrefix(s.Name),
ContainerName: s.Name,
ImageID: s.ImageID,
})
}
}
Expand Down
108 changes: 106 additions & 2 deletions pkg/pod/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,112 @@ func TestMakeTaskRunStatus(t *testing.T) {
ContainerName: "step-running-step",
}},
Sidecars: []v1alpha1.SidecarState{{
Name: "running",
ImageID: "image-id",
ContainerState: corev1.ContainerState{
Running: &corev1.ContainerStateRunning{},
},
Name: "running",
ImageID: "image-id",
ContainerName: "sidecar-running",
}},
},
},
}, {
desc: "with-sidecar-waiting",
podStatus: corev1.PodStatus{
Phase: corev1.PodRunning,
ContainerStatuses: []corev1.ContainerStatus{{
Name: "step-waiting-step",
State: corev1.ContainerState{
Waiting: &corev1.ContainerStateWaiting{
Reason: "PodInitializing",
Message: "PodInitializing",
},
},
}, {
Name: "sidecar-waiting",
ImageID: "image-id",
State: corev1.ContainerState{
Waiting: &corev1.ContainerStateWaiting{
Reason: "PodInitializing",
Message: "PodInitializing",
},
},
Ready: true,
}},
},
want: v1alpha1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{conditionRunning},
},
TaskRunStatusFields: v1alpha1.TaskRunStatusFields{
Steps: []v1alpha1.StepState{{
ContainerState: corev1.ContainerState{
Waiting: &corev1.ContainerStateWaiting{
Reason: "PodInitializing",
Message: "PodInitializing",
},
},
Name: "waiting-step",
ContainerName: "step-waiting-step",
}},
Sidecars: []v1alpha1.SidecarState{{
ContainerState: corev1.ContainerState{
Waiting: &corev1.ContainerStateWaiting{
Reason: "PodInitializing",
Message: "PodInitializing",
},
},
Name: "waiting",
ImageID: "image-id",
ContainerName: "sidecar-waiting",
}},
},
},
}, {
desc: "with-sidecar-terminated",
podStatus: corev1.PodStatus{
Phase: corev1.PodRunning,
ContainerStatuses: []corev1.ContainerStatus{{
Name: "step-running-step",
State: corev1.ContainerState{
Running: &corev1.ContainerStateRunning{},
},
}, {
Name: "sidecar-error",
ImageID: "image-id",
State: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 1,
Reason: "Error",
Message: "Error",
},
},
Ready: true,
}},
},
want: v1alpha1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{conditionRunning},
},
TaskRunStatusFields: v1alpha1.TaskRunStatusFields{
Steps: []v1alpha1.StepState{{
ContainerState: corev1.ContainerState{
Running: &corev1.ContainerStateRunning{},
},
Name: "running-step",
ContainerName: "step-running-step",
}},
Sidecars: []v1alpha1.SidecarState{{
ContainerState: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 1,
Reason: "Error",
Message: "Error",
},
},
Name: "error",
ImageID: "image-id",
ContainerName: "sidecar-error",
}},
},
},
Expand Down
136 changes: 130 additions & 6 deletions pkg/reconciler/taskrun/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ var (
),
))
clustertask = tb.ClusterTask("test-cluster-task", tb.ClusterTaskSpec(simpleStep))
taskSidecar = tb.Task("test-task-sidecar", "foo", tb.TaskSpec(
tb.Sidecar("sidecar", "image-id"),
))
taskMultipleSidecars = tb.Task("test-task-sidecar", "foo", tb.TaskSpec(
tb.Sidecar("sidecar", "image-id"),
tb.Sidecar("sidecar2", "image-id"),
))

outputTask = tb.Task("test-output-task", "foo", tb.TaskSpec(
simpleStep, tb.TaskInputs(
Expand Down Expand Up @@ -1145,7 +1152,7 @@ func TestReconcilePodFetchError(t *testing.T) {
return true, nil, errors.New("induce failure fetching pods")
})

if err := c.Reconciler.Reconcile(context.Background(), fmt.Sprintf("%s/%s", taskRun.Namespace, taskRun.Name)); err == nil {
if err := c.Reconciler.Reconcile(context.Background(), getRunName(taskRun)); err == nil {
t.Fatal("expected error when reconciling a Task for which we couldn't get the corresponding Build Pod but got nil")
}
}
Expand Down Expand Up @@ -1196,7 +1203,7 @@ func TestReconcilePodUpdateStatus(t *testing.T) {
c := testAssets.Controller
clients := testAssets.Clients

if err := c.Reconciler.Reconcile(context.Background(), fmt.Sprintf("%s/%s", taskRun.Namespace, taskRun.Name)); err != nil {
if err := c.Reconciler.Reconcile(context.Background(), getRunName(taskRun)); err != nil {
t.Fatalf("Unexpected error when Reconcile() : %v", err)
}
newTr, err := clients.Pipeline.TektonV1alpha1().TaskRuns(taskRun.Namespace).Get(taskRun.Name, metav1.GetOptions{})
Expand All @@ -1219,7 +1226,7 @@ func TestReconcilePodUpdateStatus(t *testing.T) {
if _, err := clients.Kube.CoreV1().Pods(taskRun.Namespace).UpdateStatus(pod); err != nil {
t.Errorf("Unexpected error while updating build: %v", err)
}
if err := c.Reconciler.Reconcile(context.Background(), fmt.Sprintf("%s/%s", taskRun.Namespace, taskRun.Name)); err != nil {
if err := c.Reconciler.Reconcile(context.Background(), getRunName(taskRun)); err != nil {
t.Fatalf("Unexpected error when Reconcile(): %v", err)
}

Expand Down Expand Up @@ -1259,7 +1266,7 @@ func TestReconcileOnCompletedTaskRun(t *testing.T) {
c := testAssets.Controller
clients := testAssets.Clients

if err := c.Reconciler.Reconcile(context.Background(), fmt.Sprintf("%s/%s", taskRun.Namespace, taskRun.Name)); err != nil {
if err := c.Reconciler.Reconcile(context.Background(), getRunName(taskRun)); err != nil {
t.Fatalf("Unexpected error when reconciling completed TaskRun : %v", err)
}
newTr, err := clients.Pipeline.TektonV1alpha1().TaskRuns(taskRun.Namespace).Get(taskRun.Name, metav1.GetOptions{})
Expand Down Expand Up @@ -1289,7 +1296,7 @@ func TestReconcileOnCancelledTaskRun(t *testing.T) {
c := testAssets.Controller
clients := testAssets.Clients

if err := c.Reconciler.Reconcile(context.Background(), fmt.Sprintf("%s/%s", taskRun.Namespace, taskRun.Name)); err != nil {
if err := c.Reconciler.Reconcile(context.Background(), getRunName(taskRun)); err != nil {
t.Fatalf("Unexpected error when reconciling completed TaskRun : %v", err)
}
newTr, err := clients.Pipeline.TektonV1alpha1().TaskRuns(taskRun.Namespace).Get(taskRun.Name, metav1.GetOptions{})
Expand Down Expand Up @@ -1377,7 +1384,7 @@ func TestReconcileTimeouts(t *testing.T) {
c := testAssets.Controller
clients := testAssets.Clients

if err := c.Reconciler.Reconcile(context.Background(), fmt.Sprintf("%s/%s", tc.taskRun.Namespace, tc.taskRun.Name)); err != nil {
if err := c.Reconciler.Reconcile(context.Background(), getRunName(tc.taskRun)); err != nil {
t.Fatalf("Unexpected error when reconciling completed TaskRun : %v", err)
}
newTr, err := clients.Pipeline.TektonV1alpha1().TaskRuns(tc.taskRun.Namespace).Get(tc.taskRun.Name, metav1.GetOptions{})
Expand Down Expand Up @@ -1881,3 +1888,120 @@ func TestUpdateTaskRunResourceResult_Errors(t *testing.T) {
})
}
}

func TestReconcile_Single_SidecarState(t *testing.T) {
runningState := corev1.ContainerStateRunning{StartedAt: metav1.Time{Time: time.Now()}}
taskRun := tb.TaskRun("test-taskrun-sidecars", "foo",
tb.TaskRunSpec(
tb.TaskRunTaskRef(taskSidecar.Name),
),
tb.TaskRunStatus(
tb.SidecarState(
tb.SidecarStateName("sidecar"),
tb.SidecarStateImageID("image-id"),
tb.SidecarStateContainerName("sidecar-sidecar"),
tb.SetSidecarStateRunning(runningState),
),
),
)

d := test.Data{
TaskRuns: []*v1alpha1.TaskRun{taskRun},
Tasks: []*v1alpha1.Task{taskSidecar},
}

testAssets, cancel := getTaskRunController(t, d)
defer cancel()
clients := testAssets.Clients

if err := testAssets.Controller.Reconciler.Reconcile(context.Background(), getRunName(taskRun)); err != nil {
t.Errorf("expected no error reconciling valid TaskRun but got %v", err)
}

getTaskRun, err := clients.Pipeline.TektonV1alpha1().TaskRuns(taskRun.Namespace).Get(taskRun.Name, metav1.GetOptions{})
if err != nil {
t.Fatalf("Expected completed TaskRun %s to exist but instead got error when getting it: %v", taskRun.Name, err)
}

expected := v1alpha1.SidecarState{
Name: "sidecar",
ImageID: "image-id",
ContainerName: "sidecar-sidecar",
ContainerState: corev1.ContainerState{
Running: &runningState,
},
}

if c := cmp.Diff(expected, getTaskRun.Status.Sidecars[0]); c != "" {
t.Errorf("TestReconcile_Single_SidecarState (-want, +got): %s", c)
}
}

func TestReconcile_Multiple_SidecarStates(t *testing.T) {
runningState := corev1.ContainerStateRunning{StartedAt: metav1.Time{Time: time.Now()}}
waitingState := corev1.ContainerStateWaiting{Reason: "PodInitializing"}
taskRun := tb.TaskRun("test-taskrun-sidecars", "foo",
tb.TaskRunSpec(
tb.TaskRunTaskRef(taskMultipleSidecars.Name),
),
tb.TaskRunStatus(
tb.SidecarState(
tb.SidecarStateName("sidecar1"),
tb.SidecarStateImageID("image-id"),
tb.SidecarStateContainerName("sidecar-sidecar1"),
tb.SetSidecarStateRunning(runningState),
),
),
tb.TaskRunStatus(
tb.SidecarState(
tb.SidecarStateName("sidecar2"),
tb.SidecarStateImageID("image-id"),
tb.SidecarStateContainerName("sidecar-sidecar2"),
tb.SetSidecarStateWaiting(waitingState),
),
),
)

d := test.Data{
TaskRuns: []*v1alpha1.TaskRun{taskRun},
Tasks: []*v1alpha1.Task{taskMultipleSidecars},
}

testAssets, cancel := getTaskRunController(t, d)
defer cancel()
clients := testAssets.Clients

if err := testAssets.Controller.Reconciler.Reconcile(context.Background(), getRunName(taskRun)); err != nil {
t.Errorf("expected no error reconciling valid TaskRun but got %v", err)
}

getTaskRun, err := clients.Pipeline.TektonV1alpha1().TaskRuns(taskRun.Namespace).Get(taskRun.Name, metav1.GetOptions{})
if err != nil {
t.Fatalf("Expected completed TaskRun %s to exist but instead got error when getting it: %v", taskRun.Name, err)
}

expected := []v1alpha1.SidecarState{
{
Name: "sidecar1",
ImageID: "image-id",
ContainerName: "sidecar-sidecar1",
ContainerState: corev1.ContainerState{
Running: &runningState,
},
},
{
Name: "sidecar2",
ImageID: "image-id",
ContainerName: "sidecar-sidecar2",
ContainerState: corev1.ContainerState{
Waiting: &waitingState,
},
},
}

for i, sc := range getTaskRun.Status.Sidecars {
if c := cmp.Diff(expected[i], sc); c != "" {
t.Errorf("TestReconcile_Multiple_SidecarStates sidecar%d (-want, +got): %s", i+1, c)
}
}
}
Loading

0 comments on commit 787b30b

Please sign in to comment.