function

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: MIT Imports: 14 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInputNotFound = errors.New("input not found")
View Source
var ErrInputReadingFailed = errors.New("input reading failed")
View Source
var Scheme = scheme.Scheme

Functions

func AddCustomInputType added in v0.1.5

func AddCustomInputType[Resource client.Object, Custom any](bind func(Resource) (Custom, error))

AddCustomInputType allows types that do not implement client.Object to be used as fields of Inputs structs.

Example
type myType struct {
	SecretKey string
}

AddCustomInputType(func(in *corev1.Secret) (*myType, error) {
	return &myType{
		SecretKey: string(in.Data["key"]),
	}, nil
})

type exampleInputs struct {
	CustomInput *myType `eno_key:"test-secret"`
}

fn := func(inputs exampleInputs) ([]client.Object, error) {
	output := &corev1.Pod{}
	output.Name = string(inputs.CustomInput.SecretKey)
	return []client.Object{output}, nil
}

ir := newTestInputReader()
main(fn, &mainConfig{}, ir, NewDefaultOutputWriter())
Output:

{"apiVersion":"config.kubernetes.io/v1","kind":"ResourceList","items":[{"apiVersion":"v1","kind":"Pod","metadata":{"name":"foobar\n"},"spec":{"containers":null},"status":{}}]}
Example (Map)
type myType struct {
	SecretKey string
}

AddCustomInputType(func(in *corev1.Secret) (map[string]*myType, error) {
	m := make(map[string]*myType, len(in.Data))
	for k, v := range in.Data {
		m[k] = &myType{
			SecretKey: string(v),
		}
	}

	return m, nil
})

type exampleInputs struct {
	CustomInputs map[string]*myType `eno_key:"test-secret"`
}

fn := func(inputs exampleInputs) ([]client.Object, error) {
	output := &corev1.Pod{}
	output.Name = string(inputs.CustomInputs["key"].SecretKey)
	return []client.Object{output}, nil
}

ir := newTestInputReader()
main(fn, &mainConfig{}, ir, NewDefaultOutputWriter())
Output:

{"apiVersion":"config.kubernetes.io/v1","kind":"ResourceList","items":[{"apiVersion":"v1","kind":"Pod","metadata":{"name":"foobar\n"},"spec":{"containers":null},"status":{}}]}
Example (Slice)
type myType struct {
	SecretKey string
}

AddCustomInputType(func(in *corev1.Secret) ([]*myType, error) {
	return []*myType{{
		SecretKey: string(in.Data["key"]),
	}}, nil
})

type exampleInputs struct {
	CustomInputs []*myType `eno_key:"test-secret"`
}

fn := func(inputs exampleInputs) ([]client.Object, error) {
	output := &corev1.Pod{}
	output.Name = string(inputs.CustomInputs[0].SecretKey)
	return []client.Object{output}, nil
}

ir := newTestInputReader()
main(fn, &mainConfig{}, ir, NewDefaultOutputWriter())
Output:

{"apiVersion":"config.kubernetes.io/v1","kind":"ResourceList","items":[{"apiVersion":"v1","kind":"Pod","metadata":{"name":"foobar\n"},"spec":{"containers":null},"status":{}}]}

func Main added in v0.1.4

func Main[T Inputs](fn SynthFunc[T], opts ...Option)

Main is the entrypoint for Eno synthesizer processes written using the framework defined by this package.

Example
fn := func(inputs struct{}) ([]client.Object, error) {
	output := &corev1.Pod{}
	output.Name = "test-pod"
	return []client.Object{output}, nil
}

Main(fn)
Output:

{"apiVersion":"config.kubernetes.io/v1","kind":"ResourceList","items":[{"apiVersion":"v1","kind":"Pod","metadata":{"name":"test-pod"},"spec":{"containers":null},"status":{}}]}
Example (WithMungers)
// Example using precreted mungers
fn := func(inputs struct{}) ([]client.Object, error) {
	output := &corev1.Pod{}
	output.Name = "test-pod"
	return []client.Object{output}, nil
}

//stdout of main will be compared with output comment below becausse this is an example
Main(fn, WithManagedByEno(), WithReconcilationInterval(time.Minute))
Output:

{"apiVersion":"config.kubernetes.io/v1","kind":"ResourceList","items":[{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{"eno.azure.io/reconcile-interval":"1m0s"},"labels":{"app.kubernetes.io/managed-by":"Eno"},"name":"test-pod"},"spec":{"containers":null},"status":{}}]}

func ReadInput

func ReadInput[T client.Object](ir *InputReader, key string, out T) error

func ReadManifest added in v0.1.7

func ReadManifest(path string) ([]client.Object, error)

ReadManifest reads a YAML file from disk and parses each document into an unstructured object.

Types

type InputReader

type InputReader struct {
	// contains filtered or unexported fields
}

func NewDefaultInputReader

func NewDefaultInputReader() (*InputReader, error)

func NewInputReader

func NewInputReader(r io.Reader) (*InputReader, error)

func (*InputReader) All added in v0.0.15

func (*InputReader) IsOptional added in v0.2.1

func (ir *InputReader) IsOptional(key string) bool

IsOptional returns true if the input with the given key is marked as optional. This is determined by checking the FunctionConfig.optionalRefs list which contains all optional ref keys from the synthesizer spec.

type Inputs added in v0.1.4

type Inputs interface{}

Inputs is satisfied by any struct that defines the inputs required by a SynthFunc. Use the `eno_key` struct tag to specify the corresponding ref key for each input. Each field must either be a client.Object or a custom type registered with AddCustomInputType.

Example
type exampleInputs struct {
	MySecret *corev1.Secret `eno_key:"test-secret"`
}

fn := func(inputs exampleInputs) ([]client.Object, error) {
	output := &corev1.Pod{}
	output.Name = string(inputs.MySecret.Data["key"])
	return []client.Object{output}, nil
}

ir := newTestInputReader()
main(fn, &mainConfig{}, ir, NewDefaultOutputWriter())
Output:

{"apiVersion":"config.kubernetes.io/v1","kind":"ResourceList","items":[{"apiVersion":"v1","kind":"Pod","metadata":{"name":"foobar\n"},"spec":{"containers":null},"status":{}}]}

type MungableInputs added in v0.1.24

type MungableInputs interface {
	Munge() error
}

MungableInputs is an optional interface that can be implemented by Inputs structs if it is, it gets called after inputs are read. It can fail the whole Synthesis.

type MungeFunc added in v0.0.19

type MungeFunc func(*unstructured.Unstructured)

type Option added in v0.1.22

type Option func(*mainConfig)

option defines an option for configuring/augmenting the Main function.

func WithManagedByEno added in v0.1.22

func WithManagedByEno() Option

WithManagedByEno returns an iption that annotates the given Kubernetes object to indicate that it is managed by Eno. It sets the "eno.azure.io/managed-by" annotation to the Eno controller identifier.

func WithMunger added in v0.1.22

func WithMunger(m MungeFunc) Option

WithMunger adds a munge function that will be applied to each output object. Multiple munge functions can be provided and they will be applied in order.

Example usage:

Main(synthesizer,
	WithMunger(func(obj *unstructured.Unstructured) {
		// Add common labels
		labels := obj.GetLabels()
		if labels == nil {
			labels = make(map[string]string)
		}
		labels["app.kubernetes.io/managed-by"] = "eno"
		obj.SetLabels(labels)
	}),
	WithMunger(func(obj *unstructured.Unstructured) {
		// Add environment-specific annotations
		annotations := obj.GetAnnotations()
		if annotations == nil {
			annotations = make(map[string]string)
		}
		annotations["eno.azure.io/reconcile-interval"] = "1m"
		obj.SetAnnotations(annotations)
	}),
)

func WithReconcilationInterval added in v0.1.22

func WithReconcilationInterval(interval time.Duration) Option

WithReconcilationInterval returns an option that annotates the given Kubernetes object to configure its reconciliation interval. It sets the "eno.azure.io/reconcile-interval" annotation to the provided duration string, which controls how frequently Eno will reconcile the resource.

func WithScheme added in v0.1.31

func WithScheme(scheme *runtime.Scheme) Option

WithScheme configures the runtime.Scheme used for automatic type metadata inference.

The scheme enables automatic population of TypeMeta (apiVersion and kind) fields for Kubernetes objects based on their Go types. When an object is processed and its GroupVersionKind is empty, the scheme will be consulted to determine the appropriate apiVersion and kind values.

If no scheme is provided, or if an object's type is not registered in the scheme, the object will be processed without modification.

Example usage:

scheme := runtime.NewScheme()
corev1.AddToScheme(scheme)
appsv1.AddToScheme(scheme)
Main(synthesizer, WithScheme(scheme))

type OutputWriter

type OutputWriter struct {
	// contains filtered or unexported fields
}

func NewDefaultOutputWriter

func NewDefaultOutputWriter() *OutputWriter

func NewOutputWriter

func NewOutputWriter(w io.Writer, munge MungeFunc) *OutputWriter

func (*OutputWriter) Add

func (w *OutputWriter) Add(outs ...client.Object) error

func (*OutputWriter) AddResult added in v0.1.4

func (w *OutputWriter) AddResult(result *krmv1.Result)

func (*OutputWriter) Write

func (w *OutputWriter) Write() error

type SynthFunc added in v0.1.4

type SynthFunc[T Inputs] func(inputs T) ([]client.Object, error)

SynthFunc defines a synthesizer function that takes a set of inputs and returns a list of objects.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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