You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/10/28 11:14:26 UTC

[GitHub] [apisix-ingress-controller] tao12345666333 commented on a diff in pull request #1369: feat: support plugin_metadata of apisix

tao12345666333 commented on code in PR #1369:
URL: https://github.com/apache/apisix-ingress-controller/pull/1369#discussion_r1007898742


##########
pkg/apisix/apisix.go:
##########
@@ -152,6 +154,14 @@ type PluginConfig interface {
 	Update(context.Context, *v1.PluginConfig) (*v1.PluginConfig, error)
 }
 
+type PluginMetadata interface {
+	Get(context.Context, string) (*v1.PluginMetadata, error)
+	List(context.Context) ([]*v1.PluginMetadata, error)
+	Create(context.Context, *v1.PluginMetadata) (*v1.PluginMetadata, error)
+	Delete(context.Context, *v1.PluginMetadata) error
+	Update(context.Context, *v1.PluginMetadata) (*v1.PluginMetadata, error)
+}

Review Comment:
   https://apisix.apache.org/docs/apisix/admin-api/#plugin-metadata
   
   In fact PluginMetadata only supports GET/PUT/DELETE methods
   



##########
pkg/apisix/plugin_metadata.go:
##########
@@ -0,0 +1,181 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//	http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package apisix
+
+import (
+	"context"
+	"encoding/json"
+
+	"go.uber.org/zap"
+
+	"github.com/apache/apisix-ingress-controller/pkg/log"
+	v1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1"
+)
+
+type pluginMetadataClient struct {
+	url     string
+	cluster *cluster
+}
+
+func newPluginMetadataClient(c *cluster) *pluginMetadataClient {
+	return &pluginMetadataClient{
+		url:     c.baseURL + "/plugin_metadata",
+		cluster: c,
+	}
+}
+
+func (r *pluginMetadataClient) Get(ctx context.Context, name string) (*v1.PluginMetadata, error) {
+	log.Debugw("try to look up pluginMetadata",
+		zap.String("name", name),
+		zap.String("url", r.url),
+		zap.String("cluster", "default"),
+	)
+
+	// TODO Add mutex here to avoid dog-pile effect.
+	url := r.url + "/" + name
+	resp, err := r.cluster.getResource(ctx, url, "pluginMetadata")
+	r.cluster.metricsCollector.IncrAPISIXRequest("pluginMetadata")
+	if err != nil {
+		log.Errorw("failed to get pluginMetadata from APISIX",
+			zap.String("name", name),
+			zap.String("url", url),
+			zap.String("cluster", "default"),
+			zap.Error(err),
+		)
+		return nil, err
+	}
+
+	pluginMetadata, err := resp.Item.pluginMetadata()
+	if err != nil {
+		log.Errorw("failed to convert pluginMetadata item",
+			zap.String("url", r.url),
+			zap.String("pluginMetadata_key", resp.Item.Key),
+			zap.String("pluginMetadata_value", string(resp.Item.Value)),
+			zap.Error(err),
+		)
+		return nil, err
+	}
+	return pluginMetadata, nil
+}
+
+func (r *pluginMetadataClient) List(ctx context.Context) ([]*v1.PluginMetadata, error) {
+	log.Debugw("try to list pluginMetadatas in APISIX",
+		zap.String("cluster", "default"),
+		zap.String("url", r.url),
+	)
+	pluginMetadataItems, err := r.cluster.listResource(ctx, r.url, "pluginMetadata")
+	r.cluster.metricsCollector.IncrAPISIXRequest("pluginMetadata")
+	if err != nil {
+		log.Errorf("failed to list pluginMetadatas: %s", err)
+		return nil, err
+	}
+
+	var items []*v1.PluginMetadata
+	for i, item := range pluginMetadataItems.Node.Items {
+		pluginMetadata, err := item.pluginMetadata()
+		if err != nil {
+			log.Errorw("failed to convert pluginMetadata item",
+				zap.String("url", r.url),
+				zap.String("pluginMetadata_key", item.Key),
+				zap.String("pluginMetadata_value", string(item.Value)),
+				zap.Error(err),
+			)
+			return nil, err
+		}
+
+		items = append(items, pluginMetadata)
+		log.Debugf("list pluginMetadata #%d, body: %s", i, string(item.Value))
+	}
+
+	return items, nil
+}
+
+func (r *pluginMetadataClient) Create(ctx context.Context, obj *v1.PluginMetadata) (*v1.PluginMetadata, error) {
+	log.Debugw("try to create plugin metadata",
+		zap.String("name", obj.Name),
+		zap.String("cluster", "default"),
+		zap.String("url", r.url),
+	)
+
+	if err := r.cluster.HasSynced(ctx); err != nil {
+		return nil, err
+	}
+	data, err := json.Marshal(obj.Metadata)
+	if err != nil {
+		log.Errorw("json marshal failed", zap.Error(err))
+		return nil, err
+	}
+
+	url := r.url + "/" + obj.Name
+	log.Debugw("creating pluginMetadata", zap.ByteString("body", data), zap.String("url", url))
+	resp, err := r.cluster.createResource(ctx, url, "pluginMetadata", data)
+	r.cluster.metricsCollector.IncrAPISIXRequest("pluginMetadataClient")
+	if err != nil {
+		log.Errorf("failed to create pluginMetadata: %s", err)
+		return nil, err
+	}
+
+	pluginMetadata, err := resp.Item.pluginMetadata()
+	if err != nil {
+		return nil, err
+	}
+	return pluginMetadata, nil
+}

Review Comment:
   these should be removed.



##########
pkg/apisix/apisix.go:
##########
@@ -152,6 +154,14 @@ type PluginConfig interface {
 	Update(context.Context, *v1.PluginConfig) (*v1.PluginConfig, error)
 }
 
+type PluginMetadata interface {
+	Get(context.Context, string) (*v1.PluginMetadata, error)
+	List(context.Context) ([]*v1.PluginMetadata, error)
+	Create(context.Context, *v1.PluginMetadata) (*v1.PluginMetadata, error)
+	Delete(context.Context, *v1.PluginMetadata) error
+	Update(context.Context, *v1.PluginMetadata) (*v1.PluginMetadata, error)
+}

Review Comment:
   we only need Get/Update/Delete methods



##########
pkg/apisix/nonexistentclient.go:
##########
@@ -258,6 +260,25 @@ func (f *dummyUpstreamServiceRelation) Delete(_ context.Context, _ string) error
 	return ErrClusterNotExist
 }
 
+type dummyPluginMetadata struct {
+}
+
+func (f *dummyPluginMetadata) Get(_ context.Context, _ string) (*v1.PluginMetadata, error) {
+	return nil, ErrClusterNotExist
+}
+func (f *dummyPluginMetadata) Create(_ context.Context, _ *v1.PluginMetadata) (*v1.PluginMetadata, error) {
+	return nil, ErrClusterNotExist
+}
+func (f *dummyPluginMetadata) List(_ context.Context) ([]*v1.PluginMetadata, error) {
+	return nil, ErrClusterNotExist
+}
+func (f *dummyPluginMetadata) Delete(_ context.Context, _ *v1.PluginMetadata) error {
+	return ErrClusterNotExist
+}
+func (f *dummyPluginMetadata) Update(_ context.Context, _ *v1.PluginMetadata) (*v1.PluginMetadata, error) {
+	return nil, ErrClusterNotExist
+}

Review Comment:
   ditto



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org