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 ¶
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 (*Runner[O]) RunAll ¶
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