mirror of https://github.com/portainer/portainer
fix(endpoints): remove global map to avoid panic writes EE-3160 (#6918)
parent
e806f74652
commit
c162e180e0
|
@ -92,12 +92,6 @@ func (handler *Handler) endpointList(w http.ResponseWriter, r *http.Request) *ht
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve environment groups from the database", err}
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve environment groups from the database", err}
|
||||||
}
|
}
|
||||||
|
|
||||||
// create endpoint groups as a map for more convenient access
|
|
||||||
endpointGroupNames = make(map[portainer.EndpointGroupID]string, 0)
|
|
||||||
for _, group := range endpointGroups {
|
|
||||||
endpointGroupNames[group.ID] = group.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
endpoints, err := handler.DataStore.Endpoint().Endpoints()
|
endpoints, err := handler.DataStore.Endpoint().Endpoints()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve environments from the database", err}
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve environments from the database", err}
|
||||||
|
@ -163,7 +157,7 @@ func (handler *Handler) endpointList(w http.ResponseWriter, r *http.Request) *ht
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort endpoints by field
|
// Sort endpoints by field
|
||||||
sortEndpointsByField(filteredEndpoints, sortField, sortOrder == "desc")
|
sortEndpointsByField(filteredEndpoints, endpointGroups, sortField, sortOrder == "desc")
|
||||||
|
|
||||||
filteredEndpointCount := len(filteredEndpoints)
|
filteredEndpointCount := len(filteredEndpoints)
|
||||||
|
|
||||||
|
@ -260,7 +254,7 @@ func filterEndpointsByStatuses(endpoints []portainer.Endpoint, statuses []int, s
|
||||||
return filteredEndpoints
|
return filteredEndpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
func sortEndpointsByField(endpoints []portainer.Endpoint, sortField string, isSortDesc bool) {
|
func sortEndpointsByField(endpoints []portainer.Endpoint, endpointGroups []portainer.EndpointGroup, sortField string, isSortDesc bool) {
|
||||||
|
|
||||||
switch sortField {
|
switch sortField {
|
||||||
case "Name":
|
case "Name":
|
||||||
|
@ -271,10 +265,20 @@ func sortEndpointsByField(endpoints []portainer.Endpoint, sortField string, isSo
|
||||||
}
|
}
|
||||||
|
|
||||||
case "Group":
|
case "Group":
|
||||||
|
endpointGroupNames = make(map[portainer.EndpointGroupID]string, 0)
|
||||||
|
for _, group := range endpointGroups {
|
||||||
|
endpointGroupNames[group.ID] = group.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
endpointsByGroup := EndpointsByGroup{
|
||||||
|
endpointGroupNames: endpointGroupNames,
|
||||||
|
endpoints: endpoints,
|
||||||
|
}
|
||||||
|
|
||||||
if isSortDesc {
|
if isSortDesc {
|
||||||
sort.Stable(sort.Reverse(EndpointsByGroup(endpoints)))
|
sort.Stable(sort.Reverse(endpointsByGroup))
|
||||||
} else {
|
} else {
|
||||||
sort.Stable(EndpointsByGroup(endpoints))
|
sort.Stable(endpointsByGroup)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "Status":
|
case "Status":
|
||||||
|
|
|
@ -21,23 +21,26 @@ func (e EndpointsByName) Less(i, j int) bool {
|
||||||
return sortorder.NaturalLess(strings.ToLower(e[i].Name), strings.ToLower(e[j].Name))
|
return sortorder.NaturalLess(strings.ToLower(e[i].Name), strings.ToLower(e[j].Name))
|
||||||
}
|
}
|
||||||
|
|
||||||
type EndpointsByGroup []portainer.Endpoint
|
type EndpointsByGroup struct {
|
||||||
|
endpointGroupNames map[portainer.EndpointGroupID]string
|
||||||
|
endpoints []portainer.Endpoint
|
||||||
|
}
|
||||||
|
|
||||||
func (e EndpointsByGroup) Len() int {
|
func (e EndpointsByGroup) Len() int {
|
||||||
return len(e)
|
return len(e.endpoints)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e EndpointsByGroup) Swap(i, j int) {
|
func (e EndpointsByGroup) Swap(i, j int) {
|
||||||
e[i], e[j] = e[j], e[i]
|
e.endpoints[i], e.endpoints[j] = e.endpoints[j], e.endpoints[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e EndpointsByGroup) Less(i, j int) bool {
|
func (e EndpointsByGroup) Less(i, j int) bool {
|
||||||
if e[i].GroupID == e[j].GroupID {
|
if e.endpoints[i].GroupID == e.endpoints[j].GroupID {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
groupA := endpointGroupNames[e[i].GroupID]
|
groupA := endpointGroupNames[e.endpoints[i].GroupID]
|
||||||
groupB := endpointGroupNames[e[j].GroupID]
|
groupB := endpointGroupNames[e.endpoints[j].GroupID]
|
||||||
|
|
||||||
return sortorder.NaturalLess(strings.ToLower(groupA), strings.ToLower(groupB))
|
return sortorder.NaturalLess(strings.ToLower(groupA), strings.ToLower(groupB))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue