From fb698bd17d13ae9c6e9cd15920d01cd4295061c6 Mon Sep 17 00:00:00 2001 From: Sergiy Kulanov Date: Tue, 10 Oct 2023 13:33:38 +0300 Subject: [PATCH] feat: Allow to get graph for specific Pipeline/PipelineRun provided by name Signed-off-by: Sergiy Kulanov --- .vscode/launch.json | 2 +- pkg/cmd/pipeline/graph.go | 21 +++++++++--- pkg/cmd/pipelinerun/graph.go | 21 +++++++++--- pkg/pipeline/pipeline.go | 9 ++++++ pkg/pipeline/pipeline_test.go | 50 +++++++++++++++++++++++++++++ pkg/pipelinerun/pipelinerun.go | 9 ++++++ pkg/pipelinerun/pipelinerun_test.go | 50 +++++++++++++++++++++++++++++ 7 files changed, 153 insertions(+), 9 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 9bd31f9..68d7276 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "mode": "auto", "program": "${fileDirname}", - "args": ["pipelinerun", "graph", "--namespace", "edp-delivery-tekton-dev", "--output-format", "mmd"], + "args": ["pipeline", "graph", "demo", "--namespace", "demo", "--output-format", "mmd"], "cwd": "${workspaceFolder}" } ] diff --git a/pkg/cmd/pipeline/graph.go b/pkg/cmd/pipeline/graph.go index 4890a48..37afdc1 100644 --- a/pkg/cmd/pipeline/graph.go +++ b/pkg/cmd/pipeline/graph.go @@ -9,6 +9,7 @@ import ( "github.com/spf13/cobra" "github.com/tektoncd/cli/pkg/cli" "github.com/tektoncd/cli/pkg/flags" + v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" ) type graphOptions struct { @@ -45,12 +46,24 @@ func graphCommand(p cli.Params) *cobra.Command { return err } - // Build the list of task graphs var graphs []*taskgraph.TaskGraph + var pipelines []v1.Pipeline - pipelines, err := pipelinepkg.GetAllPipelines(cs, p.Namespace()) - if err != nil { - return fmt.Errorf("failed to get Pipelines: %w", err) + switch len(args) { + case 1: + var pipeline *v1.Pipeline + pipeline, err = pipelinepkg.GetPipelineByName(cs, args[0], p.Namespace()) + if err != nil { + return fmt.Errorf("failed to run GetPipelineRunByName: %w", err) + } + pipelines = append(pipelines, *pipeline) + case 0: + pipelines, err = pipelinepkg.GetAllPipelines(cs, p.Namespace()) + if err != nil { + return fmt.Errorf("failed to run GetAllPipelineRuns: %w", err) + } + default: + return fmt.Errorf("too many arguments. Provide either no arguments to get all Pipelines or a single Pipeline name") } for i := range pipelines { diff --git a/pkg/cmd/pipelinerun/graph.go b/pkg/cmd/pipelinerun/graph.go index 798224d..f3f0e21 100644 --- a/pkg/cmd/pipelinerun/graph.go +++ b/pkg/cmd/pipelinerun/graph.go @@ -9,6 +9,7 @@ import ( "github.com/spf13/cobra" "github.com/tektoncd/cli/pkg/cli" "github.com/tektoncd/cli/pkg/flags" + v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" ) type graphOptions struct { @@ -45,12 +46,24 @@ func graphCommand(p cli.Params) *cobra.Command { return err } - // Build the list of task graphs var graphs []*taskgraph.TaskGraph + var pipelineruns []v1.PipelineRun - pipelineruns, err := pipelinerunpkg.GetAllPipelineRuns(cs, p.Namespace()) - if err != nil { - return fmt.Errorf("failed to get Pipelines: %w", err) + switch len(args) { + case 1: + var pipelinerun *v1.PipelineRun + pipelinerun, err = pipelinerunpkg.GetPipelineRunsByName(cs, args[0], p.Namespace()) + if err != nil { + return fmt.Errorf("failed to run GetPipelineRunByName: %w", err) + } + pipelineruns = append(pipelineruns, *pipelinerun) + case 0: + pipelineruns, err = pipelinerunpkg.GetAllPipelineRuns(cs, p.Namespace()) + if err != nil { + return fmt.Errorf("failed to run GetAllPipelineRuns: %w", err) + } + default: + return fmt.Errorf("too many arguments. Provide either no arguments to get all PipelineRuns or a single PipelineRuns name") } for i := range pipelineruns { diff --git a/pkg/pipeline/pipeline.go b/pkg/pipeline/pipeline.go index b15469e..961b002 100644 --- a/pkg/pipeline/pipeline.go +++ b/pkg/pipeline/pipeline.go @@ -19,3 +19,12 @@ func GetAllPipelines(c *cli.Clients, ns string) ([]v1.Pipeline, error) { } return pipelines.Items, nil } + +// Get Pipeline by name +func GetPipelineByName(c *cli.Clients, name string, ns string) (*v1.Pipeline, error) { + pipeline, err := c.Tekton.TektonV1().Pipelines(ns).Get(context.TODO(), name, metav1.GetOptions{}) + if err != nil { + return nil, fmt.Errorf("failed to get Pipeline with name %s: %w", name, err) + } + return pipeline, nil +} diff --git a/pkg/pipeline/pipeline_test.go b/pkg/pipeline/pipeline_test.go index 9e3d981..08aaed7 100644 --- a/pkg/pipeline/pipeline_test.go +++ b/pkg/pipeline/pipeline_test.go @@ -79,3 +79,53 @@ func TestGetAllPipelineWithError(t *testing.T) { t.Fatalf("Expected error message to be 'no Pipelines found in namespace my-namespace', got %s", err.Error()) } } + +func TestGetPipelineByName(t *testing.T) { + fakeClient := fakeclient.NewSimpleClientset() + + // Define the expected pipeline run + expectedPipeline := &v1.Pipeline{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pipeline-1", + Namespace: namespace, + }, + } + + // Create the fake pipeline run + _, err := fakeClient.TektonV1().Pipelines(namespace).Create(context.TODO(), expectedPipeline, metav1.CreateOptions{}) + if err != nil { + t.Fatalf("Error creating fake pipelineRun run: %v", err) + } + + c := &cli.Clients{ + Tekton: fakeClient, + } + + // Get the pipeline run + pipelineRun, err := GetPipelineByName(c, expectedPipeline.Name, namespace) + if err != nil { + t.Fatalf("Error getting pipeline run: %v", err) + } + + // Check that the pipeline run is as expected + if pipelineRun.Name != expectedPipeline.Name { + t.Fatalf("Expected pipeline run to have name %s, got %s", expectedPipeline.Name, pipelineRun.Name) + } +} + +func TestPipelineByNameWithError(t *testing.T) { + fakeClient := fakeclient.NewSimpleClientset() + + c := &cli.Clients{ + Tekton: fakeClient, + } + + // Get the pipeline runs + _, err := GetPipelineByName(c, "fake-pipeline", namespace) + if err == nil { + t.Fatal("GetPipelineByName did not return an error, expected an error") + } + if err.Error() != "failed to get Pipeline with name fake-pipeline: pipelines.tekton.dev \"fake-pipeline\" not found" { + t.Fatalf("Expected error message to be 'failed to get Pipeline with name fake-pipeline: pipelines.tekton.dev \"fake-pipeline\" not found', got %s", err.Error()) + } +} diff --git a/pkg/pipelinerun/pipelinerun.go b/pkg/pipelinerun/pipelinerun.go index 17ef228..562e999 100644 --- a/pkg/pipelinerun/pipelinerun.go +++ b/pkg/pipelinerun/pipelinerun.go @@ -19,3 +19,12 @@ func GetAllPipelineRuns(c *cli.Clients, ns string) ([]v1.PipelineRun, error) { } return pipelineruns.Items, nil } + +// Get PipelineRun by name +func GetPipelineRunsByName(c *cli.Clients, name string, ns string) (*v1.PipelineRun, error) { + pipelinerun, err := c.Tekton.TektonV1().PipelineRuns(ns).Get(context.TODO(), name, metav1.GetOptions{}) + if err != nil { + return nil, fmt.Errorf("failed to get PipelineRun with name %s: %w", name, err) + } + return pipelinerun, nil +} diff --git a/pkg/pipelinerun/pipelinerun_test.go b/pkg/pipelinerun/pipelinerun_test.go index b5ba75c..a3c3677 100644 --- a/pkg/pipelinerun/pipelinerun_test.go +++ b/pkg/pipelinerun/pipelinerun_test.go @@ -79,3 +79,53 @@ func TestGetAllPipelineRunsWithError(t *testing.T) { t.Fatalf("Expected error message to be 'no PipelineRuns found in namespace my-namespace', got %s", err.Error()) } } + +func TestGetPipelineRunsByName(t *testing.T) { + fakeClient := fakeclient.NewSimpleClientset() + + // Define the expected pipeline run + expectedPipelineRun := &v1.PipelineRun{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pipeline-1", + Namespace: namespace, + }, + } + + // Create the fake pipeline run + _, err := fakeClient.TektonV1().PipelineRuns(namespace).Create(context.TODO(), expectedPipelineRun, metav1.CreateOptions{}) + if err != nil { + t.Fatalf("Error creating fake pipelineRun run: %v", err) + } + + c := &cli.Clients{ + Tekton: fakeClient, + } + + // Get the pipeline run + pipelineRun, err := GetPipelineRunsByName(c, expectedPipelineRun.Name, namespace) + if err != nil { + t.Fatalf("Error getting pipeline run: %v", err) + } + + // Check that the pipeline run is as expected + if pipelineRun.Name != expectedPipelineRun.Name { + t.Fatalf("Expected pipeline run to have name %s, got %s", expectedPipelineRun.Name, pipelineRun.Name) + } +} + +func TestPipelineByNameWithError(t *testing.T) { + fakeClient := fakeclient.NewSimpleClientset() + + c := &cli.Clients{ + Tekton: fakeClient, + } + + // Get the pipeline runs + _, err := GetPipelineRunsByName(c, "fake-pipeline", namespace) + if err == nil { + t.Fatal("GetPipelineRunsByName did not return an error, expected an error") + } + if err.Error() != "failed to get PipelineRun with name fake-pipeline: pipelineruns.tekton.dev \"fake-pipeline\" not found" { + t.Fatalf("Expected error message to be 'failed to get PipelineRun with name fake-pipeline: pipelineruns.tekton.dev \"fake-pipeline\" not found', got %s", err.Error()) + } +}