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 18 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
57 changes: 57 additions & 0 deletions components/configstores/nacos/change_listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 "sync"

// store the group and keys that client subscribe
type subscriberHolder struct {
wenxuwan marked this conversation as resolved.
Show resolved Hide resolved
rw sync.RWMutex
keyMap map[subscriberKey]struct{}
}

type subscriberKey struct {
group string
key string
}

func newSubscriberHolder() *subscriberHolder {
return &subscriberHolder{
keyMap: make(map[subscriberKey]struct{}),
}
}

func (s *subscriberHolder) AddSubscriberKey(key subscriberKey) {
s.rw.Lock()
defer s.rw.Unlock()
s.keyMap[key] = struct{}{}
}

func (s *subscriberHolder) RemoveSubscriberKey(key subscriberKey) {
s.rw.Lock()
defer s.rw.Unlock()
delete(s.keyMap, key)
}

func (s *subscriberHolder) GetSubscriberKey() []subscriberKey {
s.rw.RLock()
defer s.rw.RUnlock()

res := make([]subscriberKey, 0, len(s.keyMap))
for k := range s.keyMap {
res = append(res, k)
}

return res
}
61 changes: 61 additions & 0 deletions components/configstores/nacos/change_listener_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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 Test_newSubscriberHolder(t *testing.T) {
listener := newSubscriberHolder()
assert.NotNil(t, listener)
assert.NotNil(t, listener.keyMap)
}

func Test_subscriberHolder_AddSubscriberKey(t *testing.T) {
listener := newSubscriberHolder()
key := subscriberKey{key: "test-key", group: "test-group"}
listener.AddSubscriberKey(key)
s, ok := listener.keyMap[key]
assert.True(t, ok)
assert.EqualValues(t, struct{}{}, s)
}

func Test_subscriberHolder_GetSubscriberKey(t *testing.T) {
listener := newSubscriberHolder()
key := subscriberKey{key: "test-key", group: "test-group"}
listener.AddSubscriberKey(key)
getSubscriberKey := listener.GetSubscriberKey()
assert.EqualValues(t, 1, len(getSubscriberKey))
assert.EqualValues(t, key, getSubscriberKey[0])

key2 := subscriberKey{key: "test-key2", group: "test-group2"}
listener.AddSubscriberKey(key2)
getSubscriberKey = listener.GetSubscriberKey()
// Due to the use of go map results, the return order of map traversal
// cannot be determined, so the testing here haven't test the key2's value.
assert.Equal(t, 2, len(getSubscriberKey))
}

func Test_subscriberHolder_RemoveSubscriberKey(t *testing.T) {
listener := newSubscriberHolder()
key := subscriberKey{key: "test-key", group: "test-group"}
listener.AddSubscriberKey(key)
assert.EqualValues(t, 1, len(listener.keyMap))

listener.RemoveSubscriberKey(key)
assert.Equal(t, 0, len(listener.keyMap))
}
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()
}
88 changes: 88 additions & 0 deletions components/configstores/nacos/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// 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
appNameKey = "app_name"
userNameKey = "username"
passwordKey = "password"
endPointKey = "end_point"
regionIdKey = "region_id"
accessKey = "access_key"
secretKey = "secret_key"
)

type Metadata struct {
AppName string
NameSpaceId string
Username string
Password string
// ACM & KMS
Endpoint string
RegionId string
AccessKey string
SecretKey string
OpenKMS bool
}

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

config := &Metadata{}
config.AppName = properties[appNameKey]
if config.AppName == "" {
wenxuwan marked this conversation as resolved.
Show resolved Hide resolved
return nil, errConfigMissingField(appNameKey)
}

// 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
}

return config, nil
}
45 changes: 45 additions & 0 deletions components/configstores/nacos/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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)
// without app_name
_, err = ParseNacosMetadata(properties)
assert.Error(t, err)

// success
appName := "app"
properties[appNameKey] = appName
metadata, err := ParseNacosMetadata(properties)
assert.Nil(t, err)
assert.EqualValues(t, appName, metadata.AppName)

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