Skip to content

Commit

Permalink
fix:持久化配置字段名调整
Browse files Browse the repository at this point in the history
  • Loading branch information
codyyu committed Jan 8, 2025
1 parent 48fdb45 commit 2644044
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 85 deletions.
22 changes: 5 additions & 17 deletions pkg/flow/configuration/file_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type ConfigFileRepo struct {
}

// ConfigFileRepoChangeListener 远程配置文件发布监听器
type ConfigFileRepoChangeListener func(configFileMetadata model.ConfigFileMetadata, newContent string, saveFilePath string, saveFileEncoding string, saveFilePostCmd string) error
type ConfigFileRepoChangeListener func(configFileMetadata model.ConfigFileMetadata, newContent string, persistent model.Persistent) error

// newConfigFileRepo 创建远程配置文件
func newConfigFileRepo(metadata model.ConfigFileMetadata,
Expand Down Expand Up @@ -113,22 +113,10 @@ func (r *ConfigFileRepo) loadRemoteFile() *configconnector.ConfigFile {
return val.(*configconnector.ConfigFile)
}

// GetFilePath 获取配置文件路径
func (r *ConfigFileRepo) GetFilePath() string {
// GetPersistent 获取配置文件持久化配置
func (r *ConfigFileRepo) GetPersistent() model.Persistent {
remoteFile := r.loadRemoteFile()
return remoteFile.GetFilePath()
}

// GetFileEncoding 获取文件编码
func (r *ConfigFileRepo) GetFileEncoding() string {
remoteFile := r.loadRemoteFile()
return remoteFile.GetFileEncoding()
}

// GetFilePostCmd 获取文件后置脚本
func (r *ConfigFileRepo) GetFilePostCmd() string {
remoteFile := r.loadRemoteFile()
return remoteFile.GetFilePostCmd()
return remoteFile.GetPersistent()
}

// GetContent 获取配置文件内容
Expand Down Expand Up @@ -342,7 +330,7 @@ func (r *ConfigFileRepo) fireChangeEvent(f *configconnector.ConfigFile) {
}

for _, listener := range r.listeners {
if err := listener(r.configFileMetadata, f.GetContent(), f.GetFilePath(), f.SaveFileEncoding, f.GetFilePostCmd()); err != nil {
if err := listener(r.configFileMetadata, f.GetContent(), f.Persistent); err != nil {
log.GetBaseLogger().Errorf("[Config] invoke config file repo change listener failed.",
zap.Any("file", r.configFileMetadata), zap.Error(err))
}
Expand Down
38 changes: 11 additions & 27 deletions pkg/flow/configuration/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ import (
type defaultConfigFile struct {
model.DefaultConfigFileMetadata

fileRepo *ConfigFileRepo
content string
saveFilePath string
saveFileEncoding string
saveFilePostCmd string
fileRepo *ConfigFileRepo
content string
persistent model.Persistent

lock sync.RWMutex
changeListeners []func(event model.ConfigFileChangeEvent)
Expand All @@ -41,11 +39,9 @@ type defaultConfigFile struct {

func newDefaultConfigFile(metadata model.ConfigFileMetadata, repo *ConfigFileRepo) *defaultConfigFile {
configFile := &defaultConfigFile{
fileRepo: repo,
content: repo.GetContent(),
saveFilePath: repo.GetFilePath(),
saveFileEncoding: repo.GetFileEncoding(),
saveFilePostCmd: repo.GetFilePostCmd(),
fileRepo: repo,
content: repo.GetContent(),
persistent: repo.GetPersistent(),
}
configFile.Namespace = metadata.GetNamespace()
configFile.FileGroup = metadata.GetFileGroup()
Expand All @@ -72,27 +68,17 @@ func (c *defaultConfigFile) GetContent() string {
return c.content
}

// GetFilePath 获取配置文件内容
func (c *defaultConfigFile) GetFilePath() string {
return c.saveFilePath
}

// GetFileEncoding 获取文件编码
func (c *defaultConfigFile) GetFileEncoding() string {
return c.saveFileEncoding
}

// GetFilePostCmd 获取文件后置脚本
func (c *defaultConfigFile) GetFilePostCmd() string {
return c.saveFilePostCmd
// GetPersistent 获取配置文件内容
func (c *defaultConfigFile) GetPersistent() model.Persistent {
return c.persistent
}

// HasContent 是否有配置内容
func (c *defaultConfigFile) HasContent() bool {
return c.content != "" && c.content != NotExistedFileContent
}

func (c *defaultConfigFile) repoChangeListener(configFileMetadata model.ConfigFileMetadata, newContent string, saveFilePath string, saveFileEncoding string, saveFilePostCmd string) error {
func (c *defaultConfigFile) repoChangeListener(configFileMetadata model.ConfigFileMetadata, newContent string, persistent model.Persistent) error {
oldContent := c.content

log.GetBaseLogger().Infof("[Config] update content. file = %+v, old content = %s, new content = %s",
Expand All @@ -117,9 +103,7 @@ func (c *defaultConfigFile) repoChangeListener(configFileMetadata model.ConfigFi
OldValue: c.content,
NewValue: newContent,
ChangeType: changeType,
SaveFilePath: saveFilePath,
SaveFileEncoding: saveFileEncoding,
SaveFilePostCmd: saveFilePostCmd,
Persistent: persistent,
}
c.content = newContent

Expand Down
36 changes: 25 additions & 11 deletions pkg/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,30 @@ type ConfigFileChangeEvent struct {
NewValue string
// ChangeType 变更类型
ChangeType ChangeType
// 文件存储路径
SaveFilePath string
// 文件编码
SaveFileEncoding string
// 配置文件持久化数据
Persistent Persistent
}

// Persistent 配置文件持久化数据
type Persistent struct {
// 文件保存编码
Encoding string
// 文件保存路径
Path string
// 后置脚本
SaveFilePostCmd string
PostCmd string
}

func (persistent Persistent) GetEncoding() string {
return persistent.Encoding
}

func (persistent Persistent) GetPath() string {
return persistent.Path
}

func (persistent Persistent) GetPostCmd() string {
return persistent.PostCmd
}

type SimpleConfigFile struct {
Expand Down Expand Up @@ -97,12 +115,8 @@ type ConfigFile interface {
AddChangeListenerWithChannel() <-chan ConfigFileChangeEvent
// AddChangeListener 增加配置文件变更监听器
AddChangeListener(cb OnConfigFileChange)
// GetFilePath 获取文件下发路径
GetFilePath() string
// GetFileEncoding 获取文件编码
GetFileEncoding() string
// GetFilePostCmd 获取文件后置脚本
GetFilePostCmd() string
// GetPersistent 获取文件持久化数据
GetPersistent() Persistent
}

// DefaultConfigFileMetadata 默认 ConfigFileMetadata 实现类
Expand Down
24 changes: 5 additions & 19 deletions pkg/plugin/configconnector/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,10 @@ type ConfigFile struct {
content string
// 该配置文件是否为不存在的场景下的占位信息
NotExist bool
// 文件保存编码
SaveFileEncoding string
// 文件保存路径
SaveFilePath string
// 后置脚本
SaveFilePostCmd string
//mode=0,默认模式,获取SDK使用的配置文件, mode=1,SDK模式,同mode=1,mode=2,Agent模式,获取Agent使用的配置文件
Mode model.GetConfigFileRequestMode
// 文件持久化配置
Persistent model.Persistent
}

func (c *ConfigFile) String() string {
Expand Down Expand Up @@ -148,26 +144,16 @@ func (c *ConfigFile) GetDataKey() string {
return ""
}

// GetFilePath 获取文件路径
func (c *ConfigFile) GetFilePath() string {
return c.SaveFilePath
// GetPersistent 获取文件持久化数据
func (c *ConfigFile) GetPersistent() model.Persistent {
return c.Persistent
}

// GetFileMode 获取文件Mode
func (c *ConfigFile) GetFileMode() model.GetConfigFileRequestMode {
return c.Mode
}

// GetFileEncoding 获取文件编码
func (c *ConfigFile) GetFileEncoding() string {
return c.SaveFileEncoding
}

// GetFilePostCmd 获取文件后置脚本
func (c *ConfigFile) GetFilePostCmd() string {
return c.SaveFilePostCmd
}

// GetEncryptAlgo 获取配置文件数据加密算法
func (c *ConfigFile) GetEncryptAlgo() string {
for _, tag := range c.Tags {
Expand Down
24 changes: 13 additions & 11 deletions plugin/configconnector/polaris/config_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,17 +420,19 @@ func transferFromClientConfigFileInfo(configFileInfo *config_manage.ClientConfig
})
}
return &configconnector.ConfigFile{
Namespace: configFileInfo.GetNamespace().GetValue(),
FileGroup: configFileInfo.GetGroup().GetValue(),
FileName: configFileInfo.GetFileName().GetValue(),
SourceContent: configFileInfo.GetContent().GetValue(),
Version: configFileInfo.GetVersion().GetValue(),
Md5: configFileInfo.GetMd5().GetValue(),
Encrypted: configFileInfo.GetEncrypted().GetValue(),
Tags: tags,
SaveFilePath: configFileInfo.GetPersistent().GetPath(),
SaveFileEncoding: configFileInfo.GetPersistent().GetEncoding(),
SaveFilePostCmd: configFileInfo.GetPersistent().GetPostCmd(),
Namespace: configFileInfo.GetNamespace().GetValue(),
FileGroup: configFileInfo.GetGroup().GetValue(),
FileName: configFileInfo.GetFileName().GetValue(),
SourceContent: configFileInfo.GetContent().GetValue(),
Version: configFileInfo.GetVersion().GetValue(),
Md5: configFileInfo.GetMd5().GetValue(),
Encrypted: configFileInfo.GetEncrypted().GetValue(),
Tags: tags,
Persistent: model.Persistent{
Encoding: configFileInfo.GetPersistent().GetEncoding(),
Path: configFileInfo.GetPersistent().GetPath(),
PostCmd: configFileInfo.GetPersistent().GetPostCmd(),
},
}
}

Expand Down

0 comments on commit 2644044

Please sign in to comment.