-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: form template data api and merge (#16)
- Loading branch information
Showing
15 changed files
with
2,310 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 38 additions & 1 deletion
39
internal/kubernetes/widgets/formtemplates/evaluator/evaluator_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
internal/kubernetes/widgets/formtemplates/merger/merger.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
78
internal/kubernetes/widgets/formtemplates/merger/merger_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.