Skip to content

Commit 7a80635

Browse files
authored
[Feature] PersistentVolume Inspector (#1309)
1 parent 2da8a75 commit 7a80635

File tree

16 files changed

+634
-10
lines changed

16 files changed

+634
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A)
44
- (Feature) ArangoBackup create retries and MaxIterations limit
55
- (Feature) Add Reason in OOM Metric
6+
- (Feature) PersistentVolume Inspector
67

78
## [1.2.27](https://github.com/arangodb/kube-arangodb/tree/1.2.27) (2023-04-27)
89
- (Feature) Add InSync Cache

pkg/deployment/resources/inspector/inspector.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/definitions"
4747
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/endpoints"
4848
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/node"
49+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/persistentvolume"
4950
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/persistentvolumeclaim"
5051
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/pod"
5152
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/poddisruptionbudget"
@@ -128,6 +129,7 @@ type inspectorState struct {
128129
services *servicesInspector
129130
serviceAccounts *serviceAccountsInspector
130131
nodes *nodesInspector
132+
persistentVolumes *persistentVolumesInspector
131133
podDisruptionBudgets *podDisruptionBudgetsInspector
132134
serviceMonitors *serviceMonitorsInspector
133135
arangoMembers *arangoMembersInspector
@@ -218,6 +220,7 @@ func (i *inspectorState) AnonymousObjects() []anonymous.Impl {
218220
i.services,
219221
i.serviceAccounts,
220222
i.nodes,
223+
i.persistentVolumes,
221224
i.podDisruptionBudgets,
222225
i.serviceMonitors,
223226
i.arangoMembers,
@@ -302,6 +305,10 @@ func (i *inspectorState) Node() node.Definition {
302305
return i.nodes
303306
}
304307

308+
func (i *inspectorState) PersistentVolume() persistentvolume.Definition {
309+
return i.persistentVolumes
310+
}
311+
305312
func (i *inspectorState) ArangoClusterSynchronization() arangoclustersynchronization.Definition {
306313
return i.arangoClusterSynchronizations
307314
}
@@ -441,6 +448,10 @@ func (i *inspectorState) validate() error {
441448
return err
442449
}
443450

451+
if err := i.persistentVolumes.validate(); err != nil {
452+
return err
453+
}
454+
444455
if err := i.podDisruptionBudgets.validate(); err != nil {
445456
return err
446457
}
@@ -479,6 +490,7 @@ func (i *inspectorState) copyCore() *inspectorState {
479490
services: i.services,
480491
serviceAccounts: i.serviceAccounts,
481492
nodes: i.nodes,
493+
persistentVolumes: i.persistentVolumes,
482494
podDisruptionBudgets: i.podDisruptionBudgets,
483495
serviceMonitors: i.serviceMonitors,
484496
arangoMembers: i.arangoMembers,

pkg/deployment/resources/inspector/inspector_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -71,6 +71,14 @@ var loaderTestDefinitions = map[string]loaderTestDefinition{
7171
return i.Node()
7272
},
7373
},
74+
"PersistentVolume": {
75+
tg: func(t throttle.Components) throttle.Throttle {
76+
return t.PersistentVolume()
77+
},
78+
get: func(i inspector.Inspector) refresh.Inspector {
79+
return i.PersistentVolume()
80+
},
81+
},
7482
"Pod": {
7583
tg: func(t throttle.Components) throttle.Throttle {
7684
return t.Pod()
@@ -134,7 +142,7 @@ func getAllTypes() []string {
134142
func Test_Inspector_RefreshMatrix(t *testing.T) {
135143
c := kclient.NewFakeClient()
136144

137-
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)
145+
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, time.Hour)
138146

139147
i := NewInspector(tc, c, "test", "test")
140148

@@ -294,7 +302,7 @@ func Test_Inspector_Load(t *testing.T) {
294302
func Test_Inspector_Invalidate(t *testing.T) {
295303
c := kclient.NewFakeClient()
296304

297-
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)
305+
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, time.Hour)
298306

299307
i := NewInspector(tc, c, "test", "test")
300308

pkg/deployment/resources/inspector/pdbs_version_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func Test_PDB_Versions(t *testing.T) {
5252
GitVersion: v,
5353
})
5454

55-
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)
55+
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, time.Hour)
5656

5757
i := NewInspector(tc, c, "test", "test")
5858
require.NoError(t, i.Refresh(context.Background()))
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2023 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 inspector
22+
23+
import (
24+
"context"
25+
"time"
26+
27+
core "k8s.io/api/core/v1"
28+
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
30+
"github.com/arangodb/kube-arangodb/pkg/util/errors"
31+
"github.com/arangodb/kube-arangodb/pkg/util/globals"
32+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/definitions"
33+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
34+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
35+
)
36+
37+
func init() {
38+
requireRegisterInspectorLoader(persistentVolumesInspectorLoaderObj)
39+
}
40+
41+
var persistentVolumesInspectorLoaderObj = persistentVolumesInspectorLoader{}
42+
43+
type persistentVolumesInspectorLoader struct {
44+
}
45+
46+
func (p persistentVolumesInspectorLoader) Component() definitions.Component {
47+
return definitions.PersistentVolume
48+
}
49+
50+
func (p persistentVolumesInspectorLoader) Load(ctx context.Context, i *inspectorState) {
51+
var q persistentVolumesInspector
52+
p.loadV1(ctx, i, &q)
53+
i.persistentVolumes = &q
54+
q.state = i
55+
q.last = time.Now()
56+
}
57+
58+
func (p persistentVolumesInspectorLoader) loadV1(ctx context.Context, i *inspectorState, q *persistentVolumesInspector) {
59+
var z persistentVolumesInspectorV1
60+
61+
z.persistentVolumeInspector = q
62+
63+
z.persistentVolumes, z.err = p.getV1PersistentVolumes(ctx, i)
64+
65+
q.v1 = &z
66+
}
67+
68+
func (p persistentVolumesInspectorLoader) getV1PersistentVolumes(ctx context.Context, i *inspectorState) (map[string]*core.PersistentVolume, error) {
69+
objs, err := p.getV1PersistentVolumesList(ctx, i)
70+
if err != nil {
71+
return nil, err
72+
}
73+
74+
r := make(map[string]*core.PersistentVolume, len(objs))
75+
76+
for id := range objs {
77+
r[objs[id].GetName()] = objs[id]
78+
}
79+
80+
return r, nil
81+
}
82+
83+
func (p persistentVolumesInspectorLoader) getV1PersistentVolumesList(ctx context.Context, i *inspectorState) ([]*core.PersistentVolume, error) {
84+
ctxChild, cancel := globals.GetGlobalTimeouts().Kubernetes().WithTimeout(ctx)
85+
defer cancel()
86+
obj, err := i.client.Kubernetes().CoreV1().PersistentVolumes().List(ctxChild, meta.ListOptions{
87+
Limit: globals.GetGlobals().Kubernetes().RequestBatchSize().Get(),
88+
})
89+
90+
if err != nil {
91+
return nil, err
92+
}
93+
94+
items := obj.Items
95+
cont := obj.Continue
96+
var s = int64(len(items))
97+
98+
if z := obj.RemainingItemCount; z != nil {
99+
s += *z
100+
}
101+
102+
ptrs := make([]*core.PersistentVolume, 0, s)
103+
104+
for {
105+
for id := range items {
106+
ptrs = append(ptrs, &items[id])
107+
}
108+
109+
if cont == "" {
110+
break
111+
}
112+
113+
items, cont, err = p.getV1PersistentVolumesListRequest(ctx, i, cont)
114+
115+
if err != nil {
116+
return nil, err
117+
}
118+
}
119+
120+
return ptrs, nil
121+
}
122+
123+
func (p persistentVolumesInspectorLoader) getV1PersistentVolumesListRequest(ctx context.Context, i *inspectorState, cont string) ([]core.PersistentVolume, string, error) {
124+
ctxChild, cancel := globals.GetGlobalTimeouts().Kubernetes().WithTimeout(ctx)
125+
defer cancel()
126+
obj, err := i.client.Kubernetes().CoreV1().PersistentVolumes().List(ctxChild, meta.ListOptions{
127+
Limit: globals.GetGlobals().Kubernetes().RequestBatchSize().Get(),
128+
Continue: cont,
129+
})
130+
131+
if err != nil {
132+
return nil, "", err
133+
}
134+
135+
return obj.Items, obj.Continue, err
136+
}
137+
138+
func (p persistentVolumesInspectorLoader) Verify(i *inspectorState) error {
139+
return nil
140+
}
141+
142+
func (p persistentVolumesInspectorLoader) Copy(from, to *inspectorState, override bool) {
143+
if to.persistentVolumes != nil {
144+
if !override {
145+
return
146+
}
147+
}
148+
149+
to.persistentVolumes = from.persistentVolumes
150+
to.persistentVolumes.state = to
151+
}
152+
153+
func (p persistentVolumesInspectorLoader) Name() string {
154+
return "persistentVolumes"
155+
}
156+
157+
type persistentVolumesInspector struct {
158+
state *inspectorState
159+
160+
last time.Time
161+
162+
v1 *persistentVolumesInspectorV1
163+
}
164+
165+
func (p *persistentVolumesInspector) LastRefresh() time.Time {
166+
return p.last
167+
}
168+
169+
func (p *persistentVolumesInspector) Refresh(ctx context.Context) error {
170+
p.Throttle(p.state.throttles).Invalidate()
171+
return p.state.refresh(ctx, persistentVolumesInspectorLoaderObj)
172+
}
173+
174+
func (p *persistentVolumesInspector) Version() version.Version {
175+
return version.V1
176+
}
177+
178+
func (p *persistentVolumesInspector) Throttle(c throttle.Components) throttle.Throttle {
179+
return c.PersistentVolume()
180+
}
181+
182+
func (p *persistentVolumesInspector) validate() error {
183+
if p == nil {
184+
return errors.Newf("PersistentVolumeInspector is nil")
185+
}
186+
187+
if p.state == nil {
188+
return errors.Newf("Parent is nil")
189+
}
190+
191+
return p.v1.validate()
192+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2023 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 inspector
22+
23+
import (
24+
core "k8s.io/api/core/v1"
25+
"k8s.io/apimachinery/pkg/runtime/schema"
26+
27+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/anonymous"
28+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/constants"
29+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/generic"
30+
)
31+
32+
func (p *persistentVolumesInspector) Anonymous(gvk schema.GroupVersionKind) (anonymous.Interface, bool) {
33+
g := constants.PersistentVolumeGKv1()
34+
35+
if g.Kind == gvk.Kind && g.Group == gvk.Group {
36+
switch gvk.Version {
37+
case constants.PersistentVolumeVersionV1, DefaultVersion:
38+
if p.v1 == nil || p.v1.err != nil {
39+
return nil, false
40+
}
41+
return anonymous.NewAnonymous[*core.PersistentVolume](g, p.state.persistentVolumes.v1, generic.WithModStatus[*core.PersistentVolume](g, generic.WithEmptyMod[*core.PersistentVolume](g))), true
42+
}
43+
}
44+
45+
return nil, false
46+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2023 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 inspector
22+
23+
import (
24+
"k8s.io/apimachinery/pkg/runtime/schema"
25+
26+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/constants"
27+
)
28+
29+
func (p *persistentVolumesInspectorV1) GroupVersionKind() schema.GroupVersionKind {
30+
return constants.PersistentVolumeGKv1()
31+
}
32+
33+
func (p *persistentVolumesInspectorV1) GroupVersionResource() schema.GroupVersionResource {
34+
return constants.PersistentVolumeGRv1()
35+
}
36+
37+
func (p *persistentVolumesInspector) GroupKind() schema.GroupKind {
38+
return constants.PersistentVolumeGK()
39+
}
40+
41+
func (p *persistentVolumesInspector) GroupResource() schema.GroupResource {
42+
return constants.PersistentVolumeGR()
43+
}

0 commit comments

Comments
 (0)