Skip to content

Commit 0b00ebb

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

File tree

11 files changed

+213
-11
lines changed

11 files changed

+213
-11
lines changed

apiserver/nacosserver/v1/config/watch.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
)
3030

3131
type LongPollWatchContext struct {
32+
lock sync.RWMutex
3233
clientId string
3334
labels map[string]string
3435
once sync.Once
@@ -76,6 +77,9 @@ func (c *LongPollWatchContext) ClientID() string {
7677

7778
// ShouldNotify .
7879
func (c *LongPollWatchContext) ShouldNotify(event *model.SimpleConfigFileRelease) bool {
80+
c.lock.RLock()
81+
defer c.lock.RUnlock()
82+
7983
key := event.FileKey()
8084
watchFile, ok := c.watchConfigFiles[key]
8185
if !ok {
@@ -90,21 +94,37 @@ func (c *LongPollWatchContext) ShouldNotify(event *model.SimpleConfigFileRelease
9094
}
9195

9296
func (c *LongPollWatchContext) ListWatchFiles() []*config_manage.ClientConfigFileInfo {
97+
c.lock.RLock()
98+
defer c.lock.RUnlock()
99+
93100
ret := make([]*config_manage.ClientConfigFileInfo, 0, len(c.watchConfigFiles))
94101
for _, v := range c.watchConfigFiles {
95102
ret = append(ret, v)
96103
}
97104
return ret
98105
}
99106

107+
func (c *LongPollWatchContext) CurWatchVersion(k string) uint64 {
108+
c.lock.RLock()
109+
defer c.lock.RUnlock()
110+
111+
return c.watchConfigFiles[k].GetVersion().GetValue()
112+
}
113+
100114
// AppendInterest .
101115
func (c *LongPollWatchContext) AppendInterest(item *config_manage.ClientConfigFileInfo) {
116+
c.lock.Lock()
117+
defer c.lock.Unlock()
118+
102119
key := model.BuildKeyForClientConfigFileInfo(item)
103120
c.watchConfigFiles[key] = item
104121
}
105122

106123
// RemoveInterest .
107124
func (c *LongPollWatchContext) RemoveInterest(item *config_manage.ClientConfigFileInfo) {
125+
c.lock.Lock()
126+
defer c.lock.Unlock()
127+
108128
key := model.BuildKeyForClientConfigFileInfo(item)
109129
delete(c.watchConfigFiles, key)
110130
}

apiserver/nacosserver/v2/config/watch.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ func (c *StreamWatchContext) ListWatchFiles() []*apiconfig.ClientConfigFileInfo
123123
return c.watchConfigFiles.Values()
124124
}
125125

126+
func (c *StreamWatchContext) CurWatchVersion(k string) uint64 {
127+
val, ok := c.watchConfigFiles.Load(k)
128+
if !ok {
129+
return 0
130+
}
131+
return val.GetVersion().GetValue()
132+
}
133+
126134
// AppendInterest .
127135
func (c *StreamWatchContext) AppendInterest(item *apiconfig.ClientConfigFileInfo) {
128136
key := model.BuildKeyForClientConfigFileInfo(item)

cache/api/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ type (
318318
QueryInstances(filter, metaFilter map[string]string, offset, limit uint32) (uint32, []*model.Instance, error)
319319
// DiscoverServiceInstances 服务发现获取实例
320320
DiscoverServiceInstances(serviceID string, onlyHealthy bool) []*model.Instance
321-
// RemoveService
321+
// RemoveService
322322
RemoveService(serviceID string)
323323
}
324324
)

common/model/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,6 @@ const (
141141
ClientLabel_Version = "CLIENT_VERSION"
142142
// ClientLabel_Language 客户端语言
143143
ClientLabel_Language = "CLIENT_LANGUAGE"
144+
// ClientLabel_Host 客户端主机名
145+
ClientLabel_Host = "CLIENT_HOST"
144146
)

common/model/config_file.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -565,17 +565,18 @@ type Subscriber struct {
565565
// ConfigSubscribers 以文件视角的监听数据
566566
type ConfigSubscribers struct {
567567
// key
568-
key ConfigFileKey
568+
Key ConfigFileKey
569569
// VersionClients 版本对应的客户端
570-
VersionClients []*struct {
571-
Versoin uint64 `json:"versoin"`
572-
Subscribers []Subscriber `json:"subscribers"`
573-
} `json:"clients"`
570+
VersionClients []*VersionClient `json:"clients"`
571+
}
572+
573+
type VersionClient struct {
574+
Versoin uint64 `json:"versoin"`
575+
Subscribers []*Subscriber `json:"subscribers"`
574576
}
575577

576578
// FileReleaseSubscribeInfo 文件订阅信息
577579
type FileReleaseSubscribeInfo struct {
578-
Id uint64 `json:"id"`
579580
Name string `json:"name"`
580581
Namespace string `json:"namespace"`
581582
Group string `json:"group"`

common/model/http.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717

1818
package model
1919

20-
import "net/http"
20+
import (
21+
"net/http"
22+
23+
api "github.com/polarismesh/polaris/common/api/v1"
24+
)
2125

2226
type DebugHandlerGroup struct {
2327
Name string
@@ -35,3 +39,10 @@ type CommonResponse struct {
3539
Info string `json:"info"`
3640
Data interface{} `json:"data"`
3741
}
42+
43+
func NewCommonResponse(code uint32) *CommonResponse {
44+
return &CommonResponse{
45+
Code: code,
46+
Info: api.Code2Info(code),
47+
}
48+
}

config/api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ type ConfigFileOperate interface {
6969
configFiles []*apiconfig.ConfigFile, conflictHandling string) *apiconfig.ConfigImportResponse
7070
// GetAllConfigEncryptAlgorithms 获取配置加密算法
7171
GetAllConfigEncryptAlgorithms(ctx context.Context) *apiconfig.ConfigEncryptAlgorithmResponse
72+
// GetClientSubscribers 获取客户端订阅者
73+
GetClientSubscribers(ctx context.Context, filter map[string]string) *model.CommonResponse
74+
// GetConfigSubscribers 获取配置订阅者
75+
GetConfigSubscribers(ctx context.Context, filter map[string]string) *model.CommonResponse
7276
}
7377

7478
// ConfigFileReleaseOperate 配置文件发布接口

config/client.go

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,103 @@ func (s *Server) PublishConfigFileFromClient(ctx context.Context,
295295

296296
// GetConfigSubscribers 根据配置视角获取订阅者列表
297297
func (s *Server) GetConfigSubscribers(ctx context.Context, filter map[string]string) *model.CommonResponse {
298-
return nil
298+
namespace := filter["namespace"]
299+
group := filter["group"]
300+
fileName := filter["file_name"]
301+
302+
key := utils.GenFileId(namespace, group, fileName)
303+
clientIds, _ := s.watchCenter.watchers.Load(key)
304+
if clientIds == nil {
305+
return model.NewCommonResponse(uint32(apimodel.Code_NotFoundResource))
306+
}
307+
308+
versionClients := map[uint64][]*model.Subscriber{}
309+
clientIds.Range(func(val string) {
310+
watchCtx, ok := s.watchCenter.clients.Load(val)
311+
if !ok {
312+
return
313+
}
314+
curVer := watchCtx.CurWatchVersion(key)
315+
if _, ok := versionClients[curVer]; !ok {
316+
versionClients[curVer] = []*model.Subscriber{}
317+
}
318+
319+
watchCtx.ClientLabels()
320+
321+
versionClients[curVer] = append(versionClients[curVer], &model.Subscriber{
322+
ID: watchCtx.ClientID(),
323+
Host: watchCtx.ClientLabels()[model.ClientLabel_Host],
324+
Version: watchCtx.ClientLabels()[model.ClientLabel_Version],
325+
ClientType: watchCtx.ClientLabels()[model.ClientLabel_Language],
326+
})
327+
})
328+
329+
rsp := model.NewCommonResponse(uint32(apimodel.Code_ExecuteSuccess))
330+
rsp.Data = &model.ConfigSubscribers{
331+
Key: model.ConfigFileKey{
332+
Namespace: namespace,
333+
Group: group,
334+
Name: fileName,
335+
},
336+
VersionClients: func() []*model.VersionClient {
337+
ret := make([]*model.VersionClient, 0, len(versionClients))
338+
for ver, clients := range versionClients {
339+
ret = append(ret, &model.VersionClient{
340+
Versoin: ver,
341+
Subscribers: clients,
342+
})
343+
}
344+
return ret
345+
}(),
346+
}
347+
return rsp
299348
}
300349

301-
// GetConfigSubscribers 根据客户端视角获取订阅的配置文件列表
350+
// GetClientSubscribers 根据客户端视角获取订阅的配置文件列表
302351
func (s *Server) GetClientSubscribers(ctx context.Context, filter map[string]string) *model.CommonResponse {
303-
return nil
352+
clientId := filter["client_id"]
353+
watchCtx, ok := s.watchCenter.clients.Load(clientId)
354+
if !ok {
355+
return model.NewCommonResponse(uint32(apimodel.Code_NotFoundResource))
356+
}
357+
358+
watchFiles := watchCtx.ListWatchFiles()
359+
data := &model.ClientSubscriber{
360+
Subscriber: model.Subscriber{
361+
ID: watchCtx.ClientID(),
362+
Host: watchCtx.ClientLabels()[model.ClientLabel_Host],
363+
Version: watchCtx.ClientLabels()[model.ClientLabel_Version],
364+
ClientType: watchCtx.ClientLabels()[model.ClientLabel_Language],
365+
},
366+
Files: []model.FileReleaseSubscribeInfo{},
367+
}
368+
369+
for _, file := range watchFiles {
370+
key := model.BuildKeyForClientConfigFileInfo(file)
371+
curVer := watchCtx.CurWatchVersion(key)
372+
373+
ns := file.GetNamespace().GetValue()
374+
group := file.GetGroup().GetValue()
375+
filename := file.GetFileName().GetValue()
376+
377+
data.Files = append(data.Files, model.FileReleaseSubscribeInfo{
378+
Name: file.GetName().GetValue(),
379+
Namespace: ns,
380+
Group: group,
381+
FileName: filename,
382+
ReleaseType: func() model.ReleaseType {
383+
if gray := s.fileCache.GetActiveGrayRelease(ns, group, filename); gray != nil {
384+
if gray.Version == curVer {
385+
return model.ReleaseTypeGray
386+
}
387+
}
388+
return model.ReleaseTypeFull
389+
}(),
390+
Version: curVer,
391+
})
392+
}
393+
394+
rsp := model.NewCommonResponse(uint32(apimodel.Code_ExecuteSuccess))
395+
rsp.Data = data
396+
return rsp
304397
}

config/interceptor/auth/config_file.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
2424

2525
api "github.com/polarismesh/polaris/common/api/v1"
26+
"github.com/polarismesh/polaris/common/model"
2627
"github.com/polarismesh/polaris/common/model/auth"
2728
"github.com/polarismesh/polaris/common/utils"
2829
)
@@ -152,3 +153,13 @@ func (s *Server) GetAllConfigEncryptAlgorithms(
152153
ctx context.Context) *apiconfig.ConfigEncryptAlgorithmResponse {
153154
return s.nextServer.GetAllConfigEncryptAlgorithms(ctx)
154155
}
156+
157+
// GetClientSubscribers 获取客户端订阅者
158+
func (s *Server) GetClientSubscribers(ctx context.Context, filter map[string]string) *model.CommonResponse {
159+
return s.nextServer.GetClientSubscribers(ctx, filter)
160+
}
161+
162+
// GetConfigSubscribers 获取配置订阅者
163+
func (s *Server) GetConfigSubscribers(ctx context.Context, filter map[string]string) *model.CommonResponse {
164+
return s.nextServer.GetConfigSubscribers(ctx, filter)
165+
}

config/interceptor/paramcheck/config_file_check.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ import (
2323

2424
apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
2525
apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
26+
"google.golang.org/protobuf/types/known/wrapperspb"
2627

2728
api "github.com/polarismesh/polaris/common/api/v1"
29+
"github.com/polarismesh/polaris/common/model"
2830
"github.com/polarismesh/polaris/common/utils"
2931
)
3032

@@ -115,3 +117,31 @@ func (s *Server) GetAllConfigEncryptAlgorithms(
115117
ctx context.Context) *apiconfig.ConfigEncryptAlgorithmResponse {
116118
return s.nextServer.GetAllConfigEncryptAlgorithms(ctx)
117119
}
120+
121+
// GetClientSubscribers 获取客户端订阅者
122+
func (s *Server) GetClientSubscribers(ctx context.Context, filter map[string]string) *model.CommonResponse {
123+
clientId := filter["client_id"]
124+
if clientId == "" {
125+
return model.NewCommonResponse(uint32(apimodel.Code_BadRequest))
126+
}
127+
return s.nextServer.GetClientSubscribers(ctx, filter)
128+
}
129+
130+
// GetConfigSubscribers 获取配置订阅者
131+
func (s *Server) GetConfigSubscribers(ctx context.Context, filter map[string]string) *model.CommonResponse {
132+
namespace := filter["namespace"]
133+
group := filter["group"]
134+
fileName := filter["file_name"]
135+
136+
if err := CheckFileName(wrapperspb.String(fileName)); err != nil {
137+
return model.NewCommonResponse(uint32(apimodel.Code_InvalidConfigFileName))
138+
}
139+
if err := utils.CheckResourceName(wrapperspb.String(group)); err != nil {
140+
return model.NewCommonResponse(uint32(apimodel.Code_InvalidConfigFileGroupName))
141+
}
142+
if err := utils.CheckResourceName(wrapperspb.String(namespace)); err != nil {
143+
return model.NewCommonResponse(uint32(apimodel.Code_InvalidNamespaceName))
144+
}
145+
146+
return s.nextServer.GetConfigSubscribers(ctx, filter)
147+
}

0 commit comments

Comments
 (0)