Skip to content
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

feat: support etcd prefix as apisix does #1477

Merged
merged 11 commits into from
Feb 24, 2021
2 changes: 1 addition & 1 deletion api/conf/conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion api/internal/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type Etcd struct {
Username string
Password string
MTLS *MTLS
Prefix string
}

type Listen struct {
Expand Down Expand Up @@ -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,
}
}
17 changes: 9 additions & 8 deletions api/internal/core/store/storehub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
74 changes: 73 additions & 1 deletion api/test/shell/cli_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,76 @@ fi

check_logfile

./manager-api stop
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
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

Expand Down Expand Up @@ -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