Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sortByResourceVersion #1

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 13 additions & 26 deletions pkg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,6 @@ func (s *Server) Run() (*http.Server, *rest.Config, error) {
return srv, &cfg, nil
}

type APIObjects struct {
GVR schema.GroupVersionResource `json:",inline,omitempty"`
Items []unstructured.Unstructured `json:"Items"`
}

func (s *Server) Checkpoint() {
s.m.Lock()
defer s.m.Unlock()
Expand All @@ -307,27 +302,20 @@ func (s *Server) NextResourceVersion() int64 {
return result
}

func (s *Server) Export() ([]APIObjects, []APIObjects) {
func (s *Server) Export() ([]unstructured.Unstructured, []unstructured.Unstructured) {
s.m.Lock()
defer s.m.Unlock()

current := make([]APIObjects, 0, len(s.stores))
deleted := make([]APIObjects, 0, len(s.stores))
current := make([]unstructured.Unstructured, 0, len(s.stores))
deleted := make([]unstructured.Unstructured, 0, len(s.stores))

for _, store := range s.stores {
current = append(current, APIObjects{
GVR: store.GVR,
Items: getDirtyObjects(store.Current, s.checkedVersion),
})
deleted = append(deleted, APIObjects{
GVR: store.GVR,
Items: getDirtyObjects(store.Deleted, s.checkedVersion),
})
current = append(current, getDirtyObjects(store.Current, s.checkedVersion)...)
deleted = append(deleted, getDirtyObjects(store.Deleted, s.checkedVersion)...)
}

sort.Slice(current, func(i, j int) bool {
if current[i].GVR.Group != current[j].GVR.Group {
return current[i].GVR.Group < current[j].GVR.Group
}
return current[i].GVR.Resource != current[j].GVR.Resource
return atoi(current[i].GetResourceVersion()) < atoi(current[j].GetResourceVersion())
})

return current, deleted
Expand All @@ -341,12 +329,6 @@ func getDirtyObjects(in map[types.NamespacedName]*unstructured.Unstructured, che
out = append(out, *obj)
}
}
sort.Slice(out, func(i, j int) bool {
if out[i].GetNamespace() != out[j].GetNamespace() {
return out[i].GetNamespace() < out[j].GetNamespace()
}
return out[i].GetName() < out[j].GetName()
})
return out
}

Expand All @@ -360,3 +342,8 @@ func (s *Server) RemoveNamespace(ns string) {
}
}
}

func atoi(s string) int {
i, _ := strconv.Atoi(s)
return i
}
Loading