From 8bfafb28ed05313fea88d8385b1784a12713fa53 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Wed, 18 Jan 2023 12:44:44 +0300 Subject: [PATCH 1/2] [TrueCloudLab#7] Require only one healthy server Signed-off-by: Denis Kirillov --- app.go | 50 +++++++++++++++++++++++++++++++++++++------------- server.go | 10 ++++------ 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/app.go b/app.go index 3328e54..b43b144 100644 --- a/app.go +++ b/app.go @@ -485,33 +485,57 @@ func (a *app) AppParams() *utils.AppParams { func (a *app) initServers(ctx context.Context) { serversInfo := fetchServers(a.cfg) - a.servers = make([]Server, len(serversInfo)) - for i, serverInfo := range serversInfo { - a.log.Info("added server", + a.servers = make([]Server, 0, len(serversInfo)) + for _, serverInfo := range serversInfo { + fields := []zap.Field{ zap.String("address", serverInfo.Address), zap.Bool("tls enabled", serverInfo.TLS.Enabled), - zap.String("tls cert", serverInfo.TLS.CertFile), zap.String("tls key", serverInfo.TLS.KeyFile)) - a.servers[i] = newServer(ctx, serverInfo, a.log) + zap.String("tls cert", serverInfo.TLS.CertFile), zap.String("tls key", serverInfo.TLS.KeyFile), + } + srv, err := newServer(ctx, serverInfo) + if err != nil { + a.log.Warn("failed to add server", append(fields, zap.Error(err))...) + continue + } + + a.servers = append(a.servers, srv) + a.log.Info("add server", fields...) + } + + if len(a.servers) == 0 { + a.log.Fatal("no healthy servers") } } func (a *app) updateServers() error { serversInfo := fetchServers(a.cfg) - if len(serversInfo) != len(a.servers) { - return fmt.Errorf("invalid servers configuration: length mismatch: old '%d', new '%d", len(a.servers), len(serversInfo)) - } - - for i, serverInfo := range serversInfo { - if serverInfo.Address != a.servers[i].Address() { - return fmt.Errorf("invalid servers configuration: addresses mismatch: old '%s', new '%s", a.servers[i].Address(), serverInfo.Address) + var found bool + for _, serverInfo := range serversInfo { + index := a.serverIndex(serverInfo.Address) + if index == -1 { + continue } if serverInfo.TLS.Enabled { - if err := a.servers[i].UpdateCert(serverInfo.TLS.CertFile, serverInfo.TLS.KeyFile); err != nil { + if err := a.servers[index].UpdateCert(serverInfo.TLS.CertFile, serverInfo.TLS.KeyFile); err != nil { return fmt.Errorf("failed to update tls certs: %w", err) } } + found = true + } + + if !found { + return fmt.Errorf("invalid servers configuration: no known server found") } return nil } + +func (a *app) serverIndex(address string) int { + for i := range a.servers { + if a.servers[i].Address() == address { + return i + } + } + return -1 +} diff --git a/server.go b/server.go index 3843081..c5852d8 100644 --- a/server.go +++ b/server.go @@ -7,8 +7,6 @@ import ( "fmt" "net" "sync" - - "go.uber.org/zap" ) type ( @@ -57,11 +55,11 @@ func (s *server) UpdateCert(certFile, keyFile string) error { return s.tlsProvider.UpdateCert(certFile, keyFile) } -func newServer(ctx context.Context, serverInfo ServerInfo, logger *zap.Logger) *server { +func newServer(ctx context.Context, serverInfo ServerInfo) (*server, error) { var lic net.ListenConfig ln, err := lic.Listen(ctx, "tcp", serverInfo.Address) if err != nil { - logger.Fatal("could not prepare listener", zap.String("address", serverInfo.Address), zap.Error(err)) + return nil, fmt.Errorf("could not prepare listener: %w", err) } tlsProvider := &certProvider{ @@ -70,7 +68,7 @@ func newServer(ctx context.Context, serverInfo ServerInfo, logger *zap.Logger) * if serverInfo.TLS.Enabled { if err = tlsProvider.UpdateCert(serverInfo.TLS.CertFile, serverInfo.TLS.KeyFile); err != nil { - logger.Fatal("failed to update cert", zap.Error(err)) + return nil, fmt.Errorf("failed to update cert: %w", err) } ln = tls.NewListener(ln, &tls.Config{ @@ -82,7 +80,7 @@ func newServer(ctx context.Context, serverInfo ServerInfo, logger *zap.Logger) * address: serverInfo.Address, listener: ln, tlsProvider: tlsProvider, - } + }, nil } func (p *certProvider) GetCertificate(*tls.ClientHelloInfo) (*tls.Certificate, error) { -- 2.45.2 From 4b1012c75d077dcf67e6658e1166376cd14a518f Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Wed, 18 Jan 2023 12:56:14 +0300 Subject: [PATCH 2/2] [TrueCloudLab#7] Fix tests Signed-off-by: Denis Kirillov --- integration_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/integration_test.go b/integration_test.go index 3d6f14b..e1a53f5 100644 --- a/integration_test.go +++ b/integration_test.go @@ -44,7 +44,6 @@ func TestIntegration(t *testing.T) { rootCtx := context.Background() aioImage := "nspccdev/neofs-aio-testcontainer:" versions := []string{ - "0.27.5", "0.28.1", "0.29.0", "0.30.0", -- 2.45.2