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 2 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
2 changes: 2 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 @@ -49,6 +50,7 @@ const separator = ","
func init() {
extension.SetCacheFactory(&memory.DefaultCacheFactory{})
extension.SetLoadBalance(&roundrobin.RoundRobin{})
extension.SetConfigMapHandler(&configMap.Store{K8sManager: configMap.GetK8sManager()})
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

Issue Found: configMap.Store does not implement the required LoadConfigMap method.

  • The Store struct is missing the LoadConfigMap(string) (*config.ApolloConfig, error) method required by extension.SetConfigMapHandler.
  • Ensure that the Store struct implements all necessary methods to satisfy the expected interface.
🔗 Analysis chain

LGTM: ConfigMap handler setup.

The new line correctly sets up the ConfigMap handler using the extension.SetConfigMapHandler function. This aligns with the PR objective of supporting Kubernetes ConfigMap for the Apollo client.

To ensure the ConfigMap functionality is properly integrated, please verify that:

  1. The configMap.Store struct implements the correct interface expected by extension.SetConfigMapHandler.
  2. The configMap.GetK8sManager() function is properly implemented and returns a valid Kubernetes manager.

You can run the following script to check the implementation:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the ConfigMap implementation

# Check if the Store struct implements the correct interface
ast-grep --lang go --pattern 'type Store struct { $$$
  LoadConfigMap(string) (*config.ApolloConfig, error)
  $$$
}'

# Check the GetK8sManager function implementation
ast-grep --lang go --pattern 'func GetK8sManager() $_ {
  $$$
}'

Length of output: 992

extension.SetFileHandler(&jsonFile.FileHandler{})
extension.SetHTTPAuth(&sign.AuthSignature{})

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
31 changes: 22 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,17 @@ 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 {
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
Loading