-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move logic out of main.go Signed-off-by: Sergiy Kulanov <[email protected]>
- Loading branch information
Showing
5 changed files
with
249 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
v1pipeline "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" | ||
"github.com/tektoncd/pipeline/pkg/client/clientset/versioned" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
type Client struct { | ||
tektonClient versioned.Interface | ||
} | ||
|
||
func NewClient() (*Client, error) { | ||
// Create the Kubernetes client | ||
config, err := rest.InClusterConfig() | ||
if err != nil { | ||
kubeconfig := os.Getenv("KUBECONFIG") | ||
if kubeconfig == "" { | ||
kubeconfig = clientcmd.RecommendedHomeFile | ||
} | ||
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get Kubernetes configuration: %w", err) | ||
} | ||
} | ||
tektonClient, err := versioned.NewForConfig(config) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create Tekton client: %w", err) | ||
} | ||
|
||
return &Client{ | ||
tektonClient: tektonClient, | ||
}, nil | ||
} | ||
|
||
func (c *Client) GetNamespace() (string, error) { | ||
namespace, _, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( | ||
clientcmd.NewDefaultClientConfigLoadingRules(), | ||
&clientcmd.ConfigOverrides{}, | ||
).Namespace() | ||
if err != nil { | ||
return "", fmt.Errorf("failed to get namespace from kubeconfig: %w", err) | ||
} | ||
if namespace == "" { | ||
namespace = "default" | ||
} | ||
return namespace, nil | ||
} | ||
|
||
func (c *Client) GetPipelines(namespace string) ([]v1pipeline.Pipeline, error) { | ||
pipelines, err := c.tektonClient.TektonV1().Pipelines(namespace).List(context.TODO(), v1.ListOptions{}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get Pipelines: %w", err) | ||
} | ||
if len(pipelines.Items) == 0 { | ||
return nil, fmt.Errorf("no Pipelines found in namespace %s", namespace) | ||
} | ||
return pipelines.Items, nil | ||
} | ||
|
||
func (c *Client) GetPipelineRuns(namespace string) ([]v1pipeline.PipelineRun, error) { | ||
pipelineRuns, err := c.tektonClient.TektonV1().PipelineRuns(namespace).List(context.TODO(), v1.ListOptions{}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get PipelineRuns: %w", err) | ||
} | ||
if len(pipelineRuns.Items) == 0 { | ||
return nil, fmt.Errorf("no PipelineRuns found in namespace %s", namespace) | ||
} | ||
return pipelineRuns.Items, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
v1pipeline "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" | ||
fakeclient "github.com/tektoncd/pipeline/pkg/client/clientset/versioned/fake" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
const ( | ||
namespace = "test-namespace" | ||
pipelineName = "test-pipeline" | ||
) | ||
|
||
func TestClient_GetPipelinesNoError(t *testing.T) { | ||
// Create a fake Tekton clientset for testing | ||
fakeClient := fakeclient.NewSimpleClientset() | ||
|
||
expectedPipelines := []v1pipeline.Pipeline{ | ||
{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "pipeline-1", | ||
Namespace: namespace, | ||
}, | ||
}, | ||
{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "pipeline-2", | ||
Namespace: namespace, | ||
}, | ||
}, | ||
} | ||
|
||
// Add the test pipelines to the fake clientset | ||
for i := range expectedPipelines { | ||
_, err := fakeClient.TektonV1().Pipelines(namespace).Create(context.TODO(), &expectedPipelines[i], v1.CreateOptions{}) | ||
if err != nil { | ||
t.Fatalf("failed to create Pipeline: %v", err) | ||
} | ||
} | ||
|
||
// Create a client instance with the fake clientset | ||
client := &Client{ | ||
tektonClient: fakeClient, | ||
} | ||
|
||
pipelines, err := client.GetPipelines(namespace) | ||
assert.NoError(t, err) | ||
// assert number of pipelines | ||
assert.Equal(t, 2, len(pipelines)) | ||
// Assert that the returned pipelines are the same as the expected pipelines | ||
assert.Equal(t, expectedPipelines, pipelines) | ||
} | ||
|
||
func TestClient_GetPipelinesError(t *testing.T) { | ||
// Create a fake Tekton clientset for testing | ||
fakeClient := fakeclient.NewSimpleClientset() | ||
|
||
// Create a client instance with the fake clientset | ||
client := &Client{ | ||
tektonClient: fakeClient, | ||
} | ||
|
||
// Call the GetPipelines function | ||
_, err := client.GetPipelines(namespace) | ||
|
||
// Assert that error occurred | ||
assert.Error(t, err) | ||
|
||
// Assert that the error message is as expected | ||
assert.Equal(t, "no Pipelines found in namespace test-namespace", err.Error()) | ||
} | ||
|
||
func TestClient_GetPipelineRunsNoError(t *testing.T) { | ||
// Create a fake Tekton clientset for testing | ||
fakeClient := fakeclient.NewSimpleClientset() | ||
|
||
expectedPipelineRuns := []v1pipeline.PipelineRun{ | ||
{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "pipeline-1", | ||
Namespace: namespace, | ||
}, | ||
}, | ||
{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "pipeline-2", | ||
Namespace: namespace, | ||
}, | ||
}, | ||
} | ||
|
||
// Add the test pipelineruns to the fake clientset | ||
for i := range expectedPipelineRuns { | ||
_, err := fakeClient.TektonV1().PipelineRuns(namespace).Create(context.TODO(), &expectedPipelineRuns[i], v1.CreateOptions{}) | ||
if err != nil { | ||
t.Fatalf("failed to create PipelineRun: %v", err) | ||
} | ||
} | ||
|
||
// Create a client instance with the fake clientset | ||
client := &Client{ | ||
tektonClient: fakeClient, | ||
} | ||
|
||
pipelineruns, err := client.GetPipelineRuns(namespace) | ||
assert.NoError(t, err) | ||
// assert number of pipelineruns | ||
assert.Equal(t, 2, len(pipelineruns)) | ||
// Assert that the returned pipelineruns are the same as the expected pipelineruns | ||
assert.Equal(t, expectedPipelineRuns, pipelineruns) | ||
} | ||
|
||
func TestClient_GetPipelineRunsError(t *testing.T) { | ||
// Create a fake Tekton clientset for testing | ||
fakeClient := fakeclient.NewSimpleClientset() | ||
|
||
// Create a client instance with the fake clientset | ||
client := &Client{ | ||
tektonClient: fakeClient, | ||
} | ||
|
||
// Call the GetPipelines function | ||
_, err := client.GetPipelineRuns(namespace) | ||
|
||
// Assert that error occurred | ||
assert.Error(t, err) | ||
|
||
// Assert that the error message is as expected | ||
assert.Equal(t, "no PipelineRuns found in namespace test-namespace", err.Error()) | ||
} |