Skip to content

Commit

Permalink
fix cardtemplate resolve status, rename samples
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasepe committed Dec 21, 2023
1 parent 288058c commit fc6a553
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 54 deletions.
8 changes: 4 additions & 4 deletions apis/apis.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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,
)
}
File renamed without changes.
170 changes: 170 additions & 0 deletions internal/kubernetes/layout/columns/columns.go
Original file line number Diff line number Diff line change
@@ -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
}
77 changes: 77 additions & 0 deletions internal/kubernetes/layout/columns/columns_test.go
Original file line number Diff line number Diff line change
@@ -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"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
)

const (
namespace = "dev-system"
name = "card-dev"
namespace = "demo-system"
name = "sample"
)

func TestCardTemplateGet(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion scripts/install-crds.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash

kubectl apply -f crds/
kubectl apply -f testdata/cardtemplate-dev.yaml
kubectl apply -f testdata/cardtemplate-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -39,7 +39,7 @@ spec:
path: "/anything"
endpointRef:
name: httpbin-endpoint
namespace: dev-system
namespace: demo-system
verb: GET
headers:
- 'Accept: application/json'
Expand Down
File renamed without changes.
39 changes: 0 additions & 39 deletions testdata/column.yaml.txt

This file was deleted.

6 changes: 3 additions & 3 deletions testdata/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
apiVersion: v1
kind: Namespace
metadata:
name: dev-system
name: demo-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
Expand All @@ -15,15 +15,15 @@ rules:
resources:
- '*'
resourceNames:
- card-dev
- sample
verbs:
- get
---
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
Expand Down

0 comments on commit fc6a553

Please sign in to comment.