1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-24 15:59:41 +02:00

feat(edgejobs): support edge groups when using edge jobs EE-3873 (#8099)

This commit is contained in:
matias-portainer 2022-12-19 18:54:51 -03:00 committed by GitHub
parent 9732d1b5d8
commit e1b474d04f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 544 additions and 83 deletions

View file

@ -2,6 +2,7 @@ package edge
import (
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/dataservices"
"github.com/portainer/portainer/api/internal/endpointutils"
"github.com/portainer/portainer/api/internal/tag"
)
@ -34,6 +35,40 @@ func EdgeGroupRelatedEndpoints(edgeGroup *portainer.EdgeGroup, endpoints []porta
return endpointIDs
}
func EdgeGroupSet(edgeGroupIDs []portainer.EdgeGroupID) map[portainer.EdgeGroupID]bool {
set := map[portainer.EdgeGroupID]bool{}
for _, edgeGroupID := range edgeGroupIDs {
set[edgeGroupID] = true
}
return set
}
func GetEndpointsFromEdgeGroups(edgeGroupIDs []portainer.EdgeGroupID, datastore dataservices.DataStore) ([]portainer.EndpointID, error) {
endpoints, err := datastore.Endpoint().Endpoints()
if err != nil {
return nil, err
}
endpointGroups, err := datastore.EndpointGroup().EndpointGroups()
if err != nil {
return nil, err
}
var response []portainer.EndpointID
for _, edgeGroupID := range edgeGroupIDs {
edgeGroup, err := datastore.EdgeGroup().EdgeGroup(edgeGroupID)
if err != nil {
return nil, err
}
response = append(response, EdgeGroupRelatedEndpoints(edgeGroup, endpoints, endpointGroups)...)
}
return response, nil
}
// edgeGroupRelatedToEndpoint returns true is edgeGroup is associated with environment(endpoint)
func edgeGroupRelatedToEndpoint(edgeGroup *portainer.EdgeGroup, endpoint *portainer.Endpoint, endpointGroup *portainer.EndpointGroup) bool {
if !edgeGroup.Dynamic {

34
api/internal/maps/maps.go Normal file
View file

@ -0,0 +1,34 @@
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
}
}

View file

@ -0,0 +1,38 @@
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", 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", result)
expected := ""
assert.Equal(t, expected, result)
})
}