mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Round times to nearest second before sorting
This commit is contained in:
parent
574acbe310
commit
888a322d9c
@ -19,7 +19,6 @@ package fieldmanager
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
|
||||||
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
@ -78,15 +77,15 @@ func (f *capManagersManager) capUpdateManagers(managed Managed) (newManaged Mana
|
|||||||
|
|
||||||
// If we have more than the maximum, sort the update entries by time, oldest first.
|
// If we have more than the maximum, sort the update entries by time, oldest first.
|
||||||
sort.Slice(updaters, func(i, j int) bool {
|
sort.Slice(updaters, func(i, j int) bool {
|
||||||
iTime, jTime, nTime := managed.Times()[updaters[i]], managed.Times()[updaters[j]], &metav1.Time{Time: time.Time{}}
|
iTime, jTime, iSeconds, jSeconds := managed.Times()[updaters[i]], managed.Times()[updaters[j]], int64(0), int64(0)
|
||||||
if iTime == nil {
|
if iTime != nil {
|
||||||
iTime = nTime
|
iSeconds = iTime.Unix()
|
||||||
}
|
}
|
||||||
if jTime == nil {
|
if jTime != nil {
|
||||||
jTime = nTime
|
jSeconds = jTime.Unix()
|
||||||
}
|
}
|
||||||
if !iTime.Equal(jTime) {
|
if iSeconds != jSeconds {
|
||||||
return iTime.Before(jTime)
|
return iSeconds < jSeconds
|
||||||
}
|
}
|
||||||
return updaters[i] < updaters[j]
|
return updaters[i] < updaters[j]
|
||||||
})
|
})
|
||||||
|
@ -20,7 +20,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@ -194,15 +193,15 @@ func sortEncodedManagedFields(encodedManagedFields []metav1.ManagedFieldsEntry)
|
|||||||
return p.Operation < q.Operation
|
return p.Operation < q.Operation
|
||||||
}
|
}
|
||||||
|
|
||||||
ntime := &metav1.Time{Time: time.Time{}}
|
pSeconds, qSeconds := int64(0), int64(0)
|
||||||
if p.Time == nil {
|
if p.Time != nil {
|
||||||
p.Time = ntime
|
pSeconds = p.Time.Unix()
|
||||||
}
|
}
|
||||||
if q.Time == nil {
|
if q.Time != nil {
|
||||||
q.Time = ntime
|
qSeconds = q.Time.Unix()
|
||||||
}
|
}
|
||||||
if !p.Time.Equal(q.Time) {
|
if pSeconds != qSeconds {
|
||||||
return p.Time.Before(q.Time)
|
return pSeconds < qSeconds
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Manager != q.Manager {
|
if p.Manager != q.Manager {
|
||||||
|
@ -326,6 +326,19 @@ func TestSortEncodedManagedFields(t *testing.T) {
|
|||||||
{Manager: "e", Operation: metav1.ManagedFieldsOperationUpdate, Time: parseTimeOrPanic("2003-01-01T01:00:00Z")},
|
{Manager: "e", Operation: metav1.ManagedFieldsOperationUpdate, Time: parseTimeOrPanic("2003-01-01T01:00:00Z")},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "sort drops nanoseconds",
|
||||||
|
managedFields: []metav1.ManagedFieldsEntry{
|
||||||
|
{Manager: "c", Operation: metav1.ManagedFieldsOperationUpdate, Time: &metav1.Time{time.Date(2000, time.January, 0, 0, 0, 0, 1, time.UTC)}},
|
||||||
|
{Manager: "a", Operation: metav1.ManagedFieldsOperationUpdate, Time: &metav1.Time{time.Date(2000, time.January, 0, 0, 0, 0, 2, time.UTC)}},
|
||||||
|
{Manager: "b", Operation: metav1.ManagedFieldsOperationUpdate, Time: &metav1.Time{time.Date(2000, time.January, 0, 0, 0, 0, 3, time.UTC)}},
|
||||||
|
},
|
||||||
|
expected: []metav1.ManagedFieldsEntry{
|
||||||
|
{Manager: "a", Operation: metav1.ManagedFieldsOperationUpdate, Time: &metav1.Time{time.Date(2000, time.January, 0, 0, 0, 0, 2, time.UTC)}},
|
||||||
|
{Manager: "b", Operation: metav1.ManagedFieldsOperationUpdate, Time: &metav1.Time{time.Date(2000, time.January, 0, 0, 0, 0, 3, time.UTC)}},
|
||||||
|
{Manager: "c", Operation: metav1.ManagedFieldsOperationUpdate, Time: &metav1.Time{time.Date(2000, time.January, 0, 0, 0, 0, 1, time.UTC)}},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
Loading…
Reference in New Issue
Block a user