// Package fluxcd provides access to the embedded vendored manifests for // installing FluxCD to K8s clusters. This package only provides a single // function for loading the embedded manifests, and all other functionality // should be implemented in the appropriate `pkg/` directory. package fluxcd import ( "embed" "fmt" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "sigs.k8s.io/kustomize/api/krusty" ktypes "sigs.k8s.io/kustomize/api/types" "edge-infra.dev/pkg/k8s/decoder" "edge-infra.dev/pkg/k8s/kustomize" ) //go:embed manifests/*.yaml //go:embed base/**/*.yaml //go:embed base/*.yaml //go:embed kustomize-helm-controllers/*.yaml var fluxcdManifests embed.FS // private rendering options struct. rendering options should be set by passing // public Options to `LoadManifests()` type options struct { // what would typically be passed to `kustomize build` kustomizeBuildTarget string } // Option is the public functional option interface type Option func(*options) // OnlyKustomizeController will cause only fluxcd/kustomize-controller and // its dependencies to be rendered. func OnlyKustomizeController() func(*options) { return func(o *options) { o.kustomizeBuildTarget = "kustomize-controller" } } // LoadManifests reads the manifests from the embedded byte mapping containing // vendored FluxCD installation manifests and our Kustomize patches + targets, // runs 'kustomize build' based on the provided options and then decodes the // data into K8s objects that can be applied to the K8s API using controller-runtime's // client. // If no options are provided, it will render the fluxcd/helm-controller and // fluxcd/kustomize-controller and their dependencies, as they are the only // FluxCD components used in Edge. func LoadManifests(opts ...Option) ([]*unstructured.Unstructured, error) { o := &options{kustomizeBuildTarget: "kustomize-helm-controllers"} for _, opt := range opts { opt(o) } ops := krusty.MakeDefaultOptions() ops.LoadRestrictions = ktypes.LoadRestrictionsNone k := krusty.MakeKustomizer( ops, ) m, err := k.Run(&kustomize.FS{FS: fluxcdManifests}, o.kustomizeBuildTarget) if err != nil { return nil, fmt.Errorf("failed to execute kustomize build: %w", err) } yaml, err := m.AsYaml() if err != nil { return nil, fmt.Errorf("failed to convert kustomize build result into bytes: %w", err) } return decoder.DecodeYAML(yaml) }