You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by ch...@apache.org on 2021/02/24 07:27:15 UTC

[apisix-dashboard] branch master updated: feat: support etcd prefix as apisix does (#1477)

This is an automated email from the ASF dual-hosted git repository.

chenjunxu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new e5f09e6  feat: support etcd prefix as apisix does (#1477)
e5f09e6 is described below

commit e5f09e6b0eb3b1e3607e2fed03208a86a20d5867
Author: liuyang211 <77...@users.noreply.github.com>
AuthorDate: Wed Feb 24 15:27:07 2021 +0800

    feat: support etcd prefix as apisix does (#1477)
---
 api/conf/conf.yaml                  |  2 +-
 api/internal/conf/conf.go           |  9 ++++-
 api/internal/core/store/storehub.go | 17 +++++----
 api/test/shell/cli_test.sh          | 74 ++++++++++++++++++++++++++++++++++++-
 4 files changed, 91 insertions(+), 11 deletions(-)

diff --git a/api/conf/conf.yaml b/api/conf/conf.yaml
index e96d662..68b1cad 100644
--- a/api/conf/conf.yaml
+++ b/api/conf/conf.yaml
@@ -32,7 +32,7 @@ conf:
       key_file: ""          # Path of your self-signed client side key
       cert_file: ""         # Path of your self-signed client side cert
       ca_file: ""           # Path of your self-signed ca cert, the CA is used to sign callers' certificates
-
+    # prefix: /apisix     # apisix config's prefix in etcd, /apisix by default
   log:
     error_log:
       level: warn       # supports levels, lower to higher: debug, info, warn, error, panic, fatal
diff --git a/api/internal/conf/conf.go b/api/internal/conf/conf.go
index ea4249f..4361e2a 100644
--- a/api/internal/conf/conf.go
+++ b/api/internal/conf/conf.go
@@ -68,6 +68,7 @@ type Etcd struct {
 	Username  string
 	Password  string
 	MTLS      *MTLS
+	Prefix    string
 }
 
 type Listen struct {
@@ -225,10 +226,16 @@ func initEtcdConfig(conf Etcd) {
 		endpoints = conf.Endpoints
 	}
 
+	prefix := "/apisix"
+	if len(conf.Prefix) > 0 {
+		prefix = conf.Prefix
+	}
+
 	ETCDConfig = &Etcd{
 		Endpoints: endpoints,
 		Username:  conf.Username,
 		Password:  conf.Password,
-		MTLS: conf.MTLS,
+		MTLS:      conf.MTLS,
+		Prefix:    prefix,
 	}
 }
diff --git a/api/internal/core/store/storehub.go b/api/internal/core/store/storehub.go
index f3a9c30..2af02da 100644
--- a/api/internal/core/store/storehub.go
+++ b/api/internal/core/store/storehub.go
@@ -20,6 +20,7 @@ import (
 	"fmt"
 	"reflect"
 
+	"github.com/apisix/manager-api/internal/conf"
 	"github.com/apisix/manager-api/internal/core/entity"
 	"github.com/apisix/manager-api/internal/log"
 	"github.com/apisix/manager-api/internal/utils"
@@ -82,7 +83,7 @@ func GetStore(key HubKey) *GenericStore {
 
 func InitStores() error {
 	err := InitStore(HubKeyConsumer, GenericStoreOption{
-		BasePath: "/apisix/consumers",
+		BasePath: conf.ETCDConfig.Prefix + "/consumers",
 		ObjType:  reflect.TypeOf(entity.Consumer{}),
 		KeyFunc: func(obj interface{}) string {
 			r := obj.(*entity.Consumer)
@@ -94,7 +95,7 @@ func InitStores() error {
 	}
 
 	err = InitStore(HubKeyRoute, GenericStoreOption{
-		BasePath: "/apisix/routes",
+		BasePath: conf.ETCDConfig.Prefix + "/routes",
 		ObjType:  reflect.TypeOf(entity.Route{}),
 		KeyFunc: func(obj interface{}) string {
 			r := obj.(*entity.Route)
@@ -106,7 +107,7 @@ func InitStores() error {
 	}
 
 	err = InitStore(HubKeyService, GenericStoreOption{
-		BasePath: "/apisix/services",
+		BasePath: conf.ETCDConfig.Prefix + "/services",
 		ObjType:  reflect.TypeOf(entity.Service{}),
 		KeyFunc: func(obj interface{}) string {
 			r := obj.(*entity.Service)
@@ -118,7 +119,7 @@ func InitStores() error {
 	}
 
 	err = InitStore(HubKeySsl, GenericStoreOption{
-		BasePath: "/apisix/ssl",
+		BasePath: conf.ETCDConfig.Prefix + "/ssl",
 		ObjType:  reflect.TypeOf(entity.SSL{}),
 		KeyFunc: func(obj interface{}) string {
 			r := obj.(*entity.SSL)
@@ -130,7 +131,7 @@ func InitStores() error {
 	}
 
 	err = InitStore(HubKeyUpstream, GenericStoreOption{
-		BasePath: "/apisix/upstreams",
+		BasePath: conf.ETCDConfig.Prefix + "/upstreams",
 		ObjType:  reflect.TypeOf(entity.Upstream{}),
 		KeyFunc: func(obj interface{}) string {
 			r := obj.(*entity.Upstream)
@@ -142,7 +143,7 @@ func InitStores() error {
 	}
 
 	err = InitStore(HubKeyScript, GenericStoreOption{
-		BasePath: "/apisix/scripts",
+		BasePath: conf.ETCDConfig.Prefix + "/scripts",
 		ObjType:  reflect.TypeOf(entity.Script{}),
 		KeyFunc: func(obj interface{}) string {
 			r := obj.(*entity.Script)
@@ -154,7 +155,7 @@ func InitStores() error {
 	}
 
 	err = InitStore(HubKeyGlobalRule, GenericStoreOption{
-		BasePath: "/apisix/global_rules",
+		BasePath: conf.ETCDConfig.Prefix + "/global_rules",
 		ObjType:  reflect.TypeOf(entity.GlobalPlugins{}),
 		KeyFunc: func(obj interface{}) string {
 			r := obj.(*entity.GlobalPlugins)
@@ -166,7 +167,7 @@ func InitStores() error {
 	}
 
 	err = InitStore(HubKeyServerInfo, GenericStoreOption{
-		BasePath: "/apisix/data_plane/server_info",
+		BasePath: conf.ETCDConfig.Prefix + "/data_plane/server_info",
 		ObjType:  reflect.TypeOf(entity.ServerInfo{}),
 		KeyFunc: func(obj interface{}) string {
 			r := obj.(*entity.ServerInfo)
diff --git a/api/test/shell/cli_test.sh b/api/test/shell/cli_test.sh
index 469b57d..75e3a32 100755
--- a/api/test/shell/cli_test.sh
+++ b/api/test/shell/cli_test.sh
@@ -341,6 +341,76 @@ fi
 check_logfile
 
 ./manager-api stop
+sleep 6
+clean_up
+
+# etcd prefix test
+# disable etcd autentication
+resp=$(curl -L http://localhost:2379/v3/auth/authenticate -X POST -d '{"name": "root", "password": "root"}')
+etcd_token=$(echo ${resp}|grep -oE "token\".*\"(.*)\""|awk -F[:\"] '{print $4}')
+if [ -z "${etcd_token}" ]; then
+    echo "authenticate failed"
+    exit 1
+fi
+curl -L http://localhost:2379/v3/auth/disable -H "Authorization: ${etcd_token}" -X POST -d ''
+
+./manager-api &
+sleep 3
+
+resp=$(curl http://127.0.0.1:9000/apisix/admin/user/login -H "Content-Type: application/json" -d '{"username":"admin", "password": "admin"}')
+token=$(echo "${resp}" | sed 's/{/\n/g' | sed 's/,/\n/g' | grep "token" | sed 's/:/\n/g' | sed '1d' | sed 's/}//g'  | sed 's/"//g')
+if [ -z "${token}" ]; then
+    echo "login failed"
+    exit 1
+fi
+# default etcd prefix value /apisix
+prefix=/apisix
+# add consumer by manager-api
+resp=$(curl -ig -XPUT http://127.0.0.1:9000/apisix/admin/consumers -i -H "Content-Type: application/json" -H "Authorization: $token" -d '{"username":"etcd_prefix_test"}')
+# get consumer by etcd v3 api
+key_base64=`echo -n $prefix/consumers/etcd_prefix_test | base64`
+resp=$(curl -L http://localhost:2379/v3/kv/range -X POST -d '{"key": "'"${key_base64}"'"}')
+count=`echo $resp | grep -oE "count.*([0-9]+)" | awk -F\" '{print $3}'`
+if [ ! $count ] || [ $count -ne 1 ]; then
+    echo "consumer failed"
+    exit 1
+fi
+
+./manager-api stop
+sleep 6
+
+clean_up
+
+# modify etcd prefix config to /apisix-test
+if [[ $KERNEL = "Darwin" ]]; then
+  sed -i "" '1,$s/# prefix: \/apisix.*/prefix: \/apisix-test/g' conf/conf.yaml
+else
+  sed -i '1,$s/# prefix: \/apisix.*/prefix: \/apisix-test/g' conf/conf.yaml
+fi
+
+./manager-api &
+sleep 3
+
+resp=$(curl http://127.0.0.1:9000/apisix/admin/user/login -H "Content-Type: application/json" -d '{"username":"admin", "password": "admin"}')
+token=$(echo "${resp}" | sed 's/{/\n/g' | sed 's/,/\n/g' | grep "token" | sed 's/:/\n/g' | sed '1d' | sed 's/}//g'  | sed 's/"//g')
+if [ -z "${token}" ]; then
+    echo "login failed"
+    exit 1
+fi
+# modified etcd prefix value /apisix-test
+prefix=/apisix-test
+# add consumer by manager-api
+resp=$(curl -ig -XPUT http://127.0.0.1:9000/apisix/admin/consumers -i -H "Content-Type: application/json" -H "Authorization: $token" -d '{"username":"etcd_prefix_test"}')
+# get consumer by etcd v3 api
+key_base64=`echo -n $prefix/consumers/etcd_prefix_test | base64`
+resp=$(curl -L http://localhost:2379/v3/kv/range -X POST -d '{"key": "'"${key_base64}"'"}')
+count=`echo $resp | grep -oE "count.*([0-9]+)" | awk -F\" '{print $3}'`
+if [ ! $count ] || [ $count -ne 1 ]; then
+    echo "consumer failed"
+    exit 1
+fi
+
+./manager-api stop
 clean_up
 
 # mtls test
@@ -382,7 +452,9 @@ if [ "$respCode" != "0" ] || [ $respMessage != "\"\"" ]; then
     exit 1
 fi
 
+./manager-api stop
+sleep 6
+
 pkill -f etcd
 
-./manager-api stop
 clean_up