fix: k8s nacos and zookeeper registry lint (#1599)

pull/1611/head
wangcong 3 years ago committed by GitHub
parent f42b1c4dd2
commit cf385f6285
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      contrib/encoding/msgpack/go.sum
  2. 3
      contrib/encoding/msgpack/msgpack.go
  3. 11
      contrib/registry/kubernetes/registry.go
  4. 5
      contrib/registry/nacos/registry.go
  5. 11
      contrib/registry/nacos/watcher.go
  6. 17
      contrib/registry/zookeeper/register.go
  7. 12
      contrib/registry/zookeeper/register_test.go
  8. 8
      contrib/registry/zookeeper/watcher.go
  9. 1
      examples/go.sum
  10. 4
      hack/.lintcheck_failures

@ -44,7 +44,6 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@ -124,7 +123,6 @@ golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
@ -152,7 +150,6 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

@ -1,8 +1,9 @@
package msgpack
import (
"github.com/go-kratos/kratos/v2/encoding"
"github.com/vmihailenco/msgpack/v5"
"github.com/go-kratos/kratos/v2/encoding"
)
// Name is the name registered for the msgpack compressor.

@ -12,7 +12,6 @@ import (
"strings"
"time"
"github.com/go-kratos/kratos/v2/registry"
jsoniter "github.com/json-iterator/go"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -22,6 +21,8 @@ import (
"k8s.io/client-go/kubernetes"
listerv1 "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"github.com/go-kratos/kratos/v2/registry"
)
// Defines the key name of specific fields
@ -166,7 +167,7 @@ func (s *Registry) Service(name string) ([]*registry.ServiceInstance, error) {
if err != nil {
return nil, err
}
var ret []*registry.ServiceInstance
ret := make([]*registry.ServiceInstance, 0, len(pods))
for _, pod := range pods {
if pod.Status.Phase != corev1.PodRunning {
continue
@ -342,14 +343,14 @@ func getProtocolMapByEndpoints(endpoints []string) (protocolMap, error) {
}
func getProtocolMapFromPod(pod *corev1.Pod) (protocolMap, error) {
protocolMap := protocolMap{}
protoMap := protocolMap{}
if s := pod.Annotations[AnnotationsKeyProtocolMap]; !isEmptyObjectString(s) {
err := unmarshal(s, &protocolMap)
err := unmarshal(s, &protoMap)
if err != nil {
return nil, &ErrorHandleResource{Namespace: pod.Namespace, Name: pod.Name, Reason: err}
}
}
return protocolMap, nil
return protoMap, nil
}
func getMetadataFromPod(pod *corev1.Pod) (map[string]string, error) {

@ -7,9 +7,10 @@ import (
"net/url"
"strconv"
"github.com/go-kratos/kratos/v2/registry"
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/vo"
"github.com/go-kratos/kratos/v2/registry"
)
var (
@ -155,7 +156,7 @@ func (r *Registry) GetService(ctx context.Context, serviceName string) ([]*regis
if err != nil {
return nil, err
}
var items []*registry.ServiceInstance
items := make([]*registry.ServiceInstance, 0, len(res))
for _, in := range res {
items = append(items, &registry.ServiceInstance{
ID: in.InstanceId,

@ -4,16 +4,15 @@ import (
"context"
"fmt"
"github.com/go-kratos/kratos/v2/registry"
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/model"
"github.com/nacos-group/nacos-sdk-go/vo"
)
var (
_ registry.Watcher = (*watcher)(nil)
"github.com/go-kratos/kratos/v2/registry"
)
var _ registry.Watcher = (*watcher)(nil)
type watcher struct {
serviceName string
clusters []string
@ -46,7 +45,6 @@ func newWatcher(ctx context.Context, cli naming_client.INamingClient, serviceNam
}
func (w *watcher) Next() ([]*registry.ServiceInstance, error) {
for {
select {
case <-w.ctx.Done():
return nil, w.ctx.Err()
@ -60,7 +58,7 @@ func (w *watcher) Next() ([]*registry.ServiceInstance, error) {
if err != nil {
return nil, err
}
var items []*registry.ServiceInstance
items := make([]*registry.ServiceInstance, 0, len(res.Hosts))
for _, in := range res.Hosts {
items = append(items, &registry.ServiceInstance{
ID: in.InstanceId,
@ -72,7 +70,6 @@ func (w *watcher) Next() ([]*registry.ServiceInstance, error) {
}
return items, nil
}
}
func (w *watcher) Stop() error {
w.cancel()

@ -8,8 +8,9 @@ import (
"sync/atomic"
"time"
"github.com/go-kratos/kratos/v2/registry"
"github.com/go-zookeeper/zk"
"github.com/go-kratos/kratos/v2/registry"
)
var (
@ -70,9 +71,11 @@ func New(zkServers []string, opts ...Option) (*Registry, error) {
}
func (r *Registry) Register(ctx context.Context, service *registry.ServiceInstance) error {
var data []byte
var err error
if err := r.ensureName(r.opts.rootPath, []byte("")); err != nil {
var (
data []byte
err error
)
if err = r.ensureName(r.opts.rootPath, []byte("")); err != nil {
return err
}
serviceNamePath := path.Join(r.opts.rootPath, service.Name)
@ -113,9 +116,9 @@ func (r *Registry) GetService(ctx context.Context, serviceName string) ([]*regis
if err != nil {
return nil, err
}
var items []*registry.ServiceInstance
items := make([]*registry.ServiceInstance, 0, len(servicesID))
for _, service := range servicesID {
var item = &registry.ServiceInstance{}
item := &registry.ServiceInstance{}
servicePath := path.Join(serviceNamePath, service)
serviceInstanceByte, _, err := r.conn.Get(servicePath)
if err != nil {
@ -135,7 +138,7 @@ func (r *Registry) Watch(ctx context.Context, serviceName string) (registry.Watc
set, ok := r.registry[serviceName]
if !ok {
set = &serviceSet{
watcher: make(map[*watcher]struct{}, 0),
watcher: make(map[*watcher]struct{}),
services: &atomic.Value{},
serviceName: serviceName,
}

@ -22,11 +22,13 @@ func TestRegistry(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer w.Stop()
defer func() {
_ = w.Stop()
}()
go func() {
for {
res, err := w.Next()
if err != nil {
res, nextErr := w.Next()
if nextErr != nil {
return
}
t.Logf("watch: %d", len(res))
@ -37,7 +39,7 @@ func TestRegistry(t *testing.T) {
}()
time.Sleep(time.Second)
if err := r.Register(ctx, s); err != nil {
if err = r.Register(ctx, s); err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)
@ -53,7 +55,7 @@ func TestRegistry(t *testing.T) {
t.Errorf("not expected: %+v", res)
}
if err := r.Deregister(ctx, s); err != nil {
if err = r.Deregister(ctx, s); err != nil {
t.Fatal(err)
}
time.Sleep(time.Second)

@ -6,9 +6,7 @@ import (
"github.com/go-kratos/kratos/v2/registry"
)
var (
_ registry.Watcher = &watcher{}
)
var _ registry.Watcher = &watcher{}
type watcher struct {
ctx context.Context
@ -25,9 +23,7 @@ func (w watcher) Next() (services []*registry.ServiceInstance, err error) {
}
ss, ok := w.set.services.Load().([]*registry.ServiceInstance)
if ok {
for _, s := range ss {
services = append(services, s)
}
services = append(services, ss...)
}
return
}

@ -748,7 +748,6 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

@ -1,3 +1 @@
./contrib/registry/nacos
./contrib/registry/kubernetes
./contrib/registry/zookeeper

Loading…
Cancel
Save