Skip to content

Commit

Permalink
fix: 不主动初始化
Browse files Browse the repository at this point in the history
  • Loading branch information
dyx1234 committed Oct 26, 2024
1 parent 1bf6fdd commit 7a6f7c9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 33 deletions.
10 changes: 0 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/apolloconfig/agollo/v4/extension"
"github.com/apolloconfig/agollo/v4/protocol/auth/sign"
"github.com/apolloconfig/agollo/v4/storage"
"github.com/apolloconfig/agollo/v4/store/configmap"
"github.com/apolloconfig/agollo/v4/utils"
"github.com/apolloconfig/agollo/v4/utils/parse/normal"
"github.com/apolloconfig/agollo/v4/utils/parse/properties"
Expand Down Expand Up @@ -115,15 +114,6 @@ func StartWithConfig(loadAppConfig func() (*config.AppConfig, error)) (Client, e
return nil, err
}

// set k8sManager before first sync
if appConfig.IsBackupConfigToConfigMap {
manager, err := configmap.GetK8sManager()
if err != nil {
return nil, err
}
extension.SetConfigMapHandler(&configmap.Store{K8sManager: manager})
}

c := create()
if appConfig != nil {
c.appConfig = appConfig
Expand Down
8 changes: 7 additions & 1 deletion component/remote/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,21 @@ func loadBackupConfig(namespace string, appConfig config.AppConfig) []*config.Ap
}

func loadBackupConfiguration(appConfig config.AppConfig, namespace string) (*config.ApolloConfig, error) {
log.Debugf("Loading backup config")
if appConfig.GetIsBackupConfig() {
c, err := extension.GetFileHandler().LoadConfigFile(appConfig.BackupConfigPath, appConfig.AppID, namespace)
if err == nil {
log.Info("The backup file was successfully loaded. config: %s", c)
return c, nil
}
}

if appConfig.GetIsBackupConfigToConfigMap() {
return extension.GetConfigMapHandler().LoadConfigMap(appConfig, appConfig.K8sNamespace)
c, err := extension.GetConfigMapHandler().LoadConfigMap(appConfig, appConfig.K8sNamespace)
if err == nil {
log.Info("The backup configmap was successfully loaded. config: %+v", c)
return c, nil
}
}

return nil, nil
Expand Down
25 changes: 12 additions & 13 deletions env/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,16 @@ type File interface {

// AppConfig 配置文件
type AppConfig struct {
AppID string `json:"appId"`
Cluster string `json:"cluster"`
NamespaceName string `json:"namespaceName"`
IP string `json:"ip"`
IsBackupConfigToConfigMap bool `default:"false" json:"isBackupConfigToConfigmap"`
K8sNamespace string `json:"k8sNamespace"`
IsBackupConfig bool `default:"true" json:"isBackupConfig"`
BackupConfigPath string `json:"backupConfigPath"`
Secret string `json:"secret"`
Label string `json:"label"`
SyncServerTimeout int `json:"syncServerTimeout"`
AppID string `json:"appId"`
Cluster string `json:"cluster"`
NamespaceName string `json:"namespaceName"`
IP string `json:"ip"`
K8sNamespace string `json:"k8sNamespace"`
IsBackupConfig bool `default:"true" json:"isBackupConfig"`
BackupConfigPath string `json:"backupConfigPath"`
Secret string `json:"secret"`
Label string `json:"label"`
SyncServerTimeout int `json:"syncServerTimeout"`
// MustStart 可用于控制第一次同步必须成功
MustStart bool `default:"false"`
notificationsMap *notificationsMap
Expand Down Expand Up @@ -79,10 +78,10 @@ func (a *AppConfig) GetBackupConfigPath() string {
}

// GetIsBackupConfigToConfigMap whether backup config to configmap after fetch config from apollo
// false : no (default)
// false : no (The value of k8sNamespace is not set)
// true : yes
func (a *AppConfig) GetIsBackupConfigToConfigMap() bool {
return a.IsBackupConfigToConfigMap
return a.K8sNamespace != ""
}

func (a *AppConfig) GetK8sNamespace() string {
Expand Down
3 changes: 3 additions & 0 deletions storage/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,9 @@ func (c *Cache) UpdateApolloConfig(apolloConfig *config.ApolloConfig, appConfigF

if appConfig.GetIsBackupConfigToConfigMap() {
// write configmap async
apolloConfig.AppID = appConfig.AppID
apolloConfig.Cluster = appConfig.Cluster
apolloConfig.NamespaceName = appConfig.NamespaceName
go extension.GetConfigMapHandler().WriteConfigMap(apolloConfig, appConfig.GetK8sNamespace())
}

Expand Down
21 changes: 14 additions & 7 deletions store/configmap/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ import (
)

type K8sManager struct {
clientSet kubernetes.Interface
clientSet kubernetes.Interface
k8sNamespace string
cluster string
}

var (
instance *K8sManager
once sync.Once
)

func GetK8sManager() (*K8sManager, error) {
func GetK8sManager(k8sNamespace string, cluster string) (*K8sManager, error) {
once.Do(func() {
inClusterConfig, err := rest.InClusterConfig()
if err != nil {
Expand All @@ -60,7 +62,9 @@ func GetK8sManager() (*K8sManager, error) {
return
}
instance = &K8sManager{
clientSet: clientSet,
clientSet: clientSet,
k8sNamespace: k8sNamespace,
cluster: cluster,
}
})
if instance == nil {
Expand All @@ -70,7 +74,7 @@ func GetK8sManager() (*K8sManager, error) {
}

// SetConfigMapWithRetry 使用k8s版本号机制解决并发问题
func (m *K8sManager) SetConfigMapWithRetry(configMapName string, k8sNamespace string, key string, config *config.ApolloConfig) error {
func (m *K8sManager) SetConfigMapWithRetry(configMapName string, key string, config *config.ApolloConfig) error {
var retryParam = wait.Backoff{
Steps: 5,
Duration: 10 * time.Millisecond,
Expand All @@ -79,14 +83,15 @@ func (m *K8sManager) SetConfigMapWithRetry(configMapName string, k8sNamespace st
}

err := retry.RetryOnConflict(retryParam, func() error {
return m.SetConfigMap(configMapName, k8sNamespace, key, config)
return m.SetConfigMap(configMapName, key, config)
})

return err
}

// SetConfigMap 将map[string]interface{}转换为JSON字符串,并创建或更新ConfigMap
func (m *K8sManager) SetConfigMap(configMapName string, k8sNamespace string, key string, config *config.ApolloConfig) error {
func (m *K8sManager) SetConfigMap(configMapName string, key string, config *config.ApolloConfig) error {
k8sNamespace := m.k8sNamespace
jsonData, err := json.Marshal(config.Configurations)
jsonString := string(jsonData)
if err != nil {
Expand Down Expand Up @@ -130,7 +135,9 @@ func (m *K8sManager) SetConfigMap(configMapName string, k8sNamespace string, key
}

// GetConfigMap 从ConfigMap中获取JSON字符串,并反序列化为map[string]interface{}
func (m *K8sManager) GetConfigMap(configMapName string, k8sNamespace string, key string) (map[string]interface{}, error) {
func (m *K8sManager) GetConfigMap(configMapName string, key string) (map[string]interface{}, error) {
k8sNamespace := m.k8sNamespace

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

Expand Down
4 changes: 2 additions & 2 deletions store/configmap/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (c *Store) LoadConfigMap(appConfig config.AppConfig, k8sNamespace string) (
configMapName := ApolloConfigCache + appConfig.AppID
key := appConfig.Cluster + "-" + appConfig.NamespaceName
// TODO 在这里把json转为ApolloConfig, 但ReleaseKey字段会丢失, 影响大不大
apolloConfig.Configurations, err = c.K8sManager.GetConfigMap(configMapName, k8sNamespace, key)
apolloConfig.Configurations, err = c.K8sManager.GetConfigMap(configMapName, key)

apolloConfig.AppID = appConfig.AppID
apolloConfig.Cluster = appConfig.Cluster
Expand All @@ -47,7 +47,7 @@ func (c *Store) LoadConfigMap(appConfig config.AppConfig, k8sNamespace string) (
func (c *Store) WriteConfigMap(config *config.ApolloConfig, k8sNamespace string) error {
configMapName := ApolloConfigCache + config.AppID
key := config.Cluster + "-" + config.NamespaceName
err := c.K8sManager.SetConfigMap(configMapName, k8sNamespace, key, config)
err := c.K8sManager.SetConfigMapWithRetry(configMapName, key, config)
if err != nil {
log.Errorf("Failed to write ConfigMap %s in namespace %s: %v", configMapName, k8sNamespace, err)
return err
Expand Down

0 comments on commit 7a6f7c9

Please sign in to comment.