Skip to content

Commit 15efb83

Browse files
committed
feat: refactor listResource and update plugin metadata listing
1 parent 4a2d925 commit 15efb83

File tree

5 files changed

+29
-43
lines changed

5 files changed

+29
-43
lines changed

api/adc/types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ type ConsumerElement struct {
9393
type Credential struct {
9494
Metadata `json:",inline" yaml:",inline"`
9595

96-
Config map[string]interface{} `json:"config" yaml:"config"`
97-
Type string `json:"type" yaml:"type"`
96+
Config map[string]any `json:"config" yaml:"config"`
97+
Type string `json:"type" yaml:"type"`
9898
}
9999

100100
type Service struct {
@@ -243,7 +243,7 @@ const (
243243
Server SSLType = "server"
244244
)
245245

246-
type Plugins map[string]interface{}
246+
type Plugins map[string]any
247247

248248
func (p *Plugins) DeepCopyInto(out *Plugins) {
249249
b, _ := json.Marshal(&p)

config/samples/gateway.apisix.io_v1alpha1_gatewayproxy.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
apiVersion: gateway.apisix.io.github.com/v1alpha1
1+
apiVersion: gateway.apisix.io/v1alpha1
22
kind: GatewayProxy
33
metadata:
44
labels:
@@ -9,4 +9,5 @@ metadata:
99
app.kubernetes.io/created-by: api7-ingress-controller
1010
name: gatewayproxy-sample
1111
spec:
12-
# TODO(user): Add fields here
12+
pluginMetadata:
13+
"error-page": {"enable":false}

pkg/dashboard/cluster.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,12 @@ func addQueryParam(urlStr string, labels map[string]string) string {
533533
}
534534

535535
func (c *cluster) listResource(ctx context.Context, url, resource string) (listResponse, error) {
536+
var list listResponse
537+
err := c.listResourceToResponse(ctx, url, resource, &list)
538+
return list, err
539+
}
540+
541+
func (c *cluster) listResourceToResponse(ctx context.Context, url, resource string, listResponse any) error {
536542
log.Debugw("list resource in cluster",
537543
zap.String("cluster_name", c.name),
538544
zap.String("name", resource),
@@ -544,29 +550,25 @@ func (c *cluster) listResource(ctx context.Context, url, resource string) (listR
544550

545551
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
546552
if err != nil {
547-
return listResponse{}, err
553+
return err
548554
}
549555
resp, err := c.do(req)
550556
if err != nil {
551-
return listResponse{}, err
557+
return err
552558
}
553559

554560
defer drainBody(resp.Body, url)
555561
if resp.StatusCode != http.StatusOK {
556562
body := readBody(resp.Body, url)
557563
if c.isFunctionDisabled(body) {
558-
return listResponse{}, ErrFunctionDisabled
564+
return ErrFunctionDisabled
559565
}
560566
err = multierr.Append(err, fmt.Errorf("unexpected status code %d", resp.StatusCode))
561567
err = multierr.Append(err, fmt.Errorf("error message: %s", body))
562-
return listResponse{}, err
563-
}
564-
var list listResponse
565-
byt, _ := io.ReadAll(resp.Body)
566-
if err := json.Unmarshal(byt, &list); err != nil {
567-
return listResponse{}, err
568+
return err
568569
}
569-
return list, nil
570+
571+
return json.NewDecoder(resp.Body).Decode(listResponse)
570572
}
571573

572574
func (c *cluster) createResource(ctx context.Context, url, resource string, body []byte) (*getResponse, error) {

pkg/dashboard/plugin_metadata.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,27 @@ func (r *pluginMetadataClient) Get(ctx context.Context, name string) (*v1.Plugin
6767
return pluginMetadata, nil
6868
}
6969

70-
func (r *pluginMetadataClient) List(ctx context.Context) ([]*v1.PluginMetadata, error) {
70+
func (r *pluginMetadataClient) List(ctx context.Context) (list []*v1.PluginMetadata, err error) {
7171
log.Debugw("try to list pluginMetadatas in APISIX",
7272
zap.String("cluster", r.cluster.name),
7373
zap.String("url", r.url),
7474
)
75-
pluginMetadataItems, err := r.cluster.listResource(ctx, r.url, "pluginMetadata")
75+
var resp = struct {
76+
Value map[string]map[string]any
77+
}{}
78+
err = r.cluster.listResourceToResponse(ctx, r.url, "plugin_metadata", &resp)
7679
if err != nil {
7780
log.Errorf("failed to list pluginMetadatas: %s", err)
7881
return nil, err
7982
}
80-
81-
items := make([]*v1.PluginMetadata, 0, len(pluginMetadataItems.List))
82-
for _, item := range pluginMetadataItems.List {
83-
pluginMetadata, err := item.pluginMetadata()
84-
if err != nil {
85-
log.Errorw("failed to convert pluginMetadata item",
86-
zap.String("url", r.url),
87-
zap.Error(err),
88-
)
89-
return nil, err
90-
}
91-
92-
items = append(items, pluginMetadata)
83+
for name, metadata := range resp.Value {
84+
list = append(list, &v1.PluginMetadata{
85+
Name: name,
86+
Metadata: metadata,
87+
})
9388
}
9489

95-
return items, nil
90+
return
9691
}
9792

9893
func (r *pluginMetadataClient) Delete(ctx context.Context, obj *v1.PluginMetadata) error {

pkg/dashboard/resource.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -348,18 +348,6 @@ func (i *getResponse) pluginMetadata() (*v1.PluginMetadata, error) {
348348
return &pluginMetadata, nil
349349
}
350350

351-
func (i *listItem) pluginMetadata() (*v1.PluginMetadata, error) {
352-
byt, err := json.Marshal(i)
353-
if err != nil {
354-
return nil, err
355-
}
356-
var pluginMetadata v1.PluginMetadata
357-
if err := json.Unmarshal(byt, &pluginMetadata.Metadata); err != nil {
358-
return nil, err
359-
}
360-
return &pluginMetadata, nil
361-
}
362-
363351
// // pluginConfig decodes item.Value and converts it to v1.PluginConfig.
364352
// func (i *item) pluginConfig() (*v1.PluginConfig, error) {
365353
// log.Debugf("got pluginConfig: %s", string(i.Value))

0 commit comments

Comments
 (0)