1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-22 06:49:40 +02:00

fix(platform): fix a data race in GetPlatform() BE-11522 (#253)

This commit is contained in:
andres-portainer 2024-12-19 09:37:50 -03:00 committed by GitHub
parent 1c62bd6ca5
commit 8d1c90f912

View file

@ -5,10 +5,12 @@ import (
"fmt" "fmt"
"slices" "slices"
"strings" "strings"
"sync"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/dataservices"
"github.com/portainer/portainer/api/internal/endpointutils" "github.com/portainer/portainer/api/internal/endpointutils"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
@ -21,38 +23,46 @@ type service struct {
dataStore dataservices.DataStore dataStore dataservices.DataStore
environment *portainer.Endpoint environment *portainer.Endpoint
platform ContainerPlatform platform ContainerPlatform
mu sync.Mutex
} }
func NewService(dataStore dataservices.DataStore) (Service, error) { func NewService(dataStore dataservices.DataStore) (*service, error) {
return &service{dataStore: dataStore}, nil
return &service{ }
dataStore: dataStore,
}, nil func (service *service) loadEnvAndPlatform() error {
if service.environment != nil {
return nil
} }
func (service *service) GetLocalEnvironment() (*portainer.Endpoint, error) {
if service.environment == nil {
environment, platform, err := guessLocalEnvironment(service.dataStore) environment, platform, err := guessLocalEnvironment(service.dataStore)
if err != nil { if err != nil {
return nil, err return err
} }
service.environment = environment service.environment = environment
service.platform = platform service.platform = platform
return nil
}
func (service *service) GetLocalEnvironment() (*portainer.Endpoint, error) {
service.mu.Lock()
defer service.mu.Unlock()
if err := service.loadEnvAndPlatform(); err != nil {
return nil, err
} }
return service.environment, nil return service.environment, nil
} }
func (service *service) GetPlatform() (ContainerPlatform, error) { func (service *service) GetPlatform() (ContainerPlatform, error) {
if service.environment == nil { service.mu.Lock()
environment, platform, err := guessLocalEnvironment(service.dataStore) defer service.mu.Unlock()
if err != nil {
return "", err
}
service.environment = environment if err := service.loadEnvAndPlatform(); err != nil {
service.platform = platform return "", err
} }
return service.platform, nil return service.platform, nil
@ -90,17 +100,18 @@ func guessLocalEnvironment(dataStore dataservices.DataStore) (*portainer.Endpoin
} }
for _, endpoint := range endpoints { for _, endpoint := range endpoints {
if slices.Contains(endpointTypes, endpoint.Type) { if !slices.Contains(endpointTypes, endpoint.Type) {
continue
}
if platform != PlatformDocker { if platform != PlatformDocker {
return &endpoint, platform, nil return &endpoint, platform, nil
} }
dockerPlatform := checkDockerEnvTypeForUpgrade(&endpoint) if dockerPlatform := checkDockerEnvTypeForUpgrade(&endpoint); dockerPlatform != "" {
if dockerPlatform != "" {
return &endpoint, dockerPlatform, nil return &endpoint, dockerPlatform, nil
} }
} }
}
return nil, "", errors.New("failed to find local environment") return nil, "", errors.New("failed to find local environment")
} }