diff --git a/CHANGELOG.md b/CHANGELOG.md index 1addd0e051..c3196939dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ Versioning](http://semver.org/spec/v2.0.0.html). ## Unreleased +### Fixed +- In addition to the context error print the connection error when sensu-go can't connect to etcd. ### Changed - Empty and zero value configuration parameters for `etcd` do not overwrite diff --git a/backend/backend.go b/backend/backend.go index 9eee1d8952..7abc842dbb 100644 --- a/backend/backend.go +++ b/backend/backend.go @@ -199,6 +199,7 @@ func newClient(ctx context.Context, config *Config, backend *Backend) (*clientv3 Password: config.EtcdClientPassword, TLS: tlsConfig, DialOptions: []grpc.DialOption{ + grpc.WithReturnConnectionError(), grpc.WithBlock(), }, } @@ -208,6 +209,7 @@ func newClient(ctx context.Context, config *Config, backend *Backend) (*clientv3 DialTimeout: 5 * time.Second, TLS: tlsConfig, DialOptions: []grpc.DialOption{ + grpc.WithReturnConnectionError(), grpc.WithBlock(), }, } @@ -538,11 +540,11 @@ func Initialize(ctx context.Context, config *Config) (*Backend, error) { // Prepare the authentication providers authenticator := &authentication.Authenticator{} - basic := &basic.Provider{ + provider := &basic.Provider{ ObjectMeta: corev2.ObjectMeta{Name: basic.Type}, Store: b.Store, } - authenticator.AddProvider(basic) + authenticator.AddProvider(provider) var clusterVersion string // only retrieve the cluster version if etcd is embedded @@ -625,7 +627,7 @@ func Initialize(ctx context.Context, config *Config) (*Backend, error) { func (b *Backend) runOnce() error { eCloser := b.StoreUpdater.(closer) - defer eCloser.Close() + defer func() { _ = eCloser.Close() }() var derr error @@ -682,7 +684,7 @@ func (b *Backend) runOnce() error { if err != nil { logger.WithError(err).Error("unable to open platform metrics log file") } else { - defer metricsLogWriter.Close() + defer func() { _ = metricsLogWriter.Close() }() metricsBridge, err := metrics.NewInfluxBridge(&metrics.InfluxBridgeConfig{ Writer: metricsLogWriter, Interval: b.Cfg.PlatformMetricsLoggingInterval, diff --git a/backend/cmd/init.go b/backend/cmd/init.go index 5a9fe652d4..1cbf4a4147 100644 --- a/backend/cmd/init.go +++ b/backend/cmd/init.go @@ -197,17 +197,23 @@ func InitCommand() *cobra.Command { if etcdClientUsername != "" && etcdClientPassword != "" { clientConfig = clientv3.Config{ - Endpoints: []string{url}, - Username: etcdClientUsername, - Password: etcdClientPassword, - TLS: tlsConfig, - DialOptions: []grpc.DialOption{grpc.WithBlock()}, + Endpoints: []string{url}, + Username: etcdClientUsername, + Password: etcdClientPassword, + TLS: tlsConfig, + DialOptions: []grpc.DialOption{ + grpc.WithReturnConnectionError(), + grpc.WithBlock(), + }, } } else { clientConfig = clientv3.Config{ - Endpoints: []string{url}, - TLS: tlsConfig, - DialOptions: []grpc.DialOption{grpc.WithBlock()}, + Endpoints: []string{url}, + TLS: tlsConfig, + DialOptions: []grpc.DialOption{ + grpc.WithReturnConnectionError(), + grpc.WithBlock(), + }, } } err := initializeStore(clientConfig, initConfig, url) @@ -254,7 +260,7 @@ func initializeStore(clientConfig clientv3.Config, initConfig initConfig, endpoi if err != nil { return fmt.Errorf("error connecting to etcd endpoint: %w", err) } - defer client.Close() + defer func() { _ = client.Close() }() // Check if etcd endpoint is reachable if _, err := client.Status(ctx, endpoint); err != nil { diff --git a/backend/etcd/etcd.go b/backend/etcd/etcd.go index 8dbfbab2f0..f102a52415 100644 --- a/backend/etcd/etcd.go +++ b/backend/etcd/etcd.go @@ -318,6 +318,7 @@ func (e *Etcd) NewClientContext(ctx context.Context) (*clientv3.Client, error) { DialTimeout: 60 * time.Second, TLS: tlsConfig, DialOptions: []grpc.DialOption{ + grpc.WithReturnConnectionError(), grpc.WithBlock(), }, Context: ctx,