Skip to content

Commit 88dadc2

Browse files
authored
feat: support APISIX_PROFILE for env-specific configuration (#2293)
1 parent 33d4cee commit 88dadc2

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

api/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ fi
4545
cd ./api && go build -o ../output/manager-api -ldflags "${GOLDFLAGS}" ./main.go && cd ..
4646

4747
cp ./api/conf/schema.json ./output/conf/schema.json
48-
cp ./api/conf/conf.yaml ./output/conf/conf.yaml
48+
cp ./api/conf/conf*.yaml ./output/conf/
4949

5050
echo "Build the Manager API successfully"

api/internal/conf/conf.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,11 @@ func InitConf() {
151151
func setupConfig() {
152152
// setup config file path
153153
if ConfigFile == "" {
154-
viper.SetConfigName("conf")
154+
ConfigFile = "conf.yaml"
155+
if profile := os.Getenv("APISIX_PROFILE"); profile != "" {
156+
ConfigFile = "conf" + "-" + profile + ".yaml"
157+
}
158+
viper.SetConfigName(ConfigFile)
155159
viper.SetConfigType("yaml")
156160
viper.AddConfigPath(WorkDir + "/conf")
157161
} else {
@@ -160,11 +164,7 @@ func setupConfig() {
160164

161165
// load config
162166
if err := viper.ReadInConfig(); err != nil {
163-
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
164-
panic(fmt.Sprintf("fail to find configuration: %s", ConfigFile))
165-
} else {
166-
panic(fmt.Sprintf("fail to read configuration: %s, err: %s", ConfigFile, err.Error()))
167-
}
167+
panic(fmt.Sprintf("fail to read configuration, err: %s", err.Error()))
168168
}
169169

170170
// unmarshal config

api/internal/core/server/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"os"
2424
"time"
2525

26+
"github.com/spf13/viper"
27+
2628
"github.com/apisix/manager-api/internal/conf"
2729
"github.com/apisix/manager-api/internal/log"
2830
"github.com/apisix/manager-api/internal/utils"
@@ -109,6 +111,7 @@ func (s *server) shutdownServer(server *http.Server) {
109111
func (s *server) printInfo() {
110112
fmt.Fprint(os.Stdout, "The manager-api is running successfully!\n\n")
111113
utils.PrintVersion()
114+
fmt.Fprintf(os.Stdout, "%-8s: %s\n", "Config File", viper.ConfigFileUsed())
112115
fmt.Fprintf(os.Stdout, "%-8s: %s:%d\n", "Listen", conf.ServerHost, conf.ServerPort)
113116
if conf.SSLCert != "" && conf.SSLKey != "" {
114117
fmt.Fprintf(os.Stdout, "%-8s: %s:%d\n", "HTTPS Listen", conf.SSLHost, conf.SSLPort)

api/test/shell/cli_test.sh

100644100755
Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
VERSION=$(cat ./VERSION)
3232
KERNEL=$(uname -s)
3333
CONF_FILE="/usr/local/apisix-dashboard/conf/conf.yaml"
34+
APISIX_PROFILE_CONF_FILE="/usr/local/apisix-dashboard/conf/conf-test.yaml"
3435
LOG_FILE="/usr/local/apisix-dashboard/logs/error.log"
3536
ACCESS_LOG_FILE="/usr/local/apisix-dashboard/logs/access.log"
3637
SERVICE_NAME="apisix-dashboard"
@@ -47,6 +48,7 @@ fi
4748

4849
recover_conf() {
4950
run cp -rf ./conf/conf.yaml ${CONF_FILE}
51+
run cp -rf ./conf/conf.yaml ${APISIX_PROFILE_CONF_FILE}
5052
[ "$status" -eq 0 ]
5153
}
5254
check_logfile() {
@@ -56,6 +58,12 @@ clean_logfile() {
5658
echo > $LOG_FILE
5759
}
5860

61+
recover_service_file() {
62+
run cp -f ./service/apisix-dashboard.service /usr/lib/systemd/system/${SERVICE_NAME}.service
63+
run systemctl daemon-reload
64+
[ "$status" -eq 0 ]
65+
}
66+
5967
start_dashboard() {
6068
run systemctl start ${SERVICE_NAME}
6169
[ "$status" -eq 0 ]
@@ -99,7 +107,7 @@ stop_dashboard() {
99107
}
100108

101109
#2
102-
@test "Check info log leve and signal" {
110+
@test "Check info log level and signal" {
103111
if [[ $KERNEL = "Darwin" ]]; then
104112
sed -i "" 's/level: warn/level: info/' ${CONF_FILE}
105113
else
@@ -241,7 +249,7 @@ stop_dashboard() {
241249
recover_conf
242250

243251
# add root user
244-
curl -L http://localhost:2379/v3/auth/user/add -d '{"name": "root", "password": "root"}'
252+
curl -L http://localhost:2379/v3/auth/user/add -X POST -d '{"name": "root", "password": "root"}'
245253

246254
# add root role
247255
curl -L http://localhost:2379/v3/auth/role/add -d '{"name": "root"}'
@@ -420,6 +428,32 @@ stop_dashboard() {
420428
stop_dashboard 6
421429
}
422430

431+
432+
#14
433+
@test "Check APISIX_PROFILE" {
434+
recover_conf
435+
436+
start_dashboard 3
437+
438+
run journalctl -u ${SERVICE_NAME}.service -n 30
439+
[ $(echo "$output" | grep -c "conf.yaml") -eq '1' ]
440+
441+
stop_dashboard 3
442+
443+
sed -i 's#-c /usr/local/apisix-dashboard/conf/conf.yaml##g' /usr/lib/systemd/system/${SERVICE_NAME}.service
444+
sed -i '$a\Environment=APISIX_PROFILE=test' /usr/lib/systemd/system/${SERVICE_NAME}.service
445+
run systemctl daemon-reload
446+
447+
start_dashboard 3
448+
449+
run journalctl -u ${SERVICE_NAME}.service -n 30
450+
[ $(echo "$output" | grep -c "conf-test.yaml") -eq '1' ]
451+
452+
stop_dashboard 3
453+
454+
recover_service_file
455+
}
456+
423457
#post
424458
@test "Clean test environment" {
425459
# kill etcd

0 commit comments

Comments
 (0)