diff --git a/apis/apis.go b/apis/apis.go index 30cfcf4..1d9bcc3 100644 --- a/apis/apis.go +++ b/apis/apis.go @@ -1,8 +1,8 @@ package apis import ( - cardtemplatev1alpha1 "github.com/krateoplatformops/krateo-bff/apis/ui/cardtemplates/v1alpha1" - columnv1alpha1 "github.com/krateoplatformops/krateo-bff/apis/ui/column/v1alpha1" + cardtemplatesv1alpha1 "github.com/krateoplatformops/krateo-bff/apis/ui/cardtemplates/v1alpha1" + columnsv1alpha1 "github.com/krateoplatformops/krateo-bff/apis/ui/columns/v1alpha1" "k8s.io/apimachinery/pkg/runtime" ) @@ -16,7 +16,7 @@ func AddToScheme(s *runtime.Scheme) error { func init() { AddToSchemes = append(AddToSchemes, - cardtemplatev1alpha1.SchemeBuilder.AddToScheme, - columnv1alpha1.SchemeBuilder.AddToScheme, + cardtemplatesv1alpha1.SchemeBuilder.AddToScheme, + columnsv1alpha1.SchemeBuilder.AddToScheme, ) } diff --git a/apis/ui/column/v1alpha1/group_version_info.go b/apis/ui/columns/v1alpha1/group_version_info.go similarity index 100% rename from apis/ui/column/v1alpha1/group_version_info.go rename to apis/ui/columns/v1alpha1/group_version_info.go diff --git a/apis/ui/column/v1alpha1/types.go b/apis/ui/columns/v1alpha1/types.go similarity index 100% rename from apis/ui/column/v1alpha1/types.go rename to apis/ui/columns/v1alpha1/types.go diff --git a/apis/ui/column/v1alpha1/zz_generated.deepcopy.go b/apis/ui/columns/v1alpha1/zz_generated.deepcopy.go similarity index 100% rename from apis/ui/column/v1alpha1/zz_generated.deepcopy.go rename to apis/ui/columns/v1alpha1/zz_generated.deepcopy.go diff --git a/internal/kubernetes/layout/columns/columns.go b/internal/kubernetes/layout/columns/columns.go new file mode 100644 index 0000000..c88ab2c --- /dev/null +++ b/internal/kubernetes/layout/columns/columns.go @@ -0,0 +1,170 @@ +package columns + +import ( + "context" + "time" + + "github.com/krateoplatformops/krateo-bff/apis" + "github.com/krateoplatformops/krateo-bff/apis/ui/columns/v1alpha1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/runtime/serializer" + "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/watch" + "k8s.io/client-go/rest" +) + +const ( + resourceName = "columns" +) + +func NewClient(rc *rest.Config) (*Client, error) { + s := runtime.NewScheme() + apis.AddToScheme(s) + + config := *rc + config.APIPath = "/apis" + config.GroupVersion = &schema.GroupVersion{ + Group: v1alpha1.Group, Version: v1alpha1.Version, + } + config.NegotiatedSerializer = serializer.NewCodecFactory(s). + WithoutConversion() + config.UserAgent = rest.DefaultKubernetesUserAgent() + + cli, err := rest.RESTClientFor(&config) + if err != nil { + return nil, err + } + + pc := runtime.NewParameterCodec(s) + + return &Client{rc: cli, pc: pc}, nil +} + +type Client struct { + rc rest.Interface + pc runtime.ParameterCodec + ns string +} + +func (c *Client) Namespace(ns string) *Client { + c.ns = ns + return c +} + +func (c *Client) Get(ctx context.Context, name string) (result *v1alpha1.Column, err error) { + result = &v1alpha1.Column{} + err = c.rc.Get(). + Namespace(c.ns). + Resource(resourceName). + Name(name). + Do(ctx). + Into(result) + // issue: https://github.com/kubernetes/client-go/issues/541 + result.SetGroupVersionKind(v1alpha1.ColumnGroupVersionKind) + return +} + +func (c *Client) List(ctx context.Context, opts metav1.ListOptions) (result *v1alpha1.ColumnList, err error) { + var timeout time.Duration + if opts.TimeoutSeconds != nil { + timeout = time.Duration(*opts.TimeoutSeconds) * time.Second + } + result = &v1alpha1.ColumnList{} + err = c.rc.Get(). + Namespace(c.ns). + Resource(resourceName). + VersionedParams(&opts, c.pc). + Timeout(timeout). + Do(ctx). + Into(result) + + result.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("List")) + return +} + +func (c *Client) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) { + var timeout time.Duration + if opts.TimeoutSeconds != nil { + timeout = time.Duration(*opts.TimeoutSeconds) * time.Second + } + opts.Watch = true + return c.rc.Get(). + Namespace(c.ns). + Resource(resourceName). + VersionedParams(&opts, c.pc). + Timeout(timeout). + Watch(ctx) +} + +func (c *Client) Create(ctx context.Context, obj *v1alpha1.Column, opts metav1.CreateOptions) (result *v1alpha1.Column, err error) { + result = &v1alpha1.Column{} + err = c.rc.Post(). + Namespace(c.ns). + Resource(resourceName). + VersionedParams(&opts, c.pc). + Body(obj). + Do(ctx). + Into(result) + // issue: https://github.com/kubernetes/client-go/issues/541 + result.SetGroupVersionKind(v1alpha1.ColumnGroupVersionKind) + return +} + +func (c *Client) Update(ctx context.Context, obj *v1alpha1.Column, opts metav1.UpdateOptions) (result *v1alpha1.Column, err error) { + result = &v1alpha1.Column{} + err = c.rc.Put(). + Namespace(c.ns). + Resource(resourceName). + Name(obj.Name). + VersionedParams(&opts, c.pc). + Body(obj). + Do(ctx). + Into(result) + // issue: https://github.com/kubernetes/client-go/issues/541 + result.SetGroupVersionKind(v1alpha1.ColumnGroupVersionKind) + return +} + +func (c *Client) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error { + return c.rc.Delete(). + Namespace(c.ns). + Resource(resourceName). + Name(name). + Body(&opts). + Do(ctx). + Error() +} + +func (c *Client) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error { + var timeout time.Duration + if listOpts.TimeoutSeconds != nil { + timeout = time.Duration(*listOpts.TimeoutSeconds) * time.Second + } + return c.rc.Delete(). + Namespace(c.ns). + Resource(resourceName). + VersionedParams(&listOpts, c.pc). + Timeout(timeout). + Body(&opts). + Do(ctx). + Error() +} + +func (c *Client) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (result *v1alpha1.Column, err error) { + result = &v1alpha1.Column{} + err = c.rc.Patch(pt). + Namespace(c.ns). + Resource(resourceName). + Name(name). + SubResource(subresources...). + VersionedParams(&opts, c.pc). + Body(data). + Do(ctx). + Into(result) + // issue: https://github.com/kubernetes/client-go/issues/541 + result.SetGroupVersionKind(v1alpha1.ColumnGroupVersionKind) + return +} diff --git a/internal/kubernetes/layout/columns/columns_test.go b/internal/kubernetes/layout/columns/columns_test.go new file mode 100644 index 0000000..9116245 --- /dev/null +++ b/internal/kubernetes/layout/columns/columns_test.go @@ -0,0 +1,77 @@ +//go:build integration +// +build integration + +package columns_test + +import ( + "context" + "encoding/json" + "os" + "path/filepath" + "testing" + + "github.com/krateoplatformops/krateo-bff/internal/kubernetes/layout/columns" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" +) + +const ( + namespace = "dev-system" + name = "sample" +) + +func TestColumnGet(t *testing.T) { + cfg, err := newRestConfig() + if err != nil { + t.Fatal(err) + } + + cli, err := columns.NewClient(cfg) + if err != nil { + t.Fatal(err) + } + + res, err := cli.Namespace(namespace).Get(context.TODO(), name) + if err != nil { + t.Fatal(err) + } + + enc := json.NewEncoder(os.Stdout) + enc.SetIndent("", " ") + if err := enc.Encode(res); err != nil { + t.Fatal(err) + } +} + +func TestColumnList(t *testing.T) { + cfg, err := newRestConfig() + if err != nil { + t.Fatal(err) + } + + cli, err := columns.NewClient(cfg) + if err != nil { + t.Fatal(err) + } + + all, err := cli.Namespace(namespace).List(context.TODO(), v1.ListOptions{}) + if err != nil { + t.Fatal(err) + } + + enc := json.NewEncoder(os.Stdout) + enc.SetIndent("", " ") + if err := enc.Encode(all); err != nil { + t.Fatal(err) + } +} + +func newRestConfig() (*rest.Config, error) { + home, err := os.UserHomeDir() + if err != nil { + return nil, err + } + + return clientcmd.BuildConfigFromFlags("", filepath.Join(home, ".kube", "config")) +} diff --git a/internal/kubernetes/widgets/cardtemplates/cardtemplates_test.go b/internal/kubernetes/widgets/cardtemplates/cardtemplates_test.go index 9715893..93f7f02 100644 --- a/internal/kubernetes/widgets/cardtemplates/cardtemplates_test.go +++ b/internal/kubernetes/widgets/cardtemplates/cardtemplates_test.go @@ -20,8 +20,8 @@ import ( ) const ( - namespace = "dev-system" - name = "card-dev" + namespace = "demo-system" + name = "sample" ) func TestCardTemplateGet(t *testing.T) { diff --git a/scripts/install-crds.sh b/scripts/install-crds.sh index a83bb47..489ad52 100755 --- a/scripts/install-crds.sh +++ b/scripts/install-crds.sh @@ -1,4 +1,4 @@ #!/bin/bash kubectl apply -f crds/ -kubectl apply -f testdata/cardtemplate-dev.yaml +kubectl apply -f testdata/cardtemplate-sample.yaml diff --git a/testdata/cardtemplate-dev.yaml b/testdata/cardtemplate-sample.yaml similarity index 91% rename from testdata/cardtemplate-dev.yaml rename to testdata/cardtemplate-sample.yaml index a6ce0c3..fdb2410 100644 --- a/testdata/cardtemplate-dev.yaml +++ b/testdata/cardtemplate-sample.yaml @@ -2,14 +2,14 @@ apiVersion: v1 kind: Namespace metadata: - name: dev-system + name: demo-system --- apiVersion: v1 kind: Secret type: Opaque metadata: name: httpbin-endpoint - namespace: dev-system + namespace: demo-system stringData: server-url: http://httpbin.org username: tik @@ -18,8 +18,8 @@ stringData: apiVersion: widgets.ui.krateo.io/v1alpha1 kind: CardTemplate metadata: - name: card-dev - namespace: dev-system + name: sample + namespace: demo-system spec: app: icon: ApartmentOutlined @@ -39,7 +39,7 @@ spec: path: "/anything" endpointRef: name: httpbin-endpoint - namespace: dev-system + namespace: demo-system verb: GET headers: - 'Accept: application/json' diff --git a/testdata/column.yaml b/testdata/column-sample.yaml similarity index 100% rename from testdata/column.yaml rename to testdata/column-sample.yaml diff --git a/testdata/column.yaml.txt b/testdata/column.yaml.txt deleted file mode 100644 index 40a9e26..0000000 --- a/testdata/column.yaml.txt +++ /dev/null @@ -1,39 +0,0 @@ ---- -apiVersion: layout.ui.krateo.io/v1alpha1 -kind: Column -metadata: - name: card-dev - namespace: dev-system -spec: - app: - props: - width: 12 --> string - content: - - toolbar: - - cardTemplatelist: -# - ${ .cardTemplateList.items } - api: - - name: cardTemplatelist - path: "/apis/widgets.ui.krateo.io/v1alpha1/cardtemplates?namespace=dev-system" - endpointRef: - name: krateogateway-endpoint - namespace: krateo-system ---- - - ---- -apiVersion: layout.ui.krateo.io/v1alpha1 -kind: Column -metadata: - name: column-dev - namespace: dev-system -spec: - props: - width: "12" - content: - cardTemplatelist: - ref: - name: card-dev - namespace: dev-system - items: # array of resolved .cardTemplateList.items => this should be in status but... - diff --git a/testdata/role.yaml b/testdata/role.yaml index 108c8d3..b1a22c4 100644 --- a/testdata/role.yaml +++ b/testdata/role.yaml @@ -2,7 +2,7 @@ apiVersion: v1 kind: Namespace metadata: - name: dev-system + name: demo-system --- apiVersion: rbac.authorization.k8s.io/v1 kind: Role @@ -15,7 +15,7 @@ rules: resources: - '*' resourceNames: - - card-dev + - sample verbs: - get --- @@ -23,7 +23,7 @@ apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: dev - namespace: dev-system + namespace: demo-system roleRef: apiGroup: rbac.authorization.k8s.io kind: Role