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

[WIP] Add states to assigned identity #255

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/cloudprovider/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func filterUserIdentity(idType *compute.ResourceIdentityType, idList *[]string,
case compute.ResourceIdentityTypeUserAssigned,
compute.ResourceIdentityTypeSystemAssignedUserAssigned:
default:
return errNotFound
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should check for TypeNone and return nil from there, but keep the default as is.

}

origLen := len(*idList)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cloudprovider/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
func TestFilterIdentity(t *testing.T) {
idList := []string{}
idType := compute.ResourceIdentityTypeNone
if err := filterUserIdentity(&idType, &idList, "A"); err == nil || err != errNotFound {
t.Fatalf("expected error %q, got: %v", errNotFound, err)
if err := filterUserIdentity(&idType, &idList, "A"); err != nil {
t.Fatalf("expected no error, got: %v", err)
}

idType = compute.ResourceIdentityTypeUserAssigned
Expand Down
41 changes: 37 additions & 4 deletions pkg/crd/crd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package crd

import (
"encoding/json"
"fmt"
"time"

Expand All @@ -13,6 +14,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
)
Expand All @@ -31,6 +33,7 @@ type ClientInt interface {
SyncCache(exit <-chan struct{})
RemoveAssignedIdentity(assignedIdentity *aadpodid.AzureAssignedIdentity) error
CreateAssignedIdentity(assignedIdentity *aadpodid.AzureAssignedIdentity) error
UpdateAzureAssignedIdentityStatus(assignedIdentity *aadpodid.AzureAssignedIdentity, status string) error
ListBindings() (res *[]aadpodid.AzureIdentityBinding, err error)
ListAssignedIDs() (res *[]aadpodid.AzureAssignedIdentity, err error)
ListIds() (res *[]aadpodid.AzureIdentity, err error)
Expand Down Expand Up @@ -191,29 +194,27 @@ func (c *Client) SyncCache(exit <-chan struct{}) {
}

func (c *Client) RemoveAssignedIdentity(assignedIdentity *aadpodid.AzureAssignedIdentity) error {
glog.V(6).Infof("Deletion of id named: %s", assignedIdentity.Name)
glog.V(6).Infof("Deletion of assigned id named: %s", assignedIdentity.Name)
begin := time.Now()
err := c.rest.Delete().Namespace(assignedIdentity.Namespace).Resource("azureassignedidentities").Name(assignedIdentity.Name).Do().Error()
stats.Update(stats.AssignedIDDel, time.Since(begin))
return err
}

func (c *Client) CreateAssignedIdentity(assignedIdentity *aadpodid.AzureAssignedIdentity) error {
glog.Infof("Got id %s to assign", assignedIdentity.Name)
glog.Infof("Got assigned id %s to create", assignedIdentity.Name)
begin := time.Now()
// Create a new AzureAssignedIdentity which maps the relationship between
// id and pod
glog.Infof("Creating assigned Id: %s", assignedIdentity.Name)
var res aadpodid.AzureAssignedIdentity
// TODO: Ensure that the status reflects the corresponding
err := c.rest.Post().Namespace(assignedIdentity.Namespace).Resource("azureassignedidentities").Body(assignedIdentity).Do().Into(&res)
if err != nil {
glog.Error(err)
return err
}

stats.Update(stats.AssignedIDAdd, time.Since(begin))
//TODO: Update the status of the assign identity to indicate that the node assignment got done.
return nil
}

Expand Down Expand Up @@ -268,3 +269,35 @@ func (c *Client) ListPodIds(podns, podname string) (*[]aadpodid.AzureIdentity, e

return &matchedIds, nil
}

type patchStatusOps struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value"`
}

// UpdateAzureAssignedIdentityStatus updates the status field in AzureAssignedIdentity to indicate current status
func (c *Client) UpdateAzureAssignedIdentityStatus(assignedIdentity *aadpodid.AzureAssignedIdentity, status string) error {
glog.Infof("Updating assigned identity %s/%s status to %s", assignedIdentity.Namespace, assignedIdentity.Name, status)

ops := make([]patchStatusOps, 1)
ops[0].Op = "replace"
ops[0].Path = "/Status/status"
ops[0].Value = status

patchBytes, err := json.Marshal(ops)
if err != nil {
return err
}

err = c.rest.
Patch(types.JSONPatchType).
Namespace(assignedIdentity.Namespace).
Resource("azureassignedidentities").
Name(assignedIdentity.Name).
Body(patchBytes).
Do().
Error()

return err
}
Loading