8000 [Feature] Endpoints inspector (#971) · sharekey/kube-arangodb@b7f7204 · GitHub
[go: up one dir, main page]

Skip to content

Commit b7f7204

Browse files
authored
[Feature] Endpoints inspector (arangodb#971)
1 parent a6986df commit b7f7204

File tree

11 files changed

+490
-5
lines changed

11 files changed

+490
-5
lines changed

CHANGELOG.md

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

33
## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A)
4+
- (Feature) Add CoreV1 Endpoints Inspector
45

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

pkg/deployment/deployment.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ func newDeploymentThrottle() throttle.Components {
226226
10*time.Second, // Secret
227227
10*time.Second, // Service
228228
30*time.Second, // SA
229-
30*time.Second) // ServiceMonitor
229+
30*time.Second, // ServiceMonitor
230+
15*time.Second) // Endpoints
230231
}
231232

232233
// New creates a new Deployment from the given API object.
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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 inspector
22+
23+
import (
24+
"context"
25+
"time"
26+
27+
"github.com/arangodb/kube-arangodb/pkg/util/errors"
28+
"github.com/arangodb/kube-arangodb/pkg/util/globals"
29+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
30+
core "k8s.io/api/core/v1"
31+
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
32+
)
33+
34+
func init() {
35+
requireRegisterInspectorLoader(endpointsInspectorLoaderObj)
36+
}
37+
38+
var endpointsInspectorLoaderObj = endpointsInspectorLoader{}
39+
40+
type endpointsInspectorLoader struct {
41+
}
42+
43+
func (p endpointsInspectorLoader) Component() throttle.Component {
44+
return throttle.Endpoints
45+
}
46+
47+
func (p endpointsInspectorLoader) Load(ctx context.Context, i *inspectorState) {
48+
var q endpointsInspector
49+
p.loadV1(ctx, i, &q)
50+
i.endpoints = &q
51+
q.state = i
52+
q.last = time.Now()
53+
}
54+
55+
func (p endpointsInspectorLoader) loadV1(ctx context.Context, i *inspectorState, q *endpointsInspector) {
56+
var z endpointsInspectorV1
57+
58+
z.endpointsInspector = q
59+
60+
z.endpoints, z.err = p.getV1Endpoints(ctx, i)
61+
62+
q.v1 = &z
63+
}
64+
65+
func (p endpointsInspectorLoader) getV1Endpoints(ctx context.Context, i *inspectorState) (map[string]*core.Endpoints, error) {
66+
objs, err := p.getV1EndpointsList(ctx, i)
67+
if err != nil {
68+
return nil, err
69+
}
70+
71+
r := make(map[string]*core.Endpoints, len(objs))
72+
73+
for id := range objs {
74+
r[objs[id].GetName()] = objs[id]
75+
}
76+
77+
return r, nil
78+
}
79+
80+
func (p endpointsInspectorLoader) getV1EndpointsList(ctx context.Context, i *inspectorState) ([]*core.Endpoints, error) {
81+
ctxChild, cancel := globals.GetGlobalTimeouts().Kubernetes().WithTimeout(ctx)
82+
defer cancel()
83+
obj, err := i.client.Kubernetes().CoreV1().Endpoints(i.namespace).List(ctxChild, meta.ListOptions{
84+
Limit: globals.GetGlobals().Kubernetes().RequestBatchSize().Get(),
85+
})
86+
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
items := obj.Items
92+
cont := obj.Continue
93+
var s = int64(len(items))
94+
95+
if z := obj.RemainingItemCount; z != nil {
96+
s += *z
97+
}
98+
99+
ptrs := make([]*core.Endpoints, 0, s)
100+
101+
for {
102+
for id := range items {
103+
ptrs = append(ptrs, &items[id])
104+
}
105+
106+
if cont == "" {
107+
break
108+
}
109+
110+
items, cont, err = p.getV1EndpointsListRequest(ctx, i, cont)
111+
112+
if err != nil {
113+
return nil, err
114+
}
115+
}
116+
117+
return ptrs, nil
118+
}
119+
120+
func (p endpointsInspectorLoader) getV1EndpointsListRequest(ctx context.Context, i *inspectorState, cont string) ([]core.Endpoints, string, error) {
121+
ctxChild, cancel := globals.GetGlobalTimeouts().Kubernetes().WithTimeout(ctx)
122+
defer cancel()
123+
obj, err := i.client.Kubernetes().CoreV1().Endpoints(i.namespace).List(ctxChild, meta.ListOptions{
124+
Limit: globals.GetGlobals().Kubernetes().RequestBatchSize().Get(),
125+
Continue: cont,
126+
})
127+
128+
if err != nil {
129+
return nil, "", err
130+
}
131+
132+
return obj.Items, obj.Continue, err
133+
}
134+
135+
func (p endpointsInspectorLoader) Verify(i *inspectorState) error {
136+
return nil
137+
}
138+
139+
func (p endpointsInspectorLoader) Copy(from, to *inspectorState, override bool) {
140+
if to.endpoints != nil {
141+
if !override {
142+
return
143+
}
144+
}
145+
146+
to.endpoints = from.endpoints
147+
to.endpoints.state = to
148+
}
149+
150+
func (p endpointsInspectorLoader) Name() string {
151+
return "endpoints"
152+
}
153+
154+
type endpointsInspector struct {
155+
state *inspectorState
156+
157+
last time.Time
158+
159+
v1 *endpointsInspectorV1
160+
}
161+
162+
func (p *endpointsInspector) LastRefresh() time.Time {
163+ return p.last
164+
}
165+
166+
func (p *endpointsInspector) Refresh(ctx context.Context) error {
167+
p.Throttle(p.state.throttles).Invalidate()
168+
return p.state.refresh(ctx, endpointsInspectorLoaderObj)
169+
}
170+
171+
func (p endpointsInspector) Throttle(c throttle.Components) throttle.Throttle {
172+
return c.Endpoints()
173+
}
174+
175+
func (p *endpointsInspector) validate() error {
176+
if p == nil {
177+
return errors.Newf("EndpointsInspector is nil")
178+
}
179+
180+
if p.state == nil {
181+
return errors.Newf("Parent is nil")
182+
}
183+
184+
return p.v1.validate()
185+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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 inspector
22+
23+
import (
24+
"context"
25+
26+
"github.com/arangodb/kube-arangodb/pkg/util/errors"
27+
ins "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/endpoints/v1"
28+
core "k8s.io/api/core/v1"
29+
apiErrors "k8s.io/apimachinery/pkg/api/errors"
30+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31+
"k8s.io/apimachinery/pkg/runtime/schema"
32+
)
33+
34+
func (p *endpointsInspector) V1() (ins.Inspector, error) {
35+
if p.v1.err != nil {
36+
return nil, p.v1.err
37+
}
38+
39+
return p.v1, nil
40+
}
41+
42+
type endpointsInspectorV1 struct {
43+
endpointsInspector *endpointsInspector
44+
45+
endpoints map[string]*core.Endpoints
46+
err error
47+
}
48+
49+
func (p *endpointsInspectorV1) Filter(filters ...ins.Filter) []*core.Endpoints {
50+
z := p.ListSimple()
51+
52+
r := make([]*core.Endpoints, 0, len(z))
53+
54+
for _, o := range z {
55+
if !ins.FilterObject(o, filters...) {
56+
continue
57+
}
58+
59+
r = append(r, o)
60+
}
61+
62+
return r
63+
}
64+
65+
func (p *endpointsInspectorV1) validate() error {
66+
if p == nil {
67+
return errors.Newf("EndpointsV1Inspector is nil")
68+
}
69+
70+
if p.endpointsInspector == nil {
71+
return errors.Newf("Parent is nil")
72+
}
73+
74+
if p.endpoints == nil && p.err == nil {
75+
return errors.Newf("Endpoints or err should be not nil")
76+
}
77+
78+
if p.endpoints != nil && p.err != nil {
79+
return errors.Newf("Endpoints or err cannot be not nil together")
80+
}
81+
82+
return nil
83+
}
84+
85+
func (p *endpointsInspectorV1) ListSimple() []*core.Endpoints {
86+
var r []*core.Endpoints
87+
for _, endpoints := range p.endpoints {
88+
r = append(r, endpoints)
89+
}
90+
91+
return r
92+
}
93+
94+
func (p *endpointsInspectorV1) GetSimple(name string) (*core.Endpoints, bool) {
95+
endpoints, ok := p.endpoints[name]
96+
if !ok {
97+
return nil, false
98+
}
99+
100+
return endpoints, true
101+
}
102+
103+
func (p *endpointsInspectorV1) Iterate(action ins.Action, filters ...ins.Filter) error {
104+
for _, endpoints := range p.endpoints {
105+
if err := p.iterateEndpoints(endpoints, action, filters...); err != nil {
106+
return err
107+
}
108+
}
109+
110+
return nil
111+
}
112+
113+
func (p *endpointsInspectorV1) iterateEndpoints(endpoints *core.Endpoints, action ins.Action, filters ...ins.Filter) error {
114+
for _, f := range filters {
115+
if f == nil {
116+
continue
117+
}
118+
119+
if !f(endpoints) {
120+
return nil
121+
}
122+
}
123+
124+
return action(endpoints)
125+
}
126+
127+
func (p *endpointsInspectorV1) Read() ins.ReadInterface {
128+
return p
129+
}
130+
131+
func (p *endpointsInspectorV1) Get(ctx context.Context, name string, opts metav1.GetOptions) (*core.Endpoints, error) {
132+
if s, ok := p.GetSimple(name); !ok {
133+
return nil, apiErrors.NewNotFound(schema.GroupResource{
134+
Group: core.GroupName,
135+
Resource: "endpoints",
136+
}, name)
137+
} else {
138+
return s, nil
139+
}
140+
}

pkg/deployment/resources/inspector/inspector.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangoclustersynchronization"
3434
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangomember"
3535
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangotask"
36+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/endpoints"
3637
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/node"
3738
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/persistentvolumeclaim"
3839
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/pod"
@@ -134,6 +135,7 @@ type inspectorState struct {
134135
arangoMembers *arangoMembersInspector
135136
arangoTasks *arangoTasksInspector
136137
arangoClusterSynchronizations *arangoClusterSynchronizationsInspector
138+
endpoints *endpointsInspector
137139

138140
throttles throttle.Components
139141

@@ -142,6 +144,10 @@ type inspectorState struct {
142144
initialised bool
143145
}
144146

147+
func (i *inspectorState) Endpoints() endpoints.Definition {
148+
return i.endpoints
149+
}
150+
145151
func (i *inspectorState) Initialised() bool {
146152
if i == nil {
147153
return false
@@ -345,6 +351,10 @@ func (i *inspectorState) validate() error {
345351
return err
346352
}
347353

354+
if err := i.endpoints.validate(); err != nil {
355+
return err
356+
}
357+
348358
return nil
349359
}
350360

@@ -365,6 +375,7 @@ func (i *inspectorState) copyCore() *inspectorState {
365375
arangoClusterSynchronizations: i.arangoClusterSynchronizations,
366376
throttles: i.throttles.Copy(),
367377
versionInfo: i.versionInfo,
378+
endpoints: i.endpoints,
368379
logger: i.logger,
369380
}
370381
}

0 commit comments

Comments
 (0)
0