k3s

package module
v0.30.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithManifest added in v0.29.0

func WithManifest(manifestPath string) testcontainers.CustomizeRequestOption

WithManifest loads the manifest into the cluster. K3s applies it automatically during the startup process

Types

type K3sContainer

type K3sContainer struct {
	testcontainers.Container
}

K3sContainer represents the K3s container type used in the module

func RunContainer

func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*K3sContainer, error)

RunContainer creates an instance of the K3s container type

Example
// runK3sContainer {
ctx := context.Background()

k3sContainer, err := k3s.RunContainer(ctx,
	testcontainers.WithImage("docker.io/rancher/k3s:v1.27.1-k3s1"),
)
if err != nil {
	log.Fatalf("failed to start container: %s", err)
}

// Clean up the container
defer func() {
	if err := k3sContainer.Terminate(ctx); err != nil {
		log.Fatalf("failed to terminate container: %s", err)
	}
}()
// }

state, err := k3sContainer.State(ctx)
if err != nil {
	log.Fatalf("failed to get container state: %s", err) // nolint:gocritic
}

fmt.Println(state.Running)

kubeConfigYaml, err := k3sContainer.GetKubeConfig(ctx)
if err != nil {
	log.Fatalf("failed to get kubeconfig: %s", err)
}

restcfg, err := clientcmd.RESTConfigFromKubeConfig(kubeConfigYaml)
if err != nil {
	log.Fatalf("failed to create rest config: %s", err)
}

k8s, err := kubernetes.NewForConfig(restcfg)
if err != nil {
	log.Fatalf("failed to create k8s client: %s", err)
}

nodes, err := k8s.CoreV1().Nodes().List(ctx, v1.ListOptions{})
if err != nil {
	log.Fatalf("failed to list nodes: %s", err)
}

fmt.Println(len(nodes.Items))
Output:

true
1

func (*K3sContainer) GetKubeConfig

func (c *K3sContainer) GetKubeConfig(ctx context.Context) ([]byte, error)

GetKubeConfig returns the modified kubeconfig with server url

func (*K3sContainer) LoadImages added in v0.25.0

func (c *K3sContainer) LoadImages(ctx context.Context, images ...string) error

LoadImages loads images into the k3s container.

type KubeConfigValue

type KubeConfigValue struct {
	APIVersion     string                   `yaml:"apiVersion"`
	Kind           string                   `yaml:"kind"`
	Clusters       []KubeconfigNamedCluster `yaml:"clusters"`
	Users          []KubeconfigUser         `yaml:"users"`
	Contexts       []KubeconfigNamedContext `yaml:"contexts"`
	CurrentContext string                   `yaml:"current-context"`
	Preferences    struct{}                 `yaml:"preferences"`
}

KubeConfigValue is a struct used to create a kubectl configuration YAML file.

type KubeconfigAuthProvider

type KubeconfigAuthProvider struct {
	Name   string            `yaml:"name"`
	Config map[string]string `yaml:"config"`
}

KubeconfigAuthProvider is a struct used to create a kubectl authentication provider

type KubeconfigCluster

type KubeconfigCluster struct {
	Server                   string `yaml:"server"`
	CertificateAuthorityData string `yaml:"certificate-authority-data"`
	CertificateAuthority     string `yaml:"certificate-authority"`
}

KubeconfigCluster is a struct used to create a kubectl configuration YAML file

type KubeconfigContext

type KubeconfigContext struct {
	Cluster   string `yaml:"cluster"`
	Namespace string `yaml:"namespace,omitempty"`
	User      string `yaml:"user"`
}

KubeconfigContext is a struct used to create a kubectl configuration YAML file

type KubeconfigNamedCluster

type KubeconfigNamedCluster struct {
	Name    string            `yaml:"name"`
	Cluster KubeconfigCluster `yaml:"cluster"`
}

KubeconfigNamedCluster is a struct used to create a kubectl configuration YAML file

type KubeconfigNamedContext

type KubeconfigNamedContext struct {
	Name    string            `yaml:"name"`
	Context KubeconfigContext `yaml:"context"`
}

KubeconfigNamedContext is a struct used to create a kubectl configuration YAML file

type KubeconfigUser

type KubeconfigUser struct {
	Name string                `yaml:"name"`
	User KubeconfigUserKeyPair `yaml:"user"`
}

KubeconfigUser is a struct used to create a kubectl configuration YAML file

type KubeconfigUserKeyPair

type KubeconfigUserKeyPair struct {
	ClientCertificateData string                 `yaml:"client-certificate-data"`
	ClientKeyData         string                 `yaml:"client-key-data"`
	AuthProvider          KubeconfigAuthProvider `yaml:"auth-provider,omitempty"`
}

KubeconfigUserKeyPair is a struct used to create a kubectl configuration YAML file

Jump to

Keyboard shortcuts

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