Skip to content

Commit

Permalink
Merge branch 'upstream'
Browse files Browse the repository at this point in the history
* upstream:
  feat: support APISIX_PROFILE for env-specific configuration (apache#2293)
  • Loading branch information
Boolean committed Feb 6, 2022
2 parents 5071c2c + 88dadc2 commit 1e2473f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
2 changes: 1 addition & 1 deletion api/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ fi
cd ./api && go build -o ../output/manager-api -ldflags "${GOLDFLAGS}" ./main.go && cd ..

cp ./api/conf/schema.json ./output/conf/schema.json
cp ./api/conf/conf.yaml ./output/conf/conf.yaml
cp ./api/conf/conf*.yaml ./output/conf/

echo "Build the Manager API successfully"
12 changes: 6 additions & 6 deletions api/internal/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ func InitConf() {
func setupConfig() {
// setup config file path
if ConfigFile == "" {
viper.SetConfigName("conf")
ConfigFile = "conf.yaml"
if profile := os.Getenv("APISIX_PROFILE"); profile != "" {
ConfigFile = "conf" + "-" + profile + ".yaml"
}
viper.SetConfigName(ConfigFile)
viper.SetConfigType("yaml")
viper.AddConfigPath(WorkDir + "/conf")
} else {
Expand All @@ -160,11 +164,7 @@ func setupConfig() {

// load config
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
panic(fmt.Sprintf("fail to find configuration: %s", ConfigFile))
} else {
panic(fmt.Sprintf("fail to read configuration: %s, err: %s", ConfigFile, err.Error()))
}
panic(fmt.Sprintf("fail to read configuration, err: %s", err.Error()))
}

// unmarshal config
Expand Down
3 changes: 3 additions & 0 deletions api/internal/core/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"os"
"time"

"github.com/spf13/viper"

"github.com/apisix/manager-api/internal/conf"
"github.com/apisix/manager-api/internal/log"
"github.com/apisix/manager-api/internal/utils"
Expand Down Expand Up @@ -109,6 +111,7 @@ func (s *server) shutdownServer(server *http.Server) {
func (s *server) printInfo() {
fmt.Fprint(os.Stdout, "The manager-api is running successfully!\n\n")
utils.PrintVersion()
fmt.Fprintf(os.Stdout, "%-8s: %s\n", "Config File", viper.ConfigFileUsed())
fmt.Fprintf(os.Stdout, "%-8s: %s:%d\n", "Listen", conf.ServerHost, conf.ServerPort)
if conf.SSLCert != "" && conf.SSLKey != "" {
fmt.Fprintf(os.Stdout, "%-8s: %s:%d\n", "HTTPS Listen", conf.SSLHost, conf.SSLPort)
Expand Down
38 changes: 36 additions & 2 deletions api/test/shell/cli_test.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
VERSION=$(cat ./VERSION)
KERNEL=$(uname -s)
CONF_FILE="/usr/local/apisix-dashboard/conf/conf.yaml"
APISIX_PROFILE_CONF_FILE="/usr/local/apisix-dashboard/conf/conf-test.yaml"
LOG_FILE="/usr/local/apisix-dashboard/logs/error.log"
ACCESS_LOG_FILE="/usr/local/apisix-dashboard/logs/access.log"
SERVICE_NAME="apisix-dashboard"
Expand All @@ -47,6 +48,7 @@ fi

recover_conf() {
run cp -rf ./conf/conf.yaml ${CONF_FILE}
run cp -rf ./conf/conf.yaml ${APISIX_PROFILE_CONF_FILE}
[ "$status" -eq 0 ]
}
check_logfile() {
Expand All @@ -56,6 +58,12 @@ clean_logfile() {
echo > $LOG_FILE
}

recover_service_file() {
run cp -f ./service/apisix-dashboard.service /usr/lib/systemd/system/${SERVICE_NAME}.service
run systemctl daemon-reload
[ "$status" -eq 0 ]
}

start_dashboard() {
run systemctl start ${SERVICE_NAME}
[ "$status" -eq 0 ]
Expand Down Expand Up @@ -99,7 +107,7 @@ stop_dashboard() {
}

#2
@test "Check info log leve and signal" {
@test "Check info log level and signal" {
if [[ $KERNEL = "Darwin" ]]; then
sed -i "" 's/level: warn/level: info/' ${CONF_FILE}
else
Expand Down Expand Up @@ -241,7 +249,7 @@ stop_dashboard() {
recover_conf

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

# add root role
curl -L http://localhost:2379/v3/auth/role/add -d '{"name": "root"}'
Expand Down Expand Up @@ -420,6 +428,32 @@ stop_dashboard() {
stop_dashboard 6
}


#14
@test "Check APISIX_PROFILE" {
recover_conf

start_dashboard 3

run journalctl -u ${SERVICE_NAME}.service -n 30
[ $(echo "$output" | grep -c "conf.yaml") -eq '1' ]

stop_dashboard 3

sed -i 's#-c /usr/local/apisix-dashboard/conf/conf.yaml##g' /usr/lib/systemd/system/${SERVICE_NAME}.service
sed -i '$a\Environment=APISIX_PROFILE=test' /usr/lib/systemd/system/${SERVICE_NAME}.service
run systemctl daemon-reload

start_dashboard 3

run journalctl -u ${SERVICE_NAME}.service -n 30
[ $(echo "$output" | grep -c "conf-test.yaml") -eq '1' ]

stop_dashboard 3

recover_service_file
}

#post
@test "Clean test environment" {
# kill etcd
Expand Down

0 comments on commit 1e2473f

Please sign in to comment.