tftestenv

package module
v0.0.0-...-01ecf2c Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 30 Imported by: 2

Documentation

Overview

Package tftestenv contains helpers to create and work with cloud infrastructure using terraform. It is similar to testenv, which helps create new test kubernetes environment and cleanup at the end of the tests. For ease of use, it also contains some helper utilities for interacting with the various cloud providers for generating kubeconfig of the created cluster, registry login and pushing images to the registries, etc.

Index

Constants

View Source
const CreatedAtTimeLayout = "x2006-01-02_15h04m05s"

CreatedAtTimeLayout is a time layout for the 'createdat' label/tag on cloud resources.

Variables

This section is empty.

Functions

func CreateAndPushImages

func CreateAndPushImages(repos map[string]string, tags []string) error

CreateAndPushImages randomly generates test images with the given tags and pushes them to the given test repositories.

func CreateKubeconfigAKS

func CreateKubeconfigAKS(ctx context.Context, kubeconfigYaml string, kcPath string) error

CreateKubeconfigAKS constructs kubeconfig for an AKS cluster from the terraform state output at the given kubeconfig path.

func CreateKubeconfigEKS

func CreateKubeconfigEKS(ctx context.Context, clusterName, eksHost, eksClusterArn, eksCa, kcPath string) error

CreateKubeconfigEKS constructs kubeconfig from the terraform state output at the given kubeconfig path. func createKubeconfigEKS(ctx context.Context, state map[string]*tfjson.StateOutput, kcPath string) error {

func CreateKubeconfigGKE

func CreateKubeconfigGKE(ctx context.Context, kubeconfigYaml string, kcPath string) error

CreateKubeconfigGKE constructs kubeconfig from the terraform state output at the given kubeconfig path.

func Destroy

func Destroy(ctx context.Context, terraformPath string, opts ...EnvironmentOption) error

Destroy configures a new Environment with the given configurations for terraform and runs terraform destroy. Ideally, this need not be used as the testenv New() handles graceful cleanup when shutdown signals are received. But in case the whole process gets terminated, use this to just perform a destroy of any created infrastructure. This can be used as the last step in CI to always run irrespective of success or failure of the test run to make sure the test infrastructure is destroyed. One such scenario is when the cloud provider takes longer than the usual time to provision the infrastructure and the test binary execution reaches timeout and the whole process gets terminated. This can be run in a separate step in CI to destroy the infrastructure.

func GetGoogleArtifactRegistryAndRepository

func GetGoogleArtifactRegistryAndRepository(project, region, repositoryID string) (string, string)

GetGoogleArtifactRegistryAndRepository returns artifact registry and artifact repository URL from the given repository ID.

func ParseCreatedAtTime

func ParseCreatedAtTime(createdat string) (time.Time, error)

ParseCreatedAtTime parses 'createdat' label/tag on resources. The time value is in a custom format due to the label/tag value restrictions on various cloud platforms. See tf-modules/utils/tags for details about the custom format.

func PushTestAppImagesACR

func PushTestAppImagesACR(ctx context.Context, localImgs map[string]string, registryURL string) (map[string]string, error)

PushTestAppImagesACR pushes app images that are being tested. It must be called after RegistryLoginACR to ensure the local docker client is already logged in and is capable of pushing the test images.

func PushTestAppImagesECR

func PushTestAppImagesECR(ctx context.Context, localImgs map[string]string, remoteImage string) (map[string]string, error)

PushTestAppImagesECR pushes app image that is being tested. It must be called after RegistryLoginECR to ensure the local docker client is already logged in and is capable of pushing the test images.

func PushTestAppImagesGCR

func PushTestAppImagesGCR(ctx context.Context, localImgs map[string]string, project, region, artifactRepoID string) (map[string]string, error)

PushTestAppImagesGCR pushes app images that are being tested. It must be called after RegistryLoginGCR to ensure the local docker client is already logged in and is capable of pushing the test images.

func RegistryLoginACR

func RegistryLoginACR(ctx context.Context, registryURL string) error

RegistryLoginACR logs into the container/artifact registries using the provider's CLI tools and returns a list of test repositories.

func RegistryLoginECR

func RegistryLoginECR(ctx context.Context, region, repoURL string) error

RegistryLoginECR logs into the container/artifact registries using the provider's CLI tools and returns a list of test repositories.

func RegistryLoginGCR

func RegistryLoginGCR(ctx context.Context, repoURL string) error

RegistryLoginGCR logs into the container/artifact registries using the provider's CLI tools and returns a list of test repositories. func registryLoginGCR(ctx context.Context, output map[string]*tfjson.StateOutput) (map[string]string, error) {

func RetagAndPush

func RetagAndPush(ctx context.Context, localImage, remoteImage string) error

RetagAndPush retags local image based on the remoteImage and pushes the remoteImage

func RunCommand

func RunCommand(ctx context.Context, dir, command string, opts RunCommandOptions) error

RunCommand executes the given command in a given directory.

func RunCommandWithOutput

func RunCommandWithOutput(ctx context.Context, dir, command string, opts RunCommandOptions) ([]byte, error)

RunCommandWithOutput executes the given command and returns the output.

Types

type CreateKubeconfig

type CreateKubeconfig func(ctx context.Context, state map[string]*tfjson.StateOutput, kcPath string) error

createKubeconfig create a kubeconfig for the target cluster and writes to the given path using the contextual values from the infrastructure state.

type Environment

type Environment struct {
	client.Client
	Config *rest.Config

	// CreateKubeconfig provides the terraform state output which is used to
	// construct kubeconfig.
	CreateKubeconfig CreateKubeconfig
	// contains filtered or unexported fields
}

Environment encapsulates a Kubernetes test environment.

func New

func New(ctx context.Context, scheme *runtime.Scheme, terraformPath string, kubeconfigPath string, opts ...EnvironmentOption) (*Environment, error)

New finds or downloads terraform binary, uses it to run terraform in the given terraformPath to create a kubernetes cluster. A kubeconfig of the created cluster is constructed at the given kubeconfigPath which is then used to construct a kubernetes client that can be used in the tests.

func (*Environment) StateOutput

func (env *Environment) StateOutput(ctx context.Context) (map[string]*tfjson.StateOutput, error)

State queries and returns the current state output of terraform.

func (*Environment) Stop

func (env *Environment) Stop(ctx context.Context) error

Stop tears down the test infrastructure created by the environment.

type EnvironmentOption

type EnvironmentOption func(*Environment)

EnvironmentOption is used to configure the Environment.

func WithBuildDir

func WithBuildDir(dir string) EnvironmentOption

WithBuildDir sets the build directory for the environment. Defaults to "build".

func WithCreateKubeconfig

func WithCreateKubeconfig(create CreateKubeconfig) EnvironmentOption

WithCreateKubeconfig configures how kubeconfig is constructured using the output state of the terraform infrastructure.

func WithExisting

func WithExisting(existing bool) EnvironmentOption

WithExisting configures the Environment to use the existing infrastructure. By default, the environment set up would fail if the terraform state is not clean.

func WithRetain

func WithRetain(retain bool) EnvironmentOption

WithRetain configures the Environment to retain the created or existing infrastructure.

func WithTfApplyOptions

func WithTfApplyOptions(opts ...tfexec.ApplyOption) EnvironmentOption

WithTfApplyOptions configures terraform apply options.

func WithTfDestroyOptions

func WithTfDestroyOptions(opts ...tfexec.DestroyOption) EnvironmentOption

WithTfDestroyOptions configures terraform destroy options.

func WithVerbose

func WithVerbose(verbose bool) EnvironmentOption

WithVerbose configures the terraform executor to run in verbose mode.

type Options

type Options struct {
	// Provider indicates the cloud provider
	Provider string
	// Retain flag, if set to true the created infrastructure is not destroyed at the end of the test.
	Retain bool
	// Existing flag make the test to use existing terraform state.
	Existing bool
	// Verbose flag to enable output of terraform execution.
	Verbose bool
	// DestroyOnly can be used to run the testenv in destroy only mode to
	// perform cleanup.
	DestroyOnly bool
}

Options contains options for creating the terraform test environment

func (*Options) Bindflags

func (o *Options) Bindflags(fs *flag.FlagSet)

Bindflags will parse the given flag.FlagSet and set the Options accordingly.

func (*Options) Validate

func (o *Options) Validate() error

Validate method ensures that the provider is set to one of the supported ones - aws, azure or gcp.

type RunCommandOptions

type RunCommandOptions struct {
	// Shell is the name of the shell program used to run the command.
	Shell string
	// EnvVars is the environment variables used with the command.
	EnvVars []string
	// StdoutOnly can be enabled to only capture the stdout of the command
	// output.
	StdoutOnly bool
	// Timeout is timeout for the command execution.
	Timeout time.Duration
	// AttachConsole attaches the stdout and stderr of the command to the
	// console.
	AttachConsole bool
}

RunCommandOptions is used to configure the RunCommand execution.

Jump to

Keyboard shortcuts

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