1
0
Fork 0
mirror of https://github.com/arangodb/kube-arangodb.git synced 2024-12-14 11:57:37 +00:00

[Feature] Add apiserver discovery (#911)

This commit is contained in:
Tomasz Mielech 2022-02-27 16:24:34 +01:00 committed by GitHub
parent 6f246bcf58
commit 9fe92a9aec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 122 additions and 41 deletions

View file

@ -30,30 +30,26 @@ import (
"strings"
"testing"
"github.com/arangodb/kube-arangodb/pkg/deployment/resources/inspector"
"github.com/stretchr/testify/require"
"github.com/arangodb/kube-arangodb/pkg/deployment/client"
monitoringFakeClient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/fake"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/probes"
"github.com/arangodb/kube-arangodb/pkg/util/arangod/conn"
"github.com/arangodb/go-driver"
driver "github.com/arangodb/go-driver"
"github.com/arangodb/go-driver/jwt"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
"github.com/arangodb/kube-arangodb/pkg/deployment/resources"
arangofake "github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned/fake"
"github.com/arangodb/kube-arangodb/pkg/util/constants"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
"github.com/arangodb/kube-arangodb/pkg/deployment/client"
monitoringFakeClient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/fake"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
recordfake "k8s.io/client-go/tools/record"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
"github.com/arangodb/kube-arangodb/pkg/deployment/resources"
"github.com/arangodb/kube-arangodb/pkg/deployment/resources/inspector"
arangofake "github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned/fake"
"github.com/arangodb/kube-arangodb/pkg/util/arangod/conn"
"github.com/arangodb/kube-arangodb/pkg/util/constants"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/probes"
)
const (
@ -493,6 +489,7 @@ func createTestDeployment(t *testing.T, config Config, arangoDeployment *api.Ara
cachedStatus, err := inspector.NewInspector(context.Background(), d.getKubeCli(), d.getMonitoringV1Cli(), d.getArangoCli(), d.GetNamespace())
require.NoError(t, err)
assert.NotEmpty(t, cachedStatus.GetVersionInfo(), "API server should not have returned empty version")
d.SetCachedStatus(cachedStatus)
arangoDeployment.Spec.SetDefaults(arangoDeployment.GetName())

View file

@ -39,9 +39,9 @@ import (
"k8s.io/client-go/kubernetes"
"github.com/arangodb/arangosync-client/client"
"github.com/arangodb/go-driver"
"github.com/arangodb/go-driver/agency"
"github.com/arangodb/go-driver"
backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
"github.com/arangodb/kube-arangodb/pkg/deployment/actions"
@ -690,12 +690,14 @@ type testCase struct {
ServiceMonitors map[string]*monitoring.ServiceMonitor
ArangoMembers map[string]*api.ArangoMember
Nodes map[string]*core.Node
VersionInfo driver.Version
Extender func(t *testing.T, r *Reconciler, c *testCase)
}
func (t testCase) Inspector() inspectorInterface.Inspector {
return inspector.NewInspectorFromData(t.Pods, t.Secrets, t.PVCS, t.Services, t.ServiceAccounts, t.PDBS, t.ServiceMonitors, t.ArangoMembers, t.Nodes)
return inspector.NewInspectorFromData(t.Pods, t.Secrets, t.PVCS, t.Services, t.ServiceAccounts, t.PDBS,
t.ServiceMonitors, t.ArangoMembers, t.Nodes, t.VersionInfo)
}
func TestCreatePlan(t *testing.T) {

View file

@ -24,22 +24,19 @@ import (
"context"
"sync"
"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
"github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned"
inspectorInterface "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector"
monitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
monitoringClient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/typed/monitoring/v1"
core "k8s.io/api/core/v1"
policy "k8s.io/api/policy/v1beta1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/arangodb/go-driver"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
"github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned"
"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
inspectorInterface "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector"
)
// SecretReadInterface has methods to work with Secret resources with ReadOnly mode.
@ -71,6 +68,7 @@ func newInspector(ctx context.Context, k kubernetes.Interface, m monitoringClien
i.c = c
if err := util.RunParallel(15,
getVersionInfo(ctx, &i, k, namespace),
podsToMap(ctx, &i, k, namespace),
secretsToMap(ctx, &i, k, namespace),
pvcsToMap(ctx, &i, k, namespace),
@ -88,7 +86,7 @@ func newInspector(ctx context.Context, k kubernetes.Interface, m monitoringClien
}
func NewEmptyInspector() inspectorInterface.Inspector {
return NewInspectorFromData(nil, nil, nil, nil, nil, nil, nil, nil, nil)
return NewInspectorFromData(nil, nil, nil, nil, nil, nil, nil, nil, nil, "")
}
func NewInspectorFromData(pods map[string]*core.Pod,
@ -99,7 +97,8 @@ func NewInspectorFromData(pods map[string]*core.Pod,
podDisruptionBudgets map[string]*policy.PodDisruptionBudget,
serviceMonitors map[string]*monitoring.ServiceMonitor,
arangoMembers map[string]*api.ArangoMember,
nodes map[string]*core.Node) inspectorInterface.Inspector {
nodes map[string]*core.Node,
version driver.Version) inspectorInterface.Inspector {
i := &inspector{
pods: pods,
secrets: secrets,
@ -109,6 +108,7 @@ func NewInspectorFromData(pods map[string]*core.Pod,
podDisruptionBudgets: podDisruptionBudgets,
serviceMonitors: serviceMonitors,
arangoMembers: arangoMembers,
versionInfo: version,
}
if nodes == nil {
@ -144,6 +144,7 @@ type inspector struct {
serviceMonitors map[string]*monitoring.ServiceMonitor
arangoMembers map[string]*api.ArangoMember
nodes *nodeLoader
versionInfo driver.Version
}
func (i *inspector) IsStatic() bool {
@ -172,6 +173,7 @@ func (i *inspector) Refresh(ctx context.Context) error {
i.serviceMonitors = new.serviceMonitors
i.arangoMembers = new.arangoMembers
i.nodes = new.nodes
i.versionInfo = new.versionInfo
return nil
}

View file

@ -23,16 +23,15 @@ package inspector
import (
"context"
"github.com/arangodb/kube-arangodb/pkg/util/globals"
core "k8s.io/api/core/v1"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/pod"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func (i *inspector) IteratePods(action pod.Action, filters ...pod.Filter) error {

View file

@ -0,0 +1,51 @@
//
// 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"
"strings"
"github.com/arangodb/go-driver"
"k8s.io/client-go/kubernetes"
)
// GetVersionInfo returns kubernetes server version information.
func (i *inspector) GetVersionInfo() driver.Version {
i.lock.Lock()
defer i.lock.Unlock()
return i.versionInfo
}
func getVersionInfo(_ context.Context, inspector *inspector, k kubernetes.Interface, _ string) func() error {
return func() error {
inspector.versionInfo = ""
if v, err := k.Discovery().ServerVersion(); err != nil {
return err
} else {
inspector.versionInfo = driver.Version(strings.TrimPrefix(v.GitVersion, "v"))
}
return nil
}
}

View file

@ -85,7 +85,7 @@ func (i inspectorMockStruct) AddService(t *testing.T, svc ...*core.Service) insp
}
func (i inspectorMockStruct) Get(t *testing.T) inspectorInterface.Inspector {
return inspector.NewInspectorFromData(nil, nil, nil, i.services, nil, nil, nil, nil, nil)
return inspector.NewInspectorFromData(nil, nil, nil, i.services, nil, nil, nil, nil, nil, "")
}
// TestCreateArangodArgsAgent tests createArangodArgs for agent.

View file

@ -23,6 +23,7 @@ package inspector
import (
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/node"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/refresh"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/server"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangomember"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/persistentvolumeclaim"
@ -44,6 +45,7 @@ type Inspector interface {
servicemonitor.Inspector
serviceaccount.Inspector
arangomember.Inspector
server.Inspector
node.Loader
}

View file

@ -0,0 +1,28 @@
//
// 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 server
import "github.com/arangodb/go-driver"
// Inspector for secrets
type Inspector interface {
GetVersionInfo() driver.Version
}

View file

@ -22,6 +22,7 @@ package util
import "sync"
// RunParallel runs actions parallelly throttling them to the given maximum number.
func RunParallel(max int, actions ...func() error) error {
c := make(chan int, max)
errors := make([]error, len(actions))
@ -37,9 +38,8 @@ func RunParallel(max int, actions ...func() error) error {
var wg sync.WaitGroup
wg.Add(len(actions))
for id, i := range actions {
wg.Add(1)
go func(id int, action func() error) {
defer func() {
c <- 0