Skip to content

Component protocol workloads #1582

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

Merged
merged 4 commits into from
Aug 27, 2021
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
17 changes: 11 additions & 6 deletions modules/cmp/steve/proxy/cache_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"sync"
"time"

"github.com/rancher/apiserver/pkg/apierror"
Expand All @@ -36,10 +35,9 @@ import (
type cacheStore struct {
types.Store

ctx context.Context
asl accesscontrol.AccessSetLookup
cache *cache.Cache
cancels sync.Map
ctx context.Context
asl accesscontrol.AccessSetLookup
cache *cache.Cache
}

type cacheKey struct {
Expand Down Expand Up @@ -94,7 +92,14 @@ func (c *cacheStore) List(apiOp *types.APIRequest, schema *types.APISchema) (typ
}

go func() {
list, err := c.Store.List(apiOp, schema)
user, ok := request.UserFrom(apiOp.Context())
if !ok {
logrus.Errorf("user not found in context when steve auth")
return
}
ctx := request.WithUser(c.ctx, user)
newOp := apiOp.WithContext(ctx)
list, err := c.Store.List(newOp, schema)
if err != nil {
logrus.Errorf("failed to list %s in steve cache store, %v", gvk.Kind, err)
return
Expand Down
2 changes: 0 additions & 2 deletions modules/cmp/steve/proxy/cache_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package proxy
import (
"context"
"net/http"
"sync"
"testing"

"github.com/rancher/apiserver/pkg/types"
Expand Down Expand Up @@ -329,7 +328,6 @@ func TestCacheStoreMethods(t *testing.T) {
ctx: ctx,
asl: accesscontrol.NewAccessStore(ctx, true, &rbacInterface{}),
cache: cache,
cancels: sync.Map{},
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://unit.test", nil)
Expand Down
9 changes: 7 additions & 2 deletions modules/cmp/steve/proxy/proxy_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package proxy

import (
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -39,6 +40,8 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"

"github.com/erda-project/erda/modules/cmp/cache"
)

var (
Expand All @@ -54,13 +57,15 @@ type Store struct {
clientGetter proxy.ClientGetter
}

func NewProxyStore(clientGetter proxy.ClientGetter, asl accesscontrol.AccessSetLookup) types.Store {
func NewProxyStore(ctx context.Context, clientGetter proxy.ClientGetter, asl accesscontrol.AccessSetLookup) types.Store {
return &errorStore{
Store: &cacheStore{
Store: &Store{
clientGetter: clientGetter,
},
asl: asl,
ctx: ctx,
cache: cache.FreeCache,
asl: asl,
},
}
}
Expand Down
6 changes: 3 additions & 3 deletions modules/cmp/steve/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func DefaultSchemaTemplates(ctx context.Context, cf *client.Factory,
discovery discovery.DiscoveryInterface, asl accesscontrol.AccessSetLookup, k8sInterface kubernetes.Interface) []schema.Template {
nodeFormatter := fm.NewNodeFormatter(ctx, k8sInterface)
return []schema.Template{
DefaultTemplate(cf, asl),
DefaultTemplate(ctx, cf, asl),
apigroups.Template(discovery),
{
ID: "configmap",
Expand All @@ -69,9 +69,9 @@ func DefaultSchemaTemplates(ctx context.Context, cf *client.Factory,
}
}

func DefaultTemplate(clientGetter proxy.ClientGetter, asl accesscontrol.AccessSetLookup) schema.Template {
func DefaultTemplate(ctx context.Context, clientGetter proxy.ClientGetter, asl accesscontrol.AccessSetLookup) schema.Template {
return schema.Template{
Store: cmpproxy.NewProxyStore(clientGetter, asl),
Store: cmpproxy.NewProxyStore(ctx, clientGetter, asl),
Formatter: formatter(),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ type ComponentEventTable struct {
}

type State struct {
PageNo uint64 `json:"pageNo,omitempty"`
PageSize uint64 `json:"pageSize,omitempty"`
Total uint64 `json:"total"`
Sorter Sorter `json:"sorterData,omitempty"`
ClusterName string `json:"clusterName,omitempty"`
FilterValues FilterValues `json:"filterValues,omitempty"`
PageNo uint64 `json:"pageNo,omitempty"`
PageSize uint64 `json:"pageSize,omitempty"`
Total uint64 `json:"total"`
Sorter Sorter `json:"sorterData,omitempty"`
ClusterName string `json:"clusterName,omitempty"`
FilterValues FilterValues `json:"filterValues,omitempty"`
EventTableUQLQuery string `json:"eventTable__urlQuery,omitempty"`
}

type FilterValues struct {
Expand Down Expand Up @@ -66,7 +67,7 @@ type Props struct {
type Column struct {
DataIndex string `json:"dataIndex"`
Title string `json:"title"`
Width string `json:"width"`
Width int `json:"width"`
Sorter bool `json:"sorter,omitempty"`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package eventTable

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
"time"

Expand All @@ -34,11 +36,9 @@ func RenderCreator() protocol.CompRender {

func (t *ComponentEventTable) Render(ctx context.Context, component *apistructs.Component, _ apistructs.ComponentProtocolScenario,
event apistructs.ComponentEvent, globalStateData *apistructs.GlobalStateData) error {
bdl := ctx.Value(protocol.GlobalInnerKeyCtxBundle.String()).(protocol.ContextBundle)
if bdl.Bdl == nil {
return errors.New("context bundle can not be empty")
if err := t.SetCtxBundle(ctx); err != nil {
return fmt.Errorf("failed to set eventTable component ctx bundle, %v", err)
}
t.ctxBdl = bdl
if err := t.GenComponentState(component); err != nil {
return err
}
Expand All @@ -53,13 +53,28 @@ func (t *ComponentEventTable) Render(ctx context.Context, component *apistructs.
event.Operation == apistructs.ChangeOrgsPageSizeOperationKey {
t.State.PageNo = 1
}
if err := t.DecodeURLQuery(); err != nil {
return fmt.Errorf("failed to decode url query for eventTable component, %v", err)
}
if err := t.RenderList(); err != nil {
return err
}
if err := t.EncodeURLQuery(); err != nil {
return fmt.Errorf("failed to encode url query for eventTable component, %v", err)
}
t.SetComponentValue()
return nil
}

func (t *ComponentEventTable) SetCtxBundle(ctx context.Context) error {
bdl := ctx.Value(protocol.GlobalInnerKeyCtxBundle.String()).(protocol.ContextBundle)
if bdl.Bdl == nil {
return errors.New("context bundle can not be empty")
}
t.ctxBdl = bdl
return nil
}

func (t *ComponentEventTable) GenComponentState(component *apistructs.Component) error {
if component == nil || component.State == nil {
return nil
Expand All @@ -80,6 +95,39 @@ func (t *ComponentEventTable) GenComponentState(component *apistructs.Component)
return nil
}

func (t *ComponentEventTable) DecodeURLQuery() error {
queryData, ok := t.ctxBdl.InParams["eventTable__urlQuery"].(string)
if !ok {
return nil
}
decode, err := base64.StdEncoding.DecodeString(queryData)
if err != nil {
return err
}
query := make(map[string]int)
if err := json.Unmarshal(decode, &query); err != nil {
return err
}
t.State.PageNo = uint64(query["pageNo"])
t.State.PageSize = uint64(query["pageSize"])
return nil
}

func (t *ComponentEventTable) EncodeURLQuery() error {
query := make(map[string]int)
query["pageNo"] = int(t.State.PageNo)
query["pageSize"] = int(t.State.PageSize)

data, err := json.Marshal(query)
if err != nil {
return err
}

encode := base64.StdEncoding.EncodeToString(data)
t.State.EventTableUQLQuery = encode
return nil
}

func (t *ComponentEventTable) RenderList() error {
userID := t.ctxBdl.Identity.UserID
orgID := t.ctxBdl.Identity.OrgID
Expand Down Expand Up @@ -232,55 +280,55 @@ func (t *ComponentEventTable) SetComponentValue() {
{
DataIndex: "lastSeen",
Title: "Last Seen",
Width: "160",
Width: 160,
Sorter: true,
},
{
DataIndex: "type",
Title: "Event Type",
Width: "100",
Width: 100,
Sorter: true,
},
{
DataIndex: "reason",
Title: "Reason",
Width: "100",
Width: 100,
Sorter: true,
},
{
DataIndex: "object",
Title: "Object",
Width: "150",
Width: 150,
Sorter: true,
},
{
DataIndex: "source",
Title: "Source",
Width: "120",
Width: 120,
Sorter: true,
},
{
DataIndex: "message",
Title: "Message",
Width: "120",
Width: 120,
Sorter: true,
},
{
DataIndex: "count",
Title: "Count",
Width: "80",
Width: 80,
Sorter: true,
},
{
DataIndex: "name",
Title: "Name",
Width: "120",
Width: 120,
Sorter: true,
},
{
DataIndex: "namespace",
Title: "Namespace",
Width: "120",
Width: 120,
Sorter: true,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ type ComponentFilter struct {
}

type State struct {
ClusterName string `json:"clusterName,omitempty"`
Conditions []Condition `json:"conditions,omitempty"`
Values Values `json:"values,omitempty"`
ClusterName string `json:"clusterName,omitempty"`
Conditions []Condition `json:"conditions,omitempty"`
Values Values `json:"values,omitempty"`
FilterURLQuery string `json:"filter_urlQuery,omitempty"`
}

type Values struct {
Expand Down
Loading