mirror of
https://github.com/rancher/norman.git
synced 2025-09-01 15:18:20 +00:00
add method to compare if error has tmp file path
add a method to check if the error message contains a tmp file path. It is to prevent updating resource's condition forever and causing the controller flapping. https://github.com/rancher/rancher/issues/15103
This commit is contained in:
committed by
Craig Jellick
parent
4bcc025ae3
commit
643868f4a6
@@ -2,6 +2,7 @@ package condition
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -12,6 +13,8 @@ import (
|
|||||||
|
|
||||||
type Cond string
|
type Cond string
|
||||||
|
|
||||||
|
var temfileRegexp = regexp.MustCompile("/tmp/[-_a-zA-Z0-9]+")
|
||||||
|
|
||||||
func (c Cond) True(obj runtime.Object) {
|
func (c Cond) True(obj runtime.Object) {
|
||||||
setStatus(obj, string(c), "True")
|
setStatus(obj, string(c), "True")
|
||||||
}
|
}
|
||||||
@@ -133,9 +136,14 @@ func (c Cond) do(obj runtime.Object, f func() (runtime.Object, error)) (runtime.
|
|||||||
|
|
||||||
// This is to prevent non stop flapping of states and update
|
// This is to prevent non stop flapping of states and update
|
||||||
if status == c.GetStatus(obj) &&
|
if status == c.GetStatus(obj) &&
|
||||||
reason == c.GetReason(obj) &&
|
reason == c.GetReason(obj) {
|
||||||
message == c.GetMessage(obj) {
|
if message != c.GetMessage(obj) {
|
||||||
c.LastUpdated(obj, ts)
|
replaced := temfileRegexp.ReplaceAllString(c.GetMessage(obj), "file_path_redacted")
|
||||||
|
c.Message(obj, replaced)
|
||||||
|
}
|
||||||
|
if message == c.GetMessage(obj) {
|
||||||
|
c.LastUpdated(obj, ts)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return obj, err
|
return obj, err
|
||||||
|
19
condition/condition_test.go
Normal file
19
condition/condition_test.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package condition
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestRegexp(t *testing.T) {
|
||||||
|
testInputs := []string{
|
||||||
|
"create file /tmp/file-uy76324 ",
|
||||||
|
"/tmp/file-y6123tsd",
|
||||||
|
}
|
||||||
|
testOutputs := []string{
|
||||||
|
"create file file_path_redacted ",
|
||||||
|
"file_path_redacted",
|
||||||
|
}
|
||||||
|
for i, s := range testInputs {
|
||||||
|
if testOutputs[i] != temfileRegexp.ReplaceAllString(s, "file_path_redacted") {
|
||||||
|
t.Fatalf("Regexp failed to check %s", testInputs[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user