From f7ebc9f017fcf0cbf2b69d26bb53f57ce3afa382 Mon Sep 17 00:00:00 2001 From: Shawn Date: Tue, 17 Sep 2019 12:38:57 +0800 Subject: [PATCH] refactor (#40) --- README.md | 4 +- configcenter/config_center.go | 12 +- configcenter/util.go | 2 +- configcenter/util_test.go | 2 +- go.mod | 4 +- servicecomb/kie_client.go | 160 ++++++++++++++ .../kie_client_test.go | 2 +- servicecombkie/kie_client.go | 204 ------------------ 8 files changed, 172 insertions(+), 218 deletions(-) create mode 100644 servicecomb/kie_client.go rename {servicecombkie => servicecomb}/kie_client_test.go (99%) delete mode 100644 servicecombkie/kie_client.go diff --git a/README.md b/README.md index b2de875..a576000 100755 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ Supported distributed configuration management service: | name | import |description | |----------|----------|:-------------:| -|config_center |github.com/go-chassis/go-chassis-config/configcenter |huawei cloud CSE config center https://www.huaweicloud.com/product/cse.html | +|config_center |github.com/go-chassis/go-chassis-config/configcenter |huawei cloud CSE config center https://www.huaweicloud.com/product/cse.html | |apollo(not longer under maintenance) |github.com/go-chassis/go-chassis-config/apollo |ctrip apollo https://github.com/ctripcorp/apollo | -|servicecomb-kie |github.com/apache/servicecomb-kie |apache servicecomb-kie https://github.com/apache/servicecomb-kie | +|servicecomb-kie |github.com/o-chassis-config/servicecomb |apache servicecomb-kie https://github.com/apache/servicecomb-kie | # Example Get a client of config center diff --git a/configcenter/config_center.go b/configcenter/config_center.go index cbfab5c..33c6ec5 100755 --- a/configcenter/config_center.go +++ b/configcenter/config_center.go @@ -42,10 +42,9 @@ var ( //ConfigCenter is ConfigCenter Implementation of ConfigCenter type ConfigCenter struct { - c *configcenter.Client - opts config.Options - refreshPort string - wsDialer *websocket.Dialer + c *configcenter.Client + opts config.Options + wsDialer *websocket.Dialer } //NewConfigCenter is a function @@ -77,9 +76,8 @@ func NewConfigCenter(options config.Options) (config.Client, error) { } cc := &ConfigCenter{ - c: c, - opts: options, - refreshPort: options.RefreshPort, + c: c, + opts: options, } openlogging.Info("new config center client", openlogging.WithTags( openlogging.Tags{ diff --git a/configcenter/util.go b/configcenter/util.go index 5461f29..792cef9 100644 --- a/configcenter/util.go +++ b/configcenter/util.go @@ -18,8 +18,8 @@ package configcenter import ( + "errors" "fmt" - "github.com/pkg/errors" "regexp" ) diff --git a/configcenter/util_test.go b/configcenter/util_test.go index b2911a8..5d4b740 100644 --- a/configcenter/util_test.go +++ b/configcenter/util_test.go @@ -19,7 +19,7 @@ package configcenter_test import ( "github.com/go-chassis/go-chassis-config/configcenter" - "gopkg.in/go-playground/assert.v1" + "github.com/stretchr/testify/assert" "testing" ) diff --git a/go.mod b/go.mod index 43c84e5..b177c32 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/go-chassis/go-chassis-config require ( github.com/apache/servicecomb-kie v0.1.0 github.com/go-chassis/foundation v0.0.0-20190621030543-c3b63f787f4c - github.com/go-chassis/paas-lager v1.0.1 - github.com/go-mesh/openlogging v1.0.0 + github.com/go-chassis/paas-lager v1.0.2-0.20190328010332-cf506050ddb2 + github.com/go-mesh/openlogging v1.0.1-0.20181205082104-3d418c478b2d github.com/gorilla/websocket v1.4.0 github.com/stretchr/testify v1.3.0 ) diff --git a/servicecomb/kie_client.go b/servicecomb/kie_client.go new file mode 100644 index 0000000..ea1d1a5 --- /dev/null +++ b/servicecomb/kie_client.go @@ -0,0 +1,160 @@ +/* + * 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 servicecomb + +import ( + "context" + "errors" + + "github.com/apache/servicecomb-kie/client" + "github.com/apache/servicecomb-kie/pkg/model" + "github.com/go-chassis/go-chassis-config" + "github.com/go-mesh/openlogging" +) + +// Client contains the implementation of Client +type Client struct { + KieClient *client.Client + DefaultLabels map[string]string + opts config.Options +} + +const ( + //Name of the Plugin + Name = "servicecomb-kie" + LabelService = "serviceName" + LabelVersion = "version" + LabelEnvironment = "environment" + LabelApp = "app" +) + +// NewClient init the necessary objects needed for seamless communication to Kie Server +func NewClient(options config.Options) (config.Client, error) { + kieClient := &Client{ + opts: options, + } + DefaultLabels := map[string]string{ + LabelApp: options.App, + LabelEnvironment: options.Env, + LabelService: options.ServiceName, + LabelVersion: options.Version, + } + configInfo := client.Config{Endpoint: kieClient.opts.ServerURI, DefaultLabels: DefaultLabels, VerifyPeer: kieClient.opts.EnableSSL} + var err error + kieClient.KieClient, err = client.New(configInfo) + if err != nil { + openlogging.Error("KieClient Initialization Failed: " + err.Error()) + } + openlogging.Debug("KieClient Initialized successfully") + return kieClient, err +} + +// PullConfigs is used for pull config from servicecomb-kie +func (c *Client) PullConfigs(serviceName, version, app, env string) (map[string]interface{}, error) { + openlogging.Debug("KieClient begin PullConfigs") + labels := map[string]string{LabelService: serviceName, LabelVersion: version, LabelApp: app, LabelEnvironment: env} + labelsAppLevel := map[string]string{LabelApp: app, LabelEnvironment: env} + configsInfo := make(map[string]interface{}) + configurationsValue, err := c.KieClient.SearchByLabels(context.TODO(), client.WithGetProject(serviceName), client.WithLabels(labels, labelsAppLevel)) + if err != nil { + openlogging.GetLogger().Errorf("Error in Querying the Response from Kie %s %#v", err.Error(), labels) + return nil, err + } + openlogging.GetLogger().Debugf("KieClient SearchByLabels. %#v", labels) + //Parse config result. + for _, docRes := range configurationsValue { + for _, docInfo := range docRes.Data { + configsInfo[docInfo.Key] = docInfo.Value + } + } + return configsInfo, nil +} + +// PullConfig get config by key and labels. +func (c *Client) PullConfig(serviceName, version, app, env, key, contentType string) (interface{}, error) { + labels := map[string]string{LabelService: serviceName, LabelVersion: version, LabelApp: app, LabelEnvironment: env} + configurationsValue, err := c.KieClient.Get(context.TODO(), key, client.WithGetProject(serviceName), client.WithLabels(labels)) + if err != nil { + openlogging.GetLogger().Error("Error in Querying the Response from Kie: " + err.Error()) + return nil, err + } + for _, doc := range configurationsValue { + for _, kvDoc := range doc.Data { + if key == kvDoc.Key { + openlogging.GetLogger().Debugf("The Key Value of : ", kvDoc.Value) + return doc, nil + } + } + } + return nil, errors.New("can not find value") +} + +//PullConfigsByDI not implemented +func (c *Client) PullConfigsByDI(dimensionInfo string) (map[string]map[string]interface{}, error) { + // TODO Return the configurations for customized Projects in Kie Configs + return nil, errors.New("not implemented") +} + +//PushConfigs put config in kie by key and labels. +func (c *Client) PushConfigs(data map[string]interface{}, serviceName, version, app, env string) (map[string]interface{}, error) { + var configReq model.KVDoc + labels := map[string]string{LabelService: serviceName, LabelVersion: version, LabelApp: app, LabelEnvironment: env} + configResult := make(map[string]interface{}) + for key, configValue := range data { + configReq.Key = key + configReq.Value = configValue.(string) + configReq.Labels = labels + configurationsValue, err := c.KieClient.Put(context.TODO(), configReq, client.WithProject(serviceName)) + if err != nil { + openlogging.Error("Error in PushConfigs to Kie: " + err.Error()) + return nil, err + } + openlogging.Debug("The Key Value of : " + configurationsValue.Value) + configResult[configurationsValue.Key] = configurationsValue.Value + } + return configResult, nil +} + +//DeleteConfigsByKeys use keyId for delete +func (c *Client) DeleteConfigsByKeys(keys []string, serviceName, version, app, env string) (map[string]interface{}, error) { + result := make(map[string]interface{}) + for _, keyId := range keys { + err := c.KieClient.Delete(context.TODO(), keyId, "", client.WithProject(serviceName)) + if err != nil { + openlogging.Error("Error in Delete from Kie. " + err.Error()) + return nil, err + } + openlogging.GetLogger().Debugf("Delete The KeyId:%s", keyId) + } + return result, nil +} + +//Watch not implemented because kie not support. +func (c *Client) Watch(f func(map[string]interface{}), errHandler func(err error)) error { + // TODO watch change events + return errors.New("not implemented") +} + +//Options. +func (c *Client) Options() config.Options { + return c.opts +} + +func init() { + config.InstallConfigClientPlugin(Name, NewClient) +} diff --git a/servicecombkie/kie_client_test.go b/servicecomb/kie_client_test.go similarity index 99% rename from servicecombkie/kie_client_test.go rename to servicecomb/kie_client_test.go index a79b59f..e4fc3db 100644 --- a/servicecombkie/kie_client_test.go +++ b/servicecomb/kie_client_test.go @@ -15,7 +15,7 @@ * limitations under the License. */ -package servicecombkie +package servicecomb import ( "encoding/json" diff --git a/servicecombkie/kie_client.go b/servicecombkie/kie_client.go deleted file mode 100644 index 2000a1a..0000000 --- a/servicecombkie/kie_client.go +++ /dev/null @@ -1,204 +0,0 @@ -/* - * 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 servicecombkie - -import ( - "context" - "crypto/tls" - "errors" - "fmt" - sckieclient "github.com/apache/servicecomb-kie/client" - "github.com/apache/servicecomb-kie/pkg/model" - "github.com/go-chassis/go-chassis-config" - "github.com/go-mesh/openlogging" -) - -// Client contains the implementation of Client -type Client struct { - KieClient *sckieclient.Client - ServiceName string - ServerURI string - EnableSSL bool - Namespace string - TenantName string - Endpoint string - Cluster string - App string - Env string - Version string - TLSConfig *tls.Config - APIVersion string - AutoDiscovery bool - RefreshPort string -} - -const ( - //Name of the Plugin - Name = "servicecomb-kie" -) - -const ( - RetSucc = 0 - RetDelErr = 1 -) - -// NewClient init the necessary objects needed for seamless communication to Kie Server -func NewClient(options config.Options) (config.Client, error) { - kieClient := &Client{ - ServiceName: options.ServiceName, - ServerURI: options.ServerURI, - EnableSSL: options.EnableSSL, - Namespace: options.Namespace, - TenantName: options.TenantName, - Endpoint: options.Endpoint, - Cluster: options.Cluster, - App: options.App, - Env: options.Env, - Version: options.Version, - TLSConfig: options.TLSConfig, - APIVersion: options.APIVersion, - AutoDiscovery: options.AutoDiscovery, - RefreshPort: options.RefreshPort, - } - defaultLabels := make(map[string]string) - configInfo := sckieclient.Config{Endpoint: kieClient.ServerURI, DefaultLabels: defaultLabels, VerifyPeer: kieClient.EnableSSL} - var err error - kieClient.KieClient, err = sckieclient.New(configInfo) - if err != nil { - openlogging.GetLogger().Error("KieClient Initialization Failed: " + err.Error()) - } - openlogging.GetLogger().Debugf("KieClient Initialized successfully") - return kieClient, err -} - -// PullConfigs is used for pull config from servicecomb-kie -func (kieClient *Client) PullConfigs(serviceName, version, app, env string) (map[string]interface{}, error) { - openlogging.GetLogger().Debugf("KieClient begin PullConfigs") - labels := map[string]string{"servicename": serviceName, "version": version, "app": app, "env": env} - labelsAE := map[string]string{"app": app, "env": env} - configsInfo := make(map[string]interface{}) - configurationsValue, err := kieClient.KieClient.SearchByLabels(context.TODO(), sckieclient.WithGetProject(serviceName), sckieclient.WithLabels(labels, labelsAE)) - if err != nil { - openlogging.GetLogger().Errorf("Error in Querying the Response from Kie %s %#v", err.Error(), labels) - return nil, err - } - openlogging.GetLogger().Debugf("KieClient SearchByLabels. %#v", labels) - //Parse config result. - for _, docRes := range configurationsValue { - for _, docInfo := range docRes.Data { - configsInfo[docInfo.Key] = docInfo.Value - } - } - return configsInfo, nil -} - -// PullConfig get config by key and labels. -func (kieClient *Client) PullConfig(serviceName, version, app, env, key, contentType string) (interface{}, error) { - fmt.Println("PullConfig") - labels := map[string]string{"servicename": serviceName, "version": version, "app": app, "env": env} - configurationsValue, err := kieClient.KieClient.Get(context.TODO(), key, sckieclient.WithGetProject(serviceName), sckieclient.WithLabels(labels)) - if err != nil { - openlogging.GetLogger().Error("Error in Querying the Response from Kie: " + err.Error()) - return nil, err - } - for _, doc := range configurationsValue { - for _, kvDoc := range doc.Data { - if key == kvDoc.Key { - openlogging.GetLogger().Debugf("The Key Value of : ", kvDoc.Value) - return doc, nil - } - } - } - return nil, errors.New("can not find value") -} - -//PullConfigsByDI not implemented -func (kieClient *Client) PullConfigsByDI(dimensionInfo string) (map[string]map[string]interface{}, error) { - // TODO Return the configurations for customized Projects in Kie Configs - fmt.Println(" PullConfigsByDI") - return nil, errors.New("not implemented") -} - -//PushConfigs put config in kie by key and labels. -func (kieClient *Client) PushConfigs(data map[string]interface{}, serviceName, version, app, env string) (map[string]interface{}, error) { - fmt.Println("PushConfigs") - var configReq model.KVDoc - labels := map[string]string{"servicename": serviceName, "version": version, "app": app, "env": env} - configResult := make(map[string]interface{}) - for key, configValue := range data { - configReq.Key = key - configReq.Value = configValue.(string) - configReq.Labels = labels - configurationsValue, err := kieClient.KieClient.Put(context.TODO(), configReq, sckieclient.WithProject(serviceName)) - if err != nil { - openlogging.GetLogger().Error("Error in PushConfigs to Kie: " + err.Error()) - return nil, err - } - openlogging.GetLogger().Debugf("The Key Value of : ", configurationsValue) - configResult[configurationsValue.Key] = configurationsValue.Value - } - return configResult, nil -} - -//DeleteConfigsByKeys use keyId for delete -func (kieClient *Client) DeleteConfigsByKeys(keys []string, serviceName, version, app, env string) (map[string]interface{}, error) { - fmt.Println("DeleteConfigsByKeys") - result := make(map[string]interface{}) - for _, keyId := range keys { - err := kieClient.KieClient.Delete(context.TODO(), keyId, "", sckieclient.WithProject(serviceName)) - if err != nil { - openlogging.GetLogger().Errorf("Error in Delete from Kie. %s " + err.Error()) - result[keyId] = RetDelErr - } else { - openlogging.GetLogger().Debugf("Delete The KeyId:%s", keyId) - result[keyId] = RetSucc - } - } - return result, nil -} - -//Watch not implemented because kie not support. -func (kieClient *Client) Watch(f func(map[string]interface{}), errHandler func(err error)) error { - return nil -} - -//Options. -func (kieClient *Client) Options() config.Options { - fmt.Println("Options") - optionInfo := config.Options{ - ServiceName: kieClient.ServiceName, - ServerURI: kieClient.ServerURI, - EnableSSL: kieClient.EnableSSL, - Namespace: kieClient.Namespace, - TenantName: kieClient.TenantName, - Endpoint: kieClient.Endpoint, - Cluster: kieClient.Cluster, - App: kieClient.App, - Env: kieClient.Env, - Version: kieClient.Version, - TLSConfig: kieClient.TLSConfig, - APIVersion: kieClient.APIVersion, - AutoDiscovery: kieClient.AutoDiscovery, - RefreshPort: kieClient.RefreshPort, - } - return optionInfo -} - -func init() { - config.InstallConfigClientPlugin(Name, NewClient) -}