From 7a6f7c9aa56a3a1aa06798cdf2d96b7082152e3b Mon Sep 17 00:00:00 2001 From: dyx1234 <2060307490@qq.com> Date: Sat, 26 Oct 2024 14:41:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B8=8D=E4=B8=BB=E5=8A=A8=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 10 ---------- component/remote/async.go | 8 +++++++- env/config/config.go | 25 ++++++++++++------------- storage/repository.go | 3 +++ store/configmap/manager.go | 21 ++++++++++++++------- store/configmap/store.go | 4 ++-- 6 files changed, 38 insertions(+), 33 deletions(-) diff --git a/client.go b/client.go index 56661c0..5f2d6e5 100644 --- a/client.go +++ b/client.go @@ -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" @@ -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 diff --git a/component/remote/async.go b/component/remote/async.go index a14d701..8ac2d5c 100644 --- a/component/remote/async.go +++ b/component/remote/async.go @@ -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 diff --git a/env/config/config.go b/env/config/config.go index 6b40183..8d1d15f 100644 --- a/env/config/config.go +++ b/env/config/config.go @@ -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 @@ -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 { diff --git a/storage/repository.go b/storage/repository.go index d8b9a42..f05b361 100644 --- a/storage/repository.go +++ b/storage/repository.go @@ -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()) } diff --git a/store/configmap/manager.go b/store/configmap/manager.go index e4b6d02..7dd2878 100644 --- a/store/configmap/manager.go +++ b/store/configmap/manager.go @@ -35,7 +35,9 @@ import ( ) type K8sManager struct { - clientSet kubernetes.Interface + clientSet kubernetes.Interface + k8sNamespace string + cluster string } var ( @@ -43,7 +45,7 @@ var ( 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 { @@ -60,7 +62,9 @@ func GetK8sManager() (*K8sManager, error) { return } instance = &K8sManager{ - clientSet: clientSet, + clientSet: clientSet, + k8sNamespace: k8sNamespace, + cluster: cluster, } }) if instance == nil { @@ -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, @@ -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 { @@ -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() diff --git a/store/configmap/store.go b/store/configmap/store.go index bee9d73..d471bbb 100644 --- a/store/configmap/store.go +++ b/store/configmap/store.go @@ -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 @@ -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