Skip to content

Commit a983be9

Browse files
committed
feat: add workload list scenario
1 parent 7fc071a commit a983be9

File tree

21 files changed

+2410
-35
lines changed

21 files changed

+2410
-35
lines changed

modules/cmp/steve/proxy/cache_store.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"crypto/sha256"
2020
"encoding/hex"
2121
"encoding/json"
22-
"sync"
2322
"time"
2423

2524
"github.com/rancher/apiserver/pkg/apierror"
@@ -39,7 +38,6 @@ type cacheStore struct {
3938
ctx context.Context
4039
asl accesscontrol.AccessSetLookup
4140
cache *cache.Cache
42-
cancels sync.Map
4341
}
4442

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

9694
go func() {
97-
list, err := c.Store.List(apiOp, schema)
95+
user, ok := request.UserFrom(apiOp.Context())
96+
if !ok {
97+
logrus.Errorf("user not found in context when steve auth")
98+
return
99+
}
100+
ctx := request.WithUser(c.ctx, user)
101+
newOp := apiOp.WithContext(ctx)
102+
list, err := c.Store.List(newOp, schema)
98103
if err != nil {
99104
logrus.Errorf("failed to list %s in steve cache store, %v", gvk.Kind, err)
100105
return

modules/cmp/steve/proxy/cache_store_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package proxy
1717
import (
1818
"context"
1919
"net/http"
20-
"sync"
2120
"testing"
2221

2322
"github.com/rancher/apiserver/pkg/types"
@@ -329,7 +328,6 @@ func TestCacheStoreMethods(t *testing.T) {
329328
ctx: ctx,
330329
asl: accesscontrol.NewAccessStore(ctx, true, &rbacInterface{}),
331330
cache: cache,
332-
cancels: sync.Map{},
333331
}
334332

335333
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://unit.test", nil)

modules/cmp/steve/proxy/proxy_store.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package proxy
1616

1717
import (
18+
"context"
1819
"encoding/json"
1920
"fmt"
2021
"io"
@@ -56,12 +57,13 @@ type Store struct {
5657
clientGetter proxy.ClientGetter
5758
}
5859

59-
func NewProxyStore(clientGetter proxy.ClientGetter, asl accesscontrol.AccessSetLookup, cache *cache.Cache) types.Store {
60+
func NewProxyStore(ctx context.Context, clientGetter proxy.ClientGetter, asl accesscontrol.AccessSetLookup, cache *cache.Cache) types.Store {
6061
return &errorStore{
6162
Store: &cacheStore{
6263
Store: &Store{
6364
clientGetter: clientGetter,
6465
},
66+
ctx: ctx,
6567
cache: cache,
6668
asl: asl,
6769
},

modules/cmp/steve/schema.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func DefaultSchemaTemplates(ctx context.Context, cf *client.Factory,
5151
cache, _ := cache.New(conf.CacheSize(), conf.CacheSegSize())
5252
nodeFormatter := fm.NewNodeFormatter(ctx, cache, k8sInterface)
5353
return []schema.Template{
54-
DefaultTemplate(cf, asl, cache),
54+
DefaultTemplate(ctx, cf, asl, cache),
5555
apigroups.Template(discovery),
5656
{
5757
ID: "configmap",
@@ -72,9 +72,9 @@ func DefaultSchemaTemplates(ctx context.Context, cf *client.Factory,
7272
}
7373
}
7474

75-
func DefaultTemplate(clientGetter proxy.ClientGetter, asl accesscontrol.AccessSetLookup, cache *cache.Cache) schema.Template {
75+
func DefaultTemplate(ctx context.Context, clientGetter proxy.ClientGetter, asl accesscontrol.AccessSetLookup, cache *cache.Cache) schema.Template {
7676
return schema.Template{
77-
Store: cmpproxy.NewProxyStore(clientGetter, asl, cache),
77+
Store: cmpproxy.NewProxyStore(ctx, clientGetter, asl, cache),
7878
Formatter: formatter(),
7979
}
8080
}

modules/openapi/component-protocol/scenarios/cmp-dashboard-events/components/eventTable/model.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ type ComponentEventTable struct {
2727
}
2828

2929
type State struct {
30-
PageNo uint64 `json:"pageNo,omitempty"`
31-
PageSize uint64 `json:"pageSize,omitempty"`
32-
Total uint64 `json:"total"`
33-
Sorter Sorter `json:"sorterData,omitempty"`
34-
ClusterName string `json:"clusterName,omitempty"`
35-
FilterValues FilterValues `json:"filterValues,omitempty"`
30+
PageNo uint64 `json:"pageNo,omitempty"`
31+
PageSize uint64 `json:"pageSize,omitempty"`
32+
Total uint64 `json:"total"`
33+
Sorter Sorter `json:"sorterData,omitempty"`
34+
ClusterName string `json:"clusterName,omitempty"`
35+
FilterValues FilterValues `json:"filterValues,omitempty"`
36+
EventTableUQLQuery string `json:"eventTable__urlQuery,omitempty"`
3637
}
3738

3839
type FilterValues struct {
@@ -66,7 +67,7 @@ type Props struct {
6667
type Column struct {
6768
DataIndex string `json:"dataIndex"`
6869
Title string `json:"title"`
69-
Width string `json:"width"`
70+
Width int `json:"width"`
7071
Sorter bool `json:"sorter,omitempty"`
7172
}
7273

modules/openapi/component-protocol/scenarios/cmp-dashboard-events/components/eventTable/render.go

+61-13
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ package eventTable
1616

1717
import (
1818
"context"
19+
"encoding/base64"
1920
"encoding/json"
21+
"fmt"
2022
"strconv"
2123
"time"
2224

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

3537
func (t *ComponentEventTable) Render(ctx context.Context, component *apistructs.Component, _ apistructs.ComponentProtocolScenario,
3638
event apistructs.ComponentEvent, globalStateData *apistructs.GlobalStateData) error {
37-
bdl := ctx.Value(protocol.GlobalInnerKeyCtxBundle.String()).(protocol.ContextBundle)
38-
if bdl.Bdl == nil {
39-
return errors.New("context bundle can not be empty")
39+
if err := t.SetCtxBundle(ctx); err != nil {
40+
return fmt.Errorf("failed to set eventTable component ctx bundle, %v", err)
4041
}
41-
t.ctxBdl = bdl
4242
if err := t.GenComponentState(component); err != nil {
4343
return err
4444
}
@@ -53,13 +53,28 @@ func (t *ComponentEventTable) Render(ctx context.Context, component *apistructs.
5353
event.Operation == apistructs.ChangeOrgsPageSizeOperationKey {
5454
t.State.PageNo = 1
5555
}
56+
if err := t.DecodeURLQuery(); err != nil {
57+
return fmt.Errorf("failed to decode url query for eventTable component, %v", err)
58+
}
5659
if err := t.RenderList(); err != nil {
5760
return err
5861
}
62+
if err := t.EncodeURLQuery(); err != nil {
63+
return fmt.Errorf("failed to encode url query for eventTable component, %v", err)
64+
}
5965
t.SetComponentValue()
6066
return nil
6167
}
6268

69+
func (t *ComponentEventTable) SetCtxBundle(ctx context.Context) error {
70+
bdl := ctx.Value(protocol.GlobalInnerKeyCtxBundle.String()).(protocol.ContextBundle)
71+
if bdl.Bdl == nil {
72+
return errors.New("context bundle can not be empty")
73+
}
74+
t.ctxBdl = bdl
75+
return nil
76+
}
77+
6378
func (t *ComponentEventTable) GenComponentState(component *apistructs.Component) error {
6479
if component == nil || component.State == nil {
6580
return nil
@@ -80,6 +95,39 @@ func (t *ComponentEventTable) GenComponentState(component *apistructs.Component)
8095
return nil
8196
}
8297

98+
func (t *ComponentEventTable) DecodeURLQuery() error {
99+
queryData, ok := t.ctxBdl.InParams["eventTable__urlQuery"].(string)
100+
if !ok {
101+
return nil
102+
}
103+
decode, err := base64.StdEncoding.DecodeString(queryData)
104+
if err != nil {
105+
return err
106+
}
107+
query := make(map[string]int)
108+
if err := json.Unmarshal(decode, &query); err != nil {
109+
return err
110+
}
111+
t.State.PageNo = uint64(query["pageNo"])
112+
t.State.PageSize = uint64(query["pageSize"])
113+
return nil
114+
}
115+
116+
func (t *ComponentEventTable) EncodeURLQuery() error {
117+
query := make(map[string]int)
118+
query["pageNo"] = int(t.State.PageNo)
119+
query["pageSize"] = int(t.State.PageSize)
120+
121+
data, err := json.Marshal(query)
122+
if err != nil {
123+
return err
124+
}
125+
126+
encode := base64.StdEncoding.EncodeToString(data)
127+
t.State.EventTableUQLQuery = encode
128+
return nil
129+
}
130+
83131
func (t *ComponentEventTable) RenderList() error {
84132
userID := t.ctxBdl.Identity.UserID
85133
orgID := t.ctxBdl.Identity.OrgID
@@ -232,55 +280,55 @@ func (t *ComponentEventTable) SetComponentValue() {
232280
{
233281
DataIndex: "lastSeen",
234282
Title: "Last Seen",
235-
Width: "160",
283+
Width: 160,
236284
Sorter: true,
237285
},
238286
{
239287
DataIndex: "type",
240288
Title: "Event Type",
241-
Width: "100",
289+
Width: 100,
242290
Sorter: true,
243291
},
244292
{
245293
DataIndex: "reason",
246294
Title: "Reason",
247-
Width: "100",
295+
Width: 100,
248296
Sorter: true,
249297
},
250298
{
251299
DataIndex: "object",
252300
Title: "Object",
253-
Width: "150",
301+
Width: 150,
254302
Sorter: true,
255303
},
256304
{
257305
DataIndex: "source",
258306
Title: "Source",
259-
Width: "120",
307+
Width: 120,
260308
Sorter: true,
261309
},
262310
{
263311
DataIndex: "message",
264312
Title: "Message",
265-
Width: "120",
313+
Width: 120,
266314
Sorter: true,
267315
},
268316
{
269317
DataIndex: "count",
270318
Title: "Count",
271-
Width: "80",
319+
Width: 80,
272320
Sorter: true,
273321
},
274322
{
275323
DataIndex: "name",
276324
Title: "Name",
277-
Width: "120",
325+
Width: 120,
278326
Sorter: true,
279327
},
280328
{
281329
DataIndex: "namespace",
282330
Title: "Namespace",
283-
Width: "120",
331+
Width: 120,
284332
Sorter: true,
285333
},
286334
},

modules/openapi/component-protocol/scenarios/cmp-dashboard-events/components/filter/model.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ type ComponentFilter struct {
2525
}
2626

2727
type State struct {
28-
ClusterName string `json:"clusterName,omitempty"`
29-
Conditions []Condition `json:"conditions,omitempty"`
30-
Values Values `json:"values,omitempty"`
28+
ClusterName string `json:"clusterName,omitempty"`
29+
Conditions []Condition `json:"conditions,omitempty"`
30+
Values Values `json:"values,omitempty"`
31+
FilterURLQuery string `json:"filter_urlQuery,omitempty"`
3132
}
3233

3334
type Values struct {

modules/openapi/component-protocol/scenarios/cmp-dashboard-events/components/filter/render.go

+44-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ package filter
1616

1717
import (
1818
"context"
19+
"encoding/base64"
1920
"encoding/json"
21+
"fmt"
2022
"sort"
2123
"strconv"
2224
"strings"
@@ -34,19 +36,58 @@ func RenderCreator() protocol.CompRender {
3436

3537
func (f *ComponentFilter) Render(ctx context.Context, component *apistructs.Component, _ apistructs.ComponentProtocolScenario,
3638
_ apistructs.ComponentEvent, _ *apistructs.GlobalStateData) error {
39+
if err := f.SetCtxBundle(ctx); err != nil {
40+
return fmt.Errorf("failed to set filter component ctx bundle, %v", err)
41+
}
42+
if err := f.DecodeURLQuery(); err != nil {
43+
return fmt.Errorf("failed to decode url query for filter component, %v", err)
44+
}
45+
if err := f.GenComponentState(component); err != nil {
46+
return fmt.Errorf("failed to gen filter component state, %v", err)
47+
}
48+
if err := f.SetComponentValue(); err != nil {
49+
return fmt.Errorf("failed to set filter component value, %v", err)
50+
}
51+
if err := f.EncodeURLQuery(); err != nil {
52+
return fmt.Errorf("failed to encode url query for filter component, %v", err)
53+
}
54+
return nil
55+
}
56+
57+
func (f *ComponentFilter) SetCtxBundle(ctx context.Context) error {
3758
bdl := ctx.Value(protocol.GlobalInnerKeyCtxBundle.String()).(protocol.ContextBundle)
3859
if bdl.Bdl == nil {
39-
return errors.New("context bundle can not be empty")
60+
return errors.New("bundle in context can not be empty")
4061
}
4162
f.ctxBdl = bdl
63+
return nil
64+
}
4265

43-
if err := f.GenComponentState(component); err != nil {
66+
func (f *ComponentFilter) DecodeURLQuery() error {
67+
queryData, ok := f.ctxBdl.InParams["filter__urlQuery"].(string)
68+
if !ok {
69+
return nil
70+
}
71+
decode, err := base64.StdEncoding.DecodeString(queryData)
72+
if err != nil {
73+
return err
74+
}
75+
var values Values
76+
if err := json.Unmarshal(decode, &values); err != nil {
4477
return err
4578
}
79+
f.State.Values = values
80+
return nil
81+
}
4682

47-
if err := f.SetComponentValue(); err != nil {
83+
func (f *ComponentFilter) EncodeURLQuery() error {
84+
data, err := json.Marshal(f.State.Values)
85+
if err != nil {
4886
return err
4987
}
88+
89+
encode := base64.StdEncoding.EncodeToString(data)
90+
f.State.FilterURLQuery = encode
5091
return nil
5192
}
5293

0 commit comments

Comments
 (0)