Skip to content

Commit

Permalink
feat: form template data api and merge (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasepe authored Feb 29, 2024
1 parent e3d13f6 commit 5581368
Show file tree
Hide file tree
Showing 15 changed files with 2,310 additions and 88 deletions.
8 changes: 8 additions & 0 deletions apis/ui/formtemplates/v1alpha1/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package v1alpha1

import (
"fmt"
"strings"

"github.com/krateoplatformops/krateo-bff/apis/core"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -11,6 +14,11 @@ type DataItem struct {
Value string `json:"value"`
}

func (di *DataItem) String() string {
return fmt.Sprintf("%s=%s",
strings.TrimSpace(di.Path), strings.TrimSpace(di.Value))
}

type FormTemplateSpec struct {
// DefinitionRef: reference to FormDefintion
DefinitionRef *core.Reference `json:"definitionRef"`
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/itchyny/gojq v0.12.13
github.com/krateoplatformops/provider-runtime v0.7.0
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.31.0
k8s.io/api v0.29.2
k8s.io/apimachinery v0.29.2
Expand All @@ -16,6 +17,7 @@ require (
k8s.io/utils v0.0.0-20240102154912-e7106e64919e
sigs.k8s.io/controller-runtime v0.17.0
sigs.k8s.io/controller-tools v0.13.0
sigs.k8s.io/yaml v1.4.0
)

require (
Expand Down Expand Up @@ -43,7 +45,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/mod v0.14.0 // indirect
Expand All @@ -64,5 +65,4 @@ require (
k8s.io/kube-openapi v0.0.0-20240126223410-2919ad4fcfec // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
67 changes: 5 additions & 62 deletions internal/kubernetes/widgets/formtemplates/evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@ package evaluator

import (
"context"
"fmt"

"github.com/krateoplatformops/krateo-bff/apis/ui/formtemplates/v1alpha1"
"github.com/krateoplatformops/krateo-bff/internal/kubernetes/dynamic"
"github.com/krateoplatformops/krateo-bff/internal/kubernetes/formdefinitions"
formdefinitionsutil "github.com/krateoplatformops/krateo-bff/internal/kubernetes/formdefinitions/util"
"github.com/krateoplatformops/krateo-bff/internal/tmpl"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
)

Expand All @@ -24,7 +17,6 @@ type EvalOptions struct {
RESTConfig *rest.Config
AuthnNS string
Subject string
Groups []string
}

func Eval(ctx context.Context, in *v1alpha1.FormTemplate, opts EvalOptions) error {
Expand All @@ -40,61 +32,12 @@ func Eval(ctx context.Context, in *v1alpha1.FormTemplate, opts EvalOptions) erro
tpl: tpl,
apiList: in.Spec.APIList,
})
_ = ds // TODO

formdefinitionsClient, err := formdefinitions.NewClient(opts.RESTConfig)
if err != nil {
return err
}

ref, err := formdefinitionsClient.Namespace(in.Spec.DefinitionRef.Namespace).
Get(ctx, in.Spec.DefinitionRef.Name)
if err != nil {
return err
}

dyn, err := dynamic.NewGetter(opts.RESTConfig)
if err != nil {
return err
}

src, err := dyn.Get(ctx, dynamic.GetOptions{
GVK: schema.GroupVersionKind{
Group: ref.Spec.Schema.Group,
Version: ref.Spec.Schema.Version,
Kind: ref.Spec.Schema.Kind,
},
Namespace: ref.Namespace,
Name: ref.Name,
})
if err != nil {
return err
}

gr := formdefinitionsutil.InferGroupResource(ref)

crd, err := dyn.Get(ctx, dynamic.GetOptions{
GVK: schema.GroupVersionKind{
Group: "apiextensions.k8s.io",
Version: "v1",
Kind: "CustomResourceDefinition",
},
Name: gr.String(),
})
if err != nil {
return err
}

sch, err := dynamic.Extract(ctx, crd, fmt.Sprintf(filter, ref.Spec.Schema.Version))
if err != nil {
return err
}

in.Status.Content = &v1alpha1.FormTemplateStatusContent{
Instance: &runtime.RawExtension{Object: src},
Schema: &runtime.RawExtension{Object: &unstructured.Unstructured{
Object: sch.(map[string]any),
}},
for _, el := range in.Spec.Data {
el.Value, err = tpl.Execute(el.Value, ds)
if err != nil {
return err
}
}

return nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@
//go:build integration
// +build integration

package evaluator

import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"

"github.com/krateoplatformops/krateo-bff/internal/kubernetes/widgets/formtemplates"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

func TestIteratorCount(t *testing.T) {
func TestEval(t *testing.T) {
cfg, err := newRestConfig()
if err != nil {
t.Fatal(err)
}

cli, err := formtemplates.NewClient(cfg)
if err != nil {
t.Fatal(err)
}

namespace := "demo-system"
name := "fireworksapp-with-api"

res, err := cli.Namespace(namespace).Get(context.TODO(), name)
if err != nil {
t.Fatal(err)
}

err = Eval(context.TODO(), res, EvalOptions{
RESTConfig: cfg,
AuthnNS: namespace,
Subject: "",
})
if err != nil {
t.Fatal(err)
}

//fin, _ := os.Create("ppp.json")
//defer fin.Close()
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(res)
}

func newRestConfig() (*rest.Config, error) {
Expand Down
17 changes: 3 additions & 14 deletions internal/kubernetes/widgets/formtemplates/formtemplates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"testing"

"github.com/krateoplatformops/krateo-bff/internal/kubernetes/widgets/formtemplates"
"github.com/krateoplatformops/krateo-bff/internal/kubernetes/widgets/formtemplates/evaluator"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -84,21 +83,11 @@ func TestFormTemplatePlain(t *testing.T) {
t.Fatal(err)
}

err = evaluator.Eval(context.TODO(), res, evaluator.EvalOptions{
RESTConfig: cfg,
AuthnNS: namespace,
Subject: "",
})
if err != nil {
t.Fatal(err)
}

fin, _ := os.Create("ppp.json")
defer fin.Close()
enc := json.NewEncoder(fin)
//fin, _ := os.Create("ppp.json")
//defer fin.Close()
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(res)

}

func newRestConfig() (*rest.Config, error) {
Expand Down
27 changes: 27 additions & 0 deletions internal/kubernetes/widgets/formtemplates/merger/merger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package merger

import (
"fmt"
"strings"

formtemplatesv1alpha1 "github.com/krateoplatformops/krateo-bff/apis/ui/formtemplates/v1alpha1"
"github.com/krateoplatformops/krateo-bff/internal/strvals"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func Merge(src *formtemplatesv1alpha1.FormTemplate, dst *unstructured.Unstructured) error {
lines := make([]string, len(src.Spec.Data))
for i, di := range src.Spec.Data {
lines[i] = di.String()
}

values := strings.Join(lines, ",")
fmt.Println(values)

err := strvals.ParseInto(values, dst.UnstructuredContent())
if err != nil {
return err
}

return nil //unstructured.SetNestedMap(dst.Object, spec, "spec")
}
78 changes: 78 additions & 0 deletions internal/kubernetes/widgets/formtemplates/merger/merger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package merger

import (
"context"
"encoding/json"
"io"
"os"
"path/filepath"
"testing"

"github.com/krateoplatformops/krateo-bff/internal/kubernetes/dynamic"
"github.com/krateoplatformops/krateo-bff/internal/kubernetes/widgets/formtemplates"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

func TestMerge(t *testing.T) {
ctx := context.TODO()
namespace := "demo-system"
name := "fireworksapp"

cfg, err := newRestConfig()
if err != nil {
t.Fatal(err)
}

dyn, err := dynamic.NewGetter(cfg)
if err != nil {
t.Fatal(err)
}

dst, err := dyn.Get(ctx, dynamic.GetOptions{
GVK: schema.GroupVersionKind{
Group: "apps.krateo.io",
Version: "v1alpha1",
Kind: "FireworksappForm",
},
Namespace: namespace,
Name: name,
})
if err != nil {
t.Fatal(err)
}

cli, err := formtemplates.NewClient(cfg)
if err != nil {
t.Fatal(err)
}

src, err := cli.Namespace(namespace).Get(ctx, name)
if err != nil {
t.Fatal(err)
}

if err := Merge(src, dst); err != nil {
t.Fatal(err)
}

dump(os.Stdout, dst)
}

func newRestConfig() (*rest.Config, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}

return clientcmd.BuildConfigFromFlags("", filepath.Join(home, ".kube", "config"))
}

func dump(w io.Writer, v any) {
//fin, _ := os.Create("ppp.json")
//defer fin.Close()
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
enc.Encode(v)
}
Loading

0 comments on commit 5581368

Please sign in to comment.