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

[Bugfix] Fix PDBs Version discovery (#1164)

This commit is contained in:
Adam Janikowski 2022-11-02 07:09:57 -04:00 committed by GitHub
parent fe434ef24a
commit 8af2e617ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 271 additions and 23 deletions

View file

@ -6,6 +6,7 @@
- (Improvement) Arango Kubernetes Client Mod Implementation
- (Refactoring) Extract kerrors package
- (Refactoring) Extract Inspector Definitions package
- (Bugfix) Fix PDBs Version discovery
## [1.2.20](https://github.com/arangodb/kube-arangodb/tree/1.2.20) (2022-10-25)
- (Feature) Add action progress

View file

@ -31,6 +31,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/definitions"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
func init() {
@ -170,7 +171,11 @@ func (p *arangoClusterSynchronizationsInspector) Refresh(ctx context.Context) er
return p.state.refresh(ctx, arangoClusterSynchronizationsInspectorLoaderObj)
}
func (p arangoClusterSynchronizationsInspector) Throttle(c throttle.Components) throttle.Throttle {
func (p *arangoClusterSynchronizationsInspector) Version() version.Version {
return version.V1
}
func (p *arangoClusterSynchronizationsInspector) Throttle(c throttle.Components) throttle.Throttle {
return c.ArangoClusterSynchronization()
}

View file

@ -31,6 +31,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/definitions"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
func init() {
@ -174,7 +175,11 @@ func (p *arangoMembersInspector) Refresh(ctx context.Context) error {
return p.state.refresh(ctx, arangoMembersInspectorLoaderObj)
}
func (p arangoMembersInspector) Throttle(c throttle.Components) throttle.Throttle {
func (p *arangoMembersInspector) Version() version.Version {
return version.V1
}
func (p *arangoMembersInspector) Throttle(c throttle.Components) throttle.Throttle {
return c.ArangoMember()
}

View file

@ -31,6 +31,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/definitions"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
func init() {
@ -170,7 +171,11 @@ func (p *arangoTasksInspector) Refresh(ctx context.Context) error {
return p.state.refresh(ctx, arangoTasksInspectorLoaderObj)
}
func (p arangoTasksInspector) Throttle(c throttle.Components) throttle.Throttle {
func (p *arangoTasksInspector) Version() version.Version {
return version.V1
}
func (p *arangoTasksInspector) Throttle(c throttle.Components) throttle.Throttle {
return c.ArangoTask()
}

View file

@ -31,6 +31,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/definitions"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
func init() {
@ -170,7 +171,11 @@ func (p *endpointsInspector) Refresh(ctx context.Context) error {
return p.state.refresh(ctx, endpointsInspectorLoaderObj)
}
func (p endpointsInspector) Throttle(c throttle.Components) throttle.Throttle {
func (p *endpointsInspector) Version() version.Version {
return version.V1
}
func (p *endpointsInspector) Throttle(c throttle.Components) throttle.Throttle {
return c.Endpoints()
}

View file

@ -31,6 +31,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/definitions"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
func init() {
@ -170,7 +171,11 @@ func (p *nodesInspector) Refresh(ctx context.Context) error {
return p.state.refresh(ctx, nodesInspectorLoaderObj)
}
func (p nodesInspector) Throttle(c throttle.Components) throttle.Throttle {
func (p *nodesInspector) Version() version.Version {
return version.V1
}
func (p *nodesInspector) Throttle(c throttle.Components) throttle.Throttle {
return c.Node()
}

View file

@ -34,6 +34,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/definitions"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
func init() {
@ -281,7 +282,15 @@ func (p *podDisruptionBudgetsInspector) Refresh(ctx context.Context) error {
return p.state.refresh(ctx, podDisruptionBudgetsInspectorLoaderObj)
}
func (p podDisruptionBudgetsInspector) Throttle(c throttle.Components) throttle.Throttle {
func (p *podDisruptionBudgetsInspector) Version() version.Version {
if p.state.versionInfo.CompareTo("1.21") >= 0 {
return version.V1
}
return version.V1Beta1
}
func (p *podDisruptionBudgetsInspector) Throttle(c throttle.Components) throttle.Throttle {
return c.PodDisruptionBudget()
}

View file

@ -0,0 +1,75 @@
//
// 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"
"testing"
"time"
"github.com/stretchr/testify/require"
kversion "k8s.io/apimachinery/pkg/version"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
)
func Test_PDB_Versions(t *testing.T) {
k8sVersions := map[string]version.Version{
"v1.18.0": version.V1Beta1,
"v1.19.0": version.V1Beta1,
"v1.20.0": version.V1Beta1,
"v1.21.0": version.V1,
"v1.22.0": version.V1,
"v1.23.0": version.V1,
"v1.24.0": version.V1,
"v1.25.0": version.V1,
"v1.26.0": version.V1,
}
for v, expected := range k8sVersions {
t.Run(v, func(t *testing.T) {
c := kclient.NewFakeClientWithVersion(&kversion.Info{
GitVersion: v,
})
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)
i := NewInspector(tc, c, "test", "test")
require.NoError(t, i.Refresh(context.Background()))
if expected.IsV1() {
_, err := i.PodDisruptionBudget().V1()
require.NoError(t, err)
_, err = i.PodDisruptionBudget().V1Beta1()
require.Error(t, err)
} else {
_, err := i.PodDisruptionBudget().V1()
require.Error(t, err)
_, err = i.PodDisruptionBudget().V1Beta1()
require.NoError(t, err)
}
})
}
}

View file

@ -31,6 +31,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/definitions"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
func init() {
@ -174,7 +175,11 @@ func (p *podsInspector) Refresh(ctx context.Context) error {
return p.state.refresh(ctx, podsInspectorLoaderObj)
}
func (p podsInspector) Throttle(c throttle.Components) throttle.Throttle {
func (p *podsInspector) Version() version.Version {
return version.V1
}
func (p *podsInspector) Throttle(c throttle.Components) throttle.Throttle {
return c.Pod()
}

View file

@ -31,6 +31,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/definitions"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
func init() {
@ -174,7 +175,11 @@ func (p *persistentVolumeClaimsInspector) Refresh(ctx context.Context) error {
return p.state.refresh(ctx, persistentVolumeClaimsInspectorLoaderObj)
}
func (p persistentVolumeClaimsInspector) Throttle(c throttle.Components) throttle.Throttle {
func (p *persistentVolumeClaimsInspector) Version() version.Version {
return version.V1
}
func (p *persistentVolumeClaimsInspector) Throttle(c throttle.Components) throttle.Throttle {
return c.PersistentVolumeClaim()
}

View file

@ -31,6 +31,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/definitions"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
func init() {
@ -174,7 +175,11 @@ func (p *serviceAccountsInspector) Refresh(ctx context.Context) error {
return p.state.refresh(ctx, serviceAccountsInspectorLoaderObj)
}
func (p serviceAccountsInspector) Throttle(c throttle.Components) throttle.Throttle {
func (p *serviceAccountsInspector) Version() version.Version {
return version.V1
}
func (p *serviceAccountsInspector) Throttle(c throttle.Components) throttle.Throttle {
return c.ServiceAccount()
}

View file

@ -31,6 +31,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/definitions"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
func init() {
@ -174,7 +175,11 @@ func (p *secretsInspector) Refresh(ctx context.Context) error {
return p.state.refresh(ctx, secretsInspectorLoaderObj)
}
func (p secretsInspector) Throttle(c throttle.Components) throttle.Throttle {
func (p *secretsInspector) Version() version.Version {
return version.V1
}
func (p *secretsInspector) Throttle(c throttle.Components) throttle.Throttle {
return c.Secret()
}

View file

@ -31,6 +31,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/definitions"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
func init() {
@ -174,7 +175,11 @@ func (p *servicesInspector) Refresh(ctx context.Context) error {
return p.state.refresh(ctx, servicesInspectorLoaderObj)
}
func (p servicesInspector) Throttle(c throttle.Components) throttle.Throttle {
func (p *servicesInspector) Version() version.Version {
return version.V1
}
func (p *servicesInspector) Throttle(c throttle.Components) throttle.Throttle {
return c.Service()
}

View file

@ -31,6 +31,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/definitions"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
func init() {
@ -170,7 +171,11 @@ func (p *serviceMonitorsInspector) Refresh(ctx context.Context) error {
return p.state.refresh(ctx, serviceMonitorsInspectorLoaderObj)
}
func (p serviceMonitorsInspector) Throttle(c throttle.Components) throttle.Throttle {
func (p *serviceMonitorsInspector) Version() version.Version {
return version.V1
}
func (p *serviceMonitorsInspector) Throttle(c throttle.Components) throttle.Throttle {
return c.ServiceMonitor()
}

View file

@ -126,23 +126,22 @@ func (r *Resources) ensurePDBForGroup(ctx context.Context, group api.ServerGroup
deplName := r.context.GetAPIObject().GetName()
pdbName := PDBNameForGroup(deplName, group)
log := r.log.Str("section", "pdb").Str("group", group.AsRole())
pdbMod := r.context.ACS().CurrentClusterCache().PodDisruptionBudgetsModInterface()
cache := r.context.ACS().CurrentClusterCache()
pdbMod := cache.PodDisruptionBudgetsModInterface()
for {
var minAvailable *intstr.IntOrString
var deletionTimestamp *meta.Time
var isV1 bool
err := globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error {
if inspector, err := r.context.ACS().CurrentClusterCache().PodDisruptionBudget().V1(); err == nil {
if inspector, err := cache.PodDisruptionBudget().V1(); err == nil {
if pdb, err := inspector.Read().Get(ctxChild, pdbName, meta.GetOptions{}); err != nil {
return err
} else {
isV1 = true
minAvailable = pdb.Spec.MinAvailable
deletionTimestamp = pdb.GetDeletionTimestamp()
}
} else if inspector, err := r.context.ACS().CurrentClusterCache().PodDisruptionBudget().V1Beta1(); err == nil {
} else if inspector, err := cache.PodDisruptionBudget().V1Beta1(); err == nil {
if pdb, err := inspector.Read().Get(ctxChild, pdbName, meta.GetOptions{}); err != nil {
return err
} else {
@ -163,7 +162,7 @@ func (r *Resources) ensurePDBForGroup(ctx context.Context, group api.ServerGroup
err = globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error {
var errInternal error
if isV1 {
if cache.PodDisruptionBudget().Version().IsV1() {
pdb := newPDBV1(wantedMinAvail, deplName, group, r.context.GetAPIObject().AsOwner())
_, errInternal = pdbMod.V1().Create(ctxChild, pdb, meta.CreateOptions{})
} else {
@ -200,7 +199,7 @@ func (r *Resources) ensurePDBForGroup(ctx context.Context, group api.ServerGroup
if deletionTimestamp == nil {
// Update the PDB.
err := globals.GetGlobalTimeouts().Kubernetes().RunWithTimeout(ctx, func(ctxChild context.Context) error {
if isV1 {
if cache.PodDisruptionBudget().Version().IsV1() {
return pdbMod.V1().Delete(ctxChild, pdbName, meta.DeleteOptions{})
}

View file

@ -25,6 +25,7 @@ import (
v1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangoclustersynchronization/v1"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/gvk"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/refresh"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
type Inspector interface {
@ -37,5 +38,7 @@ type Definition interface {
gvk.GK
anonymous.Impl
Version() version.Version
V1() (v1.Inspector, error)
}

View file

@ -25,6 +25,7 @@ import (
v1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangomember/v1"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/gvk"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/refresh"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
type Inspector interface {
@ -37,5 +38,7 @@ type Definition interface {
gvk.GK
anonymous.Impl
Version() version.Version
V1() v1.Inspector
}

View file

@ -25,6 +25,7 @@ import (
v1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangotask/v1"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/gvk"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/refresh"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
type Inspector interface {
@ -37,5 +38,7 @@ type Definition interface {
gvk.GK
anonymous.Impl
Version() version.Version
V1() (v1.Inspector, error)
}

View file

@ -25,6 +25,7 @@ import (
v1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/endpoints/v1"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/gvk"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/refresh"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
type Inspector interface {
@ -37,5 +38,7 @@ type Definition interface {
gvk.GK
anonymous.Impl
Version() version.Version
V1() (v1.Inspector, error)
}

View file

@ -25,6 +25,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/gvk"
v1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/node/v1"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/refresh"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
type Inspector interface {
@ -37,5 +38,7 @@ type Definition interface {
gvk.GK
anonymous.Impl
Version() version.Version
V1() (v1.Inspector, error)
}

View file

@ -25,6 +25,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/gvk"
v1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/persistentvolumeclaim/v1"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/refresh"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
type Inspector interface {
@ -37,5 +38,7 @@ type Definition interface {
gvk.GK
anonymous.Impl
Version() version.Version
V1() v1.Inspector
}

View file

@ -25,6 +25,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/gvk"
v1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/pod/v1"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/refresh"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
type Inspector interface {
@ -37,5 +38,7 @@ type Definition interface {
gvk.GK
anonymous.Impl
Version() version.Version
V1() v1.Inspector
}

View file

@ -26,6 +26,7 @@ import (
v1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/poddisruptionbudget/v1"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/poddisruptionbudget/v1beta1"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/refresh"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
type Inspector interface {
@ -38,6 +39,8 @@ type Definition interface {
gvk.GK
anonymous.Impl
Version() version.Version
V1() (v1.Inspector, error)
V1Beta1() (v1beta1.Inspector, error)
}

View file

@ -25,6 +25,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/gvk"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/refresh"
v1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/secret/v1"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
type Inspector interface {
@ -37,5 +38,7 @@ type Definition interface {
gvk.GK
anonymous.Impl
Version() version.Version
V1() v1.Inspector
}

View file

@ -25,6 +25,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/gvk"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/refresh"
v1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/service/v1"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
type Inspector interface {
@ -37,5 +38,7 @@ type Definition interface {
gvk.GK
anonymous.Impl
Version() version.Version
V1() v1.Inspector
}

View file

@ -25,6 +25,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/gvk"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/refresh"
v1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/serviceaccount/v1"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
type Inspector interface {
@ -37,5 +38,7 @@ type Definition interface {
gvk.GK
anonymous.Impl
Version() version.Version
V1() v1.Inspector
}

View file

@ -25,6 +25,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/gvk"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/refresh"
v1 "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/servicemonitor/v1"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version"
)
type Inspector interface {
@ -37,5 +38,7 @@ type Definition interface {
gvk.GK
anonymous.Impl
Version() version.Version
V1() (v1.Inspector, error)
}

View file

@ -0,0 +1,36 @@
//
// 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 version
type Version string
func (v Version) IsV1() bool {
return v == V1
}
func (v Version) IsV1Beta1() bool {
return v == V1Beta1
}
const (
V1 Version = "V1"
V1Beta1 Version = "V1Beta1"
)

View file

@ -95,6 +95,8 @@ type Factory interface {
Refresh() error
SetClient(c Client)
Name() string
Client() (Client, bool)
}
@ -110,6 +112,10 @@ type factory struct {
client Client
}
func (f *factory) Name() string {
return f.name
}
func (f *factory) Refresh() error {
return f.refresh()
}
@ -152,7 +158,7 @@ func (f *factory) refresh() error {
cfg.RateLimiter = GetRateLimiter(f.name)
}
client, err := newClient(cfg)
client, err := newClient(f.name, cfg)
if err != nil {
return err
}
@ -180,11 +186,14 @@ type Client interface {
Arango() versioned.Interface
Monitoring() monitoring.Interface
Name() string
Config() *rest.Config
}
func NewStaticClient(kubernetes kubernetes.Interface, kubernetesExtensions apiextensionsclient.Interface, arango versioned.Interface, monitoring monitoring.Interface) Client {
return &client{
name: "static",
kubernetes: kubernetes,
kubernetesExtensions: kubernetesExtensions,
arango: arango,
@ -192,10 +201,11 @@ func NewStaticClient(kubernetes kubernetes.Interface, kubernetesExtensions apiex
}
}
func newClient(cfg *rest.Config) (*client, error) {
func newClient(name string, cfg *rest.Config) (*client, error) {
var c client
c.config = cfg
c.name = name
if q, err := kubernetes.NewForConfig(cfg); err != nil {
return nil, err
@ -225,6 +235,8 @@ func newClient(cfg *rest.Config) (*client, error) {
}
type client struct {
name string
kubernetes kubernetes.Interface
kubernetesExtensions apiextensionsclient.Interface
arango versioned.Interface
@ -232,6 +244,10 @@ type client struct {
config *rest.Config
}
func (c *client) Name() string {
return c.name
}
func (c *client) Config() *rest.Config {
return c.config
}

View file

@ -31,6 +31,8 @@ import (
apiextensionsclientFake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/discovery/fake"
kubernetesFake "k8s.io/client-go/kubernetes/fake"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
@ -38,12 +40,18 @@ import (
)
func NewFakeClient() Client {
return NewStaticClient(kubernetesFake.NewSimpleClientset(), apiextensionsclientFake.NewSimpleClientset(), versionedFake.NewSimpleClientset(), monitoringFake.NewSimpleClientset())
return NewFakeClientWithVersion(nil)
}
func NewFakeClientWithVersion(version *version.Info) Client {
return NewFakeClientBuilder().Version(version).Client()
}
type FakeClientBuilder interface {
Add(objects ...runtime.Object) FakeClientBuilder
Version(version *version.Info) FakeClientBuilder
Client() Client
}
@ -54,9 +62,16 @@ func NewFakeClientBuilder() FakeClientBuilder {
type fakeClientBuilder struct {
lock sync.Mutex
version *version.Info
all []runtime.Object
}
func (f *fakeClientBuilder) Version(version *version.Info) FakeClientBuilder {
f.version = version
return f
}
func (f *fakeClientBuilder) Add(objects ...runtime.Object) FakeClientBuilder {
f.lock.Lock()
defer f.lock.Unlock()
@ -88,8 +103,14 @@ func (f *fakeClientBuilder) filter(reg func(s *runtime.Scheme) error) []runtime.
}
func (f *fakeClientBuilder) Client() Client {
q := kubernetesFake.NewSimpleClientset(f.filter(kubernetesFake.AddToScheme)...)
if z, ok := q.Discovery().(*fake.FakeDiscovery); ok {
z.FakedServerVersion = f.version
} else {
panic("Unable to get client")
}
return NewStaticClient(
kubernetesFake.NewSimpleClientset(f.filter(kubernetesFake.AddToScheme)...),
q,
apiextensionsclientFake.NewSimpleClientset(f.filter(apiextensionsclientFake.AddToScheme)...),
versionedFake.NewSimpleClientset(f.filter(versionedFake.AddToScheme)...),
monitoringFake.NewSimpleClientset(f.filter(monitoringFake.AddToScheme)...))