Crossplane Provider Helm

repository·main·Indexed 40 days ago

https://github.com/crossplane-contrib/provider-helm

A Crossplane Provider that enables the deployment and management of Helm Releases on Kubernetes clusters using a dedicated Release resource type. It supports OCI registries, classic repositories, and local URLs, providing programmatic control over installing, upgrading, rolling back, and uninstalling releases.

Tokens
3.3K
Snippets
7
Records
12
Agent score
50%

What's inside Crossplane Provider Helm

  1. Manage Helm Releases with the Release resource

    main
    The provider-helm provider introduces a Release resource type. This managed resource allows you to declare and manage Helm Releases on Kubernetes clusters via Crossplane. The provider's controller reconciles these Release objects to ensure the state of the Helm release matches the desired state defined in the resource.
  2. Quick start with In-Cluster credentials

    main

    For a fast setup, you can configure provider-helm to reuse existing credentials from within the control plane using InjectedIdentity.

    1. Install provider-helm using the in-cluster configuration to bind its service account to an existing role in the cluster:
      kubectl apply -f ./examples/cluster/provider-config/provider-incluster.yaml
    2. Apply a ProviderConfig that uses an InjectedIdentity source:
      kubectl apply -f ./examples/cluster/provider-config/provider-config-incluster.yaml
    3. Create a Release resource to manage a Helm release:
      kubectl create -f examples/cluster/sample/release.yaml
    kubectl apply -f ./examples/cluster/provider-config/provider-incluster.yaml
    kubectl apply -f ./examples/cluster/provider-config/provider-config-incluster.yaml
    kubectl create -f examples/cluster/sample/release.yaml
  3. Install provider-helm using Crossplane CLI

    main

    To install the latest version of provider-helm in a Kubernetes cluster where Crossplane is already installed, use the Crossplane CLI to fetch the latest version tag and install the package.

    After installation, you must create a ProviderConfig to specify the credentials required to connect to the Kubernetes API. A common pattern is to store a kubeconfig in a Kubernetes Secret and reference that Secret in your ProviderConfig.

    latest_tag=$(crane ls xpkg.crossplane.io/crossplane-contrib/provider-helm | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n1)
    crossplane xpkg install provider xpkg.crossplane.io/crossplane-contrib/provider-helm:$latest_tag
  4. Develop provider-helm locally

    main

    To run the provider-helm controller against your local Kubernetes cluster for development, follow these steps:

    1. Run the controller: Execute make run to start the controller outside the cluster.
    2. Expose the API server: In a separate terminal, run sudo kubectl proxy --port=8081 to make the API server accessible to the local controller.
    3. Prepare ProviderConfig: If using kind, generate a kubeconfig that points to the local proxy, store it in a Secret, and apply a ProviderConfig that uses that Secret.
    4. Test with a Release: Create a Release resource to verify the local setup.
    # 1. Run controller
    make run
    
    # 2. Proxy API (separate terminal)
    sudo kubectl proxy --port=8081
    
    # 3. Setup ProviderConfig for local cluster (e.g. kind)
    KUBECONFIG=$(kind get kubeconfig | sed -e 's|server:\s*.*$|server: http://localhost:8081|g') 
    kubectl -n crossplane-system create secret generic cluster-config --from-literal=kubeconfig="${KUBECONFIG}" 
    kubectl apply -f examples/cluster/provider-config/provider-config-with-secret.yaml
    
    # 4. Create a Release
    kubectl create -f examples/cluster/sample/release.yaml
  5. Configure Helm client via ArgsApplier

    main

    The NewClient function uses the ArgsApplier pattern for configuration. An ArgsApplier is a function with the signature func(*Args). You can pass multiple appliers to NewClient to set various parameters.

    While the exact fields of Args are internal to the client implementation, they control critical behaviors such as:

    • Namespace: The Kubernetes namespace for the release.
    • CABundle: Custom CA certificates for TLS trust (applied to both OCI and classic repos).
    • InsecureSkipTLSVerify: Whether to skip TLS verification.
    • PlainHTTP: Whether to use unencrypted HTTP.
    • Wait: Whether to wait for resources to be ready (uses kube.StatusWatcherStrategy if true).
    • Timeout: The timeout for Helm actions.
    • SkipCRDs: Whether to skip installing CRDs.
    • TakeOwnership: Whether to take ownership of resources.
    • SSAForceConflicts: Whether to use Server-Side Apply to force conflicts.
  6. Enable Secret Caching to reduce API server load

    main
    By default, provider-helm enables secret caching (--enable-secret-cache or ENABLE_SECRET_CACHE=true). When enabled, Secret objects are served from the informer cache rather than making direct API calls to the Kubernetes API server. This reduces load on the API server but increases the memory usage of the provider process. To disable caching, set --enable-secret-cache=false.
  7. Configure Webhook TLS certificate directory

    main

    The provider requires a directory containing TLS certificates for its webhook server. It looks for this directory using the following precedence:

    1. The TLS_SERVER_CERTS_DIR environment variable (used in newer Crossplane versions).
    2. The WEBHOOK_TLS_CERT_DIR environment variable (used in older Crossplane versions).
    3. The default path /tls/server.
  8. Use the Helm Client interface

    main

    The Client interface provides the primary programmatic way to interact with Helm releases. It supports standard Helm operations like installing, upgrading, rolling back, and uninstalling releases, as well as retrieving the last known release state and pulling/loading charts from repositories.

    Methods available on the Client interface:

    • GetLastRelease(release string) (*release.Release, error): Retrieves the most recent release information for the specified name.
    • Install(release string, chart *chart.Chart, vals map[string]interface{}, patches []ktype.Patch) (*release.Release, error): Installs a new Helm release using the provided chart, values, and optional Kustomize patches.
    • Upgrade(release string, chart *chart.Chart, vals map[string]interface{}, patches []ktype.Patch) (*release.Release, error): Upgrades an existing release. Note that Upgrade resets values so the CR remains the source of truth.
    • Rollback(release string) error: Rolls back the specified release to its previous state.
    • Uninstall(release string) error: Uninstalls the specified release.
    • PullAndLoadChart(mg resource.Managed, creds *RepoCreds) (*chart.Chart, error): Pulls a chart from a repository (supporting both classic HTTP/S and OCI registries) and loads it into a *chart.Chart object.
    type Client interface {
    	GetLastRelease(release string) (*release.Release, error)
    	Install(release string, chart *chart.Chart, vals map[string]interface{}, patches []ktype.Patch) (*release.Release, error)
    	Upgrade(release string, chart *chart.Chart, vals map[string]interface{}, patches []ktype.Patch) (*release.Release, error)
    	Rollback(release string) error
    	Uninstall(release string) error
    	PullAndLoadChart(mg resource.Managed, creds *RepoCreds) (*chart.Chart, error)
    }
  9. Initialize a new Helm Client

    main

    To create a new Helm client, use NewClient. This function requires a logging.Logger and a Kubernetes rest.Config. It also accepts optional ArgsApplier functions to configure specific Helm settings such as namespaces, CA bundles, timeouts, and TLS settings.

    NewClient handles the initialization of Helm action configurations, including setting up the registry client for OCI support and configuring the chart cache directories.

    func NewClient(log logging.Logger, restConfig *rest.Config, argAppliers ...ArgsApplier) (Client, error)
  10. Pull and load a Helm chart

    main

    The PullAndLoadChart method automates the process of fetching a chart from a remote repository and preparing it for use. It supports:

    • OCI Registries: Uses the provided URL, version, or digest. If a digest is provided, it is validated against the pulled content.
    • Classic Repositories: Uses spec.forProvider.chart.repository, name, and version.
    • Local/Direct URLs: Uses the provided chart.URL.

    It requires a resource.Managed object (specifically a *clusterv1beta1.Release or *namespacedv1beta1.Release) to extract chart metadata and a *RepoCreds object for authentication. The method handles caching the downloaded .tgz file in the configured chartCache directory.

  11. Reference: provider-helm CLI flags and environment variables

    main

    The following flags and environment variables are available for configuring the provider-helm binary.

    --debug, -d             Run with debug logging.
    --leader-election, -l   Use leader election for the controller manager. (Env: LEADER_ELECTION)
    --timeout               Controls how long helm commands may run before they are killed. (Default: 10m)
    --sync, -s               How often all resources will be double-checked for drift from the desired state. (Default: 1h)
    --poll                  How often individual resources will be checked for drift from the desired state. (Default: 10m)
    --poll-state-metric     State metric recording interval. (Default: 5s)
    --max-reconcile-rate    The global maximum rate per second at which resources may checked for drift from the desired state. (Default: 100)
    --webhook-port           The port the webhook server listens on. (Env: WEBHOOK_PORT, Default: 9443)
    --metrics-bind-address   The address the metrics server listens on. (Env: METRICS_BIND_ADDRESS, Default: :8080)
    --health-probe-bind-addr The address the health/readiness probe server listens on. (Env: HEALTH_PROBE_BIND_ADDRESS, Default: :8081)
    --enable-management-policies Enable support for Management Policies. (Env: ENABLE_MANAGEMENT_POLICIES, Default: true)
    --enable-changelogs      Enable support for capturing change logs during reconciliation. (Env: ENABLE_CHANGE_LOGS, Default: false)
    --changelogs-socket-path Path for changelogs socket (if enabled). (Env: CHANGELOGS_SOCKET_PATH, Default: /var/run/changelogs/changelogs.sock)
    --enable-secret-cache   Enable caching of Secret objects. When true, Secrets are served from the informer cache instead of direct API calls. (Env: ENABLE_SECRET_CACHE, Default: true)
  12. Configure provider-helm via CLI flags and environment variables

    main

    The provider-helm binary can be configured using several command-line flags and environment variables. These settings control reconciliation behavior, performance, and observability. If a flag is provided, it overrides the corresponding environment variable.

    # Example of running the provider with custom settings
    ./provider-helm --debug --sync 30m --poll 5m --max-reconcile-rate 50