Skip to content

Commit

Permalink
controlplane: connect enable/disable peer to heartbeat
Browse files Browse the repository at this point in the history
Signed-off-by: Kfir Toledo <[email protected]>
  • Loading branch information
kfirtoledo committed Nov 13, 2023
1 parent fd36ec1 commit 9341e77
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
26 changes: 19 additions & 7 deletions pkg/controlplane/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ const (
// client for accessing a remote peer.
type client struct {
// jsonapi clients for connecting to the remote peer (one per each gateway)
clients []*jsonapi.Client
lastSeen time.Time
active bool
stopSignal chan struct{}
lock sync.RWMutex
logger *logrus.Entry
clients []*jsonapi.Client
lastSeen time.Time
active bool
stopSignal chan struct{}
lock sync.RWMutex
logger *logrus.Entry
peerStatusCallback func(bool) // Callback function for notifying changes in peer
}

// remoteServerAuthorizationResponse represents an authorization response received from a remote controlplane server.
Expand Down Expand Up @@ -114,11 +115,17 @@ func (c *client) IsActive() bool {
// setActive the peer status (active or not).
func (c *client) setActive(active bool) {
c.lock.Lock()
defer c.lock.Unlock()
activePrevState := c.active
c.active = active
if active || c.lastSeen.IsZero() {
c.lastSeen = time.Now()
}
c.lock.Unlock()

// Update other components like the policy engine with the peer status.
if active != activePrevState && c.peerStatusCallback != nil {
c.peerStatusCallback(active)
}
}

// GetHeartbeat get a heartbeat from other peers.
Expand Down Expand Up @@ -183,6 +190,11 @@ func (c *client) heartbeatMonitor() {
}
}

// SetPeerStatusCallback set the peerStatusCallback.
func (c *client) SetPeerStatusCallback(callback func(bool)) {
c.peerStatusCallback = callback
}

// newClient returns a new Peer API client.
func newClient(peer *store.Peer, tlsConfig *tls.Config) *client {
clients := make([]*jsonapi.Client, len(peer.Gateways))
Expand Down
9 changes: 9 additions & 0 deletions pkg/controlplane/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ func (cp *Instance) CreatePeer(peer *cpstore.Peer) error {

cp.policyDecider.AddPeer(&api.Peer{Name: peer.Name, Spec: peer.PeerSpec})

client.SetPeerStatusCallback(func(isActive bool) {
if isActive {
cp.policyDecider.EnablePeer(peer.Name)
return
}

cp.policyDecider.DisablePeer(peer.Name)
})

return nil
}

Expand Down

0 comments on commit 9341e77

Please sign in to comment.