From 643868f4a6c4a0803e287d48b111d39988cc19c0 Mon Sep 17 00:00:00 2001 From: Daishan Peng Date: Thu, 16 Aug 2018 18:06:52 -0700 Subject: [PATCH] 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 --- condition/condition.go | 14 +++++++++++--- condition/condition_test.go | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 condition/condition_test.go diff --git a/condition/condition.go b/condition/condition.go index f633a7a5..809af3a2 100644 --- a/condition/condition.go +++ b/condition/condition.go @@ -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 diff --git a/condition/condition_test.go b/condition/condition_test.go new file mode 100644 index 00000000..7882466f --- /dev/null +++ b/condition/condition_test.go @@ -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]) + } + } +}