1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-23 15:29:42 +02:00

fix(edge): check all endpoint_relation db query logic [BE-11602] (#379)

This commit is contained in:
Oscar Zhou 2025-02-05 15:20:27 +13:00 committed by GitHub
parent 96285817ab
commit 2865fd6b84
7 changed files with 48 additions and 4 deletions

View file

@ -186,6 +186,7 @@ func (handler *Handler) updateEndpointStacks(tx dataservices.DataStoreTx, endpoi
if relation == nil { if relation == nil {
relation = &portainer.EndpointRelation{ relation = &portainer.EndpointRelation{
EndpointID: endpoint.ID, EndpointID: endpoint.ID,
EdgeStacks: make(map[portainer.EdgeStackID]bool),
} }
} }
relation.EdgeStacks = edgeStackSet relation.EdgeStacks = edgeStackSet

View file

@ -107,7 +107,7 @@ func (handler *Handler) updateEdgeStack(tx dataservices.DataStoreTx, stackID por
hasWrongType, err := hasWrongEnvironmentType(tx.Endpoint(), relatedEndpointIds, payload.DeploymentType) hasWrongType, err := hasWrongEnvironmentType(tx.Endpoint(), relatedEndpointIds, payload.DeploymentType)
if err != nil { if err != nil {
return nil, httperror.BadRequest("unable to check for existence of non fitting environments: %w", err) return nil, httperror.InternalServerError("unable to check for existence of non fitting environments: %w", err)
} }
if hasWrongType { if hasWrongType {
return nil, httperror.BadRequest("edge stack with config do not match the environment type", nil) return nil, httperror.BadRequest("edge stack with config do not match the environment type", nil)
@ -151,6 +151,9 @@ func (handler *Handler) handleChangeEdgeGroups(tx dataservices.DataStoreTx, edge
for endpointID := range endpointsToRemove { for endpointID := range endpointsToRemove {
relation, err := tx.EndpointRelation().EndpointRelation(endpointID) relation, err := tx.EndpointRelation().EndpointRelation(endpointID)
if err != nil { if err != nil {
if tx.IsErrObjectNotFound(err) {
continue
}
return nil, nil, errors.WithMessage(err, "Unable to find environment relation in database") return nil, nil, errors.WithMessage(err, "Unable to find environment relation in database")
} }
@ -170,10 +173,16 @@ func (handler *Handler) handleChangeEdgeGroups(tx dataservices.DataStoreTx, edge
for endpointID := range endpointsToAdd { for endpointID := range endpointsToAdd {
relation, err := tx.EndpointRelation().EndpointRelation(endpointID) relation, err := tx.EndpointRelation().EndpointRelation(endpointID)
if err != nil { if err != nil && !tx.IsErrObjectNotFound(err) {
return nil, nil, errors.WithMessage(err, "Unable to find environment relation in database") return nil, nil, errors.WithMessage(err, "Unable to find environment relation in database")
} }
if relation == nil {
relation = &portainer.EndpointRelation{
EndpointID: endpointID,
EdgeStacks: map[portainer.EdgeStackID]bool{},
}
}
relation.EdgeStacks[edgeStackID] = true relation.EdgeStacks[edgeStackID] = true
if err := tx.EndpointRelation().UpdateEndpointRelation(endpointID, relation); err != nil { if err := tx.EndpointRelation().UpdateEndpointRelation(endpointID, relation); err != nil {

View file

@ -264,6 +264,9 @@ func (handler *Handler) buildSchedules(tx dataservices.DataStoreTx, endpointID p
func (handler *Handler) buildEdgeStacks(tx dataservices.DataStoreTx, endpointID portainer.EndpointID) ([]stackStatusResponse, *httperror.HandlerError) { func (handler *Handler) buildEdgeStacks(tx dataservices.DataStoreTx, endpointID portainer.EndpointID) ([]stackStatusResponse, *httperror.HandlerError) {
relation, err := tx.EndpointRelation().EndpointRelation(endpointID) relation, err := tx.EndpointRelation().EndpointRelation(endpointID)
if err != nil { if err != nil {
if tx.IsErrObjectNotFound(err) {
return nil, nil
}
return nil, httperror.InternalServerError("Unable to retrieve relation object from the database", err) return nil, httperror.InternalServerError("Unable to retrieve relation object from the database", err)
} }

View file

@ -21,10 +21,17 @@ func (handler *Handler) updateEndpointRelations(tx dataservices.DataStoreTx, end
} }
endpointRelation, err := tx.EndpointRelation().EndpointRelation(endpoint.ID) endpointRelation, err := tx.EndpointRelation().EndpointRelation(endpoint.ID)
if err != nil { if err != nil && !tx.IsErrObjectNotFound(err) {
return err return err
} }
if endpointRelation == nil {
endpointRelation = &portainer.EndpointRelation{
EndpointID: endpoint.ID,
EdgeStacks: make(map[portainer.EdgeStackID]bool),
}
}
edgeGroups, err := tx.EdgeGroup().ReadAll() edgeGroups, err := tx.EdgeGroup().ReadAll()
if err != nil { if err != nil {
return err return err
@ -32,6 +39,9 @@ func (handler *Handler) updateEndpointRelations(tx dataservices.DataStoreTx, end
edgeStacks, err := tx.EdgeStack().EdgeStacks() edgeStacks, err := tx.EdgeStack().EdgeStacks()
if err != nil { if err != nil {
if tx.IsErrObjectNotFound(err) {
return nil
}
return err return err
} }

View file

@ -23,6 +23,7 @@ func (handler *Handler) updateEdgeRelations(tx dataservices.DataStoreTx, endpoin
relation = &portainer.EndpointRelation{ relation = &portainer.EndpointRelation{
EndpointID: endpoint.ID, EndpointID: endpoint.ID,
EdgeStacks: map[portainer.EdgeStackID]bool{},
} }
if err := tx.EndpointRelation().Create(relation); err != nil { if err := tx.EndpointRelation().Create(relation); err != nil {
return errors.WithMessage(err, "Unable to create environment relation inside the database") return errors.WithMessage(err, "Unable to create environment relation inside the database")

View file

@ -133,10 +133,17 @@ func deleteTag(tx dataservices.DataStoreTx, tagID portainer.TagID) error {
func updateEndpointRelations(tx dataservices.DataStoreTx, endpoint portainer.Endpoint, edgeGroups []portainer.EdgeGroup, edgeStacks []portainer.EdgeStack) error { func updateEndpointRelations(tx dataservices.DataStoreTx, endpoint portainer.Endpoint, edgeGroups []portainer.EdgeGroup, edgeStacks []portainer.EdgeStack) error {
endpointRelation, err := tx.EndpointRelation().EndpointRelation(endpoint.ID) endpointRelation, err := tx.EndpointRelation().EndpointRelation(endpoint.ID)
if err != nil { if err != nil && !tx.IsErrObjectNotFound(err) {
return err return err
} }
if endpointRelation == nil {
endpointRelation = &portainer.EndpointRelation{
EndpointID: endpoint.ID,
EdgeStacks: make(map[portainer.EdgeStackID]bool),
}
}
endpointGroup, err := tx.EndpointGroup().Read(endpoint.GroupID) endpointGroup, err := tx.EndpointGroup().Read(endpoint.GroupID)
if err != nil { if err != nil {
return err return err
@ -147,6 +154,7 @@ func updateEndpointRelations(tx dataservices.DataStoreTx, endpoint portainer.End
for _, edgeStackID := range endpointStacks { for _, edgeStackID := range endpointStacks {
stacksSet[edgeStackID] = true stacksSet[edgeStackID] = true
} }
endpointRelation.EdgeStacks = stacksSet endpointRelation.EdgeStacks = stacksSet
return tx.EndpointRelation().UpdateEndpointRelation(endpoint.ID, endpointRelation) return tx.EndpointRelation().UpdateEndpointRelation(endpoint.ID, endpointRelation)

View file

@ -11,6 +11,7 @@ import (
httperrors "github.com/portainer/portainer/api/http/errors" httperrors "github.com/portainer/portainer/api/http/errors"
"github.com/portainer/portainer/api/internal/edge" "github.com/portainer/portainer/api/internal/edge"
edgetypes "github.com/portainer/portainer/api/internal/edge/types" edgetypes "github.com/portainer/portainer/api/internal/edge/types"
"github.com/rs/zerolog/log"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -119,6 +120,9 @@ func (service *Service) updateEndpointRelations(tx dataservices.DataStoreTx, edg
for _, endpointID := range relatedEndpointIds { for _, endpointID := range relatedEndpointIds {
relation, err := endpointRelationService.EndpointRelation(endpointID) relation, err := endpointRelationService.EndpointRelation(endpointID)
if err != nil { if err != nil {
if tx.IsErrObjectNotFound(err) {
continue
}
return fmt.Errorf("unable to find endpoint relation in database: %w", err) return fmt.Errorf("unable to find endpoint relation in database: %w", err)
} }
@ -147,6 +151,14 @@ func (service *Service) DeleteEdgeStack(tx dataservices.DataStoreTx, edgeStackID
for _, endpointID := range relatedEndpointIds { for _, endpointID := range relatedEndpointIds {
relation, err := tx.EndpointRelation().EndpointRelation(endpointID) relation, err := tx.EndpointRelation().EndpointRelation(endpointID)
if err != nil { if err != nil {
if tx.IsErrObjectNotFound(err) {
log.Warn().
Int("endpoint_id", int(endpointID)).
Msg("Unable to find endpoint relation in database, skipping")
continue
}
return errors.WithMessage(err, "Unable to find environment relation in database") return errors.WithMessage(err, "Unable to find environment relation in database")
} }