Skip to content

Commit

Permalink
Add the command and arguments run in each step to the TaskRun.Status
Browse files Browse the repository at this point in the history
The TaskRun.Status currently serves as the best log of what happened in a TaskRun.
It contains the container digests for each step, as well as their exit code and
termination message. However, it is currently missing the Command and Arguments,
making it impossible to verify what was run inside of the container after the
pod completes.

This commit adds those fields to the API, and copies them over from the Pod
when the rest of the Status is populated.
  • Loading branch information
dlorenc committed Apr 15, 2020
1 parent dc0a8b9 commit 0211e08
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
8 changes: 7 additions & 1 deletion docs/taskruns.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ steps:
- container: step-hello
imageID: docker-pullable://busybox@sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649
name: hello
command:
- /mycommand
args:
- arg1
- arg2
terminated:
containerID: docker://d5a54f5bbb8e7a6fd3bc7761b78410403244cf4c9c5822087fb0209bf59e3621
exitCode: 0
Expand All @@ -296,7 +301,8 @@ steps:
```

Fields include start and stop times for the `TaskRun` and each `Step` and exit codes.
For each step we also include the fully-qualified image used, with the digest.
For each step we also include the fully-qualified image used, with the digest, as well
as the command and arguments run inside the container.

If any pods have been [`OOMKilled`](https://kubernetes.io/docs/tasks/administer-cluster/out-of-resource/)
by Kubernetes, the `Taskrun` will be marked as failed even if the exit code is 0.
Expand Down
16 changes: 10 additions & 6 deletions pkg/apis/pipeline/v1beta1/taskrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,21 @@ func (trs *TaskRunStatus) SetCondition(newCond *apis.Condition) {
// 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"`
Name string `json:"name,omitempty"`
ContainerName string `json:"container,omitempty"`
ImageID string `json:"imageID,omitempty"`
Command []string `json:"command,omitempty"`
Args []string `json:"args,omitempty"`
}

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

// CloudEventDelivery is the target of a cloud event along with the state of
Expand Down
9 changes: 9 additions & 0 deletions pkg/pod/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ func MakeTaskRunStatus(logger *zap.SugaredLogger, tr v1alpha1.TaskRun, pod *core
})
}

containerMap := map[string]corev1.Container{}
for _, c := range pod.Spec.Containers {
containerMap[c.Name] = c
}

trs.PodName = pod.Name
trs.Steps = []v1alpha1.StepState{}
trs.Sidecars = []v1alpha1.SidecarState{}
Expand All @@ -127,13 +132,17 @@ func MakeTaskRunStatus(logger *zap.SugaredLogger, tr v1alpha1.TaskRun, pod *core
Name: trimStepPrefix(s.Name),
ContainerName: s.Name,
ImageID: s.ImageID,
Command: containerMap[s.Name].Command,
Args: containerMap[s.Name].Args,
})
} else if isContainerSidecar(s.Name) {
trs.Sidecars = append(trs.Sidecars, v1alpha1.SidecarState{
ContainerState: *s.State.DeepCopy(),
Name: TrimSidecarPrefix(s.Name),
ContainerName: s.Name,
ImageID: s.ImageID,
Command: containerMap[s.Name].Command,
Args: containerMap[s.Name].Args,
})
}
}
Expand Down
53 changes: 53 additions & 0 deletions pkg/pod/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/logging"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1"
Expand All @@ -42,6 +43,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
for _, c := range []struct {
desc string
podStatus corev1.PodStatus
podSpec corev1.PodSpec
taskSpec v1alpha1.TaskSpec
want v1alpha1.TaskRunStatus
}{{
Expand Down Expand Up @@ -90,6 +92,56 @@ func TestMakeTaskRunStatus(t *testing.T) {
Sidecars: []v1alpha1.SidecarState{},
},
},
}, {
desc: "cmd-and-args",
podStatus: corev1.PodStatus{
Phase: corev1.PodSucceeded,
ContainerStatuses: []corev1.ContainerStatus{{
Name: "step-my-container",
State: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 0,
},
},
}},
},
podSpec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "step-my-container",
Command: []string{"/mycmd"},
Args: []string{"arg1", "arg2"},
},
},
},
want: v1alpha1.TaskRunStatus{
Status: duckv1beta1.Status{
Conditions: []apis.Condition{{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionTrue,
Reason: "Succeeded",
Message: "All Steps have completed executing",
}},
},
TaskRunStatusFields: v1alpha1.TaskRunStatusFields{
Steps: []v1alpha1.StepState{
{
ContainerState: v1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 0,
},
},
Name: "my-container",
ContainerName: "step-my-container",
Command: []string{"/mycmd"},
Args: []string{"arg1", "arg2"},
},
},
Sidecars: []v1alpha1.SidecarState{},
// We don't actually care about the time, just that it's not nil
CompletionTime: &metav1.Time{Time: time.Now()},
},
},
}, {
desc: "ignore-init-containers",
podStatus: corev1.PodStatus{
Expand Down Expand Up @@ -701,6 +753,7 @@ func TestMakeTaskRunStatus(t *testing.T) {
CreationTimestamp: now,
},
Status: c.podStatus,
Spec: c.podSpec,
}
startTime := time.Date(2010, 1, 1, 1, 1, 1, 1, time.UTC)
tr := v1alpha1.TaskRun{
Expand Down

0 comments on commit 0211e08

Please sign in to comment.