Skip to content

Commit

Permalink
feat: support deleting an affiliate
Browse files Browse the repository at this point in the history
This allows to delete an affiliate instead of refreshing the data.

Unit-tests will be handled in a discovery-service PR.

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
  • Loading branch information
smira committed Aug 25, 2022
1 parent 27a5bee commit ac5ab32
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ type Options struct {
type Client struct {
options Options

localMu sync.Mutex
localAffiliate []byte
localEndpoints [][]byte
otherEndpoints []endpointData
localUpdatesCh chan struct{}
localMu sync.Mutex
deleteAffiliate bool
localAffiliate []byte
localEndpoints [][]byte
otherEndpoints []endpointData
localUpdatesCh chan struct{}

discoveredMu sync.Mutex
discoveredAffiliates map[string]*Affiliate
Expand Down Expand Up @@ -156,6 +157,8 @@ func (client *Client) SetLocalData(localAffiliate *Affiliate, otherEndpoints []E
client.localMu.Lock()
defer client.localMu.Unlock()

client.deleteAffiliate = false

client.localAffiliate = append([]byte(nil), nonce...)
client.localAffiliate = client.gcm.Seal(client.localAffiliate, nonce, localAffiliateData, nil)

Expand Down Expand Up @@ -186,6 +189,24 @@ func (client *Client) SetLocalData(localAffiliate *Affiliate, otherEndpoints []E
return nil
}

// DeleteLocalAffiliate marks local affiliate for deletion.
//
// Actual deletion happens on the next update in the Run loop.
func (client *Client) DeleteLocalAffiliate() {
client.localMu.Lock()
defer client.localMu.Unlock()

client.deleteAffiliate = true
client.localAffiliate = nil
client.localEndpoints = nil
client.otherEndpoints = nil

select {
case client.localUpdatesCh <- struct{}{}:
default:
}
}

// GetAffiliates returns discovered affiliates.
func (client *Client) GetAffiliates() []*Affiliate {
client.discoveredMu.Lock()
Expand Down Expand Up @@ -459,11 +480,24 @@ func (client *Client) refreshData(ctx context.Context, discoveryClient serverpb.
defer cancel()

client.localMu.Lock()
deleteAffiliate := client.deleteAffiliate
localAffiliate := client.localAffiliate
localEndpoints := client.localEndpoints
otherEndpoints := client.otherEndpoints
client.localMu.Unlock()

if deleteAffiliate {
_, err := discoveryClient.AffiliateDelete(ctx, &serverpb.AffiliateDeleteRequest{
ClusterId: client.options.ClusterID,
AffiliateId: client.options.AffiliateID,
})
if err != nil {
return fmt.Errorf("error deleting local affiliate: %w", err)
}

return nil
}

if localAffiliate == nil {
// no local data yet
return nil
Expand Down

0 comments on commit ac5ab32

Please sign in to comment.