Skip to content

Commit bcb464d

Browse files
authored
Merge pull request kubernetes#69655 from CaoShuFeng/Established_lastTransitionTime
always set lastTransitionTime when crd condition changes
2 parents 1698765 + eaf59df commit bcb464d

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/helpers.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@ var swaggerMetadataDescriptions = metav1.ObjectMeta{}.SwaggerDoc()
2727

2828
// SetCRDCondition sets the status condition. It either overwrites the existing one or creates a new one.
2929
func SetCRDCondition(crd *CustomResourceDefinition, newCondition CustomResourceDefinitionCondition) {
30+
newCondition.LastTransitionTime = metav1.NewTime(time.Now())
31+
3032
existingCondition := FindCRDCondition(crd, newCondition.Type)
3133
if existingCondition == nil {
32-
newCondition.LastTransitionTime = metav1.NewTime(time.Now())
3334
crd.Status.Conditions = append(crd.Status.Conditions, newCondition)
3435
return
3536
}
3637

37-
if existingCondition.Status != newCondition.Status {
38-
existingCondition.Status = newCondition.Status
38+
if existingCondition.Status != newCondition.Status || existingCondition.LastTransitionTime.IsZero() {
3939
existingCondition.LastTransitionTime = newCondition.LastTransitionTime
4040
}
4141

42+
existingCondition.Status = newCondition.Status
4243
existingCondition.Reason = newCondition.Reason
4344
existingCondition.Message = newCondition.Message
4445
}

staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/helpers_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,67 @@ func TestSetCRDCondition(t *testing.T) {
202202
},
203203
},
204204
},
205+
{
206+
name: "set new condition which doesn't have lastTransitionTime set",
207+
crdCondition: []CustomResourceDefinitionCondition{
208+
{
209+
Type: Established,
210+
Status: ConditionTrue,
211+
Reason: "Accepted",
212+
Message: "the initial names have been accepted",
213+
LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC),
214+
},
215+
},
216+
newCondition: CustomResourceDefinitionCondition{
217+
Type: Established,
218+
Status: ConditionFalse,
219+
Reason: "NotAccepted",
220+
Message: "Not accepted",
221+
},
222+
expectedcrdCondition: []CustomResourceDefinitionCondition{
223+
{
224+
Type: Established,
225+
Status: ConditionFalse,
226+
Reason: "NotAccepted",
227+
Message: "Not accepted",
228+
LastTransitionTime: metav1.Date(2018, 1, 2, 0, 0, 0, 0, time.UTC),
229+
},
230+
},
231+
},
232+
{
233+
name: "append new condition which doesn't have lastTransitionTime set",
234+
crdCondition: []CustomResourceDefinitionCondition{
235+
{
236+
Type: Established,
237+
Status: ConditionTrue,
238+
Reason: "Accepted",
239+
Message: "the initial names have been accepted",
240+
LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC),
241+
},
242+
},
243+
newCondition: CustomResourceDefinitionCondition{
244+
Type: Terminating,
245+
Status: ConditionFalse,
246+
Reason: "NeverEstablished",
247+
Message: "resource was never established",
248+
},
249+
expectedcrdCondition: []CustomResourceDefinitionCondition{
250+
{
251+
Type: Established,
252+
Status: ConditionTrue,
253+
Reason: "Accepted",
254+
Message: "the initial names have been accepted",
255+
LastTransitionTime: metav1.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC),
256+
},
257+
{
258+
Type: Terminating,
259+
Status: ConditionFalse,
260+
Reason: "NeverEstablished",
261+
Message: "resource was never established",
262+
LastTransitionTime: metav1.Date(2018, 2, 1, 0, 0, 0, 0, time.UTC),
263+
},
264+
},
265+
},
205266
}
206267
for _, tc := range tests {
207268
crd := generateCRDwithCondition(tc.crdCondition)
@@ -213,6 +274,9 @@ func TestSetCRDCondition(t *testing.T) {
213274
if !IsCRDConditionEquivalent(&tc.expectedcrdCondition[i], &crd.Status.Conditions[i]) {
214275
t.Errorf("%v expected %v, got %v", tc.name, tc.expectedcrdCondition, crd.Status.Conditions)
215276
}
277+
if crd.Status.Conditions[i].LastTransitionTime.IsZero() {
278+
t.Errorf("%q lastTransitionTime should not be null: %v", tc.name, i, crd.Status.Conditions)
279+
}
216280
}
217281
}
218282
}

0 commit comments

Comments
 (0)