Skip to content

Commit e5ea75f

Browse files
authored
fix: Fix some errors in nacosREST (#54)
1. Skip the __names__ item when enumerating all configs 2. Allow changing some Nacos options via environment variables 3. Use local Nacos config cache as a fallback by default
1 parent d383cf3 commit e5ea75f

File tree

3 files changed

+73
-17
lines changed

3 files changed

+73
-17
lines changed

src/apiserver/pkg/options/options.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
1010
"github.com/nacos-group/nacos-sdk-go/v2/vo"
1111
"github.com/spf13/pflag"
12+
"k8s.io/klog/v2"
1213
"net/url"
1314
"os"
1415
"strconv"
@@ -20,6 +21,18 @@ const (
2021
Storage_Nacos = "nacos"
2122
)
2223

24+
var (
25+
NacosDisableUseSnapShot = utils.GetBoolFromEnv("NACOS_DISABLE_USE_SNAPSHOT", false)
26+
NacosListRefreshIntervalSecs = utils.GetIntFromEnv("NACOS_LIST_REFRESH_INTERVAL_SECS", 10)
27+
NacosConfigSearchPageSize = utils.GetIntFromEnv("NACOS_CONFIG_SEARCH_PAGE_SIZE", 50)
28+
)
29+
30+
func init() {
31+
klog.Infof("NacosDisableUseSnapShot: %v", NacosDisableUseSnapShot)
32+
klog.Infof("NacosListRefreshIntervalSecs: %v", NacosListRefreshIntervalSecs)
33+
klog.Infof("NacosConfigSearchPageSize: %v", NacosConfigSearchPageSize)
34+
}
35+
2336
func CreateAuthOptions() *AuthOptions {
2437
return &AuthOptions{}
2538
}
@@ -225,8 +238,7 @@ func (o *NacosOptions) CreateConfigClient() (config_client.IConfigClient, error)
225238
constant.WithLogDir(o.LogDir),
226239
constant.WithCacheDir(o.CacheDir),
227240
constant.WithLogLevel("info"),
228-
// Ignore snapshot so we can get the latest config right after making any change.
229-
constant.WithDisableUseSnapShot(true),
241+
constant.WithDisableUseSnapShot(NacosDisableUseSnapShot),
230242
)
231243

232244
var serverConfigs []constant.ServerConfig

src/apiserver/pkg/registry/nacos_rest.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"sync/atomic"
1515
"time"
1616

17+
"github.com/alibaba/higress/api-server/pkg/options"
1718
"github.com/alibaba/higress/api-server/pkg/utils"
1819
"github.com/nacos-group/nacos-sdk-go/v2/clients/config_client"
1920
"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
@@ -38,8 +39,6 @@ import (
3839

3940
const dataIdSeparator = "."
4041
const wildcardSuffix = dataIdSeparator + "*"
41-
const searchPageSize = 50
42-
const listRefreshInterval = 10 * time.Second
4342
const encryptionMark = "enc|"
4443
const namesSuffix = "__names__"
4544
const namesGroup = constant.DEFAULT_GROUP
@@ -133,7 +132,7 @@ func (n *nacosREST) startBackgroundWatcher() {
133132
return
134133
}
135134

136-
n.listRefreshTicker = time.NewTicker(listRefreshInterval)
135+
n.listRefreshTicker = time.NewTicker(time.Duration(options.NacosListRefreshIntervalSecs) * time.Second)
137136
go func(n *nacosREST) {
138137
for {
139138
<-n.listRefreshTicker.C
@@ -229,17 +228,16 @@ func (n *nacosREST) List(
229228
ns, _ := genericapirequest.NamespaceFrom(ctx)
230229

231230
searchConfigParam := vo.SearchConfigParam{
232-
Search: "blur",
233-
DataId: n.dataIdPrefix + wildcardSuffix,
234-
Group: ns,
235-
PageSize: searchPageSize,
231+
Search: "blur",
232+
DataId: n.dataIdPrefix + wildcardSuffix,
233+
Group: ns,
236234
}
237235
predicate := n.buildListPredicate(options)
238236
count := 0
239237
err = n.enumerateConfigs(&searchConfigParam, func(item *model.ConfigItem) {
240238
obj, err := n.decodeConfig(n.codec, item.Content, n.newFunc)
241239
if obj == nil || err != nil {
242-
klog.Errorf("failed to decode config %s/%s: %v", item.Group, item.DataId, err)
240+
klog.Errorf("failed to decode config [#3] %s/%s: %v", item.Group, item.DataId, err)
243241
return
244242
}
245243
if ok, err := predicate.Matches(obj); err == nil && ok {
@@ -499,6 +497,9 @@ func (n *nacosREST) buildListPredicate(options *metainternalversion.ListOptions)
499497
func (n *nacosREST) enumerateConfigs(param *vo.SearchConfigParam, action func(*model.ConfigItem)) error {
500498
searchConfigParam := *param
501499
searchConfigParam.PageNo = 1
500+
if searchConfigParam.PageSize < options.NacosConfigSearchPageSize {
501+
searchConfigParam.PageSize = options.NacosConfigSearchPageSize
502+
}
502503
for {
503504
page, err := n.configClient.SearchConfig(searchConfigParam)
504505
if err != nil {
@@ -510,6 +511,9 @@ func (n *nacosREST) enumerateConfigs(param *vo.SearchConfigParam, action func(*m
510511
}
511512

512513
for _, item := range page.PageItems {
514+
if item.Group == namesGroup && item.DataId == n.namesDataId {
515+
continue
516+
}
513517
localItem := *(&item)
514518
action(&localItem)
515519
}
@@ -533,7 +537,7 @@ func (n *nacosREST) read(decoder runtime.Decoder, group, dataId string, newFunc
533537
}
534538
obj, err := n.decodeConfig(decoder, config, newFunc)
535539
if err != nil {
536-
klog.Errorf("failed to decode config %s: %v", dataId, err)
540+
klog.Errorf("failed to decode config #4 %s: %v", dataId, err)
537541
return nil, config, err
538542
}
539543
return obj, config, nil
@@ -549,10 +553,12 @@ func (n *nacosREST) readRaw(group, dataId string) (string, error) {
549553
func (n *nacosREST) decodeConfig(decoder runtime.Decoder, config string, newFunc func() runtime.Object) (runtime.Object, error) {
550554
decryptedConfig, err := n.decryptConfig(config)
551555
if err != nil {
556+
klog.Infof("failed to decoded config #1: %v\n%s", err, config)
552557
return nil, err
553558
}
554559
obj, _, err := decoder.Decode([]byte(decryptedConfig), nil, newFunc())
555560
if err != nil {
561+
klog.Infof("failed to decoded config #2: %v\n%s", err, config)
556562
return nil, err
557563
}
558564
accessor, err := meta.Accessor(obj)
@@ -606,9 +612,6 @@ func (n *nacosREST) refreshConfigList() {
606612
Search: "blur",
607613
DataId: n.dataIdPrefix + wildcardSuffix,
608614
}, func(item *model.ConfigItem) {
609-
if item.Group == namesGroup && item.DataId == n.namesDataId {
610-
return
611-
}
612615
key := item.Group + "/" + item.DataId
613616
allConfigKeys = append(allConfigKeys, key)
614617
if _, ok := n.configItems[key]; !ok {
@@ -644,7 +647,7 @@ func (n *nacosREST) refreshConfigList() {
644647
configItem := configItems[key]
645648
obj, err := n.decodeConfig(n.codec, configItem.Content, n.newFunc)
646649
if err != nil {
647-
klog.Errorf("failed to decode config %s: %v", configItem.DataId, err)
650+
klog.Errorf("failed to decode config #5 %s: %v", configItem.DataId, err)
648651
delete(configItems, key)
649652
continue
650653
}
@@ -659,7 +662,7 @@ func (n *nacosREST) refreshConfigList() {
659662
OnChange: func(namespace, group, dataId, data string) {
660663
obj, err := n.decodeConfig(n.codec, data, n.newFunc)
661664
if err != nil {
662-
klog.Errorf("failed to decode config %s: %v", dataId, err)
665+
klog.Errorf("failed to decode config #6 %s: %v", dataId, err)
663666
return
664667
}
665668
klog.Infof("%s/%s is changed", group, dataId)
@@ -677,7 +680,7 @@ func (n *nacosREST) refreshConfigList() {
677680
configItem := n.configItems[key]
678681
obj, err := n.decodeConfig(n.codec, configItem.Content, n.newFunc)
679682
if err != nil {
680-
klog.Errorf("failed to decode config %s: %v", configItem.DataId, err)
683+
klog.Errorf("failed to decode config #7 %s: %v", configItem.DataId, err)
681684
continue
682685
}
683686
_ = n.configClient.CancelListenConfig(vo.ConfigParam{

src/apiserver/pkg/utils/env.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package utils
2+
3+
import (
4+
"os"
5+
"strconv"
6+
)
7+
8+
func GetStringFromEnv(key, defaultValue string) string {
9+
if len(key) == 0 {
10+
return defaultValue
11+
}
12+
value := os.Getenv(key)
13+
if len(value) != 0 {
14+
return value
15+
}
16+
return defaultValue
17+
}
18+
19+
func GetIntFromEnv(key string, defaultValue int) int {
20+
strValue := GetStringFromEnv(key, "")
21+
if len(strValue) == 0 {
22+
return defaultValue
23+
}
24+
if value, err := strconv.Atoi(strValue); err != nil {
25+
return defaultValue
26+
} else {
27+
return value
28+
}
29+
}
30+
31+
func GetBoolFromEnv(key string, defaultValue bool) bool {
32+
strValue := GetStringFromEnv(key, "")
33+
if len(strValue) == 0 {
34+
return defaultValue
35+
}
36+
if value, err := strconv.ParseBool(strValue); err != nil {
37+
return defaultValue
38+
} else {
39+
return value
40+
}
41+
}

0 commit comments

Comments
 (0)