From e45b852c0948c0d24656224f1667c0d3108cdaa4 Mon Sep 17 00:00:00 2001 From: Oscar Zhou <100548325+oscarzhou-portainer@users.noreply.github.com> Date: Wed, 12 Feb 2025 09:23:52 +1300 Subject: [PATCH] fix(platform): remove error log when local env is not found [BE-11353] (#364) --- api/http/handler/system/system_info.go | 8 +++++++- api/http/handler/system/system_upgrade.go | 7 +++++-- api/platform/service.go | 10 +++++++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/api/http/handler/system/system_info.go b/api/http/handler/system/system_info.go index ad5f944ef..64a915313 100644 --- a/api/http/handler/system/system_info.go +++ b/api/http/handler/system/system_info.go @@ -3,6 +3,7 @@ package system import ( "net/http" + "github.com/pkg/errors" "github.com/portainer/portainer/api/internal/endpointutils" plf "github.com/portainer/portainer/api/platform" httperror "github.com/portainer/portainer/pkg/libhttp/error" @@ -46,7 +47,12 @@ func (handler *Handler) systemInfo(w http.ResponseWriter, r *http.Request) *http platform, err := handler.platformService.GetPlatform() if err != nil { - return httperror.InternalServerError("Failed to get platform", err) + if !errors.Is(err, plf.ErrNoLocalEnvironment) { + return httperror.InternalServerError("Failed to get platform", err) + } + // If no local environment is detected, we assume the platform is Docker + // UI will stop showing the upgrade banner + platform = plf.PlatformDocker } return response.JSON(w, &systemInfoResponse{ diff --git a/api/http/handler/system/system_upgrade.go b/api/http/handler/system/system_upgrade.go index f881b6233..8eb41413f 100644 --- a/api/http/handler/system/system_upgrade.go +++ b/api/http/handler/system/system_upgrade.go @@ -4,6 +4,7 @@ import ( "net/http" "regexp" + ceplf "github.com/portainer/portainer/api/platform" httperror "github.com/portainer/portainer/pkg/libhttp/error" "github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/response" @@ -45,6 +46,9 @@ func (handler *Handler) systemUpgrade(w http.ResponseWriter, r *http.Request) *h environment, err := handler.platformService.GetLocalEnvironment() if err != nil { + if errors.Is(err, ceplf.ErrNoLocalEnvironment) { + return httperror.NotFound("The system upgrade feature is disabled because no local environment was detected.", err) + } return httperror.InternalServerError("Failed to get local environment", err) } @@ -53,8 +57,7 @@ func (handler *Handler) systemUpgrade(w http.ResponseWriter, r *http.Request) *h return httperror.InternalServerError("Failed to get platform", err) } - err = handler.upgradeService.Upgrade(platform, environment, payload.License) - if err != nil { + if err := handler.upgradeService.Upgrade(platform, environment, payload.License); err != nil { return httperror.InternalServerError("Failed to upgrade Portainer", err) } diff --git a/api/platform/service.go b/api/platform/service.go index 628e1cf9b..e74855fb7 100644 --- a/api/platform/service.go +++ b/api/platform/service.go @@ -14,6 +14,10 @@ import ( "github.com/rs/zerolog/log" ) +var ( + ErrNoLocalEnvironment = errors.New("No local environment was detected") +) + type Service interface { GetLocalEnvironment() (*portainer.Endpoint, error) GetPlatform() (ContainerPlatform, error) @@ -35,7 +39,7 @@ func (service *service) loadEnvAndPlatform() error { return nil } - environment, platform, err := guessLocalEnvironment(service.dataStore) + environment, platform, err := detectLocalEnvironment(service.dataStore) if err != nil { return err } @@ -73,7 +77,7 @@ var platformToEndpointType = map[ContainerPlatform][]portainer.EndpointType{ PlatformKubernetes: {portainer.KubernetesLocalEnvironment}, } -func guessLocalEnvironment(dataStore dataservices.DataStore) (*portainer.Endpoint, ContainerPlatform, error) { +func detectLocalEnvironment(dataStore dataservices.DataStore) (*portainer.Endpoint, ContainerPlatform, error) { platform := DetermineContainerPlatform() if !slices.Contains([]ContainerPlatform{PlatformDocker, PlatformKubernetes}, platform) { @@ -113,7 +117,7 @@ func guessLocalEnvironment(dataStore dataservices.DataStore) (*portainer.Endpoin } } - return nil, "", errors.New("failed to find local environment") + return nil, "", ErrNoLocalEnvironment } func checkDockerEnvTypeForUpgrade(environment *portainer.Endpoint) ContainerPlatform {