Skip to content

Commit

Permalink
Make runtime values optional
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <[email protected]>
  • Loading branch information
stefanprodan committed Nov 19, 2023
1 parent dfe10a1 commit 3d69c9f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import "strings"
kubeContext!: string
}
values: [...#RuntimeValue]
values?: [...#RuntimeValue]
}
`

Expand Down
1 change: 0 additions & 1 deletion cmd/timoni/bundle_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ runtime: {
kubeContext: "envtest"
}
}
values: []
}
`

Expand Down
1 change: 0 additions & 1 deletion cmd/timoni/bundle_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ runtime: {
kubeContext: "envtest"
}
}
values: []
}
`

Expand Down
44 changes: 19 additions & 25 deletions internal/engine/runtime_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
22 changes: 19 additions & 3 deletions internal/engine/runtime_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -108,7 +125,6 @@ runtime: {
kubeContext: "us-west-1:production"
}
}
values: []
}
`
v := ctx.CompileString(rt)
Expand Down

0 comments on commit 3d69c9f

Please sign in to comment.