Skip to content

Commit f20b892

Browse files
authored
Merge pull request kubernetes#84 from Random-Liu/fix-transition-timestamp
Only change transition timestamp when condition is changed.
2 parents 310fed3 + a986976 commit f20b892

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

pkg/kernelmonitor/kernel_monitor.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,15 @@ func (k *kernelMonitor) generateStatus(logs []*kerntypes.KernelLog, rule kerntyp
154154
condition := &k.conditions[i]
155155
if condition.Type == rule.Condition {
156156
condition.Type = rule.Condition
157+
// Update transition timestamp and message when the condition
158+
// changes. Condition is considered to be changed only when
159+
// status or reason changes.
160+
if !condition.Status || condition.Reason != rule.Reason {
161+
condition.Transition = timestamp
162+
condition.Message = message
163+
}
157164
condition.Status = true
158-
condition.Transition = timestamp
159165
condition.Reason = rule.Reason
160-
condition.Message = message
161166
break
162167
}
163168
}

pkg/kernelmonitor/kernel_monitor_test.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ func TestGenerateStatus(t *testing.T) {
3636
{
3737
Type: testConditionA,
3838
Status: true,
39-
Transition: time.Now(),
39+
Transition: time.Unix(500, 500),
40+
Reason: "initial reason",
4041
},
4142
{
4243
Type: testConditionB,
4344
Status: false,
44-
Transition: time.Now(),
45+
Transition: time.Unix(500, 500),
4546
},
4647
}
4748
logs := []*kerntypes.KernelLog{
@@ -79,6 +80,26 @@ func TestGenerateStatus(t *testing.T) {
7980
},
8081
},
8182
},
83+
// Should not update transition time when status and reason are not changed.
84+
{
85+
rule: kerntypes.Rule{
86+
Type: kerntypes.Perm,
87+
Condition: testConditionA,
88+
Reason: "initial reason",
89+
},
90+
expected: types.Status{
91+
Source: testSource,
92+
Conditions: []types.Condition{
93+
{
94+
Type: testConditionA,
95+
Status: true,
96+
Transition: time.Unix(500, 500),
97+
Reason: "initial reason",
98+
},
99+
initConditions[1],
100+
},
101+
},
102+
},
82103
{
83104
rule: kerntypes.Rule{
84105
Type: kerntypes.Temp,
@@ -100,7 +121,9 @@ func TestGenerateStatus(t *testing.T) {
100121
config: MonitorConfig{
101122
Source: testSource,
102123
},
103-
conditions: initConditions,
124+
// Copy the init conditions to make sure it's not changed
125+
// during the test.
126+
conditions: append([]types.Condition{}, initConditions...),
104127
}
105128
got := k.generateStatus(logs, test.rule)
106129
if !reflect.DeepEqual(&test.expected, got) {

0 commit comments

Comments
 (0)