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

[Refactoring] Reorganize package imports (#1347)

This commit is contained in:
Nikita Vaniasin 2023-09-29 11:45:22 +02:00 committed by GitHub
parent 266ca35b5e
commit 25113b6e39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
42 changed files with 48 additions and 1430 deletions

View file

@ -3,6 +3,7 @@
## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A)
- (Bugfix) Fix make manifests-crd-file command
- (Improvement) Allow tcp:// and ssl:// protocols in endpoints for members
- (Maintenance) Reorganize package imports / move common code to separate repos
## [1.2.33](https://github.com/arangodb/kube-arangodb/tree/1.2.33) (2023-09-27)
- (Maintenance) Bump golang.org/x/net to v0.13.0

View file

@ -49,7 +49,7 @@ import (
"github.com/arangodb/kube-arangodb/pkg/api"
deploymentApi "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
"github.com/arangodb/kube-arangodb/pkg/crd"
"github.com/arangodb/kube-arangodb/pkg/deployment/agency/cache"
agencyConfig "github.com/arangodb/kube-arangodb/pkg/deployment/agency/config"
"github.com/arangodb/kube-arangodb/pkg/deployment/features"
"github.com/arangodb/kube-arangodb/pkg/deployment/reconcile"
"github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned/scheme"
@ -237,7 +237,7 @@ func init() {
if err := features.Init(&cmdMain); err != nil {
panic(err.Error())
}
if err := cache.Init(&cmdMain); err != nil {
if err := agencyConfig.Init(&cmdMain); err != nil {
panic(err.Error())
}
if err := reconcile.ActionsConfigGlobal.Init(&cmdMain); err != nil {

8
go.mod
View file

@ -24,11 +24,13 @@ replace (
require (
github.com/arangodb-helper/go-certificates v0.0.0-20180821055445-9fca24fc2680
github.com/arangodb-helper/go-helper v0.4.2
github.com/arangodb/arangosync-client v0.9.0
github.com/arangodb/go-driver v1.4.1
github.com/arangodb/go-driver/v2 v2.0.0-20211021031401-d92dcd5a4c83
github.com/arangodb/go-driver v1.6.0
github.com/arangodb/go-driver/v2 v2.0.0-20230616090327-3b9171814ae4
github.com/arangodb/go-upgrade-rules v0.0.0-20180809110947-031b4774ff21
//github.com/arangodb/rebalancer v0.1.1
//github.com/arangodb/go-agency-helper v0.3.0
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9
github.com/gin-gonic/gin v1.9.1
@ -47,7 +49,7 @@ require (
github.com/rs/zerolog v1.19.0
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.3
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.1.0
golang.org/x/sys v0.10.0
golang.org/x/text v0.11.0

7
go.sum
View file

@ -72,12 +72,14 @@ github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kd
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220418222510-f25a4f6275ed/go.mod h1:F7bn7fEU90QkQ3tnmaTx3LTKLEDqnwWODIYppRQ5hnY=
github.com/arangodb-helper/go-certificates v0.0.0-20180821055445-9fca24fc2680 h1:5YCGq0gkf/sCEkDFIsMBPj59GOm5cMibGqDBH2OWWfQ=
github.com/arangodb-helper/go-certificates v0.0.0-20180821055445-9fca24fc2680/go.mod h1:xDyzBwyYzcEhsaDXtmxCNM4p5BrtuoVYYsRTuJqmCeg=
github.com/arangodb-helper/go-helper v0.4.2 h1:Ekf8EtPYQdhlwtqJAEn17i7zbtuavksWamasxcyOGWQ=
github.com/arangodb-helper/go-helper v0.4.2/go.mod h1:RHgEwQTFWdJ9wFDGUCgUZzaz9NLaFUskSsHgOPM5XR4=
github.com/arangodb/arangosync-client v0.9.0 h1:XhY+5gGGpl9Gk8Prqmdv0SpK3HydFUXvN2CmTIKUxKI=
github.com/arangodb/arangosync-client v0.9.0/go.mod h1:kU2UaOkv2AeHLAFMEo4Ug3qLbUQVKOuRtkfr6pZqmuk=
github.com/arangodb/go-driver v1.2.1 h1:HREDHhDmzdIWxHmfkfTESbYUnRjESjPh4WUuXq7FZa8=
github.com/arangodb/go-driver v1.2.1/go.mod h1:zdDkJJnCj8DAkfbtIjIXnsTrWIiy6VhP3Vy14p+uQeY=
github.com/arangodb/go-driver/v2 v2.0.0-20211021031401-d92dcd5a4c83 h1:PCbi3alUFastUw6InBKGEXqniveJJcQuMYspubJMRS8=
github.com/arangodb/go-driver/v2 v2.0.0-20211021031401-d92dcd5a4c83/go.mod h1:B8byYwvt1mDOQzpjiMuDTP5jOif/Y5dcEJtkdvPB7HY=
github.com/arangodb/go-driver/v2 v2.0.0-20230616090327-3b9171814ae4 h1:LpIIPBcrbZ/mVDG2ioZN92Pbgb5HQ2Vnqj/UaQAyN4E=
github.com/arangodb/go-driver/v2 v2.0.0-20230616090327-3b9171814ae4/go.mod h1:bnCd24JXOgd5rpG8uwSpHPsF6NCcH4m6rKtKh1EJ7Lo=
github.com/arangodb/go-upgrade-rules v0.0.0-20180809110947-031b4774ff21 h1:+W7D5ttxi/Ygh/39vialtypE23p9KI7P0J2qtoqUV4w=
github.com/arangodb/go-upgrade-rules v0.0.0-20180809110947-031b4774ff21/go.mod h1:RkPIG6JJ2pcJUoymc18NxAJGraZd+iAEVnOTDjZey/w=
github.com/arangodb/go-velocypack v0.0.0-20200318135517-5af53c29c67e h1:Xg+hGrY2LcQBbxd0ZFdbGSyRKTYMZCfBbw/pMJFOk1g=
@ -190,7 +192,6 @@ github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vb
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=

View file

@ -27,12 +27,13 @@ import (
"github.com/rs/zerolog"
agencyCache "github.com/arangodb-helper/go-helper/pkg/arangod/agency/cache"
"github.com/arangodb-helper/go-helper/pkg/arangod/conn"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
agencyCache "github.com/arangodb/kube-arangodb/pkg/deployment/agency/cache"
"github.com/arangodb/kube-arangodb/pkg/deployment/agency/state"
"github.com/arangodb/kube-arangodb/pkg/generated/metric_descriptions"
"github.com/arangodb/kube-arangodb/pkg/logging"
"github.com/arangodb/kube-arangodb/pkg/util/arangod/conn"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/metrics"

View file

@ -1,43 +0,0 @@
//
// DISCLAIMER
//
// Copyright 2023 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 cache
import (
"context"
"time"
"github.com/arangodb/kube-arangodb/pkg/util/arangod/conn"
)
type LeaderDiscovery interface {
Discover(ctx context.Context) (conn.Connection, error)
}
type StateLoader[T interface{}] interface {
State() (*T, uint64, bool)
Invalidate()
Valid() bool
UpdateTime() time.Time
Refresh(ctx context.Context, discovery LeaderDiscovery) error
}

View file

@ -24,7 +24,8 @@ import (
"context"
"net/http"
"github.com/arangodb/kube-arangodb/pkg/util/arangod/conn"
"github.com/arangodb-helper/go-helper/pkg/arangod/conn"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
)

View file

@ -18,7 +18,7 @@
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package cache
package config
import (
"time"

View file

@ -23,8 +23,8 @@ package agency
import (
"context"
agencyCache "github.com/arangodb/kube-arangodb/pkg/deployment/agency/cache"
"github.com/arangodb/kube-arangodb/pkg/util/arangod/conn"
agencyCache "github.com/arangodb-helper/go-helper/pkg/arangod/agency/cache"
"github.com/arangodb-helper/go-helper/pkg/arangod/conn"
)
func StaticLeaderDiscovery(in conn.Connection) agencyCache.LeaderDiscovery {

View file

@ -22,7 +22,7 @@
package agency
import agencyCache "github.com/arangodb/kube-arangodb/pkg/deployment/agency/cache"
import agencyCache "github.com/arangodb-helper/go-helper/pkg/arangod/agency/cache"
func getLoaderBase[T interface{}]() agencyCache.StateLoader[T] {
return NewSimpleStateLoader[T]()

View file

@ -25,7 +25,9 @@ import (
"sync"
"time"
agencyCache "github.com/arangodb/kube-arangodb/pkg/deployment/agency/cache"
agencyCache "github.com/arangodb-helper/go-helper/pkg/arangod/agency/cache"
agencyConfig "github.com/arangodb/kube-arangodb/pkg/deployment/agency/config"
"github.com/arangodb/kube-arangodb/pkg/util/globals"
)
@ -36,25 +38,14 @@ func getLoader[T interface{}]() agencyCache.StateLoader[T] {
loader = InvalidateOnErrorLoader[T](loader)
loader = DelayLoader[T](loader, agencyCache.GlobalConfig().RefreshDelay)
loader = RefreshLoader[T](loader, agencyCache.GlobalConfig().RefreshInterval)
loader = DelayLoader[T](loader, agencyConfig.GlobalConfig().RefreshDelay)
loader = RefreshLoader[T](loader, agencyConfig.GlobalConfig().RefreshInterval)
loader = RetryLoader[T](loader, agencyCache.GlobalConfig().Retries)
loader = RetryLoader[T](loader, agencyConfig.GlobalConfig().Retries)
return loader
}
type StateLoader[T interface{}] interface {
State() (*T, uint64, bool)
Invalidate()
Valid() bool
UpdateTime() time.Time
Refresh(ctx context.Context, discovery agencyCache.LeaderDiscovery) error
}
func NewSimpleStateLoader[T interface{}]() agencyCache.StateLoader[T] {
return &simpleStateLoader[T]{}
}

View file

@ -25,7 +25,7 @@ import (
"sync"
"time"
agencyCache "github.com/arangodb/kube-arangodb/pkg/deployment/agency/cache"
agencyCache "github.com/arangodb-helper/go-helper/pkg/arangod/agency/cache"
)
func DelayLoader[T interface{}](loader agencyCache.StateLoader[T], delay time.Duration) agencyCache.StateLoader[T] {

View file

@ -25,7 +25,7 @@ import (
"sync"
"time"
agencyCache "github.com/arangodb/kube-arangodb/pkg/deployment/agency/cache"
agencyCache "github.com/arangodb-helper/go-helper/pkg/arangod/agency/cache"
)
func InvalidateOnErrorLoader[T interface{}](loader agencyCache.StateLoader[T]) agencyCache.StateLoader[T] {

View file

@ -25,7 +25,7 @@ import (
"sync"
"time"
agencyCache "github.com/arangodb/kube-arangodb/pkg/deployment/agency/cache"
agencyCache "github.com/arangodb-helper/go-helper/pkg/arangod/agency/cache"
)
func RefreshLoader[T interface{}](loader agencyCache.StateLoader[T], delay time.Duration) agencyCache.StateLoader[T] {

View file

@ -25,7 +25,7 @@ import (
"sync"
"time"
agencyCache "github.com/arangodb/kube-arangodb/pkg/deployment/agency/cache"
agencyCache "github.com/arangodb-helper/go-helper/pkg/arangod/agency/cache"
)
func RetryLoader[T interface{}](loader agencyCache.StateLoader[T], retries int) agencyCache.StateLoader[T] {

View file

@ -24,7 +24,7 @@ import (
"context"
"time"
agencyCache "github.com/arangodb/kube-arangodb/pkg/deployment/agency/cache"
agencyCache "github.com/arangodb-helper/go-helper/pkg/arangod/agency/cache"
)
func TimeoutLoader[T interface{}](loader agencyCache.StateLoader[T], timeout time.Duration) agencyCache.StateLoader[T] {

View file

@ -24,7 +24,8 @@ import (
"context"
"net/http"
"github.com/arangodb/kube-arangodb/pkg/util/arangod/conn"
"github.com/arangodb-helper/go-helper/pkg/arangod/conn"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
)

View file

@ -26,6 +26,7 @@ import (
"strconv"
"sync"
"github.com/arangodb-helper/go-helper/pkg/arangod/conn"
driver "github.com/arangodb/go-driver"
"github.com/arangodb/go-driver/agency"
@ -33,7 +34,6 @@ import (
"github.com/arangodb/kube-arangodb/pkg/apis/shared"
"github.com/arangodb/kube-arangodb/pkg/deployment/reconciler"
"github.com/arangodb/kube-arangodb/pkg/handlers/utils"
"github.com/arangodb/kube-arangodb/pkg/util/arangod/conn"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
)

View file

@ -34,6 +34,7 @@ import (
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/arangodb-helper/go-helper/pkg/arangod/conn"
"github.com/arangodb/arangosync-client/client"
driver "github.com/arangodb/go-driver"
"github.com/arangodb/go-driver/agency"
@ -52,7 +53,6 @@ import (
"github.com/arangodb/kube-arangodb/pkg/deployment/resources"
"github.com/arangodb/kube-arangodb/pkg/operator/scope"
"github.com/arangodb/kube-arangodb/pkg/replication"
"github.com/arangodb/kube-arangodb/pkg/util/arangod/conn"
"github.com/arangodb/kube-arangodb/pkg/util/constants"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/arangodb/kube-arangodb/pkg/util/globals"

View file

@ -33,6 +33,7 @@ import (
"k8s.io/client-go/informers"
"k8s.io/client-go/tools/record"
"github.com/arangodb-helper/go-helper/pkg/arangod/conn"
"github.com/arangodb/arangosync-client/client"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
@ -54,7 +55,6 @@ import (
"github.com/arangodb/kube-arangodb/pkg/operator/scope"
"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/arangodb/kube-arangodb/pkg/util/arangod"
"github.com/arangodb/kube-arangodb/pkg/util/arangod/conn"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/arangodb/kube-arangodb/pkg/util/globals"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"

View file

@ -38,6 +38,7 @@ import (
"k8s.io/client-go/kubernetes/fake"
recordfake "k8s.io/client-go/tools/record"
"github.com/arangodb-helper/go-helper/pkg/arangod/conn"
driver "github.com/arangodb/go-driver"
"github.com/arangodb/go-driver/jwt"
@ -50,7 +51,6 @@ import (
"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"
"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/inspector/throttle"

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
// Copyright 2016-2023 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.
@ -24,11 +24,11 @@ import (
"context"
"time"
"github.com/arangodb-helper/go-helper/pkg/arangod/conn"
"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/util/arangod/conn"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/arangodb/kube-arangodb/pkg/util/globals"
)

View file

@ -26,11 +26,11 @@ import (
"path"
"time"
"github.com/arangodb-helper/go-helper/pkg/arangod/conn"
"github.com/arangodb/go-driver"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
"github.com/arangodb/kube-arangodb/pkg/deployment/features"
"github.com/arangodb/kube-arangodb/pkg/util/arangod/conn"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
)

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
// Copyright 2016-2023 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.
@ -23,12 +23,13 @@ package reconcile
import (
"context"
"github.com/arangodb-helper/go-helper/pkg/arangod/conn"
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/acs/sutil"
"github.com/arangodb/kube-arangodb/pkg/deployment/member"
"github.com/arangodb/kube-arangodb/pkg/deployment/reconciler"
"github.com/arangodb/kube-arangodb/pkg/util/arangod/conn"
)
type CreateMemberMod func(s *api.DeploymentStatus, g api.ServerGroup, m *api.MemberStatus) error

View file

@ -1,7 +1,7 @@
//
// DISCLAIMER
//
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
// Copyright 2016-2023 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.
@ -23,11 +23,12 @@ package reconcile
import (
"context"
"github.com/arangodb-helper/go-helper/pkg/arangod/conn"
backupApi "github.com/arangodb/kube-arangodb/pkg/apis/backup/v1"
"github.com/arangodb/kube-arangodb/pkg/deployment/acs/sutil"
"github.com/arangodb/kube-arangodb/pkg/deployment/member"
"github.com/arangodb/kube-arangodb/pkg/deployment/reconciler"
"github.com/arangodb/kube-arangodb/pkg/util/arangod/conn"
)
// PlanBuilderContext contains context methods provided to plan builders.

View file

@ -34,6 +34,7 @@ import (
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/arangodb-helper/go-helper/pkg/arangod/conn"
"github.com/arangodb/arangosync-client/client"
"github.com/arangodb/go-driver"
"github.com/arangodb/go-driver/agency"
@ -54,7 +55,6 @@ import (
"github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned"
"github.com/arangodb/kube-arangodb/pkg/logging"
"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/arangodb/kube-arangodb/pkg/util/arangod/conn"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/arangodb/kube-arangodb/pkg/util/errors/panics"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"

View file

@ -1,112 +0,0 @@
//
// DISCLAIMER
//
// Copyright 2016-2023 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 conn
import (
"context"
"net/http"
"path"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util/constants"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
)
type ConnectionWrap func(c driver.Connection) driver.Connection
func NewAsyncConnection(c driver.Connection) driver.Connection {
return async{
connectionPass: connectionPass{
c: c,
wrap: asyncConnectionWrap,
},
}
}
func asyncConnectionWrap(c driver.Connection) (driver.Connection, error) {
return NewAsyncConnection(c), nil
}
type async struct {
connectionPass
}
func (a async) isAsyncIDSet(ctx context.Context) (string, bool) {
if ctx != nil {
if q := ctx.Value(asyncOperatorContextKey); q != nil {
if v, ok := q.(string); ok {
return v, true
}
}
}
return "", false
}
func (a async) Do(ctx context.Context, req driver.Request) (driver.Response, error) {
if id, ok := a.isAsyncIDSet(ctx); ok {
// We have ID Set, request should be done to fetch job id
req, err := a.c.NewRequest(http.MethodPut, path.Join("/_api/job", id))
if err != nil {
return nil, err
}
resp, err := a.c.Do(ctx, req)
if err != nil {
return nil, err
}
switch resp.StatusCode() {
case http.StatusNotFound:
return nil, newAsyncErrorNotFound(id)
case http.StatusNoContent:
asyncID := resp.Header(constants.ArangoHeaderAsyncIDKey)
if asyncID == id {
// Job is done
return resp, nil
}
// Job is in progress
return nil, newAsyncJobInProgress(id)
default:
return resp, nil
}
} else {
req.SetHeader(constants.ArangoHeaderAsyncKey, constants.ArangoHeaderAsyncValue)
resp, err := a.c.Do(ctx, req)
if err != nil {
return nil, err
}
switch resp.StatusCode() {
case http.StatusAccepted:
if v := resp.Header(constants.ArangoHeaderAsyncIDKey); len(v) == 0 {
return nil, errors.Newf("Missing async key response")
} else {
return nil, newAsyncJobInProgress(v)
}
default:
return nil, resp.CheckStatus(http.StatusAccepted)
}
}
}

View file

@ -1,79 +0,0 @@
//
// 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 conn
import (
"fmt"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
)
func IsAsyncErrorNotFound(err error) bool {
if err == nil {
return false
}
if _, ok := err.(asyncErrorNotFound); ok {
return true
}
return IsAsyncErrorNotFound(errors.CauseWithNil(err))
}
func newAsyncErrorNotFound(id string) error {
return asyncErrorNotFound{
jobID: id,
}
}
type asyncErrorNotFound struct {
jobID string
}
func (a asyncErrorNotFound) Error() string {
return fmt.Sprintf("Job with ID %s not found", a.jobID)
}
func IsAsyncJobInProgress(err error) (string, bool) {
if err == nil {
return "", false
}
if v, ok := err.(asyncJobInProgress); ok {
return v.jobID, true
}
return IsAsyncJobInProgress(errors.CauseWithNil(err))
}
func newAsyncJobInProgress(id string) error {
return asyncJobInProgress{
jobID: id,
}
}
type asyncJobInProgress struct {
jobID string
}
func (a asyncJobInProgress) Error() string {
return fmt.Sprintf("Job with ID %s in progress", a.jobID)
}

View file

@ -1,38 +0,0 @@
//
// 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 conn
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
)
func Test_IsAsyncErrorNotFound_Loop(t *testing.T) {
require.False(t, IsAsyncErrorNotFound(errors.Newf("Error")))
}
func Test_IsAsyncJobInProgress_Loop(t *testing.T) {
_, ok := IsAsyncJobInProgress(errors.Newf("Error"))
require.False(t, ok)
}

View file

@ -1,80 +0,0 @@
//
// 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 conn
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/require"
"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util/tests"
)
func Test_Async(t *testing.T) {
s := tests.NewServer(t)
c := s.NewConnection()
c = NewAsyncConnection(c)
client, err := driver.NewClient(driver.ClientConfig{
Connection: c,
})
require.NoError(t, err)
a := tests.NewAsyncHandler(t, s, http.MethodGet, "/_api/version", http.StatusOK, driver.VersionInfo{
Server: "foo",
Version: "",
License: "",
Details: nil,
})
a.Start()
_, err = client.Version(context.Background())
require.Error(t, err)
id, ok := IsAsyncJobInProgress(err)
require.True(t, ok)
require.Equal(t, a.ID(), id)
a.InProgress()
ctx := WithAsyncID(context.TODO(), a.ID())
_, err = client.Version(ctx)
require.Error(t, err)
id, ok = IsAsyncJobInProgress(err)
require.True(t, ok)
require.Equal(t, a.ID(), id)
a.Done()
v, err := client.Version(ctx)
require.NoError(t, err)
require.Equal(t, v.Server, "foo")
defer s.Stop()
}

View file

@ -1,73 +0,0 @@
//
// DISCLAIMER
//
// Copyright 2023 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 conn
import (
"context"
"github.com/arangodb/go-driver"
)
func NewClosedConnection() driver.Connection {
return closedConnection{}
}
func newClosedConnectionError() error {
return closedConnectionError{}
}
type closedConnectionError struct {
}
func (c closedConnectionError) Error() string {
return "Connection Closed"
}
type closedConnection struct {
}
func (c closedConnection) NewRequest(method, path string) (driver.Request, error) {
return nil, newClosedConnectionError()
}
func (c closedConnection) Do(ctx context.Context, req driver.Request) (driver.Response, error) {
return nil, newClosedConnectionError()
}
func (c closedConnection) Unmarshal(data driver.RawObject, result interface{}) error {
return newClosedConnectionError()
}
func (c closedConnection) Endpoints() []string {
return nil
}
func (c closedConnection) UpdateEndpoints(endpoints []string) error {
return newClosedConnectionError()
}
func (c closedConnection) SetAuthentication(authentication driver.Authentication) (driver.Connection, error) {
return c, nil
}
func (c closedConnection) Protocols() driver.ProtocolSet {
return nil
}

View file

@ -1,83 +0,0 @@
//
// DISCLAIMER
//
// Copyright 2023 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 conn
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"reflect"
"github.com/arangodb/kube-arangodb/pkg/util/metrics/nctx"
)
func NewExecutor[IN, OUT interface{}](conn Connection) Executor[IN, OUT] {
return executor[IN, OUT]{
conn: conn,
}
}
type executor[IN, OUT interface{}] struct {
conn Connection
}
func (e executor[IN, OUT]) ExecuteGet(ctx context.Context, endpoint string) (*OUT, int, error) {
var t IN
return e.Execute(ctx, http.MethodGet, endpoint, t)
}
func (e executor[IN, OUT]) Execute(ctx context.Context, method string, endpoint string, in IN) (*OUT, int, error) {
var reader io.Reader
if q := reflect.ValueOf(in); q.IsValid() && !q.IsZero() && !q.IsNil() {
data, err := json.Marshal(in)
if err != nil {
return nil, 0, err
}
reader = bytes.NewReader(data)
}
resp, code, err := e.conn.Execute(ctx, method, endpoint, reader)
if err != nil {
return nil, 0, err
}
if resp == nil {
return nil, code, nil
}
defer resp.Close()
var out OUT
if err := json.NewDecoder(nctx.WithRequestReadBytes(ctx, resp)).Decode(&out); err != nil {
return nil, 0, err
}
return &out, code, err
}
type Executor[IN, OUT interface{}] interface {
ExecuteGet(ctx context.Context, endpoint string) (*OUT, int, error)
Execute(ctx context.Context, method string, endpoint string, in IN) (*OUT, int, error)
}

View file

@ -1,64 +0,0 @@
//
// DISCLAIMER
//
// Copyright 2023 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 conn
import (
"context"
"fmt"
"io"
"net/http"
)
type Connection interface {
Execute(ctx context.Context, method string, endpoint string, body io.Reader) (io.ReadCloser, int, error)
}
type connection struct {
client *http.Client
auth *string
host string
}
func (c connection) Execute(ctx context.Context, method string, endpoint string, body io.Reader) (io.ReadCloser, int, error) {
req, err := http.NewRequest(method, fmt.Sprintf("%s%s", c.host, endpoint), body)
if err != nil {
return nil, 0, err
}
req = req.WithContext(ctx)
if a := c.auth; a != nil {
req.Header.Add("Authorization", *a)
}
resp, err := c.client.Do(req)
if err != nil {
return nil, 0, err
}
if b := resp.Body; b != nil {
return b, resp.StatusCode, nil
}
return nil, resp.StatusCode, nil
}

View file

@ -1,72 +0,0 @@
//
// 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 conn
import (
"context"
"github.com/arangodb/go-driver"
)
type connectionWrap func(c driver.Connection) (driver.Connection, error)
var _ driver.Connection = connectionPass{}
type connectionPass struct {
c driver.Connection
wrap connectionWrap
}
func (c connectionPass) NewRequest(method, path string) (driver.Request, error) {
return c.c.NewRequest(method, path)
}
func (c connectionPass) Do(ctx context.Context, req driver.Request) (driver.Response, error) {
return c.c.Do(ctx, req)
}
func (c connectionPass) Unmarshal(data driver.RawObject, result interface{}) error {
return c.c.Unmarshal(data, result)
}
func (c connectionPass) Endpoints() []string {
return c.c.Endpoints()
}
func (c connectionPass) UpdateEndpoints(endpoints []string) error {
return c.c.UpdateEndpoints(endpoints)
}
func (c connectionPass) SetAuthentication(authentication driver.Authentication) (driver.Connection, error) {
newC, err := c.c.SetAuthentication(authentication)
if err != nil {
return nil, err
}
if f := c.wrap; f != nil {
return f(newC)
}
return newC, nil
}
func (c connectionPass) Protocols() driver.ProtocolSet {
return c.c.Protocols()
}

View file

@ -1,37 +0,0 @@
//
// 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 conn
import "context"
type ContextKey string
const (
asyncOperatorContextKey ContextKey = "operator-async-id"
)
func WithAsyncID(ctx context.Context, id string) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, asyncOperatorContextKey, id)
}

View file

@ -1,184 +0,0 @@
//
// DISCLAIMER
//
// Copyright 2016-2023 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
// Adam Janikowski
//
package conn
import (
http2 "net/http"
"github.com/arangodb/go-driver"
"github.com/arangodb/go-driver/agency"
"github.com/arangodb/go-driver/http"
"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
)
type Auth func() (driver.Authentication, error)
type Config func() (http.ConnectionConfig, error)
type Factory interface {
Connection(hosts ...string) (driver.Connection, error)
AgencyConnection(hosts ...string) (driver.Connection, error)
Client(hosts ...string) (driver.Client, error)
Agency(hosts ...string) (agency.Agency, error)
RawConnection(host string) (Connection, error)
GetAuth() Auth
}
func NewFactory(auth Auth, config Config) Factory {
return &factory{
auth: auth,
config: config,
}
}
type factory struct {
auth Auth
config Config
}
func (f factory) RawConnection(host string) (Connection, error) {
cfg, err := f.config()
if err != nil {
return nil, err
}
var authString *string
if f.auth != nil {
auth, err := f.auth()
if err != nil {
return nil, err
}
if auth != nil {
if auth.Type() != driver.AuthenticationTypeRaw {
return nil, errors.Newf("Only RAW Authentication is supported")
}
authString = util.NewType(auth.Get("value"))
}
}
return connection{
auth: authString,
host: host,
client: &http2.Client{
Transport: cfg.Transport,
CheckRedirect: func(req *http2.Request, via []*http2.Request) error {
return http2.ErrUseLastResponse
},
},
}, nil
}
func (f factory) GetAuth() Auth {
return f.auth
}
func (f factory) AgencyConnection(hosts ...string) (driver.Connection, error) {
cfg, err := f.config()
if err != nil {
return nil, err
}
cfg.Endpoints = hosts
conn, err := agency.NewAgencyConnection(cfg)
if err != nil {
return nil, err
}
if f.auth == nil {
return conn, nil
}
auth, err := f.auth()
if err != nil {
return nil, err
}
if auth == nil {
return conn, nil
}
return conn.SetAuthentication(auth)
}
func (f factory) Client(hosts ...string) (driver.Client, error) {
conn, err := f.Connection(hosts...)
if err != nil {
return nil, err
}
config := driver.ClientConfig{
Connection: conn,
}
if f.auth != nil {
auth, err := f.auth()
if err != nil {
return nil, err
}
if auth != nil {
config.Authentication = auth
}
}
return driver.NewClient(config)
}
func (f factory) Agency(hosts ...string) (agency.Agency, error) {
conn, err := f.AgencyConnection(hosts...)
if err != nil {
return nil, err
}
return agency.NewAgency(conn)
}
func (f factory) Connection(hosts ...string) (driver.Connection, error) {
cfg, err := f.config()
if err != nil {
return nil, err
}
cfg.Endpoints = hosts
conn, err := http.NewConnection(cfg)
if err != nil {
return nil, err
}
if f.auth == nil {
return conn, nil
}
auth, err := f.auth()
if err != nil {
return nil, err
}
if auth == nil {
return conn, nil
}
return conn.SetAuthentication(auth)
}

View file

@ -1,45 +0,0 @@
//
// DISCLAIMER
//
// Copyright 2023 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 nctx
import (
"sync"
)
type Counter struct {
lock sync.Mutex
value uint64
}
func (c *Counter) Get() uint64 {
c.lock.Lock()
defer c.lock.Unlock()
return c.value
}
func (c *Counter) add(v uint64) {
c.lock.Lock()
defer c.lock.Unlock()
c.value += v
}

View file

@ -1,65 +0,0 @@
//
// DISCLAIMER
//
// Copyright 2023 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 nctx
import (
"context"
"io"
)
const RequestReadBytesKey Key = "operator.requestReadBytes"
func (c *Counter) WithRequestReadBytes(ctx context.Context) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, RequestReadBytesKey, c)
}
type requestReadBytes struct {
c *Counter
in io.Reader
}
func (r requestReadBytes) Read(p []byte) (n int, err error) {
n, err = r.in.Read(p)
r.c.add(uint64(n))
return
}
func WithRequestReadBytes(ctx context.Context, reader io.Reader) io.Reader {
v := ctx.Value(RequestReadBytesKey)
if v == nil {
return reader
}
z, ok := v.(*Counter)
if !ok {
return reader
}
return requestReadBytes{
c: z,
in: reader,
}
}

View file

@ -1,53 +0,0 @@
//
// DISCLAIMER
//
// Copyright 2023 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 nctx
import (
"bytes"
"context"
"io"
"testing"
"github.com/stretchr/testify/require"
)
func counterRequestReadMock(t *testing.T, ctx context.Context, data io.Reader) int {
data = WithRequestReadBytes(ctx, data)
dz, err := io.ReadAll(data)
require.NoError(t, err)
return len(dz)
}
func Test_Counter_RequestRead(t *testing.T) {
data := make([]byte, 64)
var c Counter
t.Run("Read without wrapper", func(t *testing.T) {
require.EqualValues(t, 64, counterRequestReadMock(t, context.Background(), bytes.NewReader(data)))
require.EqualValues(t, 0, c.Get())
})
t.Run("Read with wrapper", func(t *testing.T) {
require.EqualValues(t, 64, counterRequestReadMock(t, c.WithRequestReadBytes(context.Background()), bytes.NewReader(data)))
require.EqualValues(t, 64, c.Get())
})
}

View file

@ -1,29 +0,0 @@
//
// DISCLAIMER
//
// Copyright 2023 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 nctx
import "context"
type Key string
type With interface {
With(ctx context.Context) context.Context
}

View file

@ -1,210 +0,0 @@
//
// 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 tests
import (
"encoding/json"
"fmt"
"net"
"net/http"
"sync"
"testing"
"github.com/stretchr/testify/require"
"github.com/arangodb/go-driver"
httpdriver "github.com/arangodb/go-driver/http"
)
func NewServer(t *testing.T) Server {
s := &server{
t: t,
stop: make(chan struct{}),
stopped: make(chan struct{}),
started: make(chan struct{}),
done: make(chan struct{}),
}
go s.run()
<-s.started
return s
}
type Server interface {
NewConnection() driver.Connection
NewClient() driver.Client
Handle(f http.HandlerFunc)
Addr() string
Stop()
}
type server struct {
lock sync.Mutex
t *testing.T
handlers []http.HandlerFunc
port int
stop, stopped, started, done chan struct{}
}
func (s *server) NewClient() driver.Client {
c, err := driver.NewClient(driver.ClientConfig{
Connection: s.NewConnection(),
})
require.NoError(s.t, err)
return c
}
func (s *server) NewConnection() driver.Connection {
c, err := httpdriver.NewConnection(httpdriver.ConnectionConfig{
Endpoints: []string{
s.Addr(),
},
ContentType: driver.ContentTypeJSON,
})
require.NoError(s.t, err)
return c
}
func (s *server) Handle(f http.HandlerFunc) {
s.lock.Lock()
defer s.lock.Unlock()
s.handlers = append(s.handlers, f)
}
func (s *server) Addr() string {
return fmt.Sprintf("http://127.0.0.1:%d", s.port)
}
func (s *server) Stop() {
s.lock.Lock()
defer s.lock.Unlock()
close(s.stop)
<-s.done
if q := len(s.handlers); q > 0 {
require.Failf(s.t, "Pending messages", "Count %d", q)
}
}
func (s *server) run() {
defer close(s.done)
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(s.t, err)
s.port = listener.Addr().(*net.TCPAddr).Port
m := http.NewServeMux()
m.HandleFunc("/", s.handle)
server := http.Server{
Handler: m,
}
var serverErr error
go func() {
defer close(s.stopped)
close(s.started)
go func() {
<-s.stop
require.NoError(s.t, server.Close())
}()
serverErr = server.Serve(listener)
}()
<-s.stopped
if serverErr != http.ErrServerClosed {
require.NoError(s.t, serverErr)
}
}
func (s *server) handle(writer http.ResponseWriter, request *http.Request) {
s.lock.Lock()
defer s.lock.Unlock()
var handler http.HandlerFunc
switch len(s.handlers) {
case 0:
require.Fail(s.t, "No pending messages")
case 1:
handler = s.handlers[0]
s.handlers = nil
default:
handler = s.handlers[0]
s.handlers = s.handlers[1:]
}
handler(writer, request)
}
func NewSimpleHandler(t *testing.T, method string, path string, resp func(t *testing.T) (int, interface{})) http.HandlerFunc {
return NewCustomRequestHandler(t, method, path, nil, nil, resp)
}
func NewCustomRequestHandler(t *testing.T, method string, path string, reqVerify func(t *testing.T, r *http.Request), respHeaders func(t *testing.T) map[string]string, resp func(t *testing.T) (int, interface{})) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
require.Equal(t, method, request.Method)
require.Equal(t, path, request.RequestURI)
if reqVerify != nil {
reqVerify(t, request)
}
code, r := resp(t)
writer.Header().Add("content-type", "application/json")
if respHeaders != nil {
h := respHeaders(t)
for k, v := range h {
writer.Header().Add(k, v)
}
}
writer.WriteHeader(code)
if r != nil {
d, err := json.Marshal(r)
require.NoError(t, err)
s, err := writer.Write(d)
require.NoError(t, err)
require.Equal(t, len(d), s)
}
}
}

View file

@ -1,115 +0,0 @@
//
// 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 tests
import (
"fmt"
"net/http"
"testing"
"github.com/dchest/uniuri"
"github.com/stretchr/testify/require"
"github.com/arangodb/kube-arangodb/pkg/util/constants"
)
func NewAsyncHandler(t *testing.T, s Server, method string, path string, retCode int, ret interface{}) AsyncHandler {
return &asyncHandler{
t: t,
s: s,
ret: ret,
retCode: retCode,
method: method,
path: path,
id: uniuri.NewLen(32),
}
}
type AsyncHandler interface {
ID() string
Start()
InProgress()
Missing()
Done()
}
type asyncHandler struct {
t *testing.T
s Server
ret interface{}
retCode int
method, path string
id string
}
func (a *asyncHandler) Missing() {
p := fmt.Sprintf("/_api/job/%s", a.id)
a.s.Handle(NewCustomRequestHandler(a.t, http.MethodPut, p, func(t *testing.T, r *http.Request) {
v := r.Header.Get(constants.ArangoHeaderAsyncKey)
require.Equal(t, "", v)
}, nil, func(t *testing.T) (int, interface{}) {
return http.StatusNotFound, nil
}))
}
func (a *asyncHandler) Start() {
a.s.Handle(NewCustomRequestHandler(a.t, a.method, a.path, func(t *testing.T, r *http.Request) {
v := r.Header.Get(constants.ArangoHeaderAsyncKey)
require.Equal(t, constants.ArangoHeaderAsyncValue, v)
}, func(t *testing.T) map[string]string {
return map[string]string{
constants.ArangoHeaderAsyncIDKey: a.id,
}
}, func(t *testing.T) (int, interface{}) {
return http.StatusAccepted, nil
}))
}
func (a *asyncHandler) InProgress() {
p := fmt.Sprintf("/_api/job/%s", a.id)
a.s.Handle(NewCustomRequestHandler(a.t, http.MethodPut, p, func(t *testing.T, r *http.Request) {
v := r.Header.Get(constants.ArangoHeaderAsyncKey)
require.Equal(t, "", v)
}, nil, func(t *testing.T) (int, interface{}) {
return http.StatusNoContent, nil
}))
}
func (a *asyncHandler) Done() {
p := fmt.Sprintf("/_api/job/%s", a.id)
a.s.Handle(NewCustomRequestHandler(a.t, http.MethodPut, p, func(t *testing.T, r *http.Request) {
v := r.Header.Get(constants.ArangoHeaderAsyncKey)
require.Equal(t, "", v)
}, nil, func(t *testing.T) (int, interface{}) {
return a.retCode, a.ret
}))
}
func (a *asyncHandler) ID() string {
return a.id
}