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

Don't require pointer to binding #2834

Merged
merged 3 commits into from
Jun 18, 2020
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
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ run:
issues-exit-code: 1
build-tags:
- e2e
skip-files:
- .*/zz_generated.deepcopy.go
skip-dirs:
- vendor
- pkg/client
Expand Down
32 changes: 19 additions & 13 deletions pkg/pod/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,14 @@ func MakeTaskRunStatus(logger *zap.SugaredLogger, tr v1beta1.TaskRun, pod *corev
for _, s := range pod.Status.ContainerStatuses {
if IsContainerStep(s.Name) {
if s.State.Terminated != nil && len(s.State.Terminated.Message) != 0 {
if err := updateStatusStartTime(&s); err != nil {
message, time, err := removeStartInfoFromTerminationMessage(s)
if err != nil {
logger.Errorf("error setting the start time of step %q in taskrun %q: %w", s.Name, tr.Name, err)
}
if time != nil {
s.State.Terminated.StartedAt = *time
s.State.Terminated.Message = message
}
}
trs.Steps = append(trs.Steps, v1beta1.StepState{
ContainerState: *s.State.DeepCopy(),
Expand Down Expand Up @@ -151,34 +156,35 @@ func MakeTaskRunStatus(logger *zap.SugaredLogger, tr v1beta1.TaskRun, pod *corev
return *trs
}

// updateStatusStartTime searches for a result called "StartedAt" in the JSON-formatted termination message
// of a step and sets the State.Terminated.StartedAt field to this time if it's found. The "StartedAt" result
// is also removed from the list of results in the container status.
func updateStatusStartTime(s *corev1.ContainerStatus) error {
// removeStartInfoFromTerminationMessage searches for a result called "StartedAt" in the JSON-formatted
// termination message of a step and returns the values to use for sets State.Terminated if it's
// found. The "StartedAt" result is also removed from the list of results in the container status.
func removeStartInfoFromTerminationMessage(s corev1.ContainerStatus) (string, *metav1.Time, error) {
r, err := termination.ParseMessage(s.State.Terminated.Message)
if err != nil {
return fmt.Errorf("termination message could not be parsed as JSON: %w", err)
return "", nil, fmt.Errorf("termination message could not be parsed as JSON: %w", err)
}
for index, result := range r {
if result.Key == "StartedAt" {
t, err := time.Parse(timeFormat, result.Value)
if err != nil {
return fmt.Errorf("could not parse time value %q in StartedAt field: %w", result.Value, err)
return "", nil, fmt.Errorf("could not parse time value %q in StartedAt field: %w", result.Value, err)
}
s.State.Terminated.StartedAt = metav1.NewTime(t)
message := ""
startedAt := metav1.NewTime(t)
// remove the entry for the starting time
r = append(r[:index], r[index+1:]...)
if len(r) == 0 {
s.State.Terminated.Message = ""
message = ""
} else if bytes, err := json.Marshal(r); err != nil {
return fmt.Errorf("error marshalling remaining results back into termination message: %w", err)
return "", nil, fmt.Errorf("error marshalling remaining results back into termination message: %w", err)
} else {
s.State.Terminated.Message = string(bytes)
message = string(bytes)
}
break
return message, &startedAt, nil
}
}
return nil
return "", nil, nil
}

func updateCompletedTaskRun(trs *v1beta1.TaskRunStatus, pod *corev1.Pod) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ type GetTaskRun func(name string) (*v1beta1.TaskRun, error)
func GetResourcesFromBindings(pr *v1beta1.PipelineRun, getResource resources.GetResource) (map[string]*resourcev1alpha1.PipelineResource, error) {
rs := map[string]*resourcev1alpha1.PipelineResource{}
for _, resource := range pr.Spec.Resources {
r, err := resources.GetResourceFromBinding(&resource, getResource)
r, err := resources.GetResourceFromBinding(resource, getResource)
if err != nil {
return rs, fmt.Errorf("error following resource reference for %s: %w", resource.Name, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/taskrun/resources/image_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func AddOutputImageDigestExporter(
return fmt.Errorf("failed to get bound resource: %w while adding output image digest exporter", err)
}

resource, err := GetResourceFromBinding(&boundResource.PipelineResourceBinding, gr)
resource, err := GetResourceFromBinding(boundResource.PipelineResourceBinding, gr)
if err != nil {
return fmt.Errorf("failed to get output pipeline Resource for taskRun %q resource %v; error: %w while adding output image digest exporter", tr.Name, boundResource, err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/reconciler/taskrun/resources/taskresourceresolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ResolveTaskResources(ts *v1beta1.TaskSpec, taskName string, kind v1beta1.Ta
}

for _, r := range inputs {
rr, err := GetResourceFromBinding(&r.PipelineResourceBinding, gr)
rr, err := GetResourceFromBinding(r.PipelineResourceBinding, gr)
if err != nil {
return nil, fmt.Errorf("couldn't retrieve referenced input PipelineResource: %w", err)
}
Expand All @@ -64,7 +64,7 @@ func ResolveTaskResources(ts *v1beta1.TaskSpec, taskName string, kind v1beta1.Ta
}

for _, r := range outputs {
rr, err := GetResourceFromBinding(&r.PipelineResourceBinding, gr)
rr, err := GetResourceFromBinding(r.PipelineResourceBinding, gr)

if err != nil {
return nil, fmt.Errorf("couldn't retrieve referenced output PipelineResource: %w", err)
Expand All @@ -77,7 +77,7 @@ func ResolveTaskResources(ts *v1beta1.TaskSpec, taskName string, kind v1beta1.Ta

// GetResourceFromBinding will return an instance of a PipelineResource to use for r, either by getting it with getter or by
// instantiating it from the embedded spec.
func GetResourceFromBinding(r *v1beta1.PipelineResourceBinding, getter GetResource) (*resourcev1alpha1.PipelineResource, error) {
func GetResourceFromBinding(r v1beta1.PipelineResourceBinding, getter GetResource) (*resourcev1alpha1.PipelineResource, error) {
if (r.ResourceRef != nil && r.ResourceRef.Name != "") && r.ResourceSpec != nil {
return nil, errors.New("Both ResourseRef and ResourceSpec are defined. Expected only one")
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/reconciler/taskrun/resources/taskresourceresolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func TestGetResourceFromBinding_Ref(t *testing.T) {
Name: "git-repo",
},
}
binding := &v1beta1.PipelineResourceBinding{
binding := v1beta1.PipelineResourceBinding{
ResourceRef: &v1beta1.PipelineResourceRef{
Name: "foo-resource",
},
Expand All @@ -292,7 +292,7 @@ func TestGetResourceFromBinding_Ref(t *testing.T) {
}

func TestGetResourceFromBinding_Spec(t *testing.T) {
binding := &v1beta1.PipelineResourceBinding{
binding := v1beta1.PipelineResourceBinding{
ResourceSpec: &resourcev1alpha1.PipelineResourceSpec{
Type: resourcev1alpha1.PipelineResourceTypeGit,
Params: []resourcev1alpha1.ResourceParam{{
Expand All @@ -318,7 +318,7 @@ func TestGetResourceFromBinding_Spec(t *testing.T) {
}

func TestGetResourceFromBinding_NoNameOrSpec(t *testing.T) {
binding := &v1beta1.PipelineResourceBinding{}
binding := v1beta1.PipelineResourceBinding{}
gr := func(n string) (*resourcev1alpha1.PipelineResource, error) {
return nil, nil
}
Expand All @@ -330,7 +330,7 @@ func TestGetResourceFromBinding_NoNameOrSpec(t *testing.T) {
}

func TestGetResourceFromBinding_NameAndSpec(t *testing.T) {
binding := &v1beta1.PipelineResourceBinding{
binding := v1beta1.PipelineResourceBinding{
ResourceSpec: &resourcev1alpha1.PipelineResourceSpec{
Type: resourcev1alpha1.PipelineResourceTypeGit,
Params: []resourcev1alpha1.ResourceParam{{
Expand All @@ -353,7 +353,7 @@ func TestGetResourceFromBinding_NameAndSpec(t *testing.T) {
}

func TestGetResourceFromBinding_ErrorGettingResource(t *testing.T) {
binding := &v1beta1.PipelineResourceBinding{
binding := v1beta1.PipelineResourceBinding{
ResourceRef: &v1beta1.PipelineResourceRef{
Name: "foo-resource",
},
Expand Down