Skip to content

Commit 6eeb2ec

Browse files
authored
Fix/sidecar xds (#1150)
1 parent 6b43fb6 commit 6eeb2ec

20 files changed

+1336
-1155
lines changed

apiserver/xdsserverv3/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (x *XDSServer) Initialize(ctx context.Context, option map[string]interface{
9696
x.versionNum = atomic.NewUint64(0)
9797
var err error
9898

99-
x.namingServer, err = service.GetServer()
99+
x.namingServer, err = service.GetOriginServer()
100100
if err != nil {
101101
log.Errorf("%v", err)
102102
return err

apiserver/xdsserverv3/sidecar.go

-8
Original file line numberDiff line numberDiff line change
@@ -525,14 +525,6 @@ func (x *XDSServer) makeSidecarRoutes(serviceInfo *ServiceInfo) []*route.Route {
525525
},
526526
},
527527
}
528-
limits, typedPerFilterConfig, err := x.makeSidecarLocalRateLimit(model.ServiceKey{
529-
Namespace: serviceInfo.Namespace,
530-
Name: serviceInfo.Name,
531-
})
532-
if err == nil {
533-
currentRoute.TypedPerFilterConfig = typedPerFilterConfig
534-
currentRoute.GetRoute().RateLimits = limits
535-
}
536528

537529
if matchAll {
538530
matchAllRoute = currentRoute

auth/defaultauth/strategy_test.go

-21
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,9 @@ func Test_UpdateStrategy(t *testing.T) {
458458
}()
459459

460460
strategyTest.strategies[2].Owner = strategyTest.users[2].ID
461-
462461
strategyTest.storage.EXPECT().GetStrategyDetail(gomock.Any()).Return(strategyTest.strategies[2], nil)
463-
464462
valCtx := context.WithValue(context.Background(), utils.ContextAuthTokenKey, strategyTest.users[0].Token)
465-
466463
strategyId := strategyTest.strategies[2].ID
467-
468464
resp := strategyTest.svr.UpdateStrategies(valCtx, []*apisecurity.ModifyAuthStrategy{
469465
{
470466
Id: &wrapperspb.StringValue{Value: strategyId},
@@ -484,11 +480,8 @@ func Test_UpdateStrategy(t *testing.T) {
484480
t.Run("更新鉴权策略-关联用户不存在", func(t *testing.T) {
485481

486482
strategyTest.storage.EXPECT().GetStrategyDetail(gomock.Any()).Return(strategyTest.strategies[0], nil)
487-
488483
valCtx := context.WithValue(context.Background(), utils.ContextAuthTokenKey, strategyTest.users[0].Token)
489-
490484
strategyId := strategyTest.strategies[0].ID
491-
492485
resp := strategyTest.svr.UpdateStrategies(valCtx, []*apisecurity.ModifyAuthStrategy{
493486
{
494487
Id: &wrapperspb.StringValue{Value: strategyId},
@@ -508,11 +501,8 @@ func Test_UpdateStrategy(t *testing.T) {
508501
t.Run("更新鉴权策略-关联用户组不存在", func(t *testing.T) {
509502

510503
strategyTest.storage.EXPECT().GetStrategyDetail(gomock.Any()).Return(strategyTest.strategies[0], nil)
511-
512504
valCtx := context.WithValue(context.Background(), utils.ContextAuthTokenKey, strategyTest.users[0].Token)
513-
514505
strategyId := strategyTest.strategies[0].ID
515-
516506
resp := strategyTest.svr.UpdateStrategies(valCtx, []*apisecurity.ModifyAuthStrategy{
517507
{
518508
Id: &wrapperspb.StringValue{Value: strategyId},
@@ -532,11 +522,8 @@ func Test_UpdateStrategy(t *testing.T) {
532522
t.Run("更新默认鉴权策略-不能更改principals成员", func(t *testing.T) {
533523

534524
strategyTest.storage.EXPECT().GetStrategyDetail(gomock.Any()).Return(strategyTest.defaultStrategies[0], nil)
535-
536525
valCtx := context.WithValue(context.Background(), utils.ContextAuthTokenKey, strategyTest.users[0].Token)
537-
538526
strategyId := strategyTest.defaultStrategies[0].ID
539-
540527
resp := strategyTest.svr.UpdateStrategies(valCtx, []*apisecurity.ModifyAuthStrategy{
541528
{
542529
Id: &wrapperspb.StringValue{Value: strategyId},
@@ -588,11 +575,8 @@ func Test_DeleteStrategy(t *testing.T) {
588575
t.Run("删除鉴权策略-目标策略不存在", func(t *testing.T) {
589576

590577
index := rand.Intn(len(strategyTest.strategies))
591-
592578
strategyTest.storage.EXPECT().GetStrategyDetail(gomock.Any()).Return(nil, nil)
593-
594579
valCtx := context.WithValue(context.Background(), utils.ContextAuthTokenKey, strategyTest.users[0].Token)
595-
596580
resp := strategyTest.svr.DeleteStrategies(valCtx, []*apisecurity.AuthStrategy{
597581
{Id: &wrapperspb.StringValue{Value: strategyTest.strategies[index].ID}},
598582
})
@@ -604,9 +588,7 @@ func Test_DeleteStrategy(t *testing.T) {
604588
index := rand.Intn(len(strategyTest.defaultStrategies))
605589

606590
strategyTest.storage.EXPECT().GetStrategyDetail(gomock.Any()).Return(strategyTest.defaultStrategies[index], nil)
607-
608591
valCtx := context.WithValue(context.Background(), utils.ContextAuthTokenKey, strategyTest.users[0].Token)
609-
610592
resp := strategyTest.svr.DeleteStrategies(valCtx, []*apisecurity.AuthStrategy{
611593
{Id: &wrapperspb.StringValue{Value: strategyTest.defaultStrategies[index].ID}},
612594
})
@@ -623,11 +605,8 @@ func Test_DeleteStrategy(t *testing.T) {
623605
}()
624606

625607
strategyTest.strategies[index].Owner = strategyTest.users[2].ID
626-
627608
strategyTest.storage.EXPECT().GetStrategyDetail(gomock.Any()).Return(strategyTest.strategies[index], nil)
628-
629609
valCtx := context.WithValue(context.Background(), utils.ContextAuthTokenKey, strategyTest.users[0].Token)
630-
631610
resp := strategyTest.svr.DeleteStrategies(valCtx, []*apisecurity.AuthStrategy{
632611
{Id: &wrapperspb.StringValue{Value: strategyTest.strategies[index].ID}},
633612
})

config/bootstrap_test.go

+7-215
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,21 @@
1515
* specific language governing permissions and limitations under the License.
1616
*/
1717

18-
package config
18+
package config_test
1919

2020
import (
21-
"context"
22-
"errors"
23-
"fmt"
24-
"os"
25-
"testing"
2621
"time"
2722

28-
"github.com/boltdb/bolt"
2923
"github.com/google/uuid"
3024
apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
31-
"gopkg.in/yaml.v2"
3225

3326
"github.com/polarismesh/polaris/auth"
3427
_ "github.com/polarismesh/polaris/auth/defaultauth"
3528
"github.com/polarismesh/polaris/cache"
3629
_ "github.com/polarismesh/polaris/cache"
3730
commonlog "github.com/polarismesh/polaris/common/log"
3831
"github.com/polarismesh/polaris/common/utils"
32+
"github.com/polarismesh/polaris/config"
3933
"github.com/polarismesh/polaris/namespace"
4034
"github.com/polarismesh/polaris/plugin"
4135
_ "github.com/polarismesh/polaris/plugin/crypto/aes"
@@ -44,11 +38,9 @@ import (
4438
_ "github.com/polarismesh/polaris/plugin/history/logger"
4539
_ "github.com/polarismesh/polaris/plugin/password"
4640
"github.com/polarismesh/polaris/store"
47-
"github.com/polarismesh/polaris/store/boltdb"
4841
_ "github.com/polarismesh/polaris/store/boltdb"
4942
_ "github.com/polarismesh/polaris/store/mysql"
50-
sqldb "github.com/polarismesh/polaris/store/mysql"
51-
testdata "github.com/polarismesh/polaris/test/data"
43+
testsuit "github.com/polarismesh/polaris/test/suit"
5244
)
5345

5446
type Bootstrap struct {
@@ -59,222 +51,22 @@ type TestConfig struct {
5951
Bootstrap Bootstrap `yaml:"bootstrap"`
6052
Cache cache.Config `yaml:"cache"`
6153
Namespace namespace.Config `yaml:"namespace"`
62-
Config Config `yaml:"config"`
54+
Config config.Config `yaml:"config"`
6355
Store store.Config `yaml:"store"`
6456
Auth auth.Config `yaml:"auth"`
6557
Plugin plugin.Config `yaml:"plugin"`
6658
}
6759

6860
type ConfigCenterTest struct {
69-
cfg *TestConfig
70-
testService ConfigCenterServer
71-
testServer *Server
72-
defaultCtx context.Context
73-
cancel context.CancelFunc
74-
storage store.Store
75-
}
76-
77-
func newConfigCenterTest(t *testing.T) (*ConfigCenterTest, error) {
78-
if err := os.RemoveAll("./config_center_test.bolt"); err != nil {
79-
return nil, err
80-
}
81-
82-
c := &ConfigCenterTest{
83-
defaultCtx: context.Background(),
84-
testServer: new(Server),
85-
cfg: new(TestConfig),
86-
}
87-
88-
if err := c.doInitialize(); err != nil {
89-
fmt.Printf("bootstrap config test module error. %s", err.Error())
90-
return nil, err
91-
}
92-
93-
return c, nil
94-
}
95-
96-
func (c *ConfigCenterTest) doInitialize() error {
97-
// 加载启动配置文件
98-
if err := c.loadBootstrapConfig(); err != nil {
99-
return err
100-
}
101-
_ = commonlog.Configure(c.cfg.Bootstrap.Logger)
102-
ctx, cancel := context.WithCancel(context.Background())
103-
c.cancel = cancel
104-
plugin.SetPluginConfig(&c.cfg.Plugin)
105-
106-
// 初始化存储层
107-
store.SetStoreConfig(&c.cfg.Store)
108-
s, err := store.TestGetStore()
109-
if err != nil {
110-
fmt.Printf("[ERROR] configure get store fail: %v\n", err)
111-
return err
112-
}
113-
c.storage = s
114-
115-
cacheMgr, err := cache.TestCacheInitialize(ctx, &c.cfg.Cache, s)
116-
if err != nil {
117-
fmt.Printf("[ERROR] configure init cache fail: %v\n", err)
118-
return err
119-
}
120-
121-
userMgn, strategyMgn, err := auth.TestInitialize(ctx, &c.cfg.Auth, s, cacheMgr)
122-
if err != nil {
123-
fmt.Printf("[ERROR] configure init auth fail: %v\n", err)
124-
return err
125-
}
126-
127-
nsOp, err := namespace.TestInitialize(ctx, &c.cfg.Namespace, s, cacheMgr, userMgn, strategyMgn)
128-
if err != nil {
129-
fmt.Printf("[ERROR] configure init namespace fail: %v\n", err)
130-
return err
131-
}
132-
133-
// 初始化配置中心模块
134-
if err := c.testServer.initialize(ctx, c.cfg.Config, s, nsOp, cacheMgr); err != nil {
135-
return err
136-
}
137-
c.testServer.initialized = true
138-
c.testService = newServerAuthAbility(c.testServer, userMgn, strategyMgn)
139-
140-
time.Sleep(5 * time.Second)
141-
142-
return nil
143-
}
144-
145-
func (c *ConfigCenterTest) loadBootstrapConfig() error {
146-
confFileName := testdata.Path("config_test.yaml")
147-
148-
// 初始化defaultCtx
149-
c.defaultCtx = context.WithValue(c.defaultCtx, utils.StringContext("request-id"), "config-test-request-id")
150-
c.defaultCtx = context.WithValue(c.defaultCtx, utils.ContextUserNameKey, "polaris")
151-
152-
if os.Getenv("STORE_MODE") == "sqldb" {
153-
fmt.Printf("run store mode : sqldb\n")
154-
confFileName = testdata.Path("config_test_sqldb.yaml")
155-
c.defaultCtx = context.WithValue(c.defaultCtx, utils.ContextAuthTokenKey, "nu/0WRA4EqSR1FagrjRj0fZwPXuGlMpX+zCuWu4uMqy8xr1vRjisSbA25aAC3mtU8MeeRsKhQiDAynUR09I=")
156-
} else {
157-
c.defaultCtx = context.WithValue(c.defaultCtx, utils.ContextAuthTokenKey, "nu/0WRA4EqSR1FagrjRj0fZwPXuGlMpX+zCuWu4uMqy8xr1vRjisSbA25aAC3mtU8MeeRsKhQiDAynUR09I=")
158-
}
159-
160-
file, err := os.Open(confFileName)
161-
if err != nil {
162-
fmt.Printf("[ERROR] %v\n", err)
163-
return err
164-
}
165-
166-
err = yaml.NewDecoder(file).Decode(c.cfg)
167-
if err != nil {
168-
fmt.Printf("[ERROR] %v\n", err)
169-
return err
170-
}
171-
172-
return err
61+
testsuit.DiscoverTestSuit
62+
cfg *TestConfig
17363
}
17464

17565
func (c *ConfigCenterTest) clearTestData() error {
17666
defer func() {
177-
c.cancel()
178-
time.Sleep(5 * time.Second)
179-
180-
c.storage.Destroy()
18167
time.Sleep(5 * time.Second)
18268
}()
183-
184-
if c.storage.Name() == sqldb.STORENAME {
185-
if err := c.clearTestDataWhenUseRDS(); err != nil {
186-
return err
187-
}
188-
} else if c.storage.Name() == boltdb.STORENAME {
189-
if err := c.clearTestDataWhenUseBoltdb(); err != nil {
190-
return err
191-
}
192-
} else {
193-
return errors.New("store impl unexpect")
194-
}
195-
196-
return nil
197-
}
198-
199-
func (c *ConfigCenterTest) clearTestDataWhenUseBoltdb() error {
200-
201-
proxyTx, err := c.storage.StartTx()
202-
if err != nil {
203-
return err
204-
}
205-
206-
tx := proxyTx.GetDelegateTx().(*bolt.Tx)
207-
208-
bucketName := []string{
209-
"ConfigFileGroup",
210-
"ConfigFileGroupID",
211-
"ConfigFile",
212-
"ConfigFileID",
213-
"ConfigFileReleaseHistory",
214-
"ConfigFileReleaseHistoryID",
215-
"ConfigFileRelease",
216-
"ConfigFileReleaseID",
217-
"ConfigFileTag",
218-
"ConfigFileTagID",
219-
"namespace",
220-
}
221-
222-
defer tx.Rollback()
223-
224-
for i := range bucketName {
225-
if err := tx.DeleteBucket([]byte(bucketName[i])); err != nil {
226-
if !errors.Is(err, bolt.ErrBucketNotFound) {
227-
return err
228-
}
229-
}
230-
}
231-
232-
return tx.Commit()
233-
}
234-
235-
func (c *ConfigCenterTest) clearTestDataWhenUseRDS() error {
236-
237-
proxyTx, err := c.storage.StartTx()
238-
if err != nil {
239-
return err
240-
}
241-
242-
tx := proxyTx.GetDelegateTx().(*sqldb.BaseTx)
243-
244-
defer tx.Rollback()
245-
246-
_, err = tx.Exec("delete from config_file_group where namespace = ? ", testNamespace)
247-
if err != nil {
248-
return err
249-
}
250-
_, err = tx.Exec("delete from config_file where namespace = ? ", testNamespace)
251-
if err != nil {
252-
return err
253-
}
254-
_, err = tx.Exec("delete from config_file_release where namespace = ? ", testNamespace)
255-
if err != nil {
256-
return err
257-
}
258-
_, err = tx.Exec("delete from config_file_release_history where namespace = ? ", testNamespace)
259-
if err != nil {
260-
return err
261-
}
262-
_, err = tx.Exec("delete from config_file_tag where namespace = ? ", testNamespace)
263-
if err != nil {
264-
return err
265-
}
266-
_, err = tx.Exec("delete from namespace where name = ? ", testNamespace)
267-
if err != nil {
268-
return err
269-
}
270-
_, err = tx.Exec("delete from config_file_template where name in (?,?) ", templateName1, templateName2)
271-
if err != nil {
272-
return err
273-
}
274-
// 清理缓存
275-
c.testServer.Cache().CleanAll()
276-
277-
return tx.Commit()
69+
return c.GetTestDataClean().ClearTestDataWhenUseRDS()
27870
}
27971

28072
func randomStr() string {

config/client_config_file.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (s *Server) WatchConfigFiles(ctx context.Context,
116116
clientAddr := utils.ParseClientAddress(ctx)
117117
watchFiles := request.GetWatchFiles()
118118
// 2. 检查客户端是否有版本落后
119-
if resp := s.doCheckClientConfigFile(ctx, watchFiles, compareByVersion); resp.Code.GetValue() != api.DataNoChange {
119+
if resp := s.DoCheckClientConfigFile(ctx, watchFiles, compareByVersion); resp.Code.GetValue() != api.DataNoChange {
120120
return func() *apiconfig.ConfigClientResponse {
121121
return resp
122122
}, nil
@@ -140,7 +140,7 @@ func compareByMD5(clientConfigFile *apiconfig.ClientConfigFileInfo, cacheEntry *
140140
return clientConfigFile.Md5.GetValue() != cacheEntry.Md5
141141
}
142142

143-
func (s *Server) doCheckClientConfigFile(ctx context.Context, configFiles []*apiconfig.ClientConfigFileInfo,
143+
func (s *Server) DoCheckClientConfigFile(ctx context.Context, configFiles []*apiconfig.ClientConfigFileInfo,
144144
compartor compareFunction) *apiconfig.ConfigClientResponse {
145145
if len(configFiles) == 0 {
146146
return api.NewConfigClientResponse(apimodel.Code_InvalidWatchConfigFileFormat, nil)

0 commit comments

Comments
 (0)