defaulting

package
v0.18.4 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package defaulting implements tools to perform defaulting in data objects.

These might be used from the CLI and/or the controller.

This package shoul not, under any circumtance, include specific defaulting logic. Only the tools to operate that logic should live here.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Default

type Default[O any] func(ctx context.Context, obj O) (O, error)

Default is the logic for a default for a type O. It should return a value of O whether it updates it or not. When there is an error, return the zero value of O and the error.

type Runner

type Runner[O any] struct {
	// contains filtered or unexported fields
}

Runner allows to compose and run validations/defaults.

func NewRunner

func NewRunner[O any]() *Runner[O]

NewRunner constructs a new Runner.

func (*Runner[O]) Register

func (r *Runner[O]) Register(defaults ...Default[O])

Register adds defaults to the Runner.

func (*Runner[O]) RunAll

func (r *Runner[O]) RunAll(ctx context.Context, obj O) (O, errors.Aggregate)

RunAll runs all defaults sequentially and returns the updated O. When there are errors, it returns the zero value of O and the aggregated errors.

Example
package main

import (
	"context"
	"fmt"

	"github.com/aws/eks-anywhere/internal/test"
	anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
	"github.com/aws/eks-anywhere/pkg/cluster"
	"github.com/aws/eks-anywhere/pkg/defaulting"
	eksaerrors "github.com/aws/eks-anywhere/pkg/errors"
)

func main() {
	r := defaulting.NewRunner[cluster.Spec]()

	r.Register(
		func(ctx context.Context, spec cluster.Spec) (cluster.Spec, error) {
			if spec.Cluster.Spec.KubernetesVersion == "" {
				spec.Cluster.Spec.KubernetesVersion = anywherev1.Kube124
			}
			return spec, nil
		},
		func(ctx context.Context, spec cluster.Spec) (cluster.Spec, error) {
			if spec.Cluster.Spec.ControlPlaneConfiguration.Count == 0 {
				spec.Cluster.Spec.ControlPlaneConfiguration.Count = 3
			}
			return spec, nil
		},
	)

	ctx := context.Background()
	spec := test.NewClusterSpec(func(s *cluster.Spec) {
		s.Cluster.Spec.KubernetesVersion = "1.24"
		s.Cluster.Spec.ControlPlaneConfiguration.Count = 5
	})
	updatedSpec, agg := r.RunAll(ctx, *spec)
	if agg != nil {
		printErrors(agg)
		return
	}

	fmt.Println("Cluster config is valid")
	fmt.Printf("Cluster is for kube version: %s\n", updatedSpec.Cluster.Spec.KubernetesVersion)
	fmt.Printf("Cluster CP replicas is: %d\n", updatedSpec.Cluster.Spec.ControlPlaneConfiguration.Count)

}

func printErrors(agg eksaerrors.Aggregate) {
	fmt.Println("Failed assigning cluster spec defaults")
	for _, err := range agg.Errors() {
		msg := "- " + err.Error()
		fmt.Println(msg)
	}
}
Output:

Cluster config is valid
Cluster is for kube version: 1.24
Cluster CP replicas is: 5

Jump to

Keyboard shortcuts

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