1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-24 15:59:41 +02:00

feat(waiting-room): choose relations when associated endpoint [EE-5187] (#8720)

This commit is contained in:
Chaim Lev-Ari 2023-05-14 09:26:11 +07:00 committed by GitHub
parent 511adabce2
commit 365316971b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
53 changed files with 1712 additions and 303 deletions

View file

@ -9,9 +9,8 @@ import (
"github.com/portainer/libhttp/request"
"github.com/portainer/libhttp/response"
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/dataservices"
"github.com/portainer/portainer/api/http/client"
"github.com/portainer/portainer/api/internal/edge"
"github.com/portainer/portainer/api/internal/tag"
)
type endpointUpdatePayload struct {
@ -120,48 +119,31 @@ func (handler *Handler) endpointUpdate(w http.ResponseWriter, r *http.Request) *
endpoint.EdgeCheckinInterval = *payload.EdgeCheckinInterval
}
groupIDChanged := false
updateRelations := false
if payload.GroupID != nil {
groupID := portainer.EndpointGroupID(*payload.GroupID)
groupIDChanged = groupID != endpoint.GroupID
endpoint.GroupID = groupID
updateRelations = updateRelations || groupID != endpoint.GroupID
}
tagsChanged := false
if payload.TagIDs != nil {
payloadTagSet := tag.Set(payload.TagIDs)
endpointTagSet := tag.Set((endpoint.TagIDs))
union := tag.Union(payloadTagSet, endpointTagSet)
intersection := tag.Intersection(payloadTagSet, endpointTagSet)
tagsChanged = len(union) > len(intersection)
err := handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
if tagsChanged {
removeTags := tag.Difference(endpointTagSet, payloadTagSet)
for tagID := range removeTags {
err = handler.DataStore.Tag().UpdateTagFunc(tagID, func(tag *portainer.Tag) {
delete(tag.Endpoints, endpoint.ID)
})
if handler.DataStore.IsErrObjectNotFound(err) {
return httperror.InternalServerError("Unable to find a tag inside the database", err)
} else if err != nil {
return httperror.InternalServerError("Unable to persist tag changes inside the database", err)
}
tagsChanged, err := updateEnvironmentTags(tx, payload.TagIDs, endpoint.TagIDs, endpoint.ID)
if err != nil {
return err
}
endpoint.TagIDs = payload.TagIDs
for _, tagID := range payload.TagIDs {
err = handler.DataStore.Tag().UpdateTagFunc(tagID, func(tag *portainer.Tag) {
tag.Endpoints[endpoint.ID] = true
})
updateRelations = updateRelations || tagsChanged
if handler.DataStore.IsErrObjectNotFound(err) {
return httperror.InternalServerError("Unable to find a tag inside the database", err)
} else if err != nil {
return httperror.InternalServerError("Unable to persist tag changes inside the database", err)
}
}
return nil
})
if err != nil {
httperror.InternalServerError("Unable to update environment tags", err)
}
}
@ -286,39 +268,13 @@ func (handler *Handler) endpointUpdate(w http.ResponseWriter, r *http.Request) *
return httperror.InternalServerError("Unable to persist environment changes inside the database", err)
}
if (endpoint.Type == portainer.EdgeAgentOnDockerEnvironment || endpoint.Type == portainer.EdgeAgentOnKubernetesEnvironment) && (groupIDChanged || tagsChanged) {
relation, err := handler.DataStore.EndpointRelation().EndpointRelation(endpoint.ID)
if updateRelations {
err := handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
return handler.updateEdgeRelations(tx, endpoint)
})
if err != nil {
return httperror.InternalServerError("Unable to find environment relation inside the database", err)
}
endpointGroup, err := handler.DataStore.EndpointGroup().EndpointGroup(endpoint.GroupID)
if err != nil {
return httperror.InternalServerError("Unable to find environment group inside the database", err)
}
edgeGroups, err := handler.DataStore.EdgeGroup().EdgeGroups()
if err != nil {
return httperror.InternalServerError("Unable to retrieve edge groups from the database", err)
}
edgeStacks, err := handler.DataStore.EdgeStack().EdgeStacks()
if err != nil {
return httperror.InternalServerError("Unable to retrieve edge stacks from the database", err)
}
currentEdgeStackSet := map[portainer.EdgeStackID]bool{}
endpointEdgeStacks := edge.EndpointRelatedEdgeStacks(endpoint, endpointGroup, edgeGroups, edgeStacks)
for _, edgeStackID := range endpointEdgeStacks {
currentEdgeStackSet[edgeStackID] = true
}
relation.EdgeStacks = currentEdgeStackSet
err = handler.DataStore.EndpointRelation().UpdateEndpointRelation(endpoint.ID, relation)
if err != nil {
return httperror.InternalServerError("Unable to persist environment relation changes inside the database", err)
return httperror.InternalServerError("Unable to update environment relations", err)
}
}