1
0
mirror of https://github.com/rancher/norman.git synced 2025-04-28 03:20:08 +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:
Daishan Peng 2018-08-16 18:06:52 -07:00 committed by Craig Jellick
parent 4bcc025ae3
commit 643868f4a6
2 changed files with 30 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package condition
import (
"reflect"
"regexp"
"time"
"github.com/pkg/errors"
@ -12,6 +13,8 @@ import (
type Cond string
var temfileRegexp = regexp.MustCompile("/tmp/[-_a-zA-Z0-9]+")
func (c Cond) True(obj runtime.Object) {
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
if status == c.GetStatus(obj) &&
reason == c.GetReason(obj) &&
message == c.GetMessage(obj) {
c.LastUpdated(obj, ts)
reason == c.GetReason(obj) {
if message != c.GetMessage(obj) {
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

View 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])
}
}
}