Skip to content

Commit

Permalink
Merge pull request #2181 from gravitl/GRA-1505/ingress-peer-removal
Browse files Browse the repository at this point in the history
GRA-1505: ingress peer removal
  • Loading branch information
0xdcarns authored Apr 6, 2023
2 parents dd77f09 + 0e4e98e commit b8858bb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
14 changes: 13 additions & 1 deletion controllers/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ func deleteIngressGateway(w http.ResponseWriter, r *http.Request) {
var params = mux.Vars(r)
nodeid := params["nodeid"]
netid := params["network"]
node, wasFailover, err := logic.DeleteIngressGateway(netid, nodeid)
node, wasFailover, removedClients, err := logic.DeleteIngressGateway(netid, nodeid)
if err != nil {
logger.Log(0, r.Header.Get("user"),
fmt.Sprintf("failed to delete ingress gateway on node [%s] on network [%s]: %v",
Expand All @@ -584,6 +584,18 @@ func deleteIngressGateway(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(apiNode)

if len(removedClients) > 0 {
host, err := logic.GetHost(node.HostID.String())
if err == nil {
go mq.PublishSingleHostPeerUpdate(
context.Background(),
host,
nil,
removedClients[:],
)
}
}

runUpdates(&node, true)
}

Expand Down
22 changes: 15 additions & 7 deletions logic/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,30 @@ func CreateIngressGateway(netid string, nodeid string, failover bool) (models.No
}

// DeleteIngressGateway - deletes an ingress gateway
func DeleteIngressGateway(networkName string, nodeid string) (models.Node, bool, error) {
func DeleteIngressGateway(networkName string, nodeid string) (models.Node, bool, []models.ExtClient, error) {
removedClients := []models.ExtClient{}
node, err := GetNodeByID(nodeid)
if err != nil {
return models.Node{}, false, err
return models.Node{}, false, removedClients, err
}
//host, err := GetHost(node.ID.String())
//if err != nil {
//return models.Node{}, false, err
//}
//network, err := GetParentNetwork(networkName)
if err != nil {
return models.Node{}, false, err
return models.Node{}, false, removedClients, err
}
clients, err := GetExtClientsByID(nodeid, networkName)
if err != nil {
return models.Node{}, false, removedClients, err
}

removedClients = clients

// delete ext clients belonging to ingress gateway
if err = DeleteGatewayExtClients(node.ID.String(), networkName); err != nil {
return models.Node{}, false, err
return models.Node{}, false, removedClients, err
}
logger.Log(3, "deleting ingress gateway")
wasFailover := node.Failover
Expand All @@ -169,14 +177,14 @@ func DeleteIngressGateway(networkName string, nodeid string) (models.Node, bool,

data, err := json.Marshal(&node)
if err != nil {
return models.Node{}, false, err
return models.Node{}, false, removedClients, err
}
err = database.Insert(node.ID.String(), string(data), database.NODES_TABLE_NAME)
if err != nil {
return models.Node{}, wasFailover, err
return models.Node{}, wasFailover, removedClients, err
}
err = SetNetworkNodesLastModified(networkName)
return node, wasFailover, err
return node, wasFailover, removedClients, err
}

// DeleteGatewayExtClients - deletes ext clients based on gateway (mac) of ingress node and network
Expand Down
19 changes: 11 additions & 8 deletions logic/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func ResetPeerUpdateContext() {
}

// GetPeerUpdateForHost - gets the consolidated peer update for the host from all networks
func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host, deletedNode *models.Node, deletedClient *models.ExtClient) (models.HostPeerUpdate, error) {
func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host, deletedNode *models.Node, deletedClients []models.ExtClient) (models.HostPeerUpdate, error) {
if host == nil {
return models.HostPeerUpdate{}, errors.New("host is nil")
}
Expand Down Expand Up @@ -408,13 +408,16 @@ func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host
hostPeerUpdate.NodePeers[i] = peer
}

if deletedClient != nil {
key, err := wgtypes.ParseKey(deletedClient.PublicKey)
if err == nil {
hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, wgtypes.PeerConfig{
PublicKey: key,
Remove: true,
})
if len(deletedClients) > 0 {
for i := range deletedClients {
deletedClient := deletedClients[i]
key, err := wgtypes.ParseKey(deletedClient.PublicKey)
if err == nil {
hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, wgtypes.PeerConfig{
PublicKey: key,
Remove: true,
})
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions mq/publishers.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ func PublishDeletedClientPeerUpdate(delClient *models.ExtClient) error {
logic.ResetPeerUpdateContext()
for _, host := range hosts {
host := host
if err = PublishSingleHostPeerUpdate(logic.PeerUpdateCtx, &host, nil, delClient); err != nil {
if err = PublishSingleHostPeerUpdate(logic.PeerUpdateCtx, &host, nil, []models.ExtClient{*delClient}); err != nil {
logger.Log(1, "failed to publish peer update to host", host.ID.String(), ": ", err.Error())
}
}
return err
}

// PublishSingleHostPeerUpdate --- determines and publishes a peer update to one host
func PublishSingleHostPeerUpdate(ctx context.Context, host *models.Host, deletedNode *models.Node, deletedClient *models.ExtClient) error {
func PublishSingleHostPeerUpdate(ctx context.Context, host *models.Host, deletedNode *models.Node, deletedClients []models.ExtClient) error {

peerUpdate, err := logic.GetPeerUpdateForHost(ctx, "", host, deletedNode, deletedClient)
peerUpdate, err := logic.GetPeerUpdateForHost(ctx, "", host, deletedNode, deletedClients)
if err != nil {
return err
}
Expand Down

0 comments on commit b8858bb

Please sign in to comment.