k8s

package
v0.0.0-...-cedb4eb Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const AppOfAppsName = "app-of-apps"

AppOfAppsName is the name of the root Application deployed by bootstrap.

Variables

This section is empty.

Functions

func ResolveContext

func ResolveContext(kubeconfig, context string) (string, error)

ResolveContext returns the kubeconfig context that NewClient would connect to for the same arguments: the explicit override when given, otherwise the current context of the resolved kubeconfig.

Types

type Client

type Client struct {
	Clientset     kubernetes.Interface
	DynamicClient dynamic.Interface
}

Client wraps the Kubernetes clientset and dynamic client.

func NewClient

func NewClient(kubeconfig, context string) (*Client, error)

NewClient creates a Kubernetes client from the given kubeconfig and context. If kubeconfig is empty, it uses the default loading rules. If context is empty, it uses the current context.

func (*Client) ApplyAppOfApps

func (c *Client) ApplyAppOfApps(ctx context.Context, repoURL, targetRevision, env, appPath string, enableCilium, dryRun bool) (string, bool, error)

ApplyAppOfApps creates or updates the App of Apps root Application CR. Returns a boolean indicating if it was created (true) or updated (false) when not in dry-run mode.

func (*Client) CreateGitCryptKeySecret

func (c *Client) CreateGitCryptKeySecret(ctx context.Context, keyData []byte) (bool, error)

CreateGitCryptKeySecret creates or updates the git-crypt-key secret in the argocd namespace. The key data is the raw symmetric key used by git-crypt. Returns a boolean indicating if it was created (true) or updated (false).

func (*Client) CreateRepoSSHSecret

func (c *Client) CreateRepoSSHSecret(ctx context.Context, repoURL, sshPrivateKey string, dryRun bool) (*corev1.Secret, bool, error)

CreateRepoSSHSecret creates or updates the repo-ssh-key secret in the argocd namespace. This matches the exact labels/annotations from the original install.sh. Returns the secret and a boolean indicating if it was created (true) or updated (false).

func (*Client) CreateSopsAgeKeySecret

func (c *Client) CreateSopsAgeKeySecret(ctx context.Context, keyData []byte) (bool, error)

CreateSopsAgeKeySecret creates or updates the sops-age-key secret in the argocd namespace. The key data is mounted by ArgoCD repo-server instances that decrypt SOPS values in-cluster. Returns a boolean indicating if it was created (true) or updated (false).

func (*Client) EnsureNamespace

func (c *Client) EnsureNamespace(ctx context.Context, name string) (bool, error)

EnsureNamespace creates a namespace if it does not already exist.

func (*Client) GetAppOfApps

func (c *Client) GetAppOfApps(ctx context.Context) (*unstructured.Unstructured, error)

GetAppOfApps returns the App of Apps root Application already present in the cluster, or nil when it does not exist. A missing ArgoCD Application CRD is also reported as absent, since no App of Apps can exist without it.

type ClientInterface

type ClientInterface interface {
	EnsureNamespace(ctx context.Context, name string) (bool, error)
	CreateRepoSSHSecret(ctx context.Context, repoURL, sshPrivateKey string, dryRun bool) (*corev1.Secret, bool, error)
	CreateGitCryptKeySecret(ctx context.Context, keyData []byte) (bool, error)
	CreateSopsAgeKeySecret(ctx context.Context, keyData []byte) (bool, error)
	GetAppOfApps(ctx context.Context) (*unstructured.Unstructured, error)
	ApplyAppOfApps(ctx context.Context, repoURL, targetRevision, env, appPath string, enableCilium, dryRun bool) (string, bool, error)
}

ClientInterface defines the interface that both Client and MockClient implement. This is useful for testing code that uses a K8s client.

type MockClient

type MockClient struct {
	// Namespaces created in this mock.
	Namespaces map[string]bool
	// Secrets created in this mock.
	Secrets map[string]map[string]*corev1.Secret
	// Applications created in this mock.
	Applications map[string]*unstructured.Unstructured
	// Simulate errors for specific operations.
	EnsureNamespaceErr       error
	CreateRepoSSHSecretErr   error
	CreateGitCryptKeyErr     error
	CreateSopsAgeKeyErr      error
	ApplyAppOfAppsErr        error
	GetAppOfAppsErr          error
	EnsureNamespaceForbidden bool
	CreateSecretForbidden    bool
}

MockClient is a mock Kubernetes client for testing. It allows simulating errors and state.

func NewMockClient

func NewMockClient() *MockClient

NewMockClient creates a new mock client for testing.

func (*MockClient) ApplyAppOfApps

func (m *MockClient) ApplyAppOfApps(ctx context.Context, repoURL, targetRevision, env, appPath string, enableCilium, dryRun bool) (string, bool, error)

ApplyAppOfApps simulates Application CR creation.

func (*MockClient) CreateGitCryptKeySecret

func (m *MockClient) CreateGitCryptKeySecret(ctx context.Context, keyData []byte) (bool, error)

CreateGitCryptKeySecret simulates git-crypt key secret creation.

func (*MockClient) CreateRepoSSHSecret

func (m *MockClient) CreateRepoSSHSecret(ctx context.Context, repoURL, sshPrivateKey string, dryRun bool) (*corev1.Secret, bool, error)

CreateRepoSSHSecret simulates secret creation with mock state.

func (*MockClient) CreateSopsAgeKeySecret

func (m *MockClient) CreateSopsAgeKeySecret(ctx context.Context, keyData []byte) (bool, error)

CreateSopsAgeKeySecret simulates SOPS age key secret creation.

func (*MockClient) EnsureNamespace

func (m *MockClient) EnsureNamespace(ctx context.Context, name string) (bool, error)

EnsureNamespace simulates namespace creation with mock state.

func (*MockClient) GetAppOfApps

func (m *MockClient) GetAppOfApps(ctx context.Context) (*unstructured.Unstructured, error)

GetAppOfApps returns the stored App of Apps Application, or nil when absent.

func (*MockClient) GetApplication

func (m *MockClient) GetApplication(name string) *unstructured.Unstructured

GetApplication retrieves a stored application from the mock (for testing verification).

func (*MockClient) GetSecret

func (m *MockClient) GetSecret(namespace, name string) *corev1.Secret

GetSecret retrieves a stored secret from the mock (for testing verification).

Jump to

Keyboard shortcuts

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