Skip to content

Commit 8980b08

Browse files
committed
feat:支持初始化管理员帐户
1 parent e8e9fb7 commit 8980b08

File tree

5 files changed

+107
-37
lines changed

5 files changed

+107
-37
lines changed

common/model/config_file.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,43 @@ func ToConfigFileTemplateStore(template *config_manage.ConfigFileTemplate) *Conf
550550
ModifyBy: template.ModifyBy.GetValue(),
551551
}
552552
}
553+
554+
type Subscriber struct {
555+
// 客户端 ID 信息
556+
ID string `json:"id"`
557+
// 客户端 Host 信息
558+
Host string `json:"host"`
559+
// 客户端版本
560+
Version string `json:"version"`
561+
// 客户端类型
562+
ClientType string `json:"client_type"`
563+
}
564+
565+
// ConfigSubscribers 以文件视角的监听数据
566+
type ConfigSubscribers struct {
567+
// key
568+
key ConfigFileKey
569+
// VersionClients 版本对应的客户端
570+
VersionClients []*struct {
571+
Versoin uint64 `json:"versoin"`
572+
Subscribers []Subscriber `json:"subscribers"`
573+
} `json:"clients"`
574+
}
575+
576+
// FileReleaseSubscribeInfo 文件订阅信息
577+
type FileReleaseSubscribeInfo struct {
578+
Id uint64 `json:"id"`
579+
Name string `json:"name"`
580+
Namespace string `json:"namespace"`
581+
Group string `json:"group"`
582+
FileName string `json:"file_name"`
583+
ReleaseType ReleaseType `json:"release_type"`
584+
Version uint64 `json:"version"`
585+
}
586+
587+
// ClientSubscriber 以客户端视角的监听数据
588+
type ClientSubscriber struct {
589+
Subscriber Subscriber `json:"subscriber"`
590+
// Files
591+
Files []FileReleaseSubscribeInfo `json:"files"`
592+
}

common/model/http.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ type DebugHandler struct {
2929
Path string
3030
Handler http.HandlerFunc
3131
}
32+
33+
type CommonResponse struct {
34+
Code uint32 `json:"code"`
35+
Info string `json:"info"`
36+
Data interface{} `json:"data"`
37+
}

config/client.go

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -206,41 +206,6 @@ func (s *Server) GetConfigGroupsWithCache(ctx context.Context, req *apiconfig.Cl
206206
return out
207207
}
208208

209-
func CompareByVersion(clientInfo *apiconfig.ClientConfigFileInfo, file *model.ConfigFileRelease) bool {
210-
return clientInfo.GetVersion().GetValue() < file.Version
211-
}
212-
213-
// only for unit test
214-
func (s *Server) checkClientConfigFile(ctx context.Context, files []*apiconfig.ClientConfigFileInfo,
215-
compartor CompareFunction) (*apiconfig.ConfigClientResponse, bool) {
216-
if len(files) == 0 {
217-
return api.NewConfigClientResponse(apimodel.Code_InvalidWatchConfigFileFormat, nil), false
218-
}
219-
for _, configFile := range files {
220-
namespace := configFile.GetNamespace().GetValue()
221-
group := configFile.GetGroup().GetValue()
222-
fileName := configFile.GetFileName().GetValue()
223-
224-
if namespace == "" || group == "" || fileName == "" {
225-
return api.NewConfigClientResponseWithInfo(apimodel.Code_BadRequest,
226-
"namespace & group & fileName can not be empty"), false
227-
}
228-
// 从缓存中获取最新的配置文件信息
229-
release := s.fileCache.GetActiveRelease(namespace, group, fileName)
230-
if release != nil && compartor(configFile, release) {
231-
ret := &apiconfig.ClientConfigFileInfo{
232-
Namespace: utils.NewStringValue(namespace),
233-
Group: utils.NewStringValue(group),
234-
FileName: utils.NewStringValue(fileName),
235-
Version: utils.NewUInt64Value(release.Version),
236-
Md5: utils.NewStringValue(release.Md5),
237-
}
238-
return api.NewConfigClientResponse(apimodel.Code_ExecuteSuccess, ret), false
239-
}
240-
}
241-
return api.NewConfigClientResponse(apimodel.Code_DataNoChange, nil), true
242-
}
243-
244209
func toClientInfo(client *apiconfig.ClientConfigFileInfo,
245210
release *model.ConfigFileRelease) (*apiconfig.ClientConfigFileInfo, error) {
246211

@@ -327,3 +292,13 @@ func (s *Server) PublishConfigFileFromClient(ctx context.Context,
327292
configResponse := s.PublishConfigFile(ctx, client)
328293
return api.NewConfigClientResponseFromConfigResponse(configResponse)
329294
}
295+
296+
// GetConfigSubscribers 根据配置视角获取订阅者列表
297+
func (s *Server) GetConfigSubscribers(ctx context.Context, filter map[string]string) *model.CommonResponse {
298+
return nil
299+
}
300+
301+
// GetConfigSubscribers 根据客户端视角获取订阅的配置文件列表
302+
func (s *Server) GetClientSubscribers(ctx context.Context, filter map[string]string) *model.CommonResponse {
303+
return nil
304+
}

config/test_export.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ import (
2222
"fmt"
2323

2424
apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
25+
apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
2526
"go.uber.org/zap"
2627

2728
"github.com/polarismesh/polaris/auth"
2829
"github.com/polarismesh/polaris/cache"
30+
api "github.com/polarismesh/polaris/common/api/v1"
2931
"github.com/polarismesh/polaris/common/model"
32+
"github.com/polarismesh/polaris/common/utils"
3033
"github.com/polarismesh/polaris/namespace"
3134
"github.com/polarismesh/polaris/plugin"
3235
"github.com/polarismesh/polaris/store"
@@ -67,11 +70,36 @@ func TestInitialize(ctx context.Context, config Config, s store.Store, cacheMgn
6770

6871
func (s *Server) TestCheckClientConfigFile(ctx context.Context, files []*apiconfig.ClientConfigFileInfo,
6972
compartor CompareFunction) (*apiconfig.ConfigClientResponse, bool) {
70-
return s.checkClientConfigFile(ctx, files, compartor)
73+
if len(files) == 0 {
74+
return api.NewConfigClientResponse(apimodel.Code_InvalidWatchConfigFileFormat, nil), false
75+
}
76+
for _, configFile := range files {
77+
namespace := configFile.GetNamespace().GetValue()
78+
group := configFile.GetGroup().GetValue()
79+
fileName := configFile.GetFileName().GetValue()
80+
81+
if namespace == "" || group == "" || fileName == "" {
82+
return api.NewConfigClientResponseWithInfo(apimodel.Code_BadRequest,
83+
"namespace & group & fileName can not be empty"), false
84+
}
85+
// 从缓存中获取最新的配置文件信息
86+
release := s.fileCache.GetActiveRelease(namespace, group, fileName)
87+
if release != nil && compartor(configFile, release) {
88+
ret := &apiconfig.ClientConfigFileInfo{
89+
Namespace: utils.NewStringValue(namespace),
90+
Group: utils.NewStringValue(group),
91+
FileName: utils.NewStringValue(fileName),
92+
Version: utils.NewUInt64Value(release.Version),
93+
Md5: utils.NewStringValue(release.Md5),
94+
}
95+
return api.NewConfigClientResponse(apimodel.Code_ExecuteSuccess, ret), false
96+
}
97+
}
98+
return api.NewConfigClientResponse(apimodel.Code_DataNoChange, nil), true
7199
}
72100

73101
func TestCompareByVersion(clientInfo *apiconfig.ClientConfigFileInfo, file *model.ConfigFileRelease) bool {
74-
return CompareByVersion(clientInfo, file)
102+
return clientInfo.GetVersion().GetValue() < file.Version
75103
}
76104

77105
// TestDecryptConfigFile 解密配置文件

service/client_info.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package service
1919

2020
import (
2121
"context"
22+
"sort"
2223

2324
apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
2425
apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
@@ -177,6 +178,26 @@ func ClientEquals(client1 *apiservice.Client, client2 *apiservice.Client) bool {
177178
if len(client1.Stat) != len(client2.Stat) {
178179
return false
179180
}
181+
182+
sortStat := func(stat []*apiservice.StatInfo) {
183+
sort.Slice(stat, func(i, j int) bool {
184+
if client1.Stat[i].GetTarget().GetValue() != client1.Stat[j].GetTarget().GetValue() {
185+
return client1.Stat[i].GetTarget().GetValue() < client1.Stat[j].GetTarget().GetValue()
186+
}
187+
if client1.Stat[i].GetPort().GetValue() != client1.Stat[j].GetPort().GetValue() {
188+
return client1.Stat[i].GetPort().GetValue() < client1.Stat[j].GetPort().GetValue()
189+
}
190+
if client1.Stat[i].GetPath().GetValue() != client1.Stat[j].GetPath().GetValue() {
191+
return client1.Stat[i].GetPath().GetValue() < client1.Stat[j].GetPath().GetValue()
192+
}
193+
return client1.Stat[i].GetProtocol().GetValue() < client1.Stat[j].GetProtocol().GetValue()
194+
})
195+
}
196+
197+
// 针对 client1 和 client2 的 stat 进行排序
198+
sortStat(client1.Stat)
199+
sortStat(client2.Stat)
200+
180201
for i := 0; i < len(client1.Stat); i++ {
181202
if client1.Stat[i].GetTarget().GetValue() != client2.Stat[i].GetTarget().GetValue() {
182203
return false

0 commit comments

Comments
 (0)