Skip to content

Commit

Permalink
feat: Allow to get graph for specific Pipeline/PipelineRun provided b…
Browse files Browse the repository at this point in the history
…y name

Signed-off-by: Sergiy Kulanov <[email protected]>
  • Loading branch information
SergK committed Oct 10, 2023
1 parent 059ea93 commit fb698bd
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
]
Expand Down
21 changes: 17 additions & 4 deletions pkg/cmd/pipeline/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 17 additions & 4 deletions pkg/cmd/pipelinerun/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions pkg/pipeline/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
50 changes: 50 additions & 0 deletions pkg/pipeline/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
9 changes: 9 additions & 0 deletions pkg/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
50 changes: 50 additions & 0 deletions pkg/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

0 comments on commit fb698bd

Please sign in to comment.