Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- (Feature) Add CoreV1 Endpoints Inspector
- (Feature) Add Current ArangoDeployment Inspector
- (Refactor) Anonymous inspector functions
- (Feature) Recursive OwnerReference discovery

## [1.2.11](https://github.com/arangodb/kube-arangodb/tree/1.2.11) (2022-04-30)
- (Bugfix) Orphan PVC are not removed
Expand Down
5 changes: 4 additions & 1 deletion pkg/deployment/resources/inspector/acs_anonymous.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func (p *arangoClusterSynchronizationsInspector) Anonymous(gvk schema.GroupVersi

if g.Kind == gvk.Kind && g.Group == gvk.Group {
switch gvk.Version {
case ArangoClusterSynchronizationVersionV1:
case ArangoClusterSynchronizationVersionV1, DefaultVersion:
if p.v1 == nil || p.v1.err != nil {
return nil, false
}
return &arangoClusterSynchronizationsInspectorAnonymousV1{i: p.v1}, true
}
}
Expand Down
65 changes: 65 additions & 0 deletions pkg/deployment/resources/inspector/ad_constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// 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 inspector

import (
"github.com/arangodb/kube-arangodb/pkg/apis/deployment"
deploymentv1 "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

// ArangoDeployment
const (
ArangoDeploymentGroup = deployment.ArangoDeploymentGroupName
ArangoDeploymentResource = deployment.ArangoDeploymentResourcePlural
ArangoDeploymentKind = deployment.ArangoDeploymentResourceKind
ArangoDeploymentVersionV1 = deploymentv1.ArangoDeploymentVersion
)

func ArangoDeploymentGK() schema.GroupKind {
return schema.GroupKind{
Group: ArangoDeploymentGroup,
Kind: ArangoDeploymentKind,
}
}

func ArangoDeploymentGKv1() schema.GroupVersionKind {
return schema.GroupVersionKind{
Group: ArangoDeploymentGroup,
Kind: ArangoDeploymentKind,
Version: ArangoDeploymentVersionV1,
}
}

func ArangoDeploymentGR() schema.GroupResource {
return schema.GroupResource{
Group: ArangoDeploymentGroup,
Resource: ArangoDeploymentResource,
}
}

func ArangoDeploymentGRv1() schema.GroupVersionResource {
return schema.GroupVersionResource{
Group: ArangoDeploymentGroup,
Resource: ArangoDeploymentResource,
Version: ArangoDeploymentVersionV1,
}
}
5 changes: 4 additions & 1 deletion pkg/deployment/resources/inspector/am_anonymous.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func (p *arangoMembersInspector) Anonymous(gvk schema.GroupVersionKind) (anonymo

if g.Kind == gvk.Kind && g.Group == gvk.Group {
switch gvk.Version {
case ArangoMemberVersionV1:
case ArangoMemberVersionV1, DefaultVersion:
if p.v1 == nil || p.v1.err != nil {
return nil, false
}
return &arangoMembersInspectorAnonymousV1{i: p.v1}, true
}
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/deployment/resources/inspector/at_anonymous.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func (p *arangoTasksInspector) Anonymous(gvk schema.GroupVersionKind) (anonymous

if g.Kind == gvk.Kind && g.Group == gvk.Group {
switch gvk.Version {
case ArangoTaskVersionV1:
case ArangoTaskVersionV1, DefaultVersion:
if p.v1 == nil || p.v1.err != nil {
return nil, false
}
return &arangoTasksInspectorAnonymousV1{i: p.v1}, true
}
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/deployment/resources/inspector/endpoints_anonymous.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func (p *endpointsInspector) Anonymous(gvk schema.GroupVersionKind) (anonymous.I

if g.Kind == gvk.Kind && g.Group == gvk.Group {
switch gvk.Version {
case EndpointsVersionV1:
case EndpointsVersionV1, DefaultVersion:
if p.v1 == nil || p.v1.err != nil {
return nil, false
}
return &endpointsInspectorAnonymousV1{i: p.v1}, true
}
}
Expand Down
67 changes: 67 additions & 0 deletions pkg/deployment/resources/inspector/gvk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// 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 inspector

import (
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
monitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
core "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func ExtractGVKFromObject(in interface{}) (schema.GroupVersionKind, bool) {
if in != nil {
switch in.(type) {
case *api.ArangoClusterSynchronization, api.ArangoClusterSynchronization:
return ArangoClusterSynchronizationGKv1(), true
case *api.ArangoMember, api.ArangoMember:
return ArangoMemberGKv1(), true
case *api.ArangoTask, api.ArangoTask:
return ArangoTaskGKv1(), true
case *core.Endpoints, core.Endpoints:
return EndpointsGKv1(), true
case *core.Node, core.Node:
return NodeGKv1(), true
case *policyv1.PodDisruptionBudget, policyv1.PodDisruptionBudget:
return PodDisruptionBudgetGKv1(), true
case *policyv1beta1.PodDisruptionBudget, policyv1beta1.PodDisruptionBudget:
return PodDisruptionBudgetGKv1Beta1(), true
case *core.Pod, core.Pod:
return PodGKv1(), true
case *core.ServiceAccount, core.ServiceAccount:
return ServiceAccountGKv1(), true
case *core.PersistentVolumeClaim, core.PersistentVolumeClaim:
return PersistentVolumeClaimGKv1(), true
case *core.Secret, core.Secret:
return SecretGKv1(), true
case *core.Service, core.Service:
return ServiceGKv1(), true
case *monitoring.ServiceMonitor, monitoring.ServiceMonitor:
return ServiceMonitorGKv1(), true
case *api.ArangoDeployment, api.ArangoDeployment:
return ArangoDeploymentGKv1(), true
}
}

return schema.GroupVersionKind{}, false
}
64 changes: 64 additions & 0 deletions pkg/deployment/resources/inspector/gvk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// 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 inspector

import (
"reflect"
"testing"

api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
monitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/stretchr/testify/require"
core "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func Test_GVK(t *testing.T) {
testGVK(t, ArangoClusterSynchronizationGKv1(), &api.ArangoClusterSynchronization{}, api.ArangoClusterSynchronization{})
testGVK(t, ArangoMemberGKv1(), &api.ArangoMember{}, api.ArangoMember{})
testGVK(t, ArangoTaskGKv1(), &api.ArangoTask{}, api.ArangoTask{})
testGVK(t, EndpointsGKv1(), &core.Endpoints{}, core.Endpoints{})
testGVK(t, NodeGKv1(), &core.Node{}, core.Node{})
testGVK(t, PodDisruptionBudgetGKv1(), &policyv1.PodDisruptionBudget{}, policyv1.PodDisruptionBudget{})
testGVK(t, PodDisruptionBudgetGKv1Beta1(), &policyv1beta1.PodDisruptionBudget{}, policyv1beta1.PodDisruptionBudget{})
testGVK(t, PodGKv1(), &core.Pod{}, core.Pod{})
testGVK(t, ServiceAccountGKv1(), &core.ServiceAccount{}, core.ServiceAccount{})
testGVK(t, ServiceGKv1(), &core.Service{}, core.Service{})
testGVK(t, PersistentVolumeClaimGKv1(), &core.PersistentVolumeClaim{}, core.PersistentVolumeClaim{})
testGVK(t, SecretGKv1(), &core.Secret{}, core.Secret{})
testGVK(t, ServiceMonitorGKv1(), &monitoring.ServiceMonitor{}, monitoring.ServiceMonitor{})
testGVK(t, ArangoDeploymentGKv1(), &api.ArangoDeployment{}, api.ArangoDeployment{})
}

func testGVK(t *testing.T, gvk schema.GroupVersionKind, in ...interface{}) {
t.Run(gvk.String(), func(t *testing.T) {
for _, z := range in {
zt := reflect.TypeOf(z)
t.Run(zt.String(), func(t *testing.T) {
g, ok := ExtractGVKFromObject(z)
require.True(t, ok)
require.Equal(t, gvk, g)
})
}
})
}
30 changes: 30 additions & 0 deletions pkg/deployment/resources/inspector/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
"github.com/rs/zerolog"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

const (
DefaultVersion = ""
)

var (
inspectorLoadersList inspectorLoaders
inspectorLoadersLock sync.Mutex
Expand Down Expand Up @@ -159,6 +164,31 @@ type inspectorState struct {
initialised bool
}

func extractGVKFromOwnerReference(o meta.OwnerReference) schema.GroupVersionKind {
z := strings.SplitN(o.APIVersion, "/", 2)

switch len(z) {
case 1:
return schema.GroupVersionKind{
Group: core.GroupName,
Version: z[0],
Kind: o.Kind,
}
case 2:
return schema.GroupVersionKind{
Group: z[0],
Version: z[1],
Kind: o.Kind,
}
default:
return schema.GroupVersionKind{
Group: core.GroupName,
Version: z[1],
Kind: o.APIVersion,
}
}
}

func (i *inspectorState) Anonymous(gvk schema.GroupVersionKind) (anonymous.Interface, bool) {
for _, o := range i.AnonymousObjects() {
if o == nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/deployment/resources/inspector/nodes_anonymous.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func (p *nodesInspector) Anonymous(gvk schema.GroupVersionKind) (anonymous.Inter

if g.Kind == gvk.Kind && g.Group == gvk.Group {
switch gvk.Version {
case NodeVersionV1:
case NodeVersionV1, DefaultVersion:
if p.v1 == nil || p.v1.err != nil {
return nil, false
}
return &nodesInspectorAnonymousV1{i: p.v1}, true
}
}
Expand Down
63 changes: 63 additions & 0 deletions pkg/deployment/resources/inspector/owner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// 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 inspector

import (
"context"

"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/anonymous"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func (i *inspectorState) IsOwnerOf(ctx context.Context, owner inspector.Object, obj meta.Object) bool {
return i.isOwnerOf(ctx, 8, i.AnonymousObjects(), owner.GroupVersionKind(), owner, obj)
}

func (i *inspectorState) isOwnerOf(ctx context.Context, jumps int, a []anonymous.Impl, gvk schema.GroupVersionKind, owner meta.Object, obj meta.Object) bool {
if jumps <= 0 {
return false
}

for _, o := range obj.GetOwnerReferences() {
ogvk := extractGVKFromOwnerReference(o)

if ogvk.Kind == gvk.Kind && ogvk.Group == gvk.Group && o.Name == owner.GetName() && o.UID == owner.GetUID() {
return true
}

for _, q := range a {
if a == nil {
continue
}
if c, ok := q.Anonymous(ogvk); ok {
if nobj, err := c.Get(ctx, o.Name, meta.GetOptions{}); err == nil {
if i.isOwnerOf(ctx, jumps-1, a, gvk, owner, nobj) {
return true
}
}
}
}
}

return false
}
Loading