From 0c81f3d75539b0561ce8c482ec20bb4149bbc8a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel=20Ortu=C3=B1o?= Date: Thu, 20 Jan 2022 19:16:06 +0100 Subject: [PATCH] removed etcd mandatory dependency (#195) --- pkg/c2s/resource_manager.go | 3 --- pkg/cluster/etcd/etcd.go | 2 +- pkg/cluster/kv/nop_kv.go | 41 ++++++++++++++++++++++++++++++++ pkg/cluster/locker/locker.go | 2 +- pkg/cluster/locker/nop_locker.go | 33 +++++++++++++++++++++++++ pkg/jackal/jackal.go | 12 ++++++---- 6 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 pkg/cluster/kv/nop_kv.go create mode 100644 pkg/cluster/locker/nop_locker.go diff --git a/pkg/c2s/resource_manager.go b/pkg/c2s/resource_manager.go index 6c207e2a9..da357af7f 100644 --- a/pkg/c2s/resource_manager.go +++ b/pkg/c2s/resource_manager.go @@ -19,7 +19,6 @@ import ( "fmt" "strings" "sync" - "time" kitlog "github.com/go-kit/log" @@ -38,8 +37,6 @@ const ( resourceKeyPrefix = "r://" ) -const clearActiveKeyTimeout = time.Minute - // ResourceManager type is in charge of keeping track of all cluster resources. type ResourceManager struct { kv kv.KV diff --git a/pkg/cluster/etcd/etcd.go b/pkg/cluster/etcd/etcd.go index 1b5a463d1..a766926f6 100644 --- a/pkg/cluster/etcd/etcd.go +++ b/pkg/cluster/etcd/etcd.go @@ -18,7 +18,7 @@ import "time" // Config contains etcd configuration parameters. type Config struct { - Endpoints []string `fig:"endpoints" default:"[http://localhost:2379]"` + Endpoints []string `fig:"endpoints"` DialTimeout time.Duration `fig:"dial_timeout" default:"20s"` DialKeepAliveTime time.Duration `fig:"dial_keep_alive_time" default:"30s"` DialKeepAliveTimeout time.Duration `fig:"dial_keep_alive_timeout" default:"10s"` diff --git a/pkg/cluster/kv/nop_kv.go b/pkg/cluster/kv/nop_kv.go new file mode 100644 index 000000000..155c0439a --- /dev/null +++ b/pkg/cluster/kv/nop_kv.go @@ -0,0 +1,41 @@ +// Copyright 2021 The jackal 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 kv + +import "context" + +// NewNopKV returns a KV that doesn't do anything. +func NewNopKV() KV { return &nopKV{} } + +type nopKV struct{} + +func (k *nopKV) Put(_ context.Context, _ string, _ string) error { return nil } +func (k *nopKV) Get(_ context.Context, _ string) ([]byte, error) { return nil, nil } + +func (k *nopKV) GetPrefix(_ context.Context, _ string) (map[string][]byte, error) { + return nil, nil +} + +func (k *nopKV) Del(_ context.Context, _ string) error { return nil } + +func (k *nopKV) Watch(_ context.Context, _ string, _ bool) <-chan WatchResp { + // return an already closed channel + retCh := make(chan WatchResp) + close(retCh) + return retCh +} + +func (k *nopKV) Start(_ context.Context) error { return nil } +func (k *nopKV) Stop(_ context.Context) error { return nil } diff --git a/pkg/cluster/locker/locker.go b/pkg/cluster/locker/locker.go index d81e94311..2768fe913 100644 --- a/pkg/cluster/locker/locker.go +++ b/pkg/cluster/locker/locker.go @@ -30,6 +30,6 @@ type Locker interface { // Lock defines distributed lock object. type Lock interface { - // Releases releases a previously acquired lock. + // Release releases a previously acquired lock. Release(ctx context.Context) error } diff --git a/pkg/cluster/locker/nop_locker.go b/pkg/cluster/locker/nop_locker.go new file mode 100644 index 000000000..d577fbc1d --- /dev/null +++ b/pkg/cluster/locker/nop_locker.go @@ -0,0 +1,33 @@ +// Copyright 2021 The jackal 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 locker + +import "context" + +// NewNopLocker returns a Locker that doesn't do anything. +func NewNopLocker() Locker { return &nopLocker{} } + +type nopLocker struct{} + +func (l *nopLocker) AcquireLock(_ context.Context, _ string) (Lock, error) { + return &nopLock{}, nil +} + +func (l *nopLocker) Start(_ context.Context) error { return nil } +func (l *nopLocker) Stop(_ context.Context) error { return nil } + +type nopLock struct{} + +func (l *nopLock) Release(_ context.Context) error { return nil } diff --git a/pkg/jackal/jackal.go b/pkg/jackal/jackal.go index 0625c26a1..ed15c9b9b 100644 --- a/pkg/jackal/jackal.go +++ b/pkg/jackal/jackal.go @@ -138,6 +138,8 @@ func New(output io.Writer, args []string) *Jackal { output: output, args: args, waitStopCh: make(chan os.Signal, 1), + locker: locker.NewNopLocker(), + kv: kv.NewNopKV(), } } @@ -212,11 +214,13 @@ func (j *Jackal) Run() error { j.hk = hook.NewHooks() // init etcd - if err := j.checkEtcdHealth(cfg.Cluster.Etcd.Endpoints); err != nil { - return err + if len(cfg.Cluster.Etcd.Endpoints) > 0 { + if err := j.checkEtcdHealth(cfg.Cluster.Etcd.Endpoints); err != nil { + return err + } + j.initLocker(cfg.Cluster.Etcd) + j.initKVStore(cfg.Cluster.Etcd) } - j.initLocker(cfg.Cluster.Etcd) - j.initKVStore(cfg.Cluster.Etcd) // init cluster connection manager j.initClusterConnManager()