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 @@ -7,6 +7,7 @@
- (Feature) Recursive OwnerReference discovery
- (Maintenance) Add check make targets
- (Feature) Create support for local variables in actions.
- (Feature) Support for asynchronous ArangoD resquests.

## [1.2.11](https://github.com/arangodb/kube-arangodb/tree/1.2.11) (2022-04-30)
- (Bugfix) Orphan PVC are not removed
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ ifdef VERBOSE
TESTVERBOSEOPTIONS := -v
endif

EXCLUDE_DIRS := tests vendor .gobuild deps tools
EXCLUDE_DIRS := vendor .gobuild deps tools
SOURCES_QUERY := find ./ -type f -name '*.go' $(foreach EXCLUDE_DIR,$(EXCLUDE_DIRS), ! -path "*/$(EXCLUDE_DIR)/*")
SOURCES := $(shell $(SOURCES_QUERY))
DASHBOARDSOURCES := $(shell find $(DASHBOARDDIR)/src -name '*.js') $(DASHBOARDDIR)/package.json
Expand Down
102 changes: 102 additions & 0 deletions pkg/util/arangod/conn/async.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//
// 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"
"path"

"github.com/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util/constants"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
)

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:
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)
}
}
}
75 changes: 75 additions & 0 deletions pkg/util/arangod/conn/async_errors.go
Original file line number Diff line number Diff line change
@@ -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 conn

import "fmt"

func IsAsyncErrorNotFound(err error) bool {
if err == nil {
return false
}

if _, ok := err.(asyncErrorNotFound); ok {
return true
}

return false
}

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 "", false
}

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)
}
78 changes: 78 additions & 0 deletions pkg/util/arangod/conn/async_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// 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/arangodb/go-driver"
"github.com/arangodb/kube-arangodb/pkg/util/tests"
"github.com/stretchr/testify/require"
)

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()
}
72 changes: 72 additions & 0 deletions pkg/util/arangod/conn/connection.pass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// 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()
}
37 changes: 37 additions & 0 deletions pkg/util/arangod/conn/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// 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)
}
Loading