From 9d9d5e2dce9c6b245ea7bacca4e22935325c9a75 Mon Sep 17 00:00:00 2001 From: Nikita Vanyasin Date: Tue, 10 May 2022 11:48:03 +0400 Subject: [PATCH] [Bugfix] Fix arangosync members state inspection (#970) --- CHANGELOG.md | 1 + pkg/deployment/member/image.go | 32 ++++++++++++ pkg/deployment/member/state.go | 78 +++++++++++++++++----------- pkg/deployment/reconciler/context.go | 4 +- pkg/deployment/server_api.go | 7 +-- 5 files changed, 86 insertions(+), 36 deletions(-) create mode 100644 pkg/deployment/member/image.go diff --git a/CHANGELOG.md b/CHANGELOG.md index e224b6f29..403e349ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Change Log ## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A) +- (Bugfix) Fix arangosync members state inspection ## [1.2.12](https://github.com/arangodb/kube-arangodb/tree/1.2.12) (2022-05-10) - (Feature) Add CoreV1 Endpoints Inspector diff --git a/pkg/deployment/member/image.go b/pkg/deployment/member/image.go new file mode 100644 index 000000000..d8c8aed0c --- /dev/null +++ b/pkg/deployment/member/image.go @@ -0,0 +1,32 @@ +// +// DISCLAIMER +// +// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany +// +// 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. +// +// Copyright holder is ArangoDB GmbH, Cologne, Germany +// + +package member + +import ( + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" +) + +func GetImageLicense(image *api.ImageInfo) string { + if image.Enterprise { + return "enterprise" + } + return "community" +} diff --git a/pkg/deployment/member/state.go b/pkg/deployment/member/state.go index 55719b57b..af516ea10 100644 --- a/pkg/deployment/member/state.go +++ b/pkg/deployment/member/state.go @@ -78,7 +78,7 @@ func (s *stateInspector) Log(logger zerolog.Logger) { defer s.lock.Unlock() for m, s := range s.members { - if s.IsInvalid() { + if !s.IsReachable() { s.Log(logger.Info()).Str("member", m).Msgf("Member is in invalid state") } } @@ -95,22 +95,9 @@ func (s *stateInspector) RefreshState(ctx context.Context, members api.Deploymen members.ForEach(func(id int) { if members[id].Group.IsArangosync() { - return - } - - results[id] = State{} - - c, err := s.client.GetServerClient(nctx, members[id].Group, members[id].Member.ID) - if err != nil { - results[id].Reachable = err - return - } - - if v, err := c.Version(nctx); err != nil { - results[id].Reachable = err - return + results[id] = s.fetchArangosyncMemberState(nctx, members[id]) } else { - results[id].Version = v + results[id] = s.fetchServerMemberState(nctx, members[id]) } }) @@ -122,11 +109,11 @@ func (s *stateInspector) RefreshState(ctx context.Context, members api.Deploymen c, err := s.client.GetDatabaseClient(ctx) if err != nil { - cs.Reachable = err + cs.NotReachableErr = err } else { v, err := c.Version(gctx) if err != nil { - cs.Reachable = err + cs.NotReachableErr = err } else { cs.Version = v } @@ -155,6 +142,46 @@ func (s *stateInspector) RefreshState(ctx context.Context, members api.Deploymen s.health = h } +func (s *stateInspector) fetchArangosyncMemberState(ctx context.Context, m api.DeploymentStatusMemberElement) State { + var state State + c, err := s.client.GetSyncServerClient(ctx, m.Group, m.Member.ID) + if err != nil { + state.NotReachableErr = err + return state + } + + if v, err := c.Version(ctx); err != nil { + state.NotReachableErr = err + } else { + // convert arangosync VersionInfo to go-driver VersionInfo for simplicity: + state.Version = driver.VersionInfo{ + Server: m.Group.AsRole(), + Version: driver.Version(v.Version), + License: GetImageLicense(m.Member.Image), + Details: map[string]interface{}{ + "arangosync-build": v.Build, + }, + } + } + return state +} + +func (s *stateInspector) fetchServerMemberState(ctx context.Context, m api.DeploymentStatusMemberElement) State { + var state State + c, err := s.client.GetServerClient(ctx, m.Group, m.Member.ID) + if err != nil { + state.NotReachableErr = err + return state + } + + if v, err := c.Version(ctx); err != nil { + state.NotReachableErr = err + } else { + state.Version = v + } + return state +} + func (s *stateInspector) MemberState(id string) (State, bool) { s.lock.Lock() defer s.lock.Unlock() @@ -175,24 +202,15 @@ type Health struct { } type State struct { - Reachable error + NotReachableErr error Version driver.VersionInfo } func (s State) IsReachable() bool { - return s.Reachable == nil + return s.NotReachableErr == nil } func (s State) Log(event *zerolog.Event) *zerolog.Event { - if !s.IsReachable() { - event = event.Bool("reachable", false).AnErr("reachableError", s.Reachable) - } else { - event = event.Bool("reachable", false) - } - return event -} - -func (s State) IsInvalid() bool { - return !s.IsReachable() + return event.Bool("reachable", s.IsReachable()).AnErr("reachableError", s.NotReachableErr) } diff --git a/pkg/deployment/reconciler/context.go b/pkg/deployment/reconciler/context.go index 397b16500..2f31f1aed 100644 --- a/pkg/deployment/reconciler/context.go +++ b/pkg/deployment/reconciler/context.go @@ -23,6 +23,8 @@ package reconciler import ( "context" + core "k8s.io/api/core/v1" + "github.com/arangodb/arangosync-client/client" "github.com/arangodb/go-driver" "github.com/arangodb/go-driver/agency" @@ -38,7 +40,6 @@ import ( servicev1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/service/v1" serviceaccountv1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/serviceaccount/v1" servicemonitorv1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/servicemonitor/v1" - core "k8s.io/api/core/v1" ) // ServerGroupIterator provides a helper to callback on every server @@ -191,4 +192,5 @@ type DeploymentClient interface { DeploymentAgencyClient DeploymentDatabaseClient DeploymentMemberClient + DeploymentSyncClient } diff --git a/pkg/deployment/server_api.go b/pkg/deployment/server_api.go index 39a331bf7..b989441d9 100644 --- a/pkg/deployment/server_api.go +++ b/pkg/deployment/server_api.go @@ -29,6 +29,7 @@ import ( api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" "github.com/arangodb/kube-arangodb/pkg/apis/shared" + memberState "github.com/arangodb/kube-arangodb/pkg/deployment/member" "github.com/arangodb/kube-arangodb/pkg/server" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" ) @@ -219,11 +220,7 @@ func (d *Deployment) DatabaseURL() string { func (d *Deployment) DatabaseVersion() (string, string) { status, _ := d.GetStatus() if current := status.CurrentImage; current != nil { - license := "community" - if current.Enterprise { - license = "enterprise" - } - return string(current.ArangoDBVersion), license + return string(current.ArangoDBVersion), memberState.GetImageLicense(status.CurrentImage) } return "", "" }