Skip to content

Commit

Permalink
feat: add layout column crd and related endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasepe committed Jan 17, 2024
1 parent bc2d252 commit e6cf4ad
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 206 deletions.
8 changes: 4 additions & 4 deletions apis/ui/columns/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ type ColumnSpec struct {

// CardTemplateListRef reference to card template list.
// +optional
CardTemplateListRef *core.ListReference `json:"cardTemplateListRef,omitempty"`
CardTemplateListRef []*core.Reference `json:"cardTemplateListRef,omitempty"`
}

type ColumnStatus struct {
// CardTemplateList list of card templates.
// Cards list of card templates.
// +optional
CardTemplateList []v1alpha1.CardTemplate `json:"cardTemplateList,omitempty"`
Cards []*v1alpha1.CardInfo `json:"cards,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:scope=Namespaced,categories={krateo,layout,column}
// +kubebuilder:resource:scope=Namespaced,categories={krateo,layout,columns}
type Column struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand Down
22 changes: 16 additions & 6 deletions apis/ui/columns/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

185 changes: 30 additions & 155 deletions crds/layout.ui.krateo.io_columns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spec:
categories:
- krateo
- layout
- column
- columns
kind: Column
listKind: ColumnList
plural: columns
Expand Down Expand Up @@ -46,169 +46,44 @@ spec:
type: object
cardTemplateListRef:
description: CardTemplateListRef reference to card template list.
properties:
namespace:
description: Namespace of the referenced list.
type: string
required:
- namespace
type: object
items:
description: A Reference to a named object.
properties:
name:
description: Name of the referenced object.
type: string
namespace:
description: Namespace of the referenced object.
type: string
required:
- name
- namespace
type: object
type: array
required:
- app
type: object
status:
properties:
cardTemplateList:
description: CardTemplateList list of card templates.
cards:
description: Cards list of card templates.
items:
description: CardTemplate is ui widgets card configuration.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this
representation of an object. Servers should convert recognized
schemas to the latest internal value, and may reject unrecognized
values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
color:
type: string
content:
type: string
date:
type: string
icon:
type: string
tags:
type: string
kind:
description: 'Kind is a string value representing the REST resource
this object represents. Servers may infer this from the endpoint
the client submits requests to. Cannot be updated. In CamelCase.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
title:
type: string
metadata:
type: object
spec:
description: CardTemplate is a template for a Krateo UI Card
widget.
properties:
api:
description: APIList list of api calls.
items:
description: API contains external api call info.
properties:
dependOn:
type: string
endpointRef:
description: A Reference to a named object.
properties:
name:
description: Name of the referenced object.
type: string
namespace:
description: Namespace of the referenced object.
type: string
required:
- name
- namespace
type: object
headers:
items:
type: string
type: array
krateoGateway:
type: boolean
name:
type: string
path:
type: string
payload:
type: string
verb:
default: GET
type: string
required:
- name
type: object
type: array
app:
description: App is the card info
properties:
actions:
items:
description: API contains external api call info.
properties:
dependOn:
type: string
endpointRef:
description: A Reference to a named object.
properties:
name:
description: Name of the referenced object.
type: string
namespace:
description: Namespace of the referenced object.
type: string
required:
- name
- namespace
type: object
headers:
items:
type: string
type: array
krateoGateway:
type: boolean
name:
type: string
path:
type: string
payload:
type: string
verb:
default: GET
type: string
required:
- name
type: object
type: array
color:
type: string
content:
type: string
date:
type: string
icon:
type: string
tags:
type: string
title:
type: string
required:
- content
- title
type: object
iterator:
type: string
required:
- app
type: object
status:
properties:
allowedActions:
items:
type: string
type: array
cards:
items:
properties:
color:
type: string
content:
type: string
date:
type: string
icon:
type: string
tags:
type: string
title:
type: string
required:
- content
- title
type: object
type: array
type: object
required:
- content
- title
type: object
type: array
type: object
Expand Down
29 changes: 15 additions & 14 deletions internal/kubernetes/layout/columns/evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/krateoplatformops/krateo-bff/apis/ui/columns/v1alpha1"
"github.com/krateoplatformops/krateo-bff/internal/kubernetes/widgets/cardtemplates"
cardtemplatesevaluator "github.com/krateoplatformops/krateo-bff/internal/kubernetes/widgets/cardtemplates/evaluator"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
)

Expand All @@ -18,29 +17,31 @@ type EvalOptions struct {
}

func Eval(ctx context.Context, in *v1alpha1.Column, opts EvalOptions) error {
return evalCardTemplateList(ctx, in, opts)
return evalCardTemplateRefs(ctx, in, opts)
}

func evalCardTemplateList(ctx context.Context, in *v1alpha1.Column, opts EvalOptions) error {
ref := in.Spec.CardTemplateListRef
if ref == nil {
func evalCardTemplateRefs(ctx context.Context, in *v1alpha1.Column, opts EvalOptions) error {
refs := in.Spec.CardTemplateListRef
if refs == nil {
return nil
}

in.Status.CardTemplateList = []cardtemplatesv1alpha1.CardTemplate{}

cli, err := cardtemplates.NewClient(opts.RESTConfig)
if err != nil {
return err
}
all, err := cli.Namespace(ref.Namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err

if in.Status.Cards == nil {
in.Status.Cards = []*cardtemplatesv1alpha1.CardInfo{}
}

for _, el := range all.Items {
obj := &el
err := cardtemplatesevaluator.Eval(ctx, obj, cardtemplatesevaluator.EvalOptions{
for _, ref := range refs {
obj, err := cli.Namespace(ref.Namespace).Get(ctx, ref.Name)
if err != nil {
return err
}

err = cardtemplatesevaluator.Eval(ctx, obj, cardtemplatesevaluator.EvalOptions{
RESTConfig: opts.RESTConfig,
AuthnNS: opts.AuthnNS,
Username: opts.Username,
Expand All @@ -49,7 +50,7 @@ func evalCardTemplateList(ctx context.Context, in *v1alpha1.Column, opts EvalOpt
return err
}

in.Status.CardTemplateList = append(in.Status.CardTemplateList, *obj)
in.Status.Cards = append(in.Status.Cards, obj.Status.Cards...)
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions internal/server/routes/layout/columns/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ func (r *getter) ServeHTTP(wri http.ResponseWriter, req *http.Request) {
m[allowedVerbsAnnotationKey] = strings.Join(verbs, ",")
obj.SetAnnotations(m)

_, err = r.client.Namespace(namespace).UpdateStatus(context.TODO(), obj)
if err != nil {
log.Err(err).Str("object", obj.GetName()).Msg("unable to update object status")
}
// _, err = r.client.Namespace(namespace).UpdateStatus(context.TODO(), obj)
// if err != nil {
// log.Err(err).Str("object", obj.GetName()).Msg("unable to update object status")
// }
}

wri.Header().Set("Content-Type", "application/json")
Expand Down
24 changes: 4 additions & 20 deletions internal/server/routes/layout/columns/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,26 +153,10 @@ func (r *lister) ServeHTTP(wri http.ResponseWriter, req *http.Request) {
m[allowedVerbsAnnotationKey] = strings.Join(verbs, ",")
obj.SetAnnotations(m)

_, err = r.client.Namespace(obj.Namespace).UpdateStatus(context.TODO(), obj)
if err != nil {
log.Err(err).Str("object", obj.GetName()).Msg("unable to update object status")
}
}

all, err = r.client.Namespace(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Err(err).
Str("sub", sub).
Strs("orgs", orgs).
Str("namespace", namespace).
Msg("unable to list columns")

if apierrors.IsNotFound(err) {
encode.NotFound(wri, err)
} else {
encode.Invalid(wri, err)
}
return
// _, err = r.client.Namespace(obj.Namespace).UpdateStatus(context.TODO(), obj)
// if err != nil {
// log.Err(err).Str("object", obj.GetName()).Msg("unable to update object status")
// }
}

wri.Header().Set("Content-Type", "application/json")
Expand Down
1 change: 1 addition & 0 deletions scripts/install-role.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash

kubectl apply -f testdata/clusterrole-widgets-viewer.yaml
kubectl apply -f testdata/clusterrole-layout-viewer.yaml
2 changes: 1 addition & 1 deletion scripts/server-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export AUTHN_STORE_NAMESPACE=demo-system
kubectl apply -f crds/
kubectl apply -f testdata/ns.yaml
kubectl apply -f testdata/cardtemplate-demo.yaml
kubectl apply -f testdata/column-sample.yaml
kubectl apply -f testdata/column-demo.yaml

go run main.go -kubeconfig $HOME/.kube/config
Loading

0 comments on commit e6cf4ad

Please sign in to comment.