mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 05:19:39 +02:00
fix(platform): fix a data race in GetPlatform() BE-11522 (#253)
This commit is contained in:
parent
1c62bd6ca5
commit
8d1c90f912
1 changed files with 37 additions and 26 deletions
|
@ -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{
|
func (service *service) loadEnvAndPlatform() error {
|
||||||
dataStore: dataStore,
|
if service.environment != nil {
|
||||||
}, nil
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
environment, platform, err := guessLocalEnvironment(service.dataStore)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
service.environment = environment
|
||||||
|
service.platform = platform
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service *service) GetLocalEnvironment() (*portainer.Endpoint, error) {
|
func (service *service) GetLocalEnvironment() (*portainer.Endpoint, error) {
|
||||||
if service.environment == nil {
|
service.mu.Lock()
|
||||||
environment, platform, err := guessLocalEnvironment(service.dataStore)
|
defer service.mu.Unlock()
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
service.environment = environment
|
if err := service.loadEnvAndPlatform(); err != nil {
|
||||||
service.platform = platform
|
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,15 +100,16 @@ 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) {
|
||||||
if platform != PlatformDocker {
|
continue
|
||||||
return &endpoint, platform, nil
|
}
|
||||||
}
|
|
||||||
|
|
||||||
dockerPlatform := checkDockerEnvTypeForUpgrade(&endpoint)
|
if platform != PlatformDocker {
|
||||||
if dockerPlatform != "" {
|
return &endpoint, platform, nil
|
||||||
return &endpoint, dockerPlatform, nil
|
}
|
||||||
}
|
|
||||||
|
if dockerPlatform := checkDockerEnvTypeForUpgrade(&endpoint); dockerPlatform != "" {
|
||||||
|
return &endpoint, dockerPlatform, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue