operator

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: Apache-2.0 Imports: 42 Imported by: 0

Documentation

Overview

Package operator assembles the kubepkg operator. A distribution that embeds kubepkg builds its own operator binary from it: start from DefaultOptions, change defaults, register its own backends, and Run.

Example (Cli)

The matching CLI, with a command of the distribution's own.

package main

import (
	"context"
	"fmt"
	"strings"

	"github.com/kuberoot-dev/kubepkg/pkg/cli"
	"github.com/kuberoot-dev/kubepkg/pkg/repo"
)

// allowedRegistries admits only versions whose charts come from the
// distribution's own registry.
type allowedRegistries struct{ prefix string }

func (allowedRegistries) AdmitIndex(context.Context, string, []byte, *repo.Index) error { return nil }

func (p allowedRegistries) AdmitVersion(_ context.Context, _, pkg string, v repo.Version) error {
	for _, variant := range v.Spec.Variants {
		for _, c := range variant.Components {
			if c.Chart != nil && !strings.HasPrefix(c.Chart.Repository, p.prefix) {
				return fmt.Errorf("%s %s: chart %s is not from %s", pkg, v.Version, c.Chart.Repository, p.prefix)
			}
		}
	}
	return nil
}

func main() {
	o := cli.DefaultOptions()
	o.Name, o.Short, o.APIGroup = "exctl", "Example Platform packages", "packages.example.org"
	o.Policy = allowedRegistries{prefix: "oci://registry.example.org/"}
	root := cli.NewRootCommand(o)
	if err := root.Execute(); err != nil {
		panic(err)
	}
}
Example (Operator)

A distribution's operator: its own API group, its own trust rules, the standard flags.

package main

import (
	"context"
	"flag"
	"fmt"
	"strings"

	ctrl "sigs.k8s.io/controller-runtime"

	"github.com/kuberoot-dev/kubepkg/pkg/operator"
	"github.com/kuberoot-dev/kubepkg/pkg/repo"
)

// allowedRegistries admits only versions whose charts come from the
// distribution's own registry.
type allowedRegistries struct{ prefix string }

func (allowedRegistries) AdmitIndex(context.Context, string, []byte, *repo.Index) error { return nil }

func (p allowedRegistries) AdmitVersion(_ context.Context, _, pkg string, v repo.Version) error {
	for _, variant := range v.Spec.Variants {
		for _, c := range variant.Components {
			if c.Chart != nil && !strings.HasPrefix(c.Chart.Repository, p.prefix) {
				return fmt.Errorf("%s %s: chart %s is not from %s", pkg, v.Version, c.Chart.Repository, p.prefix)
			}
		}
	}
	return nil
}

func main() {
	opts := operator.DefaultOptions()
	opts.Profile.Group = "packages.example.org"
	opts.Policy = allowedRegistries{prefix: "oci://registry.example.org/"}
	opts.BindFlags(flag.CommandLine)
	flag.Parse()
	if err := operator.Run(ctrl.SetupSignalHandler(), ctrl.GetConfigOrDie(), opts); err != nil {
		panic(err)
	}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArgoBackend

func ArgoBackend(env Env) (backend.Backend, controller.Preparer, error)

ArgoBackend installs through Argo CD: an Application per component.

func FluxBackend

func FluxBackend(env Env) (backend.Backend, controller.Preparer, error)

FluxBackend installs through Flux: an OCIRepository or HelmRepository and a HelmRelease per component.

func HelmBackend

func HelmBackend(env Env) (backend.Backend, controller.Preparer, error)

HelmBackend installs charts in-process with the Helm SDK.

func Run

func Run(ctx context.Context, cfg *rest.Config, o *Options) error

Run starts the operator and blocks until ctx is done.

func WerfBackend

func WerfBackend(env Env) (backend.Backend, controller.Preparer, error)

WerfBackend installs through Nelm, werf's deployment engine, with charts prepared as for the helm backend.

Types

type BackendFactory

type BackendFactory func(Env) (backend.Backend, controller.Preparer, error)

BackendFactory makes a backend and the preparer that puts charts where that backend reads them.

type Env

type Env struct {
	Config  *rest.Config
	Client  client.Client
	Options *Options
	// PlainHTTPRegistries are the registry hosts reached without TLS:
	// Options.PlainHTTPRegistries and those every Repository lists. It
	// changes while the operator runs.
	PlainHTTPRegistries *source.Registries
}

Env is what a backend factory gets to build its backend.

type Options

type Options struct {
	Profile controller.Profile
	// Backend names the entry of Backends to use.
	Backend  string
	Backends map[string]BackendFactory
	// AddToScheme registers extra types a backend needs.
	AddToScheme []func(*runtime.Scheme) error

	// IndexFetchers fetch repository indexes by URL scheme; Policy admits
	// indexes and versions. A distribution adds transports and trust
	// rules here.
	IndexFetchers repo.Fetchers
	Policy        repo.Policy

	// Mirror, an oci:// registry path, is where every chart and package
	// tree is fetched from instead of where it was published, for
	// air-gapped clusters; kubepkg bundle import fills it.
	Mirror string

	// Webhook settings for the image policy (Profile.ImagePolicy): the
	// Service in front of the operator, its namespace, the Secret for the
	// serving certificate and the MutatingWebhookConfiguration.
	WebhookPort                                                    int
	WebhookService, WebhookNamespace, WebhookSecret, WebhookConfig string

	// NelmBinary is the nelm executable (werf backend).
	NelmBinary string

	// ArgoNamespace and ArgoProject place Applications (argo backend).
	ArgoNamespace string
	ArgoProject   string

	CacheDir       string
	RegistryConfig string
	// RegistrySecrets are namespace/name of kubernetes.io/dockerconfigjson
	// Secrets with registry credentials, read on every pull and tried
	// before RegistryConfig.
	RegistrySecrets []string
	// PlainHTTPRegistries are registry hosts, host or host:port, reached
	// without TLS; Repositories add their own. Every other registry keeps
	// TLS.
	PlainHTTPRegistries []string
	// PlainHTTP reaches every registry without TLS. Deprecated: use
	// PlainHTTPRegistries, which does not take public registries off TLS
	// along with a local one.
	PlainHTTP bool

	MetricsAddr string
	ProbeAddr   string
	LeaderElect bool
	// PackageWorkers is how many packages are reconciled at once.
	PackageWorkers int
	// RetryBackoff is how long a change that failed waits before it is
	// tried again; each further failure doubles the wait, up to
	// RetryBackoffMax.
	RetryBackoff    time.Duration
	RetryBackoffMax time.Duration
	// LeaderElectionID defaults to <binary>.<group>.
	LeaderElectionID string
}

Options configure the operator.

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions are a plain kubepkg installation with the helm and flux backends.

func (*Options) BindFlags

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

BindFlags registers the standard flags. A distribution may bind them, some of them, or none and configure Options in code.

Jump to

Keyboard shortcuts

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