Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OSPP]Support Kubernetes ConfigMap for Apollo java, golang client #79 #318

Merged
merged 27 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package agollo
import (
"container/list"
"errors"
"github.com/apolloconfig/agollo/v4/store/configmap"
"strings"

"github.com/apolloconfig/agollo/v4/agcache"
Expand Down Expand Up @@ -114,6 +115,15 @@ func StartWithConfig(loadAppConfig func() (*config.AppConfig, error)) (Client, e
return nil, err
}

// set k8sManager before first sync
if appConfig.IsBackupConfigToConfigMap {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么会会修改 client?我理解整体的框架应该是没有变化。
只是提供一个新的插件,通过文档告诉用户有这个插件可以选择。

我理解只需要按下方实现方式实现即可:
https://github.sheincorp.cn/zouyx/agollo_demo/tree/master/custom/file

如果是现在这种改法,可能会在用户升级的时候出现不兼容状态。
出现这种状态,可能会导致用户出现测试成本增加,或者生产故障。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我最初是在client文件的init里,作为插件初始化的。
但是那个位置无法获取用户的配置信息(*config.AppConfig)来判断是否初始化这个插件。如果盲目初始化插件会调用GetK8sManager方法,连接不到k8s的话会直接报错。所以挪到StartWithConfig这个方法里来了。
这样会有什么问题吗,如果用户没设置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
11 changes: 10 additions & 1 deletion component/remote/async.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,16 @@ func toApolloConfig(resBody []byte) ([]*config.Notification, error) {
func loadBackupConfig(namespace string, appConfig config.AppConfig) []*config.ApolloConfig {
apolloConfigs := make([]*config.ApolloConfig, 0)
config.SplitNamespaces(namespace, func(namespace string) {
c, err := extension.GetFileHandler().LoadConfigFile(appConfig.BackupConfigPath, appConfig.AppID, namespace)
var c *config.ApolloConfig
var err error

// 增加configMap读取,但优先本地文件(configMap不支持灰度)
if appConfig.GetIsBackupConfig() {
c, err = extension.GetFileHandler().LoadConfigFile(appConfig.BackupConfigPath, appConfig.AppID, namespace)
} else if appConfig.GetIsBackupConfigToConfigMap() {
c, err = extension.GetConfigMapHandler().LoadConfigMap(appConfig, appConfig.ConfigMapNamespace)
}

if err != nil {
log.Errorf("LoadConfigFile error, error: %v", err)
return
Expand Down
34 changes: 25 additions & 9 deletions env/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ type File interface {

// AppConfig 配置文件
type AppConfig struct {
AppID string `json:"appId"`
Cluster string `json:"cluster"`
NamespaceName string `json:"namespaceName"`
IP string `json:"ip"`
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"`
IsBackupConfigToConfigMap bool `default:"false" json:"isBackupConfigToConfigmap"`
ConfigMapNamespace string `json:"configMapNamespace"`
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 All @@ -76,6 +78,20 @@ func (a *AppConfig) GetBackupConfigPath() string {
return a.BackupConfigPath
}

// GetIsBackupConfigToConfigMap whether backup config to configmap after fetch config from apollo
// false : no (default)
// true : yes
func (a *AppConfig) GetIsBackupConfigToConfigMap() bool {
return a.IsBackupConfigToConfigMap
}

func (a *AppConfig) GetConfigMapNamespace() string {
if a.ConfigMapNamespace == "" {
a.ConfigMapNamespace = "default"
}
return a.ConfigMapNamespace
}

// GetHost GetHost
func (a *AppConfig) GetHost() string {
u, err := url.Parse(a.IP)
Expand Down
32 changes: 32 additions & 0 deletions extension/configmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package extension

import "github.com/apolloconfig/agollo/v4/store"

var configMapHandler store.ConfigMapHandler

// SetConfigMapHandler Set the ConfigMap cache handler
func SetConfigMapHandler(inConfigMapHandler store.ConfigMapHandler) {
configMapHandler = inConfigMapHandler
}

// GetConfigMapHandler Get the ConfigMap cache handler
func GetConfigMapHandler() store.ConfigMapHandler {
return configMapHandler
}
44 changes: 44 additions & 0 deletions extension/configmap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package extension

import (
"github.com/apolloconfig/agollo/v4/env/config"
. "github.com/tevid/gohamcrest"
"testing"
)

type TestConfigMapHandler struct {
}

func (t *TestConfigMapHandler) LoadConfigMap(configMapNamespace string) (*config.ApolloConfig, error) {
return nil, nil
}

func (t *TestConfigMapHandler) WriteConfigMap(config *config.ApolloConfig, configMapNamespace string) error {
return nil
}

// TestSetConfigMapHandler 测试 SetConfigMapHandler 函数
func TestSetConfigMapHandler(t *testing.T) {
SetConfigMapHandler(&TestConfigMapHandler{})

resultConfigMapHandler := GetConfigMapHandler()

Assert(t, resultConfigMapHandler, NotNilVal())
}
61 changes: 59 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,66 @@
module github.com/apolloconfig/agollo/v4

require (
github.com/agiledragon/gomonkey/v2 v2.11.0 // indirect
github.com/agiledragon/gomonkey/v2 v2.11.0
github.com/spf13/viper v1.8.1
github.com/stretchr/testify v1.8.4
github.com/tevid/gohamcrest v1.1.1
k8s.io/api v0.30.1
k8s.io/apimachinery v0.30.1
k8s.io/client-go v0.30.1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Verify the Versions of Kubernetes Dependencies

The go.mod file specifies Kubernetes modules with version v0.30.1:

  • k8s.io/api v0.30.1
  • k8s.io/apimachinery v0.30.1
  • k8s.io/client-go v0.30.1

As of April 2024, the latest stable versions of these modules are around v0.28.x. Versions like v0.30.1 may not exist or might be pre-release. Using non-existent or unstable versions can lead to build failures or unexpected behavior. Please verify that these versions are correct and update them to valid, stable versions if necessary.

)

go 1.13
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

go 1.22.0

toolchain go1.22.5
Loading