8000 [Bugfix] Fix ArangoRoute Target switch in case of temporary error (#1… · arangodb/kube-arangodb@c8ed83a · GitHub
[go: up one dir, main page]

Skip to content

Commit c8ed83a

Browse files
authored
[Bugfix] Fix ArangoRoute Target switch in case of temporary error (#1804)
1 parent e8f4987 commit c8ed83a

14 files changed

+195
-72
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
- (Feature) (ML) Allow to use PlatformStorage
5252
- (Maintenance) Bump Go Image to 1.22.11
5353
- (Feature) Split Helm and KClient
54+
- (Bugfix) Fix ArangoRoute Target switch in case of temporary error
5455

5556
## EDBE [1.2.43](https://github.com/arangodb/kube-arangodb/tree/1.2.43) (2024-10-14)
5657
- (Feature) ArangoRoute CRD

pkg/handlers/networking/route/handler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2024 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2024-2025 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -23,7 +23,6 @@ package route
2323
import (
2424
"context"
2525

26-
apiErrors "k8s.io/apimachinery/pkg/api/errors"
2726
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
2827
"k8s.io/client-go/kubernetes"
2928

@@ -34,6 +33,7 @@ import (
3433
"github.com/arangodb/kube-arangodb/pkg/operatorV2/event"
3534
"github.com/arangodb/kube-arangodb/pkg/operatorV2/operation"
3635
"github.com/arangodb/kube-arangodb/pkg/util"
36+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/kerrors"
3737
)
3838

3939
var logger = logging.Global().RegisterAndGetLogger("networking-route-operator", logging.Info)
@@ -56,7 +56,7 @@ func (h *handler) Handle(ctx context.Context, item operation.Item) error {
5656

5757
object, err := util.WithKubernetesContextTimeoutP2A2(ctx, h.client.NetworkingV1alpha1().ArangoRoutes(item.Namespace).Get, item.Name, meta.GetOptions{})
5858
if err != nil {
59-
if apiErrors.IsNotFound(err) {
59+
if kerrors.IsNotFound(err) {
6060
return nil
6161
}
6262

pkg/handlers/networking/route/handler_deployment.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2024 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2024-2025 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -23,7 +23,6 @@ package route
2323
import (
2424
"context"
2525

26-
apiErrors "k8s.io/apimachinery/pkg/api/errors"
2726
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
2827

2928
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
@@ -32,6 +31,7 @@ import (
3231
operator "github.com/arangodb/kube-arangodb/pkg/operatorV2"
3332
"github.com/arangodb/kube-arangodb/pkg/operatorV2/operation"
3433
"github.com/arangodb/kube-arangodb/pkg/util"
34+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/kerrors"
3535
)
3636

3737
func (h *handler) HandleArangoDeployment(ctx context.Context, item operation.Item, extension *networkingApi.ArangoRoute, status *networkingApi.ArangoRouteStatus) (bool, error) {
@@ -43,7 +43,7 @@ func (h *handler) HandleArangoDeployment(ctx context.Context, item operation.Ite
4343

4444
deployment, err := util.WithKubernetesContextTimeoutP2A2(ctx, h.client.DatabaseV1().ArangoDeployments(item.Namespace).Get, name, meta.GetOptions{})
4545
if err != nil {
46-
if apiErrors.IsNotFound(err) {
46+
if kerrors.IsNotFound(err) {
4747
// Condition for Found should be set to false
4848
if util.Or(
4949
status.Conditions.Update(networkingApi.DeploymentFoundCondition, false, "ArangoDeployment not found", "ArangoDeployment not found"),

pkg/handlers/networking/route/handler_destination.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2024 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2024-2025 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -48,6 +48,11 @@ func (h *handler) HandleArangoDestination(ctx context.Context, item operation.It
4848

4949
func (h *handler) HandleArangoDestinationWithTargets(ctx context.Context, item operation.Item, extension *networkingApi.ArangoRoute, status *networkingApi.ArangoRouteStatus, depl *api.ArangoDeployment) (*operator.Condition, bool, error) {
5050
c, changed, err := h.HandleArangoDestination(ctx, item, extension, status, depl)
51+
52+
if operator.IsTemporary(err) {
53+
return nil, false, err
54+
}
55+
5156
if c == nil && !c.Status && status.Target != nil {
5257
status.Target = nil
5358
changed = true

pkg/handlers/networking/route/handler_destination_endpoints.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"fmt"
2626

2727
core "k8s.io/api/core/v1"
28+
kerrors "k8s.io/apimachinery/pkg/api/errors"
2829
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
2930
"k8s.io/apimachinery/pkg/util/intstr"
3031

@@ -48,19 +49,15 @@ func (h *handler) HandleArangoDestinationEndpoints(ctx context.Context, item ope
4849

4950
s, err := util.WithKubernetesContextTimeoutP2A2(ctx, h.kubeClient.CoreV1().Services(endpoints.GetNamespace(extension)).Get, endpoints.GetName(), meta.GetOptions{})
5051
if err != nil {
51-
if api.IsNotFound(err) {
52+
if kerrors.IsNotFound(err) {
5253
return &operator.Condition{
5354
Status: false,
5455
Reason: "Destination Not Found",
5556
Message: fmt.Sprintf("Service `%s/%s` Not found", endpoints.GetNamespace(extension), endpoints.GetName()),
5657
}, false, nil
5758
}
5859

59-
return &operator.Condition{
60-
Status: false,
61-
Reason: "Destination Not Found",
62-
Message: fmt.Sprintf("Unknown error for service `%s/%s`: %s", endpoints.GetNamespace(extension), endpoints.GetName(), err.Error()),
63-
}, false, nil
60+
return nil, false, operator.Temporary(err, "Unable to get service")
6461
}
6562

6663
if !endpoints.Equals(s) {
@@ -73,19 +70,15 @@ func (h *handler) HandleArangoDestinationEndpoints(ctx context.Context, item ope
7370

7471
e, err := util.WithKubernetesContextTimeoutP2A2(ctx, h.kubeClient.CoreV1().Endpoints(endpoints.GetNamespace(extension)).Get, endpoints.GetName(), meta.GetOptions{})
7572
if err != nil {
76-
if api.IsNotFound(err) {
73+
if kerrors.IsNotFound(err) {
7774
return &operator.Condition{
7875
Status: false,
7976
Reason: "Destination Not Found",
8077
Message: fmt.Sprintf("Endpoints `%s/%s` Not found", endpoints.GetNamespace(extension), endpoints.GetName()),
8178
}, false, nil
8279
}
8380

84-
return &operator.Condition{
85-
Status: false,
86-
Reason: "Destination Not Found",
87-
Message: fmt.Sprintf("Unknown error for endpoints `%s/%s`: %s", endpoints.GetNamespace(extension), endpoints.GetName(), err.Error()),
88-
}, false, nil
81+
return nil, false, operator.Temporary(err, "Unable to get endpoints")
8982
}
9083

9184
// Discover port name - empty names are allowed

pkg/handlers/networking/route/handler_destination_service.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
operator "github.com/arangodb/kube-arangodb/pkg/operatorV2"
3434
"github.com/arangodb/kube-arangodb/pkg/operatorV2/operation"
3535
"github.com/arangodb/kube-arangodb/pkg/util"
36+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/kerrors"
3637
)
3738

3839
func (h *handler) HandleArangoDestinationService(ctx context.Context, item operation.Item, extension *networkingApi.ArangoRoute, status *networkingApi.ArangoRouteStatus, deployment *api.ArangoDeployment, dest *networkingApi.ArangoRouteSpecDestination, svc *networkingApi.ArangoRouteSpecDestinationService) (*operator.Condition, bool, error) {
@@ -48,19 +49,15 @@ func (h *handler) HandleArangoDestinationService(ctx context.Context, item opera
4849

4950
s, err := util.WithKubernetesContextTimeoutP2A2(ctx, h.kubeClient.CoreV1().Services(svc.GetNamespace(extension)).Get, svc.GetName(), meta.GetOptions{})
5051
if err != nil {
51-
if api.IsNotFound(err) {
52+
if kerrors.IsNotFound(err) {
5253
return &operator.Condition{
5354
Status: false,
5455
Reason: "Destination Not Found",
5556
Message: fmt.Sprintf("Service `%s/%s` Not found", svc.GetNamespace(extension), svc.GetName()),
5657
}, false, nil
5758
}
5859

59-
return &operator.Condition{
60-
Status: false,
61-
Reason: "Destination Not Found",
62-
Message: fmt.Sprintf("Unknown error for service `%s/%s`: %s", svc.GetNamespace(extension), svc.GetName(), err.Error()),
63-
}, false, nil
60+
return nil, false, operator.Temporary(err, "Unable to get service")
6461
}
6562

6663
if !svc.Equals(s) {

pkg/handlers/networking/route/handler_destination_service_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2024 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2024-2025 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -72,7 +72,7 @@ func Test_Handler_Destination_Service_Missing(t *testing.T) {
7272
c, ok := extension.Status.Conditions.Get(networkingApi.DestinationValidCondition)
7373
require.True(t, ok)
7474
require.EqualValues(t, c.Reason, "Destination Not Found")
75-
require.EqualValues(t, c.Message, "Unknown error for service `fake/deployment`: services \"deployment\" not found")
75+
require.EqualValues(t, c.Message, "Service `fake/deployment` Not found")
7676
}
7777

7878
func Test_Handler_Destination_Service_Valid(t *testing.T) {

pkg/operatorV2/errors_reconcile.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2023-2025 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -20,7 +20,11 @@
2020

2121
package operator
2222

23-
import "fmt"
23+
import (
24+
"fmt"
25+
26+
"github.com/arangodb/kube-arangodb/pkg/util/errors"
27+
)
2428

2529
func Reconcile(msg string, args ...interface{}) error {
2630
return reconcile{
@@ -37,11 +41,7 @@ func (r reconcile) Error() string {
3741
}
3842

3943
func IsReconcile(err error) bool {
40-
if err == nil {
41-
return false
42-
}
43-
44-
if _, ok := err.(reconcile); ok {
44+
if _, ok := errors.ExtractCause[reconcile](err); ok {
4545
return true
4646
}
4747

pkg/operatorV2/errors_stop.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2023-2025 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -20,7 +20,11 @@
2020

2121
package operator
2222

23-
import "fmt"
23+
import (
24+
"fmt"
25+
26+
"github.com/arangodb/kube-arangodb/pkg/util/errors"
27+
)
2428

2529
func Stop(msg string, args ...interface{}) error {
2630
return stop{
@@ -37,11 +41,7 @@ func (r stop) Error() string {
3741
}
3842

3943
func IsStop(err error) bool {
40-
if err == nil {
41-
return false
42-
}
43-
44-
if _, ok := err.(stop); ok {
44+
if _, ok := errors.ExtractCause[stop](err); ok {
4545
return true
4646
}
4747

pkg/operatorV2/errors_temporary.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2023-2025 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+ 10000
//
20+
21+
package operator
22+
23+
import (
24+
"github.com/arangodb/kube-arangodb/pkg/util/errors"
25+
)
26+
27+
func Temporary(cause error, msg string, args ...interface{}) error {
28+
if cause == nil {
29+
return temporary{
30+
cause: errors.Errorf(msg, args...),
31+
}
32+
}
33+
34+
return temporary{
35+
cause: errors.Wrapf(cause, msg, args...),
36+
}
37+
}
38+
39+
type temporary struct {
40+
cause error
41+
}
42+
43+
func (t temporary) Error() string {
44+
return t.cause.Error()
45+
}
46+
47+
func (t temporary) Temporary() bool {
48+
return true
49+
}
50+
51+
func (t temporary) Cause() error {
52+
return t.cause
53+
}
54+
55+
func IsTemporary(err error) bool {
56+
if _, ok := errors.ExtractCause[temporary](err); ok {
57+
return true
58+
}
59+
60+
return false
61+
}

pkg/util/errors/errors.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2016-2025 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -55,7 +55,7 @@ func ExtractCause[T error](err error) (T, bool) {
5555
var d T
5656

5757
if err == nil {
58-
return d, true
58+
return d, false
5959
}
6060

6161
var v T
@@ -70,24 +70,6 @@ func ExtractCause[T error](err error) (T, bool) {
7070
return d, false
7171
}
7272

73-
func ExtractCauseHelper[T error](err error, extr func(err error) (T, bool)) (T, bool) {
74-
var d T
75-
76-
if err == nil {
77-
return d, true
78-
}
79-
80-
if v, ok := extr(err); ok {
81-
return v, true
82-
}
83-
84-
if err := CauseWithNil(err); err != nil {
85-
return ExtractCauseHelper[T](err, extr)
86-
}
87-
88-
return d, false
89-
}
90-
9173
func New(message string) error {
9274
return errors.New(message)
9375
}

0 commit comments

Comments
 (0)
0