tektontaskrun

package
v0.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2023 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Overview

Package tektontaskrun implements ODS Pipeline independent functionality to run Tekton tasks in a KinD cluster.

Using tektontaskrun it is possible to start a KinD cluster, configure it (e.g. by setting up a temporary namespace), and running a Tekton task.

tektontaskrun is intended to be used by CLI programs and as a library for testing Tekton tasks using Go.

Example usage:

package test

import (
	"log"
	"os"
	"path/filepath"
	"testing"

	ttr "github.com/opendevstack/ods-pipeline/pkg/tektontaskrun"
)

var (
	namespaceConfig *ttr.NamespaceConfig
	rootPath        = "../.."
)

func TestMain(m *testing.M) {
	cc, err := ttr.StartKinDCluster(
		ttr.LoadImage(ttr.ImageBuildConfig{
			Dockerfile: "build/images/Dockerfile.my-task",
			ContextDir: rootPath,
		}),
	)
	if err != nil {
		log.Fatal("Could not start KinD cluster: ", err)
	}
	nc, cleanup, err := ttr.SetupTempNamespace(
		cc,
		ttr.InstallTaskFromPath(
			filepath.Join(rootPath, "build/tasks/my-task.yaml"),
			nil,
		),
	)
	if err != nil {
		log.Fatal("Could not setup temporary namespace: ", err)
	}
	defer cleanup()
	namespaceConfig = nc
	os.Exit(m.Run())
}

func TestMyTask(t *testing.T) {
	if err := ttr.RunTask(
		ttr.InNamespace(namespaceConfig.Name),
		ttr.UsingTask("my-task"),
		ttr.WithStringParams(map[string]string{
			"go-os":       runtime.GOOS,
			"go-arch":     runtime.GOARCH,
		}),
		ttr.WithWorkspace("source", "my-sample-app"),
		ttr.AfterRun(func(config *ttr.TaskRunConfig, run *tekton.TaskRun) {
			wd := config.WorkspaceConfigs["source"].Dir
			// e.g. check files in workspace ...
		}),
	); err != nil {
		t.Fatal(err)
	}
}

// further tests here ...

Index

Constants

View Source
const (
	DefaultServiceAccountName = "pipeline"
	KinDMountHostPath         = "/tmp/ods-pipeline/kind-mount"
	KinDMountContainerPath    = "/files"
	KinDRegistry              = "localhost:5000"
	KinDName                  = "ods-pipeline"
)
View Source
const (
	DefaultTimeout = 5 * time.Minute
)

Variables

This section is empty.

Functions

func RunTask

func RunTask(opts ...TaskRunOpt) error

RunTask executes a task run after applying all given TaskRunOpt.

func SetupWorkspaceDir

func SetupWorkspaceDir(sourceDir string) (dir string, cleanup func(), err error)

SetupWorkspaceDir copies sourceDir to the KinD mount host path, which is set to /tmp/ods-pipeline/kind-mount. The created folder can then be used as a Tekton task run workspace. SetupWorkspaceDir returns the created directory as well as a function to clean it up.

func TektonParamsFromStringParams

func TektonParamsFromStringParams(stringParams map[string]string) []tekton.Param

Types

type ClusterConfig

type ClusterConfig struct {
	StorageSourceDir  string
	StorageCapacity   string
	StorageClassName  string
	Registry          string
	DefaultRepository string
}

ClusterConfig represents key configuration of the KinD cluster.

func NewClusterConfig

func NewClusterConfig() *ClusterConfig

NewClusterConfig creates a new ClusterConfig instance.

func StartKinDCluster

func StartKinDCluster(opts ...ClusterOpt) (*ClusterConfig, error)

StartKinDCluster starts a KinD cluster with Tekton installed. Afterwards, any given ClusterOpt is applied.

func (*ClusterConfig) DefaultImageRepository

func (c *ClusterConfig) DefaultImageRepository() string

DefaultImageRepository returns the registry + default repository combination.

func (*ClusterConfig) DefaultTaskTemplateData

func (c *ClusterConfig) DefaultTaskTemplateData() map[string]string

DefaultTaskTemplateData returns a map with default values which can be used in task templates.

type ClusterOpt

type ClusterOpt func(c *ClusterConfig) error

ClusterOpt allows to further configure the KinD cluster after its creation.

func LoadImage

func LoadImage(ibc ImageBuildConfig) ClusterOpt

LoadImage builds a container image using the docker CLI based on the given ImageBuildConfig.

The ImageBuildConfig must set at least Dockerfile and ContextDir option. If Tag is unset, it is inferred from the default registry and the Dockerfile name. For example, given a Dockerfile of "Dockerfile.foobar", the tag is defaulted to localhost:5000/ods-pipeline/foobar. Passing the flag -ods-reuse-images to the tests will skip image rebuilding.

type ImageBuildConfig

type ImageBuildConfig struct {
	Dockerfile string
	Tag        string
	ContextDir string
}

ImageBuildConfig represents the config used to build a container image.

func (*ImageBuildConfig) Process

func (ibc *ImageBuildConfig) Process(defaultImageRepository string) error

Process validates the configuration and defaults the image tag if unset using the defaultImageRepository and Dockerfile values.

type NamespaceConfig

type NamespaceConfig struct {
	Name string
}

NamespaceConfig represents key configuration of the K8s namespace.

func SetupTempNamespace

func SetupTempNamespace(cc *ClusterConfig, opts ...NamespaceOpt) (nc *NamespaceConfig, cleanup func(), err error)

SetupTempNamespace sets up a new namespace using a pseduo-random name, applies any given NamespaceOpt and returns a function to clean up the namespace at a later time.

type NamespaceOpt

type NamespaceOpt func(cc *ClusterConfig, nc *NamespaceConfig) error

NamespaceOpt allows to further configure the K8s namespace after its creation.

func InstallTaskFromPath

func InstallTaskFromPath(path string, data map[string]string) NamespaceOpt

InstallTaskFromPath renders the task template at path using the given data, then installs the resulting task into the namespace identified by NamespaceConfig.

type TaskRunConfig

type TaskRunConfig struct {
	Name               string
	Params             []tekton.Param
	Workspaces         map[string]string
	Namespace          string
	ServiceAccountName string
	Timeout            time.Duration
	AfterRunFunc       func(config *TaskRunConfig, taskRun *tekton.TaskRun, logs bytes.Buffer)
	CleanupFuncs       []func()
	NamespaceConfig    *NamespaceConfig
	WorkspaceConfigs   map[string]*WorkspaceConfig
	ExpectFailure      bool
}

TaskRunConfig represents key configuration of the Tekton task run.

func (*TaskRunConfig) Cleanup

func (nc *TaskRunConfig) Cleanup()

Cleanup calls all registered CleanupFuncs.

type TaskRunOpt

type TaskRunOpt func(c *TaskRunConfig) error

TaskRunOpt allows to configure the Tekton task run before it is started.

func AfterRun

func AfterRun(f func(c *TaskRunConfig, r *tekton.TaskRun, l bytes.Buffer)) TaskRunOpt

AfterRun registers a function which is run after the task run completes. The function will receive the task run configuration, as well as an instance of the TaskRun.

func ExpectFailure

func ExpectFailure() TaskRunOpt

ExpectFailure sets up an expectation that the task will fail. If the task does not fail, RunTask will error. Conversely, if ExpectFailure is not set, RunTask will error when the task run fails.

func InNamespace

func InNamespace(namespace string) TaskRunOpt

InNamespace configures the task run to execute in given namespace.

func InTempNamespace

func InTempNamespace(cc *ClusterConfig, opts ...NamespaceOpt) TaskRunOpt

InTempNamespace configures the task run to execute in a newly created, temporary namespace.

func UsingTask

func UsingTask(name string) TaskRunOpt

UsingTask configures the task run to execute the Task identified by name in the configured namespace.

func WithParams

func WithParams(params ...tekton.Param) TaskRunOpt

WithParams configures the task run to use the specified Tekton parameters.

func WithServiceAccountName

func WithServiceAccountName(name string) TaskRunOpt

WithServiceAccountName configures the task run to execute under the specified serviceaccount name.

func WithStringParams

func WithStringParams(params map[string]string) TaskRunOpt

WithStringParams configures the task run to use the specified string parameters. WithStringParams is a more convenient way to configure simple parameters compares to WithParams.

func WithTimeout

func WithTimeout(timeout time.Duration) TaskRunOpt

WithTimeout configures the task run to execute within the given duration.

func WithWorkspace

func WithWorkspace(name, sourceDir string, opts ...WorkspaceOpt) TaskRunOpt

WithWorkspace sets up a workspace with given name and contents of sourceDir. sourceDir is copied to a temporary directory so that the original contents remain unchanged.

type WorkspaceConfig

type WorkspaceConfig struct {
	// Name of the Tekton workspace.
	Name string
	// Directory on the host of the workspace.
	Dir string
	// Cleanup function.
	Cleanup func()
}

WorkspaceConfig describes a Tekton workspace.

type WorkspaceOpt

type WorkspaceOpt func(c *WorkspaceConfig) error

WorkspaceOpt allows to further configure a Tekton workspace after its creation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL