Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
batch processing
Browse files Browse the repository at this point in the history
wip

batch process createorupdate

update logic

update events

fix events

generate set to use unique values

update interface

reorder check flow

update to using one get per node

update tests

add e2e test for testing scale perf

create assigned identity before assignment

add unit tests for UpdateUserMSI interface
  • Loading branch information
aramase committed Jun 25, 2019
1 parent 5f49b30 commit 5ecf29d
Show file tree
Hide file tree
Showing 12 changed files with 404 additions and 86 deletions.
57 changes: 57 additions & 0 deletions pkg/cloudprovider/cloudprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ type Client struct {
Config config.AzureConfig
}

// ClientInt client interface
type ClientInt interface {
RemoveUserMSI(userAssignedMSIID string, node *corev1.Node) error
AssignUserMSI(userAssignedMSIID string, node *corev1.Node) error
UpdateUserMSI(addUserAssignedMSIIDs []string, removeUserAssignedMSIIDs []string, node *corev1.Node) error
GetUserMSIs(node *corev1.Node) ([]string, error)
}

// NewCloudProvider returns a azure cloud provider client
Expand Down Expand Up @@ -112,6 +115,59 @@ func withInspection() autorest.PrepareDecorator {
}
}

// GetUserMSIs will return a list of all identities on the node
func (c *Client) GetUserMSIs(node *corev1.Node) ([]string, error) {
idH, _, err := c.getIdentityResource(node)
if err != nil {
glog.Errorf("GetUserMSIs: get identity resource failed with error %v", err)
return nil, err
}
info := idH.IdentityInfo()
if info == nil {
return nil, fmt.Errorf("identity info is nil")
}
idList := info.GetUserIdentityList()
return idList, nil
}

// UpdateUserMSI will batch process the removal and addition of ids
func (c *Client) UpdateUserMSI(addUserAssignedMSIIDs []string, removeUserAssignedMSIIDs []string, node *corev1.Node) error {
idH, updateFunc, err := c.getIdentityResource(node)
if err != nil {
return err
}

info := idH.IdentityInfo()
if info == nil {
info = idH.ResetIdentity()
}

requiresUpdate := false
// remove msi ids from the list
for _, userAssignedMSIID := range removeUserAssignedMSIIDs {
requiresUpdate = true
if err := info.RemoveUserIdentity(userAssignedMSIID); err != nil {
return fmt.Errorf("could not remove identity from node %s: %v", node.Name, err)
}
}
// add new ids to the list
for _, userAssignedMSIID := range addUserAssignedMSIIDs {
addedToList := info.AppendUserIdentity(userAssignedMSIID)
if !addedToList {
glog.V(6).Infof("Identity %s already assigned to node %s. Skipping assignment.", userAssignedMSIID, node.Name)
}
requiresUpdate = requiresUpdate || addedToList
}
if requiresUpdate {
timeStarted := time.Now()
if err := updateFunc(); err != nil {
return err
}
glog.V(6).Infof("UpdateUserMSI of %s completed in %s", node.Name, time.Since(timeStarted))
}
return nil
}

//RemoveUserMSI - Use the underlying cloud api calls and remove the given user assigned MSI from the vm.
func (c *Client) RemoveUserMSI(userAssignedMSIID string, node *corev1.Node) error {
idH, updateFunc, err := c.getIdentityResource(node)
Expand All @@ -137,6 +193,7 @@ func (c *Client) RemoveUserMSI(userAssignedMSIID string, node *corev1.Node) erro
return nil
}

// AssignUserMSI - Use the underlying cloud api call and add the given user assigned MSI to the vm
func (c *Client) AssignUserMSI(userAssignedMSIID string, node *corev1.Node) error {
// Get the vm using the VmClient
// Update the assigned identity into the VM using the CreateOrUpdate
Expand Down
22 changes: 22 additions & 0 deletions pkg/cloudprovider/cloudprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ func TestSimple(t *testing.T) {
cloudClient.PrintMSI(t)
t.Error("MSI mismatch")
}

// test the UpdateUserMSI interface
cloudClient.UpdateUserMSI([]string{"ID1", "ID2", "ID3"}, []string{"ID0again"}, node0)
testMSI = []string{"ID1", "ID2", "ID3"}
if !cloudClient.CompareMSI(node0, testMSI) {
cloudClient.PrintMSI(t)
t.Error("MSI mismatch")
}

cloudClient.UpdateUserMSI(nil, []string{"ID3"}, node3)
testMSI = []string{}
if !cloudClient.CompareMSI(node3, testMSI) {
cloudClient.PrintMSI(t)
t.Error("MSI mismatch")
}

cloudClient.UpdateUserMSI([]string{"ID3"}, nil, node4)
testMSI = []string{"ID4", "ID3"}
if !cloudClient.CompareMSI(node4, testMSI) {
cloudClient.PrintMSI(t)
t.Error("MSI mismatch")
}
})
}
}
Expand Down
16 changes: 12 additions & 4 deletions pkg/cloudprovider/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type IdentityHolder interface {
type IdentityInfo interface {
AppendUserIdentity(id string) bool
RemoveUserIdentity(id string) error
GetUserIdentityList() []string
}

var (
Expand Down Expand Up @@ -78,10 +79,8 @@ func appendUserIdentity(idType *compute.ResourceIdentityType, idList *[]string,
switch *idType {
case compute.ResourceIdentityTypeUserAssigned, compute.ResourceIdentityTypeSystemAssignedUserAssigned:
// check if this ID is already in the list
for _, id := range *idList {
if id == newID {
return false
}
if checkIfIDInList(*idList, newID) {
return false
}
case compute.ResourceIdentityTypeSystemAssigned:
*idType = compute.ResourceIdentityTypeSystemAssignedUserAssigned
Expand All @@ -92,3 +91,12 @@ func appendUserIdentity(idType *compute.ResourceIdentityType, idList *[]string,
*idList = append(*idList, newID)
return true
}

func checkIfIDInList(idList []string, desiredID string) bool {
for _, id := range idList {
if id == desiredID {
return true
}
}
return false
}
4 changes: 4 additions & 0 deletions pkg/cloudprovider/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,7 @@ func (i *vmIdentityInfo) AppendUserIdentity(id string) bool {
}
return appendUserIdentity(&i.info.Type, i.info.IdentityIds, id)
}

func (i *vmIdentityInfo) GetUserIdentityList() []string {
return *i.info.IdentityIds
}
4 changes: 4 additions & 0 deletions pkg/cloudprovider/vmss.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,7 @@ func (i *vmssIdentityInfo) AppendUserIdentity(id string) bool {
}
return appendUserIdentity(&i.info.Type, i.info.IdentityIds, id)
}

func (i *vmssIdentityInfo) GetUserIdentityList() []string {
return *i.info.IdentityIds
}
Loading

0 comments on commit 5ecf29d

Please sign in to comment.