tftestenv

package module
v0.0.0-...-f683b60 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2022 License: Apache-2.0 Imports: 23 Imported by: 0

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

This section is empty.

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 GetGoogleArtifactRegistryAndRepository

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

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

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 RunCommand

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

RunCommand executes given command in a given directory.

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 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 WithVerbose

func WithVerbose(verbose bool) EnvironmentOption

WithVerbose configures the terraform executor to run in verbose mode.

type RunCommandOptions

type RunCommandOptions struct {
	Shell   string
	EnvVars []string
}

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