Merge pull request #75075 from kwiesmueller/fix-fieldmanager-sorting
fix handling of nil times in managedFields sorting
This commit is contained in:
		| @@ -20,6 +20,7 @@ import ( | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"sort" | ||||
| 	"time" | ||||
|  | ||||
| 	"k8s.io/apimachinery/pkg/api/meta" | ||||
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||
| @@ -160,8 +161,12 @@ func sortEncodedManagedFields(encodedManagedFields []metav1.ManagedFieldsEntry) | ||||
| 			return p.Operation < q.Operation | ||||
| 		} | ||||
|  | ||||
| 		if p.Time == nil || q.Time == nil { | ||||
| 			return false | ||||
| 		ntime := &metav1.Time{Time: time.Time{}} | ||||
| 		if p.Time == nil { | ||||
| 			p.Time = ntime | ||||
| 		} | ||||
| 		if q.Time == nil { | ||||
| 			q.Time = ntime | ||||
| 		} | ||||
| 		if !p.Time.Equal(q.Time) { | ||||
| 			return p.Time.Before(q.Time) | ||||
|   | ||||
| @@ -228,6 +228,30 @@ func TestSortEncodedManagedFields(t *testing.T) { | ||||
| 				{Manager: "a", Operation: metav1.ManagedFieldsOperationUpdate, Time: parseTimeOrPanic("2001-01-01T01:00:00Z")}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name: "manager without time first", | ||||
| 			managedFields: []metav1.ManagedFieldsEntry{ | ||||
| 				{Manager: "a", Operation: metav1.ManagedFieldsOperationApply, Time: nil}, | ||||
| 				{Manager: "a", Operation: metav1.ManagedFieldsOperationApply, Time: parseTimeOrPanic("2001-01-01T01:00:00Z")}, | ||||
| 			}, | ||||
| 			expected: []metav1.ManagedFieldsEntry{ | ||||
| 				{Manager: "a", Operation: metav1.ManagedFieldsOperationApply, Time: nil}, | ||||
| 				{Manager: "a", Operation: metav1.ManagedFieldsOperationApply, Time: parseTimeOrPanic("2001-01-01T01:00:00Z")}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name: "manager without time first name last", | ||||
| 			managedFields: []metav1.ManagedFieldsEntry{ | ||||
| 				{Manager: "a", Operation: metav1.ManagedFieldsOperationApply, Time: parseTimeOrPanic("2001-01-01T01:00:00Z")}, | ||||
| 				{Manager: "b", Operation: metav1.ManagedFieldsOperationApply, Time: nil}, | ||||
| 				{Manager: "a", Operation: metav1.ManagedFieldsOperationApply, Time: nil}, | ||||
| 			}, | ||||
| 			expected: []metav1.ManagedFieldsEntry{ | ||||
| 				{Manager: "a", Operation: metav1.ManagedFieldsOperationApply, Time: nil}, | ||||
| 				{Manager: "b", Operation: metav1.ManagedFieldsOperationApply, Time: nil}, | ||||
| 				{Manager: "a", Operation: metav1.ManagedFieldsOperationApply, Time: parseTimeOrPanic("2001-01-01T01:00:00Z")}, | ||||
| 			}, | ||||
| 		}, | ||||
| 		{ | ||||
| 			name: "apply first", | ||||
| 			managedFields: []metav1.ManagedFieldsEntry{ | ||||
| @@ -274,18 +298,24 @@ func TestSortEncodedManagedFields(t *testing.T) { | ||||
| 			managedFields: []metav1.ManagedFieldsEntry{ | ||||
| 				{Manager: "c", Operation: metav1.ManagedFieldsOperationUpdate, Time: parseTimeOrPanic("2001-01-01T01:00:00Z")}, | ||||
| 				{Manager: "a", Operation: metav1.ManagedFieldsOperationApply, Time: nil}, | ||||
| 				{Manager: "g", Operation: metav1.ManagedFieldsOperationApply, Time: nil}, | ||||
| 				{Manager: "f", Operation: metav1.ManagedFieldsOperationUpdate, Time: parseTimeOrPanic("2002-01-01T01:00:00Z")}, | ||||
| 				{Manager: "i", Operation: metav1.ManagedFieldsOperationApply, Time: nil}, | ||||
| 				{Manager: "d", Operation: metav1.ManagedFieldsOperationUpdate, Time: parseTimeOrPanic("2002-01-01T01:00:00Z")}, | ||||
| 				{Manager: "e", Operation: metav1.ManagedFieldsOperationUpdate, Time: parseTimeOrPanic("2002-01-01T01:00:00Z")}, | ||||
| 				{Manager: "h", Operation: metav1.ManagedFieldsOperationApply, Time: nil}, | ||||
| 				{Manager: "e", Operation: metav1.ManagedFieldsOperationUpdate, Time: parseTimeOrPanic("2003-01-01T01:00:00Z")}, | ||||
| 				{Manager: "b", Operation: metav1.ManagedFieldsOperationApply, Time: nil}, | ||||
| 			}, | ||||
| 			expected: []metav1.ManagedFieldsEntry{ | ||||
| 				{Manager: "a", Operation: metav1.ManagedFieldsOperationApply, Time: nil}, | ||||
| 				{Manager: "b", Operation: metav1.ManagedFieldsOperationApply, Time: nil}, | ||||
| 				{Manager: "g", Operation: metav1.ManagedFieldsOperationApply, Time: nil}, | ||||
| 				{Manager: "h", Operation: metav1.ManagedFieldsOperationApply, Time: nil}, | ||||
| 				{Manager: "i", Operation: metav1.ManagedFieldsOperationApply, Time: nil}, | ||||
| 				{Manager: "c", Operation: metav1.ManagedFieldsOperationUpdate, Time: parseTimeOrPanic("2001-01-01T01:00:00Z")}, | ||||
| 				{Manager: "d", Operation: metav1.ManagedFieldsOperationUpdate, Time: parseTimeOrPanic("2002-01-01T01:00:00Z")}, | ||||
| 				{Manager: "e", Operation: metav1.ManagedFieldsOperationUpdate, Time: parseTimeOrPanic("2002-01-01T01:00:00Z")}, | ||||
| 				{Manager: "f", Operation: metav1.ManagedFieldsOperationUpdate, Time: parseTimeOrPanic("2002-01-01T01:00:00Z")}, | ||||
| 				{Manager: "e", Operation: metav1.ManagedFieldsOperationUpdate, Time: parseTimeOrPanic("2003-01-01T01:00:00Z")}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Kubernetes Prow Robot
					Kubernetes Prow Robot