Skip to content

Commit

Permalink
feat: isolate nacos-sdk-go into pkg/nacos/client/impl (#9)
Browse files Browse the repository at this point in the history
限制nacos sdk引用在pkg/nacos/client/impl包下
  • Loading branch information
junfengP authored Nov 13, 2023
1 parent 92250bb commit aff4595
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 231 deletions.
6 changes: 5 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package main
import (
"flag"
"github.com/nacos-group/nacos-controller/pkg/nacos"
"github.com/nacos-group/nacos-controller/pkg/nacos/auth"
"github.com/nacos-group/nacos-controller/pkg/nacos/client/impl"
"os"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
Expand Down Expand Up @@ -92,7 +94,9 @@ func main() {
os.Exit(1)
}

if err = controller.NewDynamicConfigurationReconciler(mgr.GetClient(), mgr.GetScheme(), nacos.SyncConfigOptions{}).SetupWithManager(mgr); err != nil {
if err = controller.NewDynamicConfigurationReconciler(mgr.GetClient(), mgr.GetScheme(), nacos.SyncConfigOptions{
ConfigClient: impl.NewDefaultNacosConfigClient(auth.NewDefaultNacosAuthProvider(mgr.GetClient())),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DynamicConfiguration")
os.Exit(1)
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/controller/dynamicconfiguration_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
v12 "github.com/nacos-group/nacos-controller/api/v1"
"github.com/nacos-group/nacos-controller/pkg/nacos/auth"
"github.com/nacos-group/nacos-controller/pkg/nacos/client/impl"
"github.com/nacos-group/nacos-sdk-go/v2/vo"
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
Expand Down Expand Up @@ -391,7 +392,7 @@ func checkDynamicConfigurationStatus(dc *v12.DynamicConfiguration) {
}

func getContentByDataId(dc *v12.DynamicConfiguration) (string, bool) {
configClient, err := auth.GetNacosAuthManger().GetNacosConfigClient(&auth.DefaultNaocsAuthProvider{Client: k8sClient}, dc)
configClient, err := impl.GetNacosClientBuilder().Build(&auth.DefaultNacosAuthProvider{Client: k8sClient}, dc)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
content, err := configClient.GetConfig(vo.ConfigParam{
Group: dc.Spec.NacosServer.Group,
Expand All @@ -402,7 +403,7 @@ func getContentByDataId(dc *v12.DynamicConfiguration) (string, bool) {
}

func createOrUpdateContentInNaocs(dc *v12.DynamicConfiguration, dataId, content string) {
configClient, err := auth.GetNacosAuthManger().GetNacosConfigClient(&auth.DefaultNaocsAuthProvider{Client: k8sClient}, dc)
configClient, err := impl.GetNacosClientBuilder().Build(&auth.DefaultNacosAuthProvider{Client: k8sClient}, dc)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
serverContent, err := configClient.GetConfig(vo.ConfigParam{
Group: dc.Spec.NacosServer.Group,
Expand All @@ -420,7 +421,7 @@ func createOrUpdateContentInNaocs(dc *v12.DynamicConfiguration, dataId, content
}

func deleteDataIdInNaocs(dc *v12.DynamicConfiguration, dataId string) {
configClient, err := auth.GetNacosAuthManger().GetNacosConfigClient(&auth.DefaultNaocsAuthProvider{Client: k8sClient}, dc)
configClient, err := impl.GetNacosClientBuilder().Build(&auth.DefaultNacosAuthProvider{Client: k8sClient}, dc)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
_, err = configClient.DeleteConfig(vo.ConfigParam{
Group: dc.Spec.NacosServer.Group,
Expand Down
95 changes: 0 additions & 95 deletions pkg/nacos/auth/auth_manager.go

This file was deleted.

24 changes: 20 additions & 4 deletions pkg/nacos/auth/auth_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,31 @@ var (
secretGVK = schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Secret"}
)

type ConfigClientParam struct {
Endpoint string
ServerAddr string
Namespace string
AuthInfo ConfigClientAuthInfo
}

type ConfigClientAuthInfo struct {
AccessKey string
SecretKey string
}

type NacosAuthProvider interface {
GetNacosClientParams(*nacosiov1.DynamicConfiguration) (*ConfigClientParam, error)
}

type DefaultNaocsAuthProvider struct {
type DefaultNacosAuthProvider struct {
client.Client
}

func (p *DefaultNaocsAuthProvider) GetNacosClientParams(dc *nacosiov1.DynamicConfiguration) (*ConfigClientParam, error) {
func NewDefaultNacosAuthProvider(c client.Client) NacosAuthProvider {
return &DefaultNacosAuthProvider{Client: c}
}

func (p *DefaultNacosAuthProvider) GetNacosClientParams(dc *nacosiov1.DynamicConfiguration) (*ConfigClientParam, error) {
if dc == nil {
return nil, fmt.Errorf("empty DynamicConfiguration")
}
Expand Down Expand Up @@ -56,7 +72,7 @@ func (p *DefaultNaocsAuthProvider) GetNacosClientParams(dc *nacosiov1.DynamicCon
return nil, fmt.Errorf("either endpoint or serverAddr should be set")
}

func (p *DefaultNaocsAuthProvider) getNacosAuthInfo(obj *v1.ObjectReference) (*ConfigClientAuthInfo, error) {
func (p *DefaultNacosAuthProvider) getNacosAuthInfo(obj *v1.ObjectReference) (*ConfigClientAuthInfo, error) {
switch obj.GroupVersionKind().String() {
case secretGVK.String():
return p.getNaocsAuthFromSecret(obj)
Expand All @@ -65,7 +81,7 @@ func (p *DefaultNaocsAuthProvider) getNacosAuthInfo(obj *v1.ObjectReference) (*C
}
}

func (p *DefaultNaocsAuthProvider) getNaocsAuthFromSecret(obj *v1.ObjectReference) (*ConfigClientAuthInfo, error) {
func (p *DefaultNacosAuthProvider) getNaocsAuthFromSecret(obj *v1.ObjectReference) (*ConfigClientAuthInfo, error) {
s := v1.Secret{}
err := p.Get(context.TODO(), types.NamespacedName{Namespace: obj.Namespace, Name: obj.Name}, &s)
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions pkg/nacos/client/config_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package client

import (
nacosv1 "github.com/nacos-group/nacos-controller/api/v1"
"sync"
)

var _defaultClient NacosConfigClient
var _lock = sync.Mutex{}

func RegisterNacosClientIfAbsent(c NacosConfigClient) {
_lock.Lock()
defer _lock.Unlock()
if _defaultClient == nil {
_defaultClient = c
}
}

func RegisterNacosClient(c NacosConfigClient) {
_lock.Lock()
defer _lock.Unlock()
_defaultClient = c
}

func GetDefaultNacosClient() NacosConfigClient {
if _defaultClient == nil {
panic("No default NacosConfigClient registered")
}
return _defaultClient
}

type NacosConfigClient interface {
GetConfig(param NacosConfigParam) (string, error)
PublishConfig(param NacosConfigParam) (bool, error)
DeleteConfig(param NacosConfigParam) (bool, error)
ListenConfig(param NacosConfigParam) error
CancelListenConfig(param NacosConfigParam) error
}

type NacosConfigParam struct {
DynamicConfiguration *nacosv1.DynamicConfiguration
DataId string
Group string
Content string
OnChange func(namespace, group, dataId, data string)
}
Loading

0 comments on commit aff4595

Please sign in to comment.