...

Source file src/edge-infra.dev/third_party/k8s/fluxcd/fluxcd.go

Documentation: edge-infra.dev/third_party/k8s/fluxcd

     1  // Package fluxcd provides access to the embedded vendored manifests for
     2  // installing FluxCD to K8s clusters. This package only provides a single
     3  // function for loading the embedded manifests, and all other functionality
     4  // should be implemented in the appropriate `pkg/` directory.
     5  package fluxcd
     6  
     7  import (
     8  	"embed"
     9  	"fmt"
    10  
    11  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    12  	"sigs.k8s.io/kustomize/api/krusty"
    13  	ktypes "sigs.k8s.io/kustomize/api/types"
    14  
    15  	"edge-infra.dev/pkg/k8s/decoder"
    16  	"edge-infra.dev/pkg/k8s/kustomize"
    17  )
    18  
    19  //go:embed manifests/*.yaml
    20  //go:embed base/**/*.yaml
    21  //go:embed base/*.yaml
    22  //go:embed kustomize-helm-controllers/*.yaml
    23  var fluxcdManifests embed.FS
    24  
    25  // private rendering options struct.  rendering options should be set by passing
    26  // public Options to `LoadManifests()`
    27  type options struct {
    28  	// what would typically be passed to `kustomize build`
    29  	kustomizeBuildTarget string
    30  }
    31  
    32  // Option is the public functional option interface
    33  type Option func(*options)
    34  
    35  // OnlyKustomizeController will cause only fluxcd/kustomize-controller and
    36  // its dependencies to be rendered.
    37  func OnlyKustomizeController() func(*options) {
    38  	return func(o *options) {
    39  		o.kustomizeBuildTarget = "kustomize-controller"
    40  	}
    41  }
    42  
    43  // LoadManifests reads the manifests from the embedded byte mapping containing
    44  // vendored FluxCD installation manifests and our Kustomize patches + targets,
    45  // runs 'kustomize build' based on the provided options and then decodes the
    46  // data into K8s objects that can be applied to the K8s API using controller-runtime's
    47  // client.
    48  // If no options are provided, it will render the fluxcd/helm-controller and
    49  // fluxcd/kustomize-controller and their dependencies, as they are the only
    50  // FluxCD components used in Edge.
    51  func LoadManifests(opts ...Option) ([]*unstructured.Unstructured, error) {
    52  	o := &options{kustomizeBuildTarget: "kustomize-helm-controllers"}
    53  	for _, opt := range opts {
    54  		opt(o)
    55  	}
    56  
    57  	ops := krusty.MakeDefaultOptions()
    58  	ops.LoadRestrictions = ktypes.LoadRestrictionsNone
    59  	k := krusty.MakeKustomizer(
    60  		ops,
    61  	)
    62  
    63  	m, err := k.Run(&kustomize.FS{FS: fluxcdManifests}, o.kustomizeBuildTarget)
    64  	if err != nil {
    65  		return nil, fmt.Errorf("failed to execute kustomize build: %w", err)
    66  	}
    67  
    68  	yaml, err := m.AsYaml()
    69  	if err != nil {
    70  		return nil, fmt.Errorf("failed to convert kustomize build result into bytes: %w", err)
    71  	}
    72  
    73  	return decoder.DecodeYAML(yaml)
    74  }
    75  

View as plain text