Skip to content

Commit

Permalink
Add resolver support in tkn pr log commands
Browse files Browse the repository at this point in the history
  • Loading branch information
pratap0007 committed Jul 21, 2023
1 parent 941edb3 commit f5bde8b
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 6 deletions.
168 changes: 168 additions & 0 deletions pkg/cmd/pipelinerun/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3768,6 +3768,174 @@ func TestPipelinerunLog_finally(t *testing.T) {
test.AssertOutput(t, expected, output)
}

func TestLogs_Cluster_Resolver(t *testing.T) {
var (
pipelineName = "pipeline"
prName = "pipeline-run"
ns = "namespace"
taskName = "task"
trName = "taskrun"
taskPodName = "taskPod"
tr1StartTime = test.FakeClock().Now().Add(20 * time.Second)
tr1Step1Name = "step-1"
)

nsList := []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: ns,
},
},
}

prs := []*v1.PipelineRun{
{
ObjectMeta: metav1.ObjectMeta{
Name: prName,
Namespace: ns,
Labels: map[string]string{"tekton.dev/pipeline": prName},
},
Spec: v1.PipelineRunSpec{
PipelineRef: &v1.PipelineRef{
ResolverRef: v1.ResolverRef{
Resolver: "cluster",
Params: v1.Params{
{
Name: "kind",
Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: "pipeline"},
},
{
Name: "name",
Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: pipelineName},
},
{
Name: "namespace",
Value: v1.ParamValue{Type: v1.ParamTypeString, StringVal: ns},
},
},
},
},
},
Status: v1.PipelineRunStatus{
Status: duckv1.Status{
Conditions: duckv1.Conditions{
{
Status: corev1.ConditionTrue,
Message: "Success",
},
},
},
PipelineRunStatusFields: v1.PipelineRunStatusFields{
ChildReferences: []v1.ChildStatusReference{
{
Name: trName,
PipelineTaskName: taskName,
TypeMeta: runtime.TypeMeta{
APIVersion: "tekton.dev/v1beta1",
Kind: "TaskRun",
},
},
},
PipelineSpec: &v1.PipelineSpec{
Tasks: []v1.PipelineTask{
{
Name: taskName,
TaskRef: &v1.TaskRef{
Kind: "task",
Name: taskName,
},
},
},
},
},
},
},
}

ps := []*v1.Pipeline{
{
ObjectMeta: metav1.ObjectMeta{
Name: pipelineName,
Namespace: ns,
},
Spec: v1.PipelineSpec{
Tasks: []v1.PipelineTask{
{
Name: taskName,
TaskRef: &v1.TaskRef{
Name: taskName,
},
},
},
},
},
}

trs := []*v1.TaskRun{
{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: trName,
},
Spec: v1.TaskRunSpec{
TaskRef: &v1.TaskRef{
Name: taskName,
},
},
Status: v1.TaskRunStatus{
Status: duckv1.Status{
Conditions: duckv1.Conditions{
{
Status: corev1.ConditionTrue,
Type: apis.ConditionSucceeded,
},
},
},
TaskRunStatusFields: v1.TaskRunStatusFields{
StartTime: &metav1.Time{Time: tr1StartTime},
PodName: taskPodName,
Steps: []v1.StepState{
{
Name: tr1Step1Name,
ContainerState: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 0,
},
},
},
},
},
},
},
}

cs, _ := test.SeedTestData(t, test.Data{PipelineRuns: prs, Pipelines: ps, Namespaces: nsList})
cs.Pipeline.Resources = cb.APIResourceList(version, []string{"task", "taskrun", "pipelinerun"})
tdc := testDynamic.Options{}
dc, err := tdc.Client(
cb.UnstructuredP(ps[0], version),
cb.UnstructuredPR(prs[0], version),
cb.UnstructuredTR(trs[0], version),
)
if err != nil {
t.Errorf("unable to create dynamic client: %v", err)
}

fakeLogStream := fake.Logs(
fake.Task(taskPodName,
fake.Step(tr1Step1Name, "task-1 completed"),
),
)
prlo := logOpts(prName, ns, cs, dc, fake.Streamer(fakeLogStream), false, false, false, taskName)

output, err := fetchLogs(prlo)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

test.AssertOutput(t, "task-1 completed", output)
}

func logOptsv1beta1(name string, ns string, cs pipelinev1beta1test.Clients, dc dynamic.Interface, streamer stream.NewStreamerFunc, allSteps bool, follow bool, prefixing bool, tasks ...string) *options.LogOptions {
p := test.Params{
Kube: cs.Kube,
Expand Down
19 changes: 13 additions & 6 deletions pkg/log/pipeline_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,22 @@ func (r *Reader) setUpTask(taskNumber int, tr taskrunpkg.Run) {
// and return trh.Run after converted taskruns into trh.Run.
func (r *Reader) getOrderedTasks(pr *v1.PipelineRun) ([]taskrunpkg.Run, error) {
var tasks []v1.PipelineTask

switch {
case pr.Spec.PipelineRef != nil:
pl, err := pipelinepkg.GetPipeline(pipelineGroupResource, r.clients, pr.Spec.PipelineRef.Name, r.ns)
if err != nil {
return nil, err
if pr.Spec.PipelineRef.Resolver != "" {
if pr.Status.PipelineSpec != nil {
tasks = append(tasks, pr.Status.PipelineSpec.Tasks...)
} else {
return nil, fmt.Errorf("pipelinerun %s does not have the PipelineRunSpec", pr.Name)
}
} else {
pl, err := pipelinepkg.GetPipeline(pipelineGroupResource, r.clients, pr.Spec.PipelineRef.Name, r.ns)
if err != nil {
return nil, err
}
tasks = pl.Spec.Tasks
tasks = append(tasks, pl.Spec.Finally...)
}
tasks = pl.Spec.Tasks
tasks = append(tasks, pl.Spec.Finally...)
case pr.Spec.PipelineSpec != nil:
tasks = pr.Spec.PipelineSpec.Tasks
tasks = append(tasks, pr.Spec.PipelineSpec.Finally...)
Expand Down

0 comments on commit f5bde8b

Please sign in to comment.