Skip to content

Commit 45fefba

Browse files
authored
[Feature] ArangoDeployment Inspector (#972)
1 parent b7f7204 commit 45fefba

File tree

10 files changed

+80
-24
lines changed

10 files changed

+80
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A)
44
- (Feature) Add CoreV1 Endpoints Inspector
5+
- (Feature) Add Current ArangoDeployment Inspector
56

67
## [1.2.11](https://github.com/arangodb/kube-arangodb/tree/1.2.11) (2022-04-30)
78
- (Bugfix) Orphan PVC are not removed

pkg/deployment/deployment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func New(config Config, deps Dependencies, apiObject *api.ArangoDeployment) (*De
245245
eventCh: make(chan *deploymentEvent, deploymentEventQueueSize),
246246
stopCh: make(chan struct{}),
247247
agencyCache: agency.NewCache(apiObject.Spec.Mode),
248-
currentState: inspector.NewInspector(newDeploymentThrottle(), deps.Client, apiObject.GetNamespace()),
248+
currentState: inspector.NewInspector(newDeploymentThrottle(), deps.Client, apiObject.GetNamespace(), apiObject.GetName()),
249249
}
250250

251251
d.memberState = memberState.NewStateInspector(d)

pkg/deployment/deployment_inspector.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import (
4444
"github.com/arangodb/kube-arangodb/pkg/upgrade"
4545
"github.com/arangodb/kube-arangodb/pkg/util"
4646
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
47-
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
4847
)
4948

5049
var (
@@ -81,12 +80,7 @@ func (d *Deployment) inspectDeployment(lastInterval util.Interval) util.Interval
8180
}
8281

8382
// Check deployment still exists
84-
var updated *api.ArangoDeployment
85-
err = globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctxReconciliation, func(ctxChild context.Context) error {
86-
var err error
87-
updated, err = d.deps.Client.Arango().DatabaseV1().ArangoDeployments(d.GetNamespace()).Get(ctxChild, deploymentName, meta.GetOptions{})
88-
return err
89-
})
83+
updated, err := d.currentState.GetCurrentArangoDeployment()
9084
if k8sutil.IsNotFound(err) {
9185
// Deployment is gone
9286
log.Info().Msg("Deployment is gone")

pkg/deployment/deployment_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ func createTestDeployment(t *testing.T, config Config, arangoDeployment *api.Ara
487487
deps: deps,
488488
eventCh: make(chan *deploymentEvent, deploymentEventQueueSize),
489489
stopCh: make(chan struct{}),
490-
currentState: inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), deps.Client, arangoDeployment.GetNamespace()),
490+
currentState: inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), deps.Client, arangoDeployment.GetNamespace(), arangoDeployment.GetName()),
491491
}
492492
d.clientCache = client.NewClientCache(d, conn.NewFactory(d.getAuth, d.getConnConfig))
493493

pkg/deployment/resources/inspector/inspector.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ import (
2727
"time"
2828

2929
"github.com/arangodb/go-driver"
30+
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
3031
"github.com/arangodb/kube-arangodb/pkg/logging"
3132
"github.com/arangodb/kube-arangodb/pkg/util"
33+
"github.com/arangodb/kube-arangodb/pkg/util/errors"
3234
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector"
3335
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangoclustersynchronization"
3436
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangomember"
@@ -45,6 +47,7 @@ import (
4547
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
4648
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
4749
"github.com/rs/zerolog"
50+
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
4851
)
4952

5053
var (
@@ -99,26 +102,36 @@ type inspectorLoader interface {
99102

100103
var _ inspector.Inspector = &inspectorState{}
101104

102-
func NewInspector(throttles throttle.Components, client kclient.Client, namespace string) inspector.Inspector {
105+
func NewInspector(throttles throttle.Components, client kclient.Client, namespace, deploymentName string) inspector.Inspector {
103106
if throttles == nil {
104107
throttles = throttle.NewAlwaysThrottleComponents()
105108
}
106109

107110
i := &inspectorState{
108-
namespace: namespace,
109-
client: client,
110-
throttles: throttles,
111-
logger: logging.GlobalLogger().MustGetLogger(logging.LoggerNameInspector),
111+
namespace: namespace,
112+
deploymentName: deploymentName,
113+
client: client,
114+
throttles: throttles,
115+
logger: logging.GlobalLogger().MustGetLogger(logging.LoggerNameInspector),
112116
}
113117

114118
return i
115119
}
116120

121+
type inspectorStateDeploymentResult struct {
122+
depl *api.ArangoDeployment
123+
err error
124+
}
125+
117126
type inspectorState struct {
118127
lock sync.Mutex
119128

120-
namespace string
121-
client kclient.Client
129+
namespace string
130+
deploymentName string
131+
132+
deploymentResult *inspectorStateDeploymentResult
133+
134+
client kclient.Client
122135

123136
last time.Time
124137

@@ -144,6 +157,14 @@ type inspectorState struct {
144157
initialised bool
145158
}
146159

160+
func (i *inspectorState) GetCurrentArangoDeployment() (*api.ArangoDeployment, error) {
161+
if i.deploymentResult == nil {
162+
return nil, errors.Newf("Deployment not initialised")
163+
}
164+
165+
return i.deploymentResult.depl, i.deploymentResult.err
166+
}
167+
147168
func (i *inspectorState) Endpoints() endpoints.Definition {
148169
return i.endpoints
149170
}
@@ -248,6 +269,12 @@ func (i *inspectorState) refreshInThreads(ctx context.Context, threads int, load
248269
}
249270

250271
start := time.Now()
272+
i.logger.Debug().Msg("Pre-inspector refresh start")
273+
d, err := i.client.Arango().DatabaseV1().ArangoDeployments(i.namespace).Get(context.Background(), i.deploymentName, meta.GetOptions{})
274+
n.deploymentResult = &inspectorStateDeploymentResult{
275+
depl: d,
276+
err: err,
277+
}
251278

252279
i.logger.Debug().Msg("Inspector refresh start")
253280

@@ -298,6 +325,8 @@ func (i *inspectorState) refreshInThreads(ctx context.Context, threads int, load
298325
loaders[id].Copy(n, i, true)
299326
}
300327

328+
i.deploymentResult = n.deploymentResult
329+
301330
i.throttles = n.throttles
302331

303332
i.last = time.Now()
@@ -361,6 +390,7 @@ func (i *inspectorState) validate() error {
361390
func (i *inspectorState) copyCore() *inspectorState {
362391
return &inspectorState{
363392
namespace: i.namespace,
393+
deploymentName: i.deploymentName,
364394
client: i.client,
365395
pods: i.pods,
366396
secrets: i.secrets,
@@ -376,6 +406,7 @@ func (i *inspectorState) copyCore() *inspectorState {
376406
throttles: i.throttles.Copy(),
377407
versionInfo: i.versionInfo,
378408
endpoints: i.endpoints,
409+
deploymentResult: i.deploymentResult,
379410
logger: i.logger,
380411
}
381412
}

pkg/deployment/resources/inspector/inspector_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func Test_Inspector_RefreshMatrix(t *testing.T) {
135135

136136
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)
137137

138-
i := NewInspector(tc, c, "test")
138+
i := NewInspector(tc, c, "test", "test")
139139

140140
require.NoError(t, i.Refresh(context.Background()))
141141

@@ -285,7 +285,7 @@ func getTimes(i inspector.Inspector) map[string]time.Time {
285285
func Test_Inspector_Load(t *testing.T) {
286286
c := kclient.NewFakeClient()
287287

288-
i := NewInspector(throttle.NewAlwaysThrottleComponents(), c, "test")
288+
i := NewInspector(throttle.NewAlwaysThrottleComponents(), c, "test", "test")
289289

290290
require.NoError(t, i.Refresh(context.Background()))
291291
}
@@ -295,7 +295,7 @@ func Test_Inspector_Invalidate(t *testing.T) {
295295

296296
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)
297297

298-
i := NewInspector(tc, c, "test")
298+
i := NewInspector(tc, c, "test", "test")
299299

300300
require.NoError(t, i.Refresh(context.Background()))
301301

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package arangodeployment
22+
23+
import api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
24+
25+
type Inspector interface {
26+
GetCurrentArangoDeployment() (*api.ArangoDeployment, error)
27+
}

pkg/util/k8sutil/inspector/inspector.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/server"
2727

2828
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangoclustersynchronization"
29+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangodeployment"
2930
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangomember"
3031
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangotask"
3132
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/endpoints"
@@ -60,6 +61,8 @@ type Inspector interface {
6061
server.Inspector
6162
endpoints.Inspector
6263

64+
arangodeployment.Inspector
65+
6366
node.Inspector
6467
arangoclustersynchronization.Inspector
6568
arangotask.Inspector

pkg/util/kclient/helpers/secret_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func Test_SecretConfigGetter(t *testing.T) {
3636
t.Run("Missing secret", func(t *testing.T) {
3737
c := kclient.NewFakeClient()
3838

39-
i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, "default")
39+
i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, "default", "default")
4040
require.NoError(t, i.Refresh(context.Background()))
4141

4242
_, _, err := SecretConfigGetter(i, "secret", "key")()
@@ -56,7 +56,7 @@ func Test_SecretConfigGetter(t *testing.T) {
5656
_, err := c.Kubernetes().CoreV1().Secrets("default").Create(context.Background(), &s, meta.CreateOptions{})
5757
require.NoError(t, err)
5858

59-
i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, "default")
59+
i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, "default", "default")
6060
require.NoError(t, i.Refresh(context.Background()))
6161

6262
_, _, err = SecretConfigGetter(i, "secret", "key")()
@@ -81,7 +81,7 @@ random data
8181
_, err := c.Kubernetes().CoreV1().Secrets("default").Create(context.Background(), &s, meta.CreateOptions{})
8282
require.NoError(t, err)
8383

84-
i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, "default")
84+
i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, "default", "default")
8585
require.NoError(t, i.Refresh(context.Background()))
8686

8787
_, _, err = SecretConfigGetter(i, "secret", "key")()
@@ -123,7 +123,7 @@ users:
123123
_, err := c.Kubernetes().CoreV1().Secrets("default").Create(context.Background(), &s, meta.CreateOptions{})
124124
require.NoError(t, err)
125125

126-
i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, "default")
126+
i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, "default", "default")
127127
require.NoError(t, i.Refresh(context.Background()))
128128

129129
_, _, err = SecretConfigGetter(i, "secret", "key")()

pkg/util/tests/inspector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
const FakeNamespace = "fake"
3434

3535
func NewInspector(t *testing.T, c kclient.Client) inspectorInterface.Inspector {
36-
i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, FakeNamespace)
36+
i := inspector.NewInspector(throttle.NewAlwaysThrottleComponents(), c, FakeNamespace, FakeNamespace)
3737
require.NoError(t, i.Refresh(context.Background()))
3838

3939
return i

0 commit comments

Comments
 (0)