mirror of
https://github.com/portainer/portainer.git
synced 2025-08-02 20:35:25 +02:00
feature(kubeconfig): access to all kube environment contexts from within the Portainer UI [EE-1727] (#5966)
This commit is contained in:
parent
c0a4727114
commit
6be1ff4d9c
22 changed files with 404 additions and 434 deletions
|
@ -45,3 +45,60 @@ func Test_IsKubernetesEndpoint(t *testing.T) {
|
|||
assert.Equal(t, test.expected, ans)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_FilterByExcludeIDs(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
inputArray []portainer.Endpoint
|
||||
inputExcludeIDs []portainer.EndpointID
|
||||
asserts func(*testing.T, []portainer.Endpoint)
|
||||
}{
|
||||
{
|
||||
name: "filter endpoints",
|
||||
inputArray: []portainer.Endpoint{
|
||||
{ID: portainer.EndpointID(1)},
|
||||
{ID: portainer.EndpointID(2)},
|
||||
{ID: portainer.EndpointID(3)},
|
||||
{ID: portainer.EndpointID(4)},
|
||||
},
|
||||
inputExcludeIDs: []portainer.EndpointID{
|
||||
portainer.EndpointID(2),
|
||||
portainer.EndpointID(3),
|
||||
},
|
||||
asserts: func(t *testing.T, output []portainer.Endpoint) {
|
||||
assert.Contains(t, output, portainer.Endpoint{ID: portainer.EndpointID(1)})
|
||||
assert.NotContains(t, output, portainer.Endpoint{ID: portainer.EndpointID(2)})
|
||||
assert.NotContains(t, output, portainer.Endpoint{ID: portainer.EndpointID(3)})
|
||||
assert.Contains(t, output, portainer.Endpoint{ID: portainer.EndpointID(4)})
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "empty input",
|
||||
inputArray: []portainer.Endpoint{},
|
||||
inputExcludeIDs: []portainer.EndpointID{
|
||||
portainer.EndpointID(2),
|
||||
},
|
||||
asserts: func(t *testing.T, output []portainer.Endpoint) {
|
||||
assert.Equal(t, 0, len(output))
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no filter",
|
||||
inputArray: []portainer.Endpoint{
|
||||
{ID: portainer.EndpointID(1)},
|
||||
{ID: portainer.EndpointID(2)},
|
||||
},
|
||||
inputExcludeIDs: []portainer.EndpointID{},
|
||||
asserts: func(t *testing.T, output []portainer.Endpoint) {
|
||||
assert.Equal(t, 2, len(output))
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
output := FilterByExcludeIDs(tt.inputArray, tt.inputExcludeIDs)
|
||||
tt.asserts(t, output)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ func IsLocalEndpoint(endpoint *portainer.Endpoint) bool {
|
|||
return strings.HasPrefix(endpoint.URL, "unix://") || strings.HasPrefix(endpoint.URL, "npipe://") || endpoint.Type == 5
|
||||
}
|
||||
|
||||
// IsKubernetesEndpoint returns true if this is a kubernetes endpoint
|
||||
// IsKubernetesEndpoint returns true if this is a kubernetes environment(endpoint)
|
||||
func IsKubernetesEndpoint(endpoint *portainer.Endpoint) bool {
|
||||
return endpoint.Type == portainer.KubernetesLocalEnvironment ||
|
||||
endpoint.Type == portainer.AgentOnKubernetesEnvironment ||
|
||||
|
@ -29,3 +29,24 @@ func IsDockerEndpoint(endpoint *portainer.Endpoint) bool {
|
|||
func IsEdgeEndpoint(endpoint *portainer.Endpoint) bool {
|
||||
return endpoint.Type == portainer.EdgeAgentOnDockerEnvironment || endpoint.Type == portainer.EdgeAgentOnKubernetesEnvironment
|
||||
}
|
||||
|
||||
// FilterByExcludeIDs receives an environment(endpoint) array and returns a filtered array using an excludeIds param
|
||||
func FilterByExcludeIDs(endpoints []portainer.Endpoint, excludeIds []portainer.EndpointID) []portainer.Endpoint {
|
||||
if len(excludeIds) == 0 {
|
||||
return endpoints
|
||||
}
|
||||
|
||||
filteredEndpoints := make([]portainer.Endpoint, 0)
|
||||
|
||||
idsSet := make(map[portainer.EndpointID]bool)
|
||||
for _, id := range excludeIds {
|
||||
idsSet[id] = true
|
||||
}
|
||||
|
||||
for _, endpoint := range endpoints {
|
||||
if !idsSet[endpoint.ID] {
|
||||
filteredEndpoints = append(filteredEndpoints, endpoint)
|
||||
}
|
||||
}
|
||||
return filteredEndpoints
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue