From 4a391224155dbf1cf08268ef2867a17d39ad55ed Mon Sep 17 00:00:00 2001 From: andres-portainer <91705312+andres-portainer@users.noreply.github.com> Date: Tue, 5 Sep 2023 22:35:16 -0300 Subject: [PATCH] fix(code): remove code that is no longer necessary EE-6078 (#10256) --- api/docker/images/status.go | 2 +- api/exec/common.go | 45 ------------- .../containers/container_gpus_inspect.go | 2 +- api/http/handler/edgegroups/edgegroup_list.go | 2 +- .../handler/edgegroups/edgegroup_update.go | 2 +- api/http/handler/edgejobs/edgejob_create.go | 2 +- api/http/handler/edgejobs/edgejob_delete.go | 2 +- .../edgejobs/edgejob_tasklogs_clear.go | 2 +- .../edgejobs/edgejob_tasklogs_collect.go | 2 +- .../handler/edgejobs/edgejob_tasks_list.go | 2 +- api/http/handler/edgejobs/edgejob_update.go | 4 +- api/http/handler/endpoints/endpoint_delete.go | 19 ++---- api/http/handler/endpoints/filter.go | 2 +- api/http/handler/endpoints/filter_test.go | 1 + .../endpoints/utils_update_edge_groups.go | 5 +- api/http/handler/tags/tag_delete.go | 27 ++++---- api/internal/maps/maps.go | 34 ---------- api/internal/maps/maps_test.go | 38 ----------- api/internal/slices/slices.go | 64 ------------------- 19 files changed, 33 insertions(+), 224 deletions(-) delete mode 100644 api/internal/maps/maps.go delete mode 100644 api/internal/maps/maps_test.go diff --git a/api/docker/images/status.go b/api/docker/images/status.go index d24ac7b60..97aac1a31 100644 --- a/api/docker/images/status.go +++ b/api/docker/images/status.go @@ -2,6 +2,7 @@ package images import ( "context" + "slices" "strings" "time" @@ -9,7 +10,6 @@ import ( "github.com/docker/docker/api/types/filters" portainer "github.com/portainer/portainer/api" consts "github.com/portainer/portainer/api/docker/consts" - "github.com/portainer/portainer/api/internal/slices" "github.com/opencontainers/go-digest" "github.com/patrickmn/go-cache" diff --git a/api/exec/common.go b/api/exec/common.go index 97a10f432..3d11c31b6 100644 --- a/api/exec/common.go +++ b/api/exec/common.go @@ -3,48 +3,3 @@ package exec import "regexp" var stackNameNormalizeRegex = regexp.MustCompile("[^-_a-z0-9]+") - -type StringSet map[string]bool - -func NewStringSet() StringSet { - return make(StringSet) -} - -func (s StringSet) Add(x string) { - s[x] = true -} - -func (s StringSet) Remove(x string) { - if s.Contains(x) { - delete(s, x) - } -} - -func (s StringSet) Contains(x string) bool { - _, ok := s[x] - return ok -} - -func (s StringSet) Len() int { - return len(s) -} - -func (s StringSet) List() []string { - list := make([]string, s.Len()) - - i := 0 - for k := range s { - list[i] = k - i++ - } - - return list -} - -func (s StringSet) Union(x StringSet) { - if x.Len() != 0 { - for k := range x { - s.Add(k) - } - } -} diff --git a/api/http/handler/docker/containers/container_gpus_inspect.go b/api/http/handler/docker/containers/container_gpus_inspect.go index 504322025..c3ac8f806 100644 --- a/api/http/handler/docker/containers/container_gpus_inspect.go +++ b/api/http/handler/docker/containers/container_gpus_inspect.go @@ -2,11 +2,11 @@ package containers import ( "net/http" + "slices" "strings" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/http/middlewares" - "github.com/portainer/portainer/api/internal/slices" httperror "github.com/portainer/portainer/pkg/libhttp/error" "github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/response" diff --git a/api/http/handler/edgegroups/edgegroup_list.go b/api/http/handler/edgegroups/edgegroup_list.go index 237324f5c..82b91368b 100644 --- a/api/http/handler/edgegroups/edgegroup_list.go +++ b/api/http/handler/edgegroups/edgegroup_list.go @@ -3,10 +3,10 @@ package edgegroups import ( "fmt" "net/http" + "slices" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" - "github.com/portainer/portainer/api/internal/slices" httperror "github.com/portainer/portainer/pkg/libhttp/error" ) diff --git a/api/http/handler/edgegroups/edgegroup_update.go b/api/http/handler/edgegroups/edgegroup_update.go index 02756268f..38f975739 100644 --- a/api/http/handler/edgegroups/edgegroup_update.go +++ b/api/http/handler/edgegroups/edgegroup_update.go @@ -3,12 +3,12 @@ package edgegroups import ( "errors" "net/http" + "slices" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/internal/edge" "github.com/portainer/portainer/api/internal/endpointutils" - "github.com/portainer/portainer/api/internal/slices" "github.com/portainer/portainer/api/internal/unique" httperror "github.com/portainer/portainer/pkg/libhttp/error" "github.com/portainer/portainer/pkg/libhttp/request" diff --git a/api/http/handler/edgejobs/edgejob_create.go b/api/http/handler/edgejobs/edgejob_create.go index 73135dea1..d5b142450 100644 --- a/api/http/handler/edgejobs/edgejob_create.go +++ b/api/http/handler/edgejobs/edgejob_create.go @@ -2,6 +2,7 @@ package edgejobs import ( "errors" + "maps" "net/http" "strconv" "strings" @@ -11,7 +12,6 @@ import ( "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/internal/edge" "github.com/portainer/portainer/api/internal/endpointutils" - "github.com/portainer/portainer/api/internal/maps" httperror "github.com/portainer/portainer/pkg/libhttp/error" "github.com/portainer/portainer/pkg/libhttp/request" diff --git a/api/http/handler/edgejobs/edgejob_delete.go b/api/http/handler/edgejobs/edgejob_delete.go index 355562894..f2211a9de 100644 --- a/api/http/handler/edgejobs/edgejob_delete.go +++ b/api/http/handler/edgejobs/edgejob_delete.go @@ -2,13 +2,13 @@ package edgejobs import ( "errors" + "maps" "net/http" "strconv" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/internal/edge" - "github.com/portainer/portainer/api/internal/maps" httperror "github.com/portainer/portainer/pkg/libhttp/error" "github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/response" diff --git a/api/http/handler/edgejobs/edgejob_tasklogs_clear.go b/api/http/handler/edgejobs/edgejob_tasklogs_clear.go index 8d712afd2..2fd6acf4f 100644 --- a/api/http/handler/edgejobs/edgejob_tasklogs_clear.go +++ b/api/http/handler/edgejobs/edgejob_tasklogs_clear.go @@ -3,12 +3,12 @@ package edgejobs import ( "errors" "net/http" + "slices" "strconv" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/internal/edge" - "github.com/portainer/portainer/api/internal/slices" httperror "github.com/portainer/portainer/pkg/libhttp/error" "github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/response" diff --git a/api/http/handler/edgejobs/edgejob_tasklogs_collect.go b/api/http/handler/edgejobs/edgejob_tasklogs_collect.go index dfb10e350..a0f5940f8 100644 --- a/api/http/handler/edgejobs/edgejob_tasklogs_collect.go +++ b/api/http/handler/edgejobs/edgejob_tasklogs_collect.go @@ -3,11 +3,11 @@ package edgejobs import ( "errors" "net/http" + "slices" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/internal/edge" - "github.com/portainer/portainer/api/internal/slices" httperror "github.com/portainer/portainer/pkg/libhttp/error" "github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/response" diff --git a/api/http/handler/edgejobs/edgejob_tasks_list.go b/api/http/handler/edgejobs/edgejob_tasks_list.go index f18ca6979..91669ada5 100644 --- a/api/http/handler/edgejobs/edgejob_tasks_list.go +++ b/api/http/handler/edgejobs/edgejob_tasks_list.go @@ -2,12 +2,12 @@ package edgejobs import ( "fmt" + "maps" "net/http" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/internal/edge" - "github.com/portainer/portainer/api/internal/maps" httperror "github.com/portainer/portainer/pkg/libhttp/error" "github.com/portainer/portainer/pkg/libhttp/request" ) diff --git a/api/http/handler/edgejobs/edgejob_update.go b/api/http/handler/edgejobs/edgejob_update.go index 43e9e0b88..ef02a3531 100644 --- a/api/http/handler/edgejobs/edgejob_update.go +++ b/api/http/handler/edgejobs/edgejob_update.go @@ -2,15 +2,15 @@ package edgejobs import ( "errors" + "maps" "net/http" + "slices" "strconv" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/internal/edge" "github.com/portainer/portainer/api/internal/endpointutils" - "github.com/portainer/portainer/api/internal/maps" - "github.com/portainer/portainer/api/internal/slices" httperror "github.com/portainer/portainer/pkg/libhttp/error" "github.com/portainer/portainer/pkg/libhttp/request" diff --git a/api/http/handler/endpoints/endpoint_delete.go b/api/http/handler/endpoints/endpoint_delete.go index 878ed944a..5a71faee1 100644 --- a/api/http/handler/endpoints/endpoint_delete.go +++ b/api/http/handler/endpoints/endpoint_delete.go @@ -3,6 +3,7 @@ package endpoints import ( "errors" "net/http" + "slices" "strconv" portainer "github.com/portainer/portainer/api" @@ -117,9 +118,11 @@ func (handler *Handler) deleteEndpoint(tx dataservices.DataStoreTx, endpointID p } for _, edgeGroup := range edgeGroups { - edgeGroup.Endpoints = removeElement(edgeGroup.Endpoints, endpoint.ID) - tx.EdgeGroup().Update(edgeGroup.ID, &edgeGroup) + edgeGroup.Endpoints = slices.DeleteFunc(edgeGroup.Endpoints, func(e portainer.EndpointID) bool { + return e == endpoint.ID + }) + err = tx.EdgeGroup().Update(edgeGroup.ID, &edgeGroup) if err != nil { log.Warn().Err(err).Msgf("Unable to update edge group") } @@ -183,15 +186,3 @@ func (handler *Handler) deleteEndpoint(tx dataservices.DataStoreTx, endpointID p return nil } - -func removeElement(slice []portainer.EndpointID, elem portainer.EndpointID) []portainer.EndpointID { - for i, id := range slice { - if id == elem { - slice[i] = slice[len(slice)-1] - - return slice[:len(slice)-1] - } - } - - return slice -} diff --git a/api/http/handler/endpoints/filter.go b/api/http/handler/endpoints/filter.go index d3d9cfa7a..bde2f1740 100644 --- a/api/http/handler/endpoints/filter.go +++ b/api/http/handler/endpoints/filter.go @@ -3,6 +3,7 @@ package endpoints import ( "fmt" "net/http" + "slices" "strconv" "strings" "time" @@ -12,7 +13,6 @@ import ( "github.com/portainer/portainer/api/http/handler/edgegroups" "github.com/portainer/portainer/api/internal/edge" "github.com/portainer/portainer/api/internal/endpointutils" - "github.com/portainer/portainer/api/internal/slices" "github.com/portainer/portainer/api/internal/unique" "github.com/portainer/portainer/pkg/libhttp/request" diff --git a/api/http/handler/endpoints/filter_test.go b/api/http/handler/endpoints/filter_test.go index aa8a50913..dc1b10184 100644 --- a/api/http/handler/endpoints/filter_test.go +++ b/api/http/handler/endpoints/filter_test.go @@ -7,6 +7,7 @@ import ( "github.com/portainer/portainer/api/datastore" "github.com/portainer/portainer/api/internal/slices" "github.com/portainer/portainer/api/internal/testhelpers" + "github.com/stretchr/testify/assert" ) diff --git a/api/http/handler/endpoints/utils_update_edge_groups.go b/api/http/handler/endpoints/utils_update_edge_groups.go index 764b738db..42e1dba76 100644 --- a/api/http/handler/endpoints/utils_update_edge_groups.go +++ b/api/http/handler/endpoints/utils_update_edge_groups.go @@ -1,11 +1,12 @@ package endpoints import ( + "slices" + "github.com/pkg/errors" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/internal/set" - "github.com/portainer/portainer/api/internal/slices" ) func updateEnvironmentEdgeGroups(tx dataservices.DataStoreTx, newEdgeGroups []portainer.EdgeGroupID, environmentID portainer.EndpointID) (bool, error) { @@ -52,7 +53,7 @@ func updateEnvironmentEdgeGroups(tx dataservices.DataStoreTx, newEdgeGroups []po removeEdgeGroups := environmentEdgeGroupsSet.Difference(newEdgeGroupsSet) err = updateSet(removeEdgeGroups, func(edgeGroup *portainer.EdgeGroup) { - edgeGroup.Endpoints = slices.RemoveItem(edgeGroup.Endpoints, func(eID portainer.EndpointID) bool { + edgeGroup.Endpoints = slices.DeleteFunc(edgeGroup.Endpoints, func(eID portainer.EndpointID) bool { return eID == environmentID }) }) diff --git a/api/http/handler/tags/tag_delete.go b/api/http/handler/tags/tag_delete.go index 378502f5e..ad8ef1347 100644 --- a/api/http/handler/tags/tag_delete.go +++ b/api/http/handler/tags/tag_delete.go @@ -3,6 +3,7 @@ package tags import ( "errors" "net/http" + "slices" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" @@ -61,7 +62,10 @@ func deleteTag(tx dataservices.DataStoreTx, tagID portainer.TagID) error { return httperror.InternalServerError("Unable to retrieve environment from the database", err) } - endpoint.TagIDs = removeElement(endpoint.TagIDs, tagID) + endpoint.TagIDs = slices.DeleteFunc(endpoint.TagIDs, func(t portainer.TagID) bool { + return t == tagID + }) + err = tx.Endpoint().UpdateEndpoint(endpoint.ID, endpoint) if err != nil { return httperror.InternalServerError("Unable to update environment", err) @@ -74,7 +78,10 @@ func deleteTag(tx dataservices.DataStoreTx, tagID portainer.TagID) error { return httperror.InternalServerError("Unable to retrieve environment group from the database", err) } - endpointGroup.TagIDs = removeElement(endpointGroup.TagIDs, tagID) + endpointGroup.TagIDs = slices.DeleteFunc(endpointGroup.TagIDs, func(t portainer.TagID) bool { + return t == tagID + }) + err = tx.EndpointGroup().Update(endpointGroup.ID, endpointGroup) if err != nil { return httperror.InternalServerError("Unable to update environment group", err) @@ -106,7 +113,9 @@ func deleteTag(tx dataservices.DataStoreTx, tagID portainer.TagID) error { } for _, edgeGroup := range edgeGroups { - edgeGroup.TagIDs = removeElement(edgeGroup.TagIDs, tagID) + edgeGroup.TagIDs = slices.DeleteFunc(edgeGroup.TagIDs, func(t portainer.TagID) bool { + return t == tagID + }) err = tx.EdgeGroup().Update(edgeGroup.ID, &edgeGroup) if err != nil { @@ -142,15 +151,3 @@ func updateEndpointRelations(tx dataservices.DataStoreTx, endpoint portainer.End return tx.EndpointRelation().UpdateEndpointRelation(endpoint.ID, endpointRelation) } - -func removeElement(slice []portainer.TagID, elem portainer.TagID) []portainer.TagID { - for i, id := range slice { - if id == elem { - slice[i] = slice[len(slice)-1] - - return slice[:len(slice)-1] - } - } - - return slice -} diff --git a/api/internal/maps/maps.go b/api/internal/maps/maps.go deleted file mode 100644 index b3e2a5695..000000000 --- a/api/internal/maps/maps.go +++ /dev/null @@ -1,34 +0,0 @@ -package maps - -import "strings" - -// Get a key from a nested map. Not support array for the moment -func Get(mapObj map[string]interface{}, path string, key string) interface{} { - if path == "" { - return mapObj[key] - } - paths := strings.Split(path, ".") - v := mapObj - for _, p := range paths { - if p == "" { - continue - } - value, ok := v[p].(map[string]interface{}) - if ok { - v = value - } else { - return "" - } - } - return v[key] -} - -// Copy copies all key/value pairs in src adding them to dst. -// When a key in src is already present in dst, -// the value in dst will be overwritten by the value associated -// with the key in src. -func Copy[M ~map[K]V, K comparable, V any](dst, src M) { - for k, v := range src { - dst[k] = v - } -} diff --git a/api/internal/maps/maps_test.go b/api/internal/maps/maps_test.go deleted file mode 100644 index 41a6992f9..000000000 --- a/api/internal/maps/maps_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package maps - -import ( - "encoding/json" - "fmt" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestGet(t *testing.T) { - t.Run("xx", func(t *testing.T) { - jsonStr := "{\"data\":{\"yesterday\":{\"sunrise\":\"06:19\"}}}" - data := make(map[string]interface{}) - err := json.Unmarshal([]byte(jsonStr), &data) - if err != nil { - fmt.Printf("error: %s", err) - return - } - result := Get(data, "data.yesterday", "sunrise") - fmt.Printf("result: %s\n", result) - expected := "06:19" - assert.Equal(t, expected, result) - }) - t.Run("xx", func(t *testing.T) { - jsonStr := "{\"data\":{\"yesterday\": \"hahaha\"}}" - data := make(map[string]interface{}) - err := json.Unmarshal([]byte(jsonStr), &data) - if err != nil { - fmt.Printf("error: %s", err) - return - } - result := Get(data, "data.yesterday", "sunrise") - fmt.Printf("result: %s\n", result) - expected := "" - assert.Equal(t, expected, result) - }) -} diff --git a/api/internal/slices/slices.go b/api/internal/slices/slices.go index 7d0fa21c7..a2677cd87 100644 --- a/api/internal/slices/slices.go +++ b/api/internal/slices/slices.go @@ -1,69 +1,5 @@ package slices -// Contains is a generic function that returns true if the element is contained within the slice -func Contains[T comparable](elems []T, v T) bool { - return ContainsFunc(elems, func(s T) bool { - return s == v - }) -} - -// Contains is a generic function that returns true if the element is contained within the slice -func ContainsFunc[T any](elems []T, f func(T) bool) bool { - for _, s := range elems { - if f(s) { - return true - } - } - return false -} - -func Find[T any](elems []T, f func(T) bool) (T, bool) { - for _, s := range elems { - if f(s) { - return s, true - } - } - - // return default value - var result T - return result, false -} - -// IndexFunc returns the first index i satisfying f(s[i]), -// or -1 if none do. -func IndexFunc[E any](s []E, f func(E) bool) int { - for i, v := range s { - if f(v) { - return i - } - } - return -1 -} - -// RemoveItem removes the first element from the slice that satisfies the given predicate -func RemoveItem[E comparable](s []E, predicate func(E) bool) []E { - index := IndexFunc(s, predicate) - if index == -1 { - return s - } - - return RemoveIndex(s, index) -} - -// RemoveIndex removes the element at the given index from the slice -func RemoveIndex[T any](s []T, index int) []T { - if len(s) == 0 { - return s - } - - if index < 0 || index >= len(s) { - return s - } - - s[index] = s[len(s)-1] - return s[:len(s)-1] -} - // Map applies the given function to each element of the slice and returns a new slice with the results func Map[T, U any](s []T, f func(T) U) []U { result := make([]U, len(s))