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

feat: support nacos configstore component (level1) #939

Merged
merged 26 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0efc4ce
feat: nacos configstore component support get and set operate.
cyb0225 May 30, 2023
d2f44c1
feat: register nacos to configstore factory.
cyb0225 May 30, 2023
b06b4c5
feat: add nacos config file.
cyb0225 May 30, 2023
8946661
feat: nacos configstore component add default logger and support list…
cyb0225 Jun 6, 2023
aa33500
feat: nacos configstore component add test file for change_listener.
cyb0225 Jun 6, 2023
82ce0a3
feat: nacos configstore component delete config change listening when…
cyb0225 Jun 7, 2023
819574b
chore: nacos component files add license and change the import order.
cyb0225 Jun 7, 2023
213ee96
feat: nacos configstore component add mock.
cyb0225 Jun 8, 2023
8de6925
feat: nacos configstore component add config parse.
cyb0225 Jun 8, 2023
d901215
feat: nacos configstore component improve error check mechanism.
cyb0225 Jun 8, 2023
7544a56
feat: nacos configstore component use const variables instead of stri…
cyb0225 Jun 8, 2023
6fd7e1b
fix: nacos configstore component check config nil field to prevent pa…
cyb0225 Jun 8, 2023
3b4fa12
fix: fix nacos configstore component bugs when use get key level conf…
cyb0225 Jun 8, 2023
211d7ea
test: nacos configstore component add test files.
cyb0225 Jun 8, 2023
df1fb1a
test: nacos configstore component improve testing strategy
cyb0225 Jun 9, 2023
29b795f
refactor: nacos configstore component.
cyb0225 Jun 15, 2023
1e36ff0
feat: improve nacos configstore component test case.
cyb0225 Jun 15, 2023
2f3ce66
feat: nacos configstore component support pagination.
cyb0225 Jun 16, 2023
e71547d
refactor: nacos configstore component uses sync map instead of map.
cyb0225 Jun 19, 2023
5572588
refactor: nacos configstore component delete the global variable of s…
cyb0225 Jun 19, 2023
5eac53a
refactor: nacos configstore component support user set cache/log file…
cyb0225 Jun 20, 2023
51e4aee
refactor: configstore component add app_id field
cyb0225 Jun 20, 2023
647c5eb
refactor: use the log in mosn to replace nacos sdk log.
cyb0225 Jun 20, 2023
286f354
Merge remote-tracking branch 'upstream/main' into component-nacos
cyb0225 Jun 29, 2023
313479d
refactor: update nacos configstore component.
cyb0225 Jun 29, 2023
aa6d958
docs: nacos configstore component add config usage docs.
cyb0225 Jun 29, 2023
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 cmd/layotto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import (
"mosn.io/pkg/log"

"mosn.io/layotto/components/configstores/etcdv3"
"mosn.io/layotto/components/configstores/nacos"
"mosn.io/layotto/components/file"
"mosn.io/layotto/components/sequencer"
"mosn.io/layotto/pkg/runtime/bindings"
Expand Down Expand Up @@ -278,6 +279,7 @@ func NewRuntimeGrpcServer(data json.RawMessage, opts ...grpc.ServerOption) (mgrp
runtime.WithConfigStoresFactory(
configstores.NewStoreFactory("apollo", apollo.NewStore),
configstores.NewStoreFactory("etcd", etcdv3.NewStore),
configstores.NewStoreFactory("nacos", nacos.NewStore),
),

// RPC
Expand Down
2 changes: 2 additions & 0 deletions cmd/layotto_multiple_api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import (
// Configuration
"mosn.io/layotto/components/configstores"
"mosn.io/layotto/components/configstores/apollo"
"mosn.io/layotto/components/configstores/nacos"

// Pub/Sub
dapr_comp_pubsub "github.com/dapr/components-contrib/pubsub"
Expand Down Expand Up @@ -288,6 +289,7 @@ func NewRuntimeGrpcServer(data json.RawMessage, opts ...grpc.ServerOption) (mgrp
runtime.WithConfigStoresFactory(
configstores.NewStoreFactory("apollo", apollo.NewStore),
configstores.NewStoreFactory("etcd", etcdv3.NewStore),
configstores.NewStoreFactory("nacos", nacos.NewStore),
),

// RPC
Expand Down
2 changes: 2 additions & 0 deletions cmd/layotto_without_xds/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import (
"mosn.io/pkg/log"

"mosn.io/layotto/components/configstores/etcdv3"
"mosn.io/layotto/components/configstores/nacos"
"mosn.io/layotto/components/file"
"mosn.io/layotto/components/sequencer"
"mosn.io/layotto/pkg/runtime/bindings"
Expand Down Expand Up @@ -261,6 +262,7 @@ func NewRuntimeGrpcServer(data json.RawMessage, opts ...grpc.ServerOption) (mgrp
runtime.WithConfigStoresFactory(
configstores.NewStoreFactory("apollo", apollo.NewStore),
configstores.NewStoreFactory("etcd", etcdv3.NewStore),
configstores.NewStoreFactory("nacos", nacos.NewStore),
),

// RPC
Expand Down
65 changes: 65 additions & 0 deletions components/configstores/nacos/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2021 Layotto Authors
// Licensed 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 nacos

import (
"github.com/nacos-group/nacos-sdk-go/v2/model"
"github.com/nacos-group/nacos-sdk-go/v2/vo"
)

type Client interface {
// GetConfig use to get config from nacos server
// dataId require
// group require
// tenant ==>nacos.namespace optional
GetConfig(param vo.ConfigParam) (string, error)

// PublishConfig use to publish config to nacos server
// dataId require
// group require
// content require
// tenant ==>nacos.namespace optional
PublishConfig(param vo.ConfigParam) (bool, error)

// DeleteConfig use to delete config
// dataId require
// group require
// tenant ==>nacos.namespace optional
DeleteConfig(param vo.ConfigParam) (bool, error)

// ListenConfig use to listen config change,it will callback OnChange() when config change
// dataId require
// group require
// onchange require
// tenant ==>nacos.namespace optional
ListenConfig(params vo.ConfigParam) (err error)

//CancelListenConfig use to cancel listen config change
// dataId require
// group require
// tenant ==>nacos.namespace optional
CancelListenConfig(params vo.ConfigParam) (err error)

// SearchConfig use to search nacos config
// search require search=accurate--精确搜索 search=blur--模糊搜索
// group option
// dataId option
// tenant ==>nacos.namespace optional
// pageNo option,default is 1
// pageSize option,default is 10
SearchConfig(param vo.SearchConfigParam) (*model.ConfigPage, error)

// CloseClient Close the GRPC client
CloseClient()
}
105 changes: 105 additions & 0 deletions components/configstores/nacos/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2021 Layotto Authors
// Licensed 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 nacos

// map keys
const (
namespaceIdKey = "namespace_id"
wenxuwan marked this conversation as resolved.
Show resolved Hide resolved
userNameKey = "username"
passwordKey = "password"
endPointKey = "end_point"
regionIdKey = "region_id"
accessKey = "access_key"
secretKey = "secret_key"
logDirKey = "log_dir"
cacheDirKey = "cache_dir"
logLevelKey = "log_level" // support debug, info, warn, error
)

type Metadata struct {
NameSpaceId string
Username string
Password string
// ACM & KMS
Endpoint string
RegionId string
AccessKey string
SecretKey string
OpenKMS bool
// log & cache files
LogDir string
CacheDir string
LogLevel string
}

func ParseNacosMetadata(properties map[string]string) (*Metadata, error) {
if properties == nil {
return nil, errConfigMissingField("metadata")
}

config := &Metadata{}

// the namespace of config, not required
config.NameSpaceId = properties[namespaceIdKey]
if config.NameSpaceId == "" {
config.NameSpaceId = defaultNamespaceId
}

if v, ok := properties[userNameKey]; ok && v != "" {
config.Username = v
}

if v, ok := properties[passwordKey]; ok && v != "" {
config.Password = v
}

// ACM & KMS
if v, ok := properties[endPointKey]; ok && v != "" {
config.Endpoint = v
config.OpenKMS = true
}

if v, ok := properties[regionIdKey]; ok && v != "" {
config.RegionId = v
config.OpenKMS = true
}

if v, ok := properties[accessKey]; ok && v != "" {
config.AccessKey = v
config.OpenKMS = true
}

if v, ok := properties[secretKey]; ok && v != "" {
config.SecretKey = v
config.OpenKMS = true
}

// log & cache files
config.LogDir = properties[logDirKey]
if config.LogDir == "" {
config.LogDir = defaultLogDir
}

config.LogLevel = properties[logLevelKey]
if config.LogLevel == "" {
config.LogLevel = defaultLogLevel
}

config.CacheDir = properties[cacheDirKey]
if config.CacheDir == "" {
config.CacheDir = defaultCacheDir
}

return config, nil
}
39 changes: 39 additions & 0 deletions components/configstores/nacos/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2021 Layotto Authors
// Licensed 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 nacos

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseNacosMetadata(t *testing.T) {
// without metadata
_, err := ParseNacosMetadata(nil)
assert.Error(t, err)

properties := make(map[string]string)

// success
_, err = ParseNacosMetadata(properties)
assert.Nil(t, err)

// test set namespace
namespaceId := "namespace"
properties[namespaceIdKey] = namespaceId
metadata, err := ParseNacosMetadata(properties)
assert.Nil(t, err)
assert.Equal(t, namespaceId, metadata.NameSpaceId)
}
Loading