2021-03-24 18:27:32 +00:00
|
|
|
package endpointutils
|
|
|
|
|
|
|
|
import (
|
2023-01-30 20:58:57 +00:00
|
|
|
"fmt"
|
2021-03-24 18:27:32 +00:00
|
|
|
"strings"
|
2023-01-30 20:58:57 +00:00
|
|
|
"time"
|
2021-03-24 18:27:32 +00:00
|
|
|
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
2022-12-16 03:03:40 +00:00
|
|
|
"github.com/portainer/portainer/api/dataservices"
|
|
|
|
"github.com/portainer/portainer/api/kubernetes/cli"
|
|
|
|
log "github.com/rs/zerolog/log"
|
2021-03-24 18:27:32 +00:00
|
|
|
)
|
|
|
|
|
2021-09-20 00:14:22 +00:00
|
|
|
// IsLocalEndpoint returns true if this is a local environment(endpoint)
|
2021-03-24 18:27:32 +00:00
|
|
|
func IsLocalEndpoint(endpoint *portainer.Endpoint) bool {
|
|
|
|
return strings.HasPrefix(endpoint.URL, "unix://") || strings.HasPrefix(endpoint.URL, "npipe://") || endpoint.Type == 5
|
|
|
|
}
|
2021-06-16 05:28:44 +00:00
|
|
|
|
2021-11-22 18:05:09 +00:00
|
|
|
// IsKubernetesEndpoint returns true if this is a kubernetes environment(endpoint)
|
2021-06-16 05:28:44 +00:00
|
|
|
func IsKubernetesEndpoint(endpoint *portainer.Endpoint) bool {
|
|
|
|
return endpoint.Type == portainer.KubernetesLocalEnvironment ||
|
|
|
|
endpoint.Type == portainer.AgentOnKubernetesEnvironment ||
|
|
|
|
endpoint.Type == portainer.EdgeAgentOnKubernetesEnvironment
|
|
|
|
}
|
|
|
|
|
2021-09-20 00:14:22 +00:00
|
|
|
// IsDockerEndpoint returns true if this is a docker environment(endpoint)
|
2021-06-16 05:28:44 +00:00
|
|
|
func IsDockerEndpoint(endpoint *portainer.Endpoint) bool {
|
|
|
|
return endpoint.Type == portainer.DockerEnvironment ||
|
|
|
|
endpoint.Type == portainer.AgentOnDockerEnvironment ||
|
|
|
|
endpoint.Type == portainer.EdgeAgentOnDockerEnvironment
|
|
|
|
}
|
2021-09-24 04:56:22 +00:00
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
// IsEdgeEndpoint returns true if this is an Edge endpoint
|
2021-09-24 04:56:22 +00:00
|
|
|
func IsEdgeEndpoint(endpoint *portainer.Endpoint) bool {
|
|
|
|
return endpoint.Type == portainer.EdgeAgentOnDockerEnvironment || endpoint.Type == portainer.EdgeAgentOnKubernetesEnvironment
|
|
|
|
}
|
2021-11-22 18:05:09 +00:00
|
|
|
|
2022-01-23 19:48:04 +00:00
|
|
|
// IsAgentEndpoint returns true if this is an Agent endpoint
|
|
|
|
func IsAgentEndpoint(endpoint *portainer.Endpoint) bool {
|
|
|
|
return endpoint.Type == portainer.AgentOnDockerEnvironment ||
|
|
|
|
endpoint.Type == portainer.EdgeAgentOnDockerEnvironment ||
|
|
|
|
endpoint.Type == portainer.AgentOnKubernetesEnvironment ||
|
|
|
|
endpoint.Type == portainer.EdgeAgentOnKubernetesEnvironment
|
|
|
|
}
|
|
|
|
|
2021-11-22 18:05:09 +00:00
|
|
|
// 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
|
|
|
|
}
|
2022-04-08 14:27:38 +00:00
|
|
|
|
|
|
|
// EndpointSet receives an environment(endpoint) array and returns a set
|
|
|
|
func EndpointSet(endpointIDs []portainer.EndpointID) map[portainer.EndpointID]bool {
|
|
|
|
set := map[portainer.EndpointID]bool{}
|
|
|
|
|
|
|
|
for _, endpointID := range endpointIDs {
|
|
|
|
set[endpointID] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return set
|
|
|
|
}
|
2022-12-16 03:03:40 +00:00
|
|
|
|
|
|
|
func InitialIngressClassDetection(endpoint *portainer.Endpoint, endpointService dataservices.EndpointService, factory *cli.ClientFactory) {
|
2023-03-16 02:39:26 +00:00
|
|
|
if endpoint.Kubernetes.Flags.IsServerIngressClassDetected {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
endpoint.Kubernetes.Flags.IsServerIngressClassDetected = true
|
|
|
|
endpointService.UpdateEndpoint(
|
|
|
|
portainer.EndpointID(endpoint.ID),
|
|
|
|
endpoint,
|
|
|
|
)
|
|
|
|
}()
|
2022-12-16 03:03:40 +00:00
|
|
|
cli, err := factory.GetKubeClient(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Err(err).Msg("unable to create kubernetes client for ingress class detection")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
controllers, err := cli.GetIngressControllers()
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Err(err).Msg("failed to fetch ingressclasses")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var updatedClasses []portainer.KubernetesIngressClassConfig
|
|
|
|
for i := range controllers {
|
|
|
|
var updatedClass portainer.KubernetesIngressClassConfig
|
|
|
|
updatedClass.Name = controllers[i].ClassName
|
|
|
|
updatedClass.Type = controllers[i].Type
|
|
|
|
updatedClasses = append(updatedClasses, updatedClass)
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint.Kubernetes.Configuration.IngressClasses = updatedClasses
|
|
|
|
err = endpointService.UpdateEndpoint(
|
|
|
|
portainer.EndpointID(endpoint.ID),
|
|
|
|
endpoint,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Err(err).Msg("unable to store found IngressClasses inside the database")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitialMetricsDetection(endpoint *portainer.Endpoint, endpointService dataservices.EndpointService, factory *cli.ClientFactory) {
|
2023-03-16 02:39:26 +00:00
|
|
|
if endpoint.Kubernetes.Flags.IsServerMetricsDetected {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
endpoint.Kubernetes.Flags.IsServerMetricsDetected = true
|
|
|
|
endpointService.UpdateEndpoint(
|
|
|
|
portainer.EndpointID(endpoint.ID),
|
|
|
|
endpoint,
|
|
|
|
)
|
|
|
|
}()
|
2022-12-16 03:03:40 +00:00
|
|
|
cli, err := factory.GetKubeClient(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Err(err).Msg("unable to create kubernetes client for initial metrics detection")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = cli.GetMetrics()
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Err(err).Msg("unable to fetch metrics: leaving metrics collection disabled.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
endpoint.Kubernetes.Configuration.UseServerMetrics = true
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Err(err).Msg("unable to enable UseServerMetrics inside the database")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 20:58:57 +00:00
|
|
|
func storageDetect(endpoint *portainer.Endpoint, endpointService dataservices.EndpointService, factory *cli.ClientFactory) error {
|
2022-12-16 03:03:40 +00:00
|
|
|
cli, err := factory.GetKubeClient(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Err(err).Msg("unable to create Kubernetes client for initial storage detection")
|
2023-01-30 20:58:57 +00:00
|
|
|
return err
|
2022-12-16 03:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
storage, err := cli.GetStorage()
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Err(err).Msg("unable to fetch storage classes: leaving storage classes disabled")
|
2023-01-30 20:58:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(storage) == 0 {
|
|
|
|
log.Info().Err(err).Msg("zero storage classes found: they may be still building, retrying in 30 seconds")
|
|
|
|
return fmt.Errorf("zero storage classes found: they may be still building, retrying in 30 seconds")
|
2022-12-16 03:03:40 +00:00
|
|
|
}
|
|
|
|
endpoint.Kubernetes.Configuration.StorageClasses = storage
|
|
|
|
err = endpointService.UpdateEndpoint(
|
|
|
|
portainer.EndpointID(endpoint.ID),
|
|
|
|
endpoint,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Err(err).Msg("unable to enable storage class inside the database")
|
2023-01-30 20:58:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitialStorageDetection(endpoint *portainer.Endpoint, endpointService dataservices.EndpointService, factory *cli.ClientFactory) {
|
2023-03-16 02:39:26 +00:00
|
|
|
if endpoint.Kubernetes.Flags.IsServerStorageDetected {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
endpoint.Kubernetes.Flags.IsServerStorageDetected = true
|
|
|
|
endpointService.UpdateEndpoint(
|
|
|
|
portainer.EndpointID(endpoint.ID),
|
|
|
|
endpoint,
|
|
|
|
)
|
|
|
|
}()
|
2023-01-30 20:58:57 +00:00
|
|
|
log.Info().Msg("attempting to detect storage classes in the cluster")
|
|
|
|
err := storageDetect(endpoint, endpointService, factory)
|
|
|
|
if err == nil {
|
2022-12-16 03:03:40 +00:00
|
|
|
return
|
|
|
|
}
|
2023-01-30 20:58:57 +00:00
|
|
|
log.Err(err).Msg("error while detecting storage classes")
|
|
|
|
go func() {
|
|
|
|
// Retry after 30 seconds if the initial detection failed.
|
|
|
|
log.Info().Msg("retrying storage detection in 30 seconds")
|
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
err := storageDetect(endpoint, endpointService, factory)
|
|
|
|
log.Err(err).Msg("final error while detecting storage classes")
|
|
|
|
}()
|
2022-12-16 03:03:40 +00:00
|
|
|
}
|
2023-04-05 21:09:22 +00:00
|
|
|
|
|
|
|
func UpdateEdgeEndpointHeartbeat(endpoint *portainer.Endpoint, settings *portainer.Settings) {
|
|
|
|
if IsEdgeEndpoint(endpoint) {
|
2023-06-01 22:36:14 +00:00
|
|
|
endpoint.QueryDate = time.Now().Unix()
|
2023-04-05 21:09:22 +00:00
|
|
|
checkInInterval := getEndpointCheckinInterval(endpoint, settings)
|
|
|
|
endpoint.Heartbeat = endpoint.QueryDate-endpoint.LastCheckInDate <= int64(checkInInterval*2+20)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEndpointCheckinInterval(endpoint *portainer.Endpoint, settings *portainer.Settings) int {
|
|
|
|
if endpoint.Edge.AsyncMode {
|
|
|
|
defaultInterval := 60
|
|
|
|
intervals := [][]int{
|
|
|
|
{endpoint.Edge.PingInterval, settings.Edge.PingInterval},
|
|
|
|
{endpoint.Edge.CommandInterval, settings.Edge.CommandInterval},
|
|
|
|
{endpoint.Edge.SnapshotInterval, settings.Edge.SnapshotInterval},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(intervals); i++ {
|
|
|
|
effectiveInterval := intervals[i][0]
|
|
|
|
if effectiveInterval <= 0 {
|
|
|
|
effectiveInterval = intervals[i][1]
|
|
|
|
}
|
|
|
|
if effectiveInterval > 0 && effectiveInterval < defaultInterval {
|
|
|
|
defaultInterval = effectiveInterval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint.EdgeCheckinInterval > 0 {
|
|
|
|
return endpoint.EdgeCheckinInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings.EdgeAgentCheckinInterval
|
|
|
|
}
|