From 3d69c9f540873dc26420c8758b640d6fce184343 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Sun, 19 Nov 2023 23:08:34 +0200 Subject: [PATCH] Make runtime values optional Signed-off-by: Stefan Prodan --- api/v1alpha1/runtime.go | 2 +- cmd/timoni/bundle_delete_test.go | 1 - cmd/timoni/bundle_status_test.go | 1 - internal/engine/runtime_builder.go | 44 +++++++++++-------------- internal/engine/runtime_builder_test.go | 22 +++++++++++-- 5 files changed, 39 insertions(+), 31 deletions(-) diff --git a/api/v1alpha1/runtime.go b/api/v1alpha1/runtime.go index f1965ec2..e5bb1a28 100644 --- a/api/v1alpha1/runtime.go +++ b/api/v1alpha1/runtime.go @@ -65,7 +65,7 @@ import "strings" kubeContext!: string } - values: [...#RuntimeValue] + values?: [...#RuntimeValue] } ` diff --git a/cmd/timoni/bundle_delete_test.go b/cmd/timoni/bundle_delete_test.go index 47839399..9053ced3 100644 --- a/cmd/timoni/bundle_delete_test.go +++ b/cmd/timoni/bundle_delete_test.go @@ -233,7 +233,6 @@ runtime: { kubeContext: "envtest" } } - values: [] } ` diff --git a/cmd/timoni/bundle_status_test.go b/cmd/timoni/bundle_status_test.go index 64a1b16c..5833a951 100644 --- a/cmd/timoni/bundle_status_test.go +++ b/cmd/timoni/bundle_status_test.go @@ -224,7 +224,6 @@ runtime: { kubeContext: "envtest" } } - values: [] } ` diff --git a/internal/engine/runtime_builder.go b/internal/engine/runtime_builder.go index 5947b1bb..93f01622 100644 --- a/internal/engine/runtime_builder.go +++ b/internal/engine/runtime_builder.go @@ -145,34 +145,9 @@ func (b *RuntimeBuilder) GetRuntime(v cue.Value) (*apiv1.Runtime, error) { return nil, fmt.Errorf("lookup %s failed: %w", apiv1.RuntimeName.String(), runtimeNameValue.Err()) } - runtimeValuesCue := v.LookupPath(cue.ParsePath(apiv1.RuntimeValuesSelector.String())) - if runtimeValuesCue.Err() != nil { - return nil, fmt.Errorf("lookup %s failed: %w", apiv1.RuntimeValuesSelector.String(), runtimeValuesCue.Err()) - } - - runtimeValues := []apiv1.RuntimeValue{} - - err = runtimeValuesCue.Decode(&runtimeValues) - if err != nil { - return nil, fmt.Errorf("values decoding failed: %w", err) - } - - var refs []apiv1.RuntimeResourceRef - - for _, rv := range runtimeValues { - ref, err := rv.ToResourceRef() - if err != nil { - return nil, fmt.Errorf("value decoding failed: %w", err) - } - - refs = append(refs, *ref) - } - clusters := []apiv1.RuntimeCluster{} - clustersCue := v.LookupPath(cue.ParsePath(apiv1.RuntimeClustersSelector.String())) if clustersCue.Err() == nil { - iter, err := clustersCue.Fields(cue.Concrete(true)) if err != nil { return nil, err @@ -196,6 +171,25 @@ func (b *RuntimeBuilder) GetRuntime(v cue.Value) (*apiv1.Runtime, error) { } } + var refs []apiv1.RuntimeResourceRef + runtimeValuesCue := v.LookupPath(cue.ParsePath(apiv1.RuntimeValuesSelector.String())) + if runtimeValuesCue.Err() == nil { + runtimeValues := []apiv1.RuntimeValue{} + err = runtimeValuesCue.Decode(&runtimeValues) + if err != nil { + return nil, fmt.Errorf("values decoding failed: %w", err) + } + + for _, rv := range runtimeValues { + ref, err := rv.ToResourceRef() + if err != nil { + return nil, fmt.Errorf("value decoding failed: %w", err) + } + + refs = append(refs, *ref) + } + } + return &apiv1.Runtime{ Name: runtimeName, Clusters: clusters, diff --git a/internal/engine/runtime_builder_test.go b/internal/engine/runtime_builder_test.go index cbf21b9f..27740528 100644 --- a/internal/engine/runtime_builder_test.go +++ b/internal/engine/runtime_builder_test.go @@ -26,7 +26,24 @@ import ( apiv1 "github.com/stefanprodan/timoni/api/v1alpha1" ) -func TestGetRuntime(t *testing.T) { +func TestRuntimeBuilder_Minimal(t *testing.T) { + g := NewWithT(t) + ctx := cuecontext.New() + + rt := ` +runtime: { + apiVersion: "v1alpha1" + name: "minimal" +} +` + v := ctx.CompileString(rt) + builder := NewRuntimeBuilder(ctx, []string{}) + b, err := builder.GetRuntime(v) + g.Expect(err).ToNot(HaveOccurred()) + g.Expect(b.Name).To(BeEquivalentTo("minimal")) +} + +func TestRuntimeBuilder_Values(t *testing.T) { g := NewWithT(t) ctx := cuecontext.New() @@ -82,7 +99,7 @@ runtime: { g.Expect(b.Refs[2].Namespace).To(BeEmpty()) } -func TestGetRuntimeClusters(t *testing.T) { +func TestRuntimeBuilder_Clusters(t *testing.T) { g := NewWithT(t) ctx := cuecontext.New() @@ -108,7 +125,6 @@ runtime: { kubeContext: "us-west-1:production" } } - values: [] } ` v := ctx.CompileString(rt)