10000 [Feature] Recursive OwnerReference Discovery (#974) · sharekey/kube-arangodb@db52b3e · GitHub
[go: up one dir, main page]

Skip to content

Commit db52b3e

Browse files
authored
[Feature] Recursive OwnerReference Discovery (arangodb#974)
1 parent df37ed1 commit db52b3e

21 files changed

+703
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- (Feature) Add CoreV1 Endpoints Inspector
55
- (Feature) Add Current ArangoDeployment Inspector
66
- (Refactor) Anonymous inspector functions
7+
- (Feature) Recursive OwnerReference discovery
78

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

pkg/deployment/resources/inspector/acs_anonymous.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ func (p *arangoClusterSynchronizationsInspector) Anonymous(gvk schema.GroupVersi
3030

3131
if g.Kind == gvk.Kind && g.Group == gvk.Group {
3232
switch gvk.Version {
33-
case ArangoClusterSynchronizationVersionV1:
33+
case ArangoClusterSynchronizationVersionV1, DefaultVersion:
34+
if p.v1 == nil || p.v1.err != nil {
35+
return nil, false
36+
}
3437
return &arangoClusterSynchronizationsInspectorAnonymousV1{i: p.v1}, true
3538
}
3639
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
"github.com/arangodb/kube-arangodb/pkg/apis/deployment"
25+
deploymentv1 "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
26+
"k8s.io/apimachinery/pkg/runtime/schema"
27+
)
28+
29+
// ArangoDeployment
30+
const (
31+
ArangoDeploymentGroup = deployment.ArangoDeploymentGroupName
32+
ArangoDeploymentResource = deployment.ArangoDeploymentResourcePlural
33+
ArangoDeploymentKind = deployment.ArangoDeploymentResourceKind
34+
ArangoDeploymentVersionV1 = deploymentv1.ArangoDeploymentVersion
35+
)
36+
37+
func ArangoDeploymentGK() schema.GroupKind {
38+
return schema.GroupKind{
39+
Group: ArangoDeploymentGroup,
40+
Kind: ArangoDeploymentKind,
41+
}
42+
}
43+
44+
func ArangoDeploymentGKv1() schema.GroupVersionKind {
45+
return schema.GroupVersionKind{
46+
Group: ArangoDeploymentGroup,
47+
Kind: ArangoDeploymentKind,
48+
Version: ArangoDeploymentVersionV1,
49+
}
50+
}
51+
52+
func ArangoDeploymentGR() schema.GroupResource {
53+
return schema.GroupResource{
54+
Group: ArangoDeploymentGroup,
55+
Resource: ArangoDeploymentResource,
56+
}
57+
}
58+
59+
func ArangoDeploymentGRv1() schema.GroupVersionResource {
60+
return schema.GroupVersionResource{
61+
Group: ArangoDeploymentGroup,
62+
Resource: ArangoDeploymentResource,
63+
Version: ArangoDeploymentVersionV1,
64+
}
65+
}

pkg/deployment/resources/inspector/am_anonymous.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ func (p *arangoMembersInspector) Anonymous(gvk schema.GroupVersionKind) (anonymo
3030

3131
if g.Kind == gvk.Kind && g.Group == gvk.Group {
3232
switch gvk.Version {
33-
case ArangoMemberVersionV1:
33+
case ArangoMemberVersionV1, DefaultVersion:
34+
if p.v1 == nil || p.v1.err != nil {
35+
return nil, false
36+
}
3437
return &arangoMembersInspectorAnonymousV1{i: p.v1}, true
3538
}
3639
}

pkg/deployment/resources/inspector/at_anonymous.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ func (p *arangoTasksInspector) Anonymous(gvk schema.GroupVersionKind) (anonymous
3030

3131
if g.Kind == gvk.Kind && g.Group == gvk.Group {
3232
switch gvk.Version {
33-
case ArangoTaskVersionV1:
33+
case ArangoTaskVersionV1, DefaultVersion:
34+
if p.v1 == nil || p.v1.err != nil {
35+
return nil, false
36+
}
3437
return &arangoTasksInspectorAnonymousV1{i: p.v1}, true
3538
}
3639
}

pkg/deployment/resources/inspector/endpoints_anonymous.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ func (p *endpointsInspector) Anonymous(gvk schema.GroupVersionKind) (anonymous.I
3030

3131
if g.Kind == gvk.Kind && g.Group == gvk.Group {
3232
switch gvk.Version {
33-
case EndpointsVersionV1:
33+
case EndpointsVersionV1, DefaultVersion:
34+
if p.v1 == nil || p.v1.err != nil {
35+
return nil, false
36+
}
3437
return &endpointsInspectorAnonymousV1{i: p.v1}, true
3538
}
3639
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
25+
monitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
26+
core "k8s.io/api/core/v1"
27+
policyv1 "k8s.io/api/policy/v1"
28+
policyv1beta1 "k8s.io/api/policy/v1beta1"
29+
"k8s.io/apimachinery/pkg/runtime/schema"
30+
)
31+
32+
func ExtractGVKFromObject(in interface{}) (schema.GroupVersionKind, bool) {
33+
if in != nil {
34+
switch in.(type) {
35+
case *api.ArangoClusterSynchronization, api.ArangoClusterSynchronization:
36+
return ArangoClusterSynchronizationGKv1(), true
37+
case *api.ArangoMember, api.ArangoMember:
38+
return ArangoMemberGKv1(), true
39+
case *api.ArangoTask, api.ArangoTask:
40+
return ArangoTaskGKv1(), true
41+
case *core.Endpoints, core.Endpoints:
42+
return EndpointsGKv1(), true
43+
case *core.Node, core.Node:
44+
return NodeGKv1(), true
45+
case *policyv1.PodDisruptionBudget, policyv1.PodDisruptionBudget:
46+
return PodDisruptionBudgetGKv1(), true
47+
case *policyv1beta1.PodDisruptionBudget, policyv1beta1.PodDisruptionBudget:
48+
return PodDisruptionBudgetGKv1Beta1(), true
49+
case *core.Pod, core.Pod:
50+
return PodGKv1(), true
51+
case *core.ServiceAccount, core.ServiceAccount:
52+
return ServiceAccountGKv1(), true
53+
case *core.PersistentVolumeClaim, core.PersistentVolumeClaim:
54+
return PersistentVolumeClaimGKv1(), true
55+
case *core.Secret, core.Secret:
56+
return SecretGKv1(), true
57+
case *core.Service, core.Service:
58+
return ServiceGKv1(), true
59+
case *monitoring.ServiceMonitor, monitoring.ServiceMonitor:
60+
return ServiceMonitorGKv1(), true
61+
case *api.ArangoDeployment, api.ArangoDeployment:
62+
return ArangoDeploymentGKv1(), true
63+
}
64+
}
65+
66+
return schema.GroupVersionKind{}, false
67+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
"reflect"
25+
"testing"
26+
27+
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
28+
monitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
29+
"github.com/stretchr/testify/require"
30+
core "k8s.io/api/core/v1"
31+
policyv1 "k8s.io/api/policy/v1"
32+
policyv1beta1 "k8s.io/api/policy/v1beta1"
33+
"k8s.io/apimachinery/pkg/runtime/schema"
34+
)
35+
36+
func Test_GVK(t *testing.T) {
37+
testGVK(t, ArangoClusterSynchronizationGKv1(), &api.ArangoClusterSynchronization{}, api.ArangoClusterSynchronization{})
38+
testGVK(t, ArangoMemberGKv1(), &api.ArangoMember{}, api.ArangoMember{})
39+
testGVK(t, ArangoTaskGKv1(), &api.ArangoTask{}, api.ArangoTask{})
40+
testGVK(t, EndpointsGKv1(), &core.Endpoints{}, core.Endpoints{})
41+
testGVK(t, NodeGKv1(), &core.Node{}, core.Node{})
42+
testGVK(t, PodDisruptionBudgetGKv1(), &policyv1.PodDisruptionBudget{}, policyv1.PodDisruptionBudget{})
43+
testGVK(t, PodDisruptionBudgetGKv1Beta1(), &policyv1beta1.PodDisruptionBudget{}, policyv1beta1.PodDisruptionBudget{})
44+
testGVK(t, PodGKv1(), &core.Pod{}, core.Pod{})
45+
testGVK(t, ServiceAccountGKv1(), &core.ServiceAccount{}, core.ServiceAccount{})
46+
testGVK(t, ServiceGKv1(), &core.Service{}, core.Service{})
47+
testGVK(t, PersistentVolumeClaimGKv1(), &core 10000 .PersistentVolumeClaim{}, core.PersistentVolumeClaim{})
48+
testGVK(t, SecretGKv1(), &core.Secret{}, core.Secret{})
49+
testGVK(t, ServiceMonitorGKv1(), &monitoring.ServiceMonitor{}, monitoring.ServiceMonitor{})
50+
testGVK(t, ArangoDeploymentGKv1(), &api.ArangoDeployment{}, api.ArangoDeployment{})
51+
}
52+
53+
func testGVK(t *testing.T, gvk schema.GroupVersionKind, in ...interface{}) {
54+
t.Run(gvk.String(), func(t *testing.T) {
55+
for _, z := range in {
56+
zt := reflect.TypeOf(z)
57+
t.Run(zt.String(), func(t *testing.T) {
58+
g, ok := ExtractGVKFromObject(z)
59+
require.True(t, ok)
60+
require.Equal(t, gvk, g)
61+
})
62+
}
63+
})
64+
}

pkg/deployment/resources/inspector/inspector.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ import (
4848
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
4949
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
5050
"github.com/rs/zerolog"
51+
core "k8s.io/api/core/v1"
5152
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
5253
"k8s.io/apimachinery/pkg/runtime/schema"
5354
)
5455

56+
const (
57+
DefaultVersion = ""
58+
)
59+
5560
var (
5661
inspectorLoadersList inspectorLoaders
5762
inspectorLoadersLock sync.Mutex
@@ -159,6 +164,31 @@ type inspectorState struct {
159164
initialised bool
160165
}
161166

167+
func extractGVKFromOwnerReference(o meta.OwnerReference) schema.GroupVersionKind {
168+
z := strings.SplitN(o.APIVersion, "/", 2)
169+
170+
switch len(z) {
171+
case 1:
172+
return schema.GroupVersionKind{
173+
Group: core.GroupName,
174+
Version: z[0],
175+
Kind: o.Kind,
176+
}
177+
case 2:
178+
return schema.GroupVersionKind{
179+
Group: z[0],
180+
Version: z[1],
181+
Kind: o.Kind,
182+
}
183+
default:
184+
return schema.GroupVersionKind{
185+
Group: core.GroupName,
186+
Version: z[1],
187+
Kind: o.APIVersion,
188+
}
189+
}
190+
}
191+
162192
func (i *inspectorState) Anonymous(gvk schema.GroupVersionKind) (anonymous.Interface, bool) {
163193
for _, o := range i.AnonymousObjects() {
164194
if o == nil {

pkg/deployment/resources/inspector/nodes_anonymous.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ func (p *nodesInspector) Anonymous(gvk schema.GroupVersionKind) (anonymous.Inter
3030

3131
if g.Kind == gvk.Kind && g.Group == gvk.Group {
3232
switch gvk.Version {
33-
case NodeVersionV1:
33+
case NodeVersionV1, DefaultVersion:
34+
if p.v1 == nil || p.v1.err != nil {
35+
return nil, false
36+
}
3437
return &nodesInspectorAnonymousV1{i: p.v1}, true
3538
}
3639
}

0 commit comments

Comments
 (0)
0