fix(snapshots): fix background snapshots on environment creation EE-7273 (#12022)

pull/12036/head
andres-portainer 5 months ago committed by GitHub
parent 566e37535f
commit fa4711946d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -14,8 +14,9 @@ import (
func TestPingAgentPanic(t *testing.T) { func TestPingAgentPanic(t *testing.T) {
endpoint := &portainer.Endpoint{ endpoint := &portainer.Endpoint{
ID: 1, ID: 1,
Type: portainer.EdgeAgentOnDockerEnvironment, EdgeID: "test-edge-id",
Type: portainer.EdgeAgentOnDockerEnvironment,
} }
_, store := datastore.MustNewTestStore(t, true, true) _, store := datastore.MustNewTestStore(t, true, true)
@ -38,7 +39,8 @@ func TestPingAgentPanic(t *testing.T) {
require.NoError(t, http.Serve(ln, mux)) require.NoError(t, http.Serve(ln, mux))
}() }()
s.Open(endpoint) err = s.Open(endpoint)
require.NoError(t, err)
s.activeTunnels[endpoint.ID].Port = ln.Addr().(*net.TCPAddr).Port s.activeTunnels[endpoint.ID].Port = ln.Addr().(*net.TCPAddr).Port
require.Error(t, s.pingAgent(endpoint.ID)) require.Error(t, s.pingAgent(endpoint.ID))

@ -24,14 +24,24 @@ const (
maxAvailablePort = 65535 maxAvailablePort = 65535
) )
var (
ErrNonEdgeEnv = errors.New("cannot open a tunnel for non-edge environments")
ErrAsyncEnv = errors.New("cannot open a tunnel for async edge environments")
ErrInvalidEnv = errors.New("cannot open a tunnel for an invalid environment")
)
// Open will mark the tunnel as REQUIRED so the agent opens it // Open will mark the tunnel as REQUIRED so the agent opens it
func (s *Service) Open(endpoint *portainer.Endpoint) error { func (s *Service) Open(endpoint *portainer.Endpoint) error {
if !endpointutils.IsEdgeEndpoint(endpoint) { if !endpointutils.IsEdgeEndpoint(endpoint) {
return errors.New("cannot open a tunnel for non-edge environments") return ErrNonEdgeEnv
} }
if endpoint.Edge.AsyncMode { if endpoint.Edge.AsyncMode {
return errors.New("cannot open a tunnel for async edge environments") return ErrAsyncEnv
}
if endpoint.ID == 0 || endpoint.EdgeID == "" {
return ErrInvalidEnv
} }
s.mu.Lock() s.mu.Lock()

@ -20,6 +20,8 @@ import (
httperror "github.com/portainer/portainer/pkg/libhttp/error" httperror "github.com/portainer/portainer/pkg/libhttp/error"
"github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/request"
"github.com/portainer/portainer/pkg/libhttp/response" "github.com/portainer/portainer/pkg/libhttp/response"
"github.com/rs/zerolog/log"
) )
type stackStatusResponse struct { type stackStatusResponse struct {
@ -93,6 +95,8 @@ func (handler *Handler) endpointEdgeStatusInspect(w http.ResponseWriter, r *http
return httperror.Forbidden("Permission denied to access environment", errors.New("the device has not been trusted yet")) return httperror.Forbidden("Permission denied to access environment", errors.New("the device has not been trusted yet"))
} }
firstConn := endpoint.LastCheckInDate == 0
err = handler.requestBouncer.AuthorizedEdgeEndpointOperation(r, endpoint) err = handler.requestBouncer.AuthorizedEdgeEndpointOperation(r, endpoint)
if err != nil { if err != nil {
return httperror.Forbidden("Permission denied to access environment", err) return httperror.Forbidden("Permission denied to access environment", err)
@ -107,7 +111,7 @@ func (handler *Handler) endpointEdgeStatusInspect(w http.ResponseWriter, r *http
var statusResponse *endpointEdgeStatusInspectResponse var statusResponse *endpointEdgeStatusInspectResponse
err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error { err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
statusResponse, err = handler.inspectStatus(tx, r, portainer.EndpointID(endpointID)) statusResponse, err = handler.inspectStatus(tx, r, portainer.EndpointID(endpointID), firstConn)
return err return err
}) })
if err != nil { if err != nil {
@ -122,7 +126,7 @@ func (handler *Handler) endpointEdgeStatusInspect(w http.ResponseWriter, r *http
return cacheResponse(w, endpoint.ID, *statusResponse) return cacheResponse(w, endpoint.ID, *statusResponse)
} }
func (handler *Handler) inspectStatus(tx dataservices.DataStoreTx, r *http.Request, endpointID portainer.EndpointID) (*endpointEdgeStatusInspectResponse, error) { func (handler *Handler) inspectStatus(tx dataservices.DataStoreTx, r *http.Request, endpointID portainer.EndpointID, firstConn bool) (*endpointEdgeStatusInspectResponse, error) {
endpoint, err := tx.Endpoint().Endpoint(endpointID) endpoint, err := tx.Endpoint().Endpoint(endpointID)
if err != nil { if err != nil {
return nil, err return nil, err
@ -134,8 +138,10 @@ func (handler *Handler) inspectStatus(tx dataservices.DataStoreTx, r *http.Reque
} }
// Take an initial snapshot // Take an initial snapshot
if endpoint.LastCheckInDate == 0 { if firstConn {
handler.ReverseTunnelService.Open(endpoint) if err := handler.ReverseTunnelService.Open(endpoint); err != nil {
log.Error().Err(err).Msg("could not open the tunnel")
}
} }
agentPlatform, agentPlatformErr := parseAgentPlatform(r) agentPlatform, agentPlatformErr := parseAgentPlatform(r)

@ -64,14 +64,16 @@ func NewBackgroundSnapshotter(dataStore dataservices.DataStore, tunnelService po
} }
for _, e := range endpoints { for _, e := range endpoints {
if !endpointutils.IsEdgeEndpoint(&e) { if !endpointutils.IsEdgeEndpoint(&e) || e.Edge.AsyncMode {
continue continue
} }
s, err := tx.Snapshot().Read(e.ID) s, err := tx.Snapshot().Read(e.ID)
if dataservices.IsErrObjectNotFound(err) || if dataservices.IsErrObjectNotFound(err) ||
(err == nil && s.Docker == nil && s.Kubernetes == nil) { (err == nil && s.Docker == nil && s.Kubernetes == nil) {
tunnelService.Open(&e) if err := tunnelService.Open(&e); err != nil {
log.Error().Err(err).Msg("could not open the tunnel")
}
} }
} }

Loading…
Cancel
Save