diff --git a/CHANGELOG.md b/CHANGELOG.md index bb6c097a3..a3befe651 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A) - (Feature) Add CoreV1 Endpoints Inspector +- (Feature) Add Current ArangoDeployment Inspector ## [1.2.11](https://github.com/arangodb/kube-arangodb/tree/1.2.11) (2022-04-30) - (Bugfix) Orphan PVC are not removed diff --git a/pkg/deployment/deployment.go b/pkg/deployment/deployment.go index 78709b732..3348709d5 100644 --- a/pkg/deployment/deployment.go +++ b/pkg/deployment/deployment.go @@ -245,7 +245,7 @@ func New(config Config, deps Dependencies, apiObject *api.ArangoDeployment) (*De eventCh: make(chan *deploymentEvent, deploymentEventQueueSize), stopCh: make(chan struct{}), agencyCache: agency.NewCache(apiObject.Spec.Mode), - currentState: inspector.NewInspector(newDeploymentThrottle(), deps.Client, apiObject.GetNamespace()), + currentState: inspector.NewInspector(newDeploymentThrottle(), deps.Client, apiObject.GetNamespace(), apiObject.GetName()), } d.memberState = memberState.NewStateInspector(d) diff --git a/pkg/deployment/deployment_inspector.go b/pkg/deployment/deployment_inspector.go index 90d7a629f..8ad5f0a93 100644 --- a/pkg/deployment/deployment_inspector.go +++ b/pkg/deployment/deployment_inspector.go @@ -44,7 +44,6 @@ import ( "github.com/arangodb/kube-arangodb/pkg/upgrade" "github.com/arangodb/kube-arangodb/pkg/util" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" - meta "k8s.io/apimachinery/pkg/apis/meta/v1" ) var ( @@ -81,12 +80,7 @@ func (d *Deployment) inspectDeployment(lastInterval util.Interval) util.Interval } // Check deployment still exists - var updated *api.ArangoDeployment - err = globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctxReconciliation, func(ctxChild context.Context) error { - var err error - updated, err = d.deps.Client.Arango().DatabaseV1().ArangoDeployments(d.GetNamespace()).Get(ctxChild, deploymentName, meta.GetOptions{}) - return err - }) + updated, err := d.currentState.GetCurrentArangoDeployment() if k8sutil.IsNotFound(err) { // Deployment is gone log.Info().Msg("Deployment is gone") diff --git a/pkg/deployment/deployment_suite_test.go b/pkg/deployment/deployment_suite_test.go index 43b71a501..b1fd36a9b 100644 --- a/pkg/deployment/deployment_suite_test.go +++ b/pkg/deployment/deployment_suite_test.go @@ -487,7 +487,7 @@ func createTestDeployment(t *testing.T, config Config, arangoDeployment *api.Ara deps: deps, eventCh: make(chan *deploymentEvent, deploymentEventQueueSize), stopCh: make(chan struct{}), - currentState: inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), deps.Client, arangoDeployment.GetNamespace()), + currentState: inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), deps.Client, arangoDeployment.GetNamespace(), arangoDeployment.GetName()), } d.clientCache = client.NewClientCache(d, conn.NewFactory(d.getAuth, d.getConnConfig)) diff --git a/pkg/deployment/resources/inspector/inspector.go b/pkg/deployment/resources/inspector/inspector.go index 1c36b9ab5..b4cf0acf3 100644 --- a/pkg/deployment/resources/inspector/inspector.go +++ b/pkg/deployment/resources/inspector/inspector.go @@ -27,8 +27,10 @@ import ( "time" "github.com/arangodb/go-driver" + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" "github.com/arangodb/kube-arangodb/pkg/logging" "github.com/arangodb/kube-arangodb/pkg/util" + "github.com/arangodb/kube-arangodb/pkg/util/errors" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangoclustersynchronization" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangomember" @@ -45,6 +47,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle" "github.com/arangodb/kube-arangodb/pkg/util/kclient" "github.com/rs/zerolog" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" ) var ( @@ -99,26 +102,36 @@ type inspectorLoader interface { var _ inspector.Inspector = &inspectorState{} -func NewInspector(throttles throttle.Components, client kclient.Client, namespace string) inspector.Inspector { +func NewInspector(throttles throttle.Components, client kclient.Client, namespace, deploymentName string) inspector.Inspector { if throttles == nil { throttles = throttle.NewAlwaysThrottleComponents() } i := &inspectorState{ - namespace: namespace, - client: client, - throttles: throttles, - logger: logging.GlobalLogger().MustGetLogger(logging.LoggerNameInspector), + namespace: namespace, + deploymentName: deploymentName, + client: client, + throttles: throttles, + logger: logging.GlobalLogger().MustGetLogger(logging.LoggerNameInspector), } return i } +type inspectorStateDeploymentResult struct { + depl *api.ArangoDeployment + err error +} + type inspectorState struct { lock sync.Mutex - namespace string - client kclient.Client + namespace string + deploymentName string + + deploymentResult *inspectorStateDeploymentResult + + client kclient.Client last time.Time @@ -144,6 +157,14 @@ type inspectorState struct { initialised bool } +func (i *inspectorState) GetCurrentArangoDeployment() (*api.ArangoDeployment, error) { + if i.deploymentResult == nil { + return nil, errors.Newf("Deployment not initialised") + } + + return i.deploymentResult.depl, i.deploymentResult.err +} + func (i *inspectorState) Endpoints() endpoints.Definition { return i.endpoints } @@ -248,6 +269,12 @@ func (i *inspectorState) refreshInThreads(ctx context.Context, threads int, load } start := time.Now() + i.logger.Debug().Msg("Pre-inspector refresh start") + d, err := i.client.Arango().DatabaseV1().ArangoDeployments(i.namespace).Get(context.Background(), i.deploymentName, meta.GetOptions{}) + n.deploymentResult = &inspectorStateDeploymentResult{ + depl: d, + err: err, + } i.logger.Debug().Msg("Inspector refresh start") @@ -298,6 +325,8 @@ func (i *inspectorState) refreshInThreads(ctx context.Context, threads int, load loaders[id].Copy(n, i, true) } + i.deploymentResult = n.deploymentResult + i.throttles = n.throttles i.last = time.Now() @@ -361,6 +390,7 @@ func (i *inspectorState) validate() error { func (i *inspectorState) copyCore() *inspectorState { return &inspectorState{ namespace: i.namespace, + deploymentName: i.deploymentName, client: i.client, pods: i.pods, secrets: i.secrets, @@ -376,6 +406,7 @@ func (i *inspectorState) copyCore() *inspectorState { throttles: i.throttles.Copy(), versionInfo: i.versionInfo, endpoints: i.endpoints, + deploymentResult: i.deploymentResult, logger: i.logger, } } diff --git a/pkg/deployment/resources/inspector/inspector_test.go b/pkg/deployment/resources/inspector/inspector_test.go index 14ad38d78..f14384161 100644 --- a/pkg/deployment/resources/inspector/inspector_test.go +++ b/pkg/deployment/resources/inspector/inspector_test.go @@ -135,7 +135,7 @@ func Test_Inspector_RefreshMatrix(t *testing.T) { tc := throttle.NewThrottleComponents(time.Hour, time.Hour, time.Hour, time.Hour, time.Hour, time.Hour, time.Hour, time.Hour, time.Hour, time.Hour, time.Hour, time.Hour) - i := NewInspector(tc, c, "test") + i := NewInspector(tc, c, "test", "test") require.NoError(t, i.Refresh(context.Background())) @@ -285,7 +285,7 @@ func getTimes(i inspector.Inspector) map[string]time.Time { func Test_Inspector_Load(t *testing.T) { c := kclient.NewFakeClient() - i := NewInspector(throttle.NewAlwaysThrottleComponents(), c, "test") + i := NewInspector(throttle.NewAlwaysThrottleComponents(), c, "test", "test") require.NoError(t, i.Refresh(context.Background())) } @@ -295,7 +295,7 @@ func Test_Inspector_Invalidate(t *testing.T) { tc := throttle.NewThrottleComponents(time.Hour, time.Hour, time.Hour, time.Hour, time.Hour, time.Hour, time.Hour, time.Hour, time.Hour, time.Hour, time.Hour, time.Hour) - i := NewInspector(tc, c, "test") + i := NewInspector(tc, c, "test", "test") require.NoError(t, i.Refresh(context.Background())) diff --git a/pkg/util/k8sutil/inspector/arangodeployment/arangodeployment.go b/pkg/util/k8sutil/inspector/arangodeployment/arangodeployment.go new file mode 100644 index 000000000..a01556930 --- /dev/null +++ b/pkg/util/k8sutil/inspector/arangodeployment/arangodeployment.go @@ -0,0 +1,27 @@ +// +// 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 arangodeployment + +import api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" + +type Inspector interface { + GetCurrentArangoDeployment() (*api.ArangoDeployment, error) +} diff --git a/pkg/util/k8sutil/inspector/inspector.go b/pkg/util/k8sutil/inspector/inspector.go index 65ede96e9..ea464cd08 100644 --- a/pkg/util/k8sutil/inspector/inspector.go +++ b/pkg/util/k8sutil/inspector/inspector.go @@ -26,6 +26,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/server" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangoclustersynchronization" + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangodeployment" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangomember" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangotask" "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/endpoints" @@ -60,6 +61,8 @@ type Inspector interface { server.Inspector endpoints.Inspector + arangodeployment.Inspector + node.Inspector arangoclustersynchronization.Inspector arangotask.Inspector diff --git a/pkg/util/kclient/helpers/secret_test.go b/pkg/util/kclient/helpers/secret_test.go index 05c4a86ca..45acaa42c 100644 --- a/pkg/util/kclient/helpers/secret_test.go +++ b/pkg/util/kclient/helpers/secret_test.go @@ -36,7 +36,7 @@ func Test_SecretConfigGetter(t *testing.T) { t.Run("Missing secret", func(t *testing.T) { c := kclient.NewFakeClient() - i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, "default") + i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, "default", "default") require.NoError(t, i.Refresh(context.Background())) _, _, err := SecretConfigGetter(i, "secret", "key")() @@ -56,7 +56,7 @@ func Test_SecretConfigGetter(t *testing.T) { _, err := c.Kubernetes().CoreV1().Secrets("default").Create(context.Background(), &s, meta.CreateOptions{}) require.NoError(t, err) - i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, "default") + i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, "default", "default") require.NoError(t, i.Refresh(context.Background())) _, _, err = SecretConfigGetter(i, "secret", "key")() @@ -81,7 +81,7 @@ random data _, err := c.Kubernetes().CoreV1().Secrets("default").Create(context.Background(), &s, meta.CreateOptions{}) require.NoError(t, err) - i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, "default") + i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, "default", "default") require.NoError(t, i.Refresh(context.Background())) _, _, err = SecretConfigGetter(i, "secret", "key")() @@ -123,7 +123,7 @@ users: _, err := c.Kubernetes().CoreV1().Secrets("default").Create(context.Background(), &s, meta.CreateOptions{}) require.NoError(t, err) - i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, "default") + i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, "default", "default") require.NoError(t, i.Refresh(context.Background())) _, _, err = SecretConfigGetter(i, "secret", "key")() diff --git a/pkg/util/tests/inspector.go b/pkg/util/tests/inspector.go index 61352daf0..a096799b6 100644 --- a/pkg/util/tests/inspector.go +++ b/pkg/util/tests/inspector.go @@ -33,7 +33,7 @@ import ( const FakeNamespace = "fake" func NewInspector(t *testing.T, c kclient.Client) inspectorInterface.Inspector { - i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, FakeNamespace) + i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, FakeNamespace, FakeNamespace) require.NoError(t, i.Refresh(context.Background())) return i