Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(frontend): support search based on SQL syntax #77

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ header:
- '**/lcov.info'
- '**/Makefile'
- '**/OWNERS'
- 'ui/**'
- '.github/**'
- '.idea/**'
- '.vscode/**'
Expand Down
104 changes: 52 additions & 52 deletions pkg/registry/search/uniresource/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,87 +15,87 @@
package uniresource

import (
"context"
"fmt"

"github.com/KusionStack/karbour/pkg/apis/search"
"github.com/KusionStack/karbour/pkg/search/storage"
filtersutil "github.com/KusionStack/karbour/pkg/util/filters"
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
"context"
"fmt"

"github.com/KusionStack/karbour/pkg/apis/search"
"github.com/KusionStack/karbour/pkg/search/storage"
filtersutil "github.com/KusionStack/karbour/pkg/util/filters"
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
)

var (
_ rest.Storage = &REST{}
_ rest.Scoper = &REST{}
_ rest.Lister = &REST{}
_ rest.ShortNamesProvider = &REST{}
_ rest.Storage = &REST{}
_ rest.Scoper = &REST{}
_ rest.Lister = &REST{}
_ rest.ShortNamesProvider = &REST{}
)

type REST struct {
Storage storage.SearchStorage
Storage storage.SearchStorage
}

func NewREST(searchStorageGetter storage.SearchStorageGetter) (rest.Storage, error) {
searchStorage, err := searchStorageGetter.GetSearchStorage()
if err != nil {
return nil, err
}

return &REST{
Storage: searchStorage,
}, nil
searchStorage, err := searchStorageGetter.GetSearchStorage()
if err != nil {
return nil, err
}

return &REST{
Storage: searchStorage,
}, nil
}

func (r *REST) New() runtime.Object {
return &search.UniResource{}
return &search.UniResource{}
}

func (r *REST) Destroy() {
}

func (r *REST) NamespaceScoped() bool {
return false
return false
}

func (r *REST) NewList() runtime.Object {
return &search.UniResourceList{}
return &search.UniResourceList{}
}

func (r *REST) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) {
queryString, ok := filtersutil.SearchQueryFrom(ctx)
if !ok {
return nil, fmt.Errorf("query can't be empty")
}

patternType, ok := filtersutil.PatternTypeFrom(ctx)
if !ok {
return nil, fmt.Errorf("pattern type can't be empty")
}

res, err := r.Storage.Search(ctx, queryString, patternType)
if err != nil {
return nil, err
}

rt := &search.UniResourceList{}
for _, resource := range res.Resources {
unObj := &unstructured.Unstructured{}
unObj.SetUnstructuredContent(resource.Object)
rt.Items = append(rt.Items, unObj)
}
return rt, nil
queryString, ok := filtersutil.SearchQueryFrom(ctx)
if !ok {
return nil, fmt.Errorf("query can't be empty")
}

patternType, ok := filtersutil.PatternTypeFrom(ctx)
if !ok {
return nil, fmt.Errorf("pattern type can't be empty")
}

res, err := r.Storage.Search(ctx, queryString, patternType)
if err != nil {
return nil, err
}

rt := &search.UniResourceList{}
for _, resource := range res.Resources {
unObj := &unstructured.Unstructured{}
unObj.SetUnstructuredContent(resource.Object)
rt.Items = append(rt.Items, unObj)
}
return rt, nil
}

func (r *REST) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
// TODO: add real logic of convert to table when the storage layer is implemented
return rest.NewDefaultTableConvertor(search.Resource("uniresources")).ConvertToTable(ctx, object, tableOptions)
// TODO: add real logic of convert to table when the storage layer is implemented
return rest.NewDefaultTableConvertor(search.Resource("uniresources")).ConvertToTable(ctx, object, tableOptions)
}

// ShortNames implements the ShortNamesProvider interface. Returns a list of short names for a resource.
func (r *REST) ShortNames() []string {
return []string{"ur"}
return []string{"ur"}
}
43 changes: 0 additions & 43 deletions pkg/search/storage/elasticsearch/search_test.go

This file was deleted.

44 changes: 22 additions & 22 deletions pkg/search/storage/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,48 @@
package storage

import (
"context"
"context"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime"
)

const (
Equals string = "="
DSLPatternType = "dsl"
SQLPatternType = "sql"
Equals = "="
DSLPatternType = "dsl"
SQLPatternType = "sql"
)

type Storage interface {
Get(ctx context.Context, cluster string, obj runtime.Object) error
Create(ctx context.Context, cluster string, obj runtime.Object) error
Update(ctx context.Context, cluster string, obj runtime.Object) error
Delete(ctx context.Context, cluster string, obj runtime.Object) error
Get(ctx context.Context, cluster string, obj runtime.Object) error
Create(ctx context.Context, cluster string, obj runtime.Object) error
Update(ctx context.Context, cluster string, obj runtime.Object) error
Delete(ctx context.Context, cluster string, obj runtime.Object) error
}

type Query struct {
Key string
Values []string
Operator string
Key string
Values []string
Operator string
}

type SearchStorage interface {
Search(ctx context.Context, queryString, patternType string) (*SearchResult, error)
Search(ctx context.Context, queryString, patternType string) (*SearchResult, error)
}

type SearchStorageGetter interface {
GetSearchStorage() (SearchStorage, error)
GetSearchStorage() (SearchStorage, error)
}

type Resource struct {
Cluster string `json:"cluster"`
Namespace string `json:"namespace"`
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Name string `json:"name"`
Object map[string]interface{} `json:"object"`
Cluster string `json:"cluster"`
Namespace string `json:"namespace"`
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Name string `json:"name"`
Object map[string]interface{} `json:"object"`
}

type SearchResult struct {
Total int
Resources []*Resource
Total int
Resources []*Resource
}
5 changes: 3 additions & 2 deletions pkg/util/filters/search_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/KusionStack/karbour/pkg/search/storage"
)

type ctxTyp string

const (
searchQueryKey = "query"
patternTypeKey = "patternType"
Expand Down Expand Up @@ -63,6 +65,5 @@ func FromQueryToContext(req *http.Request, key string, defaultVal string) *http.
query.Del(key)
val = queryVal[0]
}

return req.WithContext(context.WithValue(req.Context(), key, val))
return req.WithContext(context.WithValue(req.Context(), ctxTyp(key), val))
}
7 changes: 7 additions & 0 deletions ui/LEGAL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Legal Disclaimer

Within this source code, the comments in Chinese shall be the original, governing version. Any comment in other languages are for reference only. In the event of any conflict between the Chinese language version comments and other language version comments, the Chinese language version shall prevail.

法律免责声明

关于代码注释部分,中文注释为官方版本,其它语言注释仅做参考。中文注释可能与其它语言注释存在不一致,当中文注释与其它语言注释存在不一致时,请以中文注释为准。
28 changes: 28 additions & 0 deletions ui/craco.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const CracoLessPlugin = require("craco-less");
const CracoCSSModules = require("craco-css-modules");
const path = require("path");

module.exports = {
plugins: [
{
plugin: CracoLessPlugin,
options: {
lessLoaderOptions: {
lessOptions: {
modifyVars: {},
javascriptEnabled: true,
},
},
},
},
{
plugin: CracoCSSModules
}
],
webpack: {
// 配置别名,设置别名是为了让后续引用的地方减少路径的复杂度
alias: {
"@": path.resolve(__dirname, "src"),
},
},
}
Loading
Loading