Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit 6ee37df

Browse files
authored
Added IsTransient function to check if an error is transient or not (#309)
1 parent df0004a commit 6ee37df

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

pkg/controller/mongodb/mongodb_status_options.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mongodb
22

33
import (
44
mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/pkg/apis/mongodb/v1"
5+
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/apierrors"
56
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/result"
67
"go.uber.org/zap"
78

@@ -112,6 +113,10 @@ func (o *optionBuilder) withStatefulSetReplicas(members int) *optionBuilder {
112113
}
113114

114115
func (o *optionBuilder) withMessage(severityLevel severity, msg string) *optionBuilder {
116+
if apierrors.IsTransientMessage(msg) {
117+
severityLevel = Debug
118+
msg = ""
119+
}
115120
o.options = append(o.options, messageOption{
116121
message: message{
117122
messageString: msg,

pkg/util/apierrors/apierrors.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package apierrors
2+
3+
import "strings"
4+
5+
// objectModifiedText is an error indicating that we are trying to update a resource that has since been updated.
6+
// in this case we just want to retry but not log it as an error.
7+
var objectModifiedText = "the object has been modified; please apply your changes to the latest version and try again"
8+
9+
// IsTransientError returns a boolean indicating if a given error is transient.
10+
func IsTransientError(err error) bool {
11+
return IsTransientMessage(err.Error())
12+
}
13+
14+
// IsTransientMessage returns a boolean indicating if a given error message is transient.
15+
func IsTransientMessage(msg string) bool {
16+
return strings.Contains(strings.ToLower(msg), objectModifiedText)
17+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package apierrors
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestIsTransientError(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
err error
12+
want bool
13+
}{
14+
{
15+
"Test Transient capitalised error",
16+
fmt.Errorf("Error updating the status of the MongoDB resource: Operation cannot be fulfilled on mongodbcommunity.mongodb.com \"mdb0\": The object has been modified; please apply your changes to the latest version and try again"),
17+
true,
18+
},
19+
{
20+
"Test Transient lower case error",
21+
fmt.Errorf("error updating the status of the MongoDB resource: Operation cannot be fulfilled on mongodbcommunity.mongodb.com \"mdb0\": the object has been modified; please apply your changes to the latest version and try again"),
22+
true,
23+
},
24+
{
25+
"Test Not Transient Error",
26+
fmt.Errorf(" error found deployments.extensions \"default\" not found"),
27+
false,
28+
},
29+
}
30+
for _, tt := range tests {
31+
t.Run(tt.name, func(t *testing.T) {
32+
if got := IsTransientError(tt.err); got != tt.want {
33+
t.Errorf("IsTransientError() = %v, want %v", got, tt.want)
34+
}
35+
})
36+
}
37+
}

0 commit comments

Comments
 (0)