Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dyx1234 committed Oct 22, 2024
1 parent 5703446 commit 0a633c5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
6 changes: 3 additions & 3 deletions store/configmap/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ var (

func GetK8sManager() (*K8sManager, error) {
once.Do(func() {
config, err := rest.InClusterConfig()
inClusterConfig, err := rest.InClusterConfig()
if err != nil {
instance = nil
once = sync.Once{}
log.Errorf("Error creating in-cluster config: %v", err)
log.Errorf("Error creating in-cluster inClusterConfig: %v", err)
return
}
clientSet, err := kubernetes.NewForConfig(config)
clientSet, err := kubernetes.NewForConfig(inClusterConfig)
if err != nil {
instance = nil
once = sync.Once{}
Expand Down
4 changes: 2 additions & 2 deletions store/configmap/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

func TestK8sManager_SetConfigMap(t *testing.T) {
// 创建fake clientset
// 创建fake clientSet
clientSet := fake.NewSimpleClientset()

// 创建K8sManager实例
Expand All @@ -55,7 +55,7 @@ func TestK8sManager_SetConfigMap(t *testing.T) {
}

func TestK8sManager_GetConfigMap(t *testing.T) {
// 创建fake clientset,并预先创建一个ConfigMap
// 创建fake clientSet,并预先创建一个ConfigMap
clientSet := fake.NewSimpleClientset(&coreV1.ConfigMap{
ObjectMeta: metaV1.ObjectMeta{
Name: "test-configmap",
Expand Down
15 changes: 8 additions & 7 deletions store/configmap/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,22 @@ type Store struct {

// LoadConfigMap load ApolloConfig from configmap
func (c *Store) LoadConfigMap(appConfig config.AppConfig, configMapNamespace string) (*config.ApolloConfig, error) {
var config = &config.ApolloConfig{}
var apolloConfig = &config.ApolloConfig{}
var err error
configMapName := appConfig.AppID
key := appConfig.Cluster + "-" + appConfig.NamespaceName
// TODO 在这里把json转为ApolloConfig, 但ReleaseKey字段会丢失, 影响大不大
config.Configurations, _ = c.K8sManager.GetConfigMap(configMapName, configMapNamespace, key)
apolloConfig.Configurations, err = c.K8sManager.GetConfigMap(configMapName, configMapNamespace, key)

config.AppID = appConfig.AppID
config.Cluster = appConfig.Cluster
config.NamespaceName = appConfig.NamespaceName
return config, nil
apolloConfig.AppID = appConfig.AppID
apolloConfig.Cluster = appConfig.Cluster
apolloConfig.NamespaceName = appConfig.NamespaceName
return apolloConfig, err
}

// WriteConfigMap write apollo config to configmap
func (c *Store) WriteConfigMap(config *config.ApolloConfig, configMapNamespace string) error {
// AppId作为configMap的name,cluster-namespace作为key, value为config
// AppId作为configMap的name,cluster-namespace作为key, 配置信息的JSON作为value
configMapName := config.AppID
key := config.Cluster + "-" + config.NamespaceName
err := c.K8sManager.SetConfigMap(configMapName, configMapNamespace, key, config)
Expand Down
20 changes: 14 additions & 6 deletions store/configmap/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var testData = map[string]interface{}{
func TestStore_LoadConfigMap(t *testing.T) {
// 初始化fake clientset
clientset := fake.NewSimpleClientset()
jsonData, err := json.MarshalIndent(testData, "", "")
jsonData, err := json.Marshal(testData)
if err != nil {
fmt.Println("Error marshalling map to JSON:", err)
return
Expand Down Expand Up @@ -90,15 +90,19 @@ func TestStore_LoadConfigMap(t *testing.T) {
loadedConfig, err := store.LoadConfigMap(appConfig, configMapNamespace)

// 测试LoadConfigMap方法
loadedJson, _ := json.Marshal(loadedConfig.Configurations)
assert.NoError(t, err)
assert.NotNil(t, loadedConfig)
assert.Equal(t, testData, loadedConfig.Configurations)
assert.Equal(t, jsonData, loadedJson)
}

func TestStore_WriteConfigMap(t *testing.T) {
// 初始化fake clientset
clientset := fake.NewSimpleClientset()
jsonData, err := json.MarshalIndent(testData, "", "")

var err error
var key = cluster + "-" + namespace
jsonData, err := json.Marshal(testData)
if err != nil {
fmt.Println("Error marshalling map to JSON:", err)
return
Expand All @@ -113,8 +117,7 @@ func TestStore_WriteConfigMap(t *testing.T) {

// 反序列化到ApolloConfig
apolloConfig := &config.ApolloConfig{}
err = json.Unmarshal(jsonData, apolloConfig)
assert.NoError(t, err)
apolloConfig.Configurations = testData
apolloConfig.AppID = appId
apolloConfig.Cluster = cluster
apolloConfig.NamespaceName = namespace
Expand All @@ -125,8 +128,13 @@ func TestStore_WriteConfigMap(t *testing.T) {

// 验证ConfigMap是否被正确创建或更新
configMap, err := clientset.CoreV1().ConfigMaps(configMapNamespace).Get(context.TODO(), appId, metav1.GetOptions{})
loadedJson, _ := configMap.Data[key]

var configurations map[string]interface{}
err = json.Unmarshal([]byte(loadedJson), &configurations)

assert.NoError(t, err)
assert.NotNil(t, configMap)
assert.Equal(t, testData, configMap.Data)
assert.Equal(t, jsonData, []byte(loadedJson))

}

0 comments on commit 0a633c5

Please sign in to comment.