Skip to content

Commit

Permalink
added logic to remove the deleted instance from cache
Browse files Browse the repository at this point in the history
  • Loading branch information
shilparamasamyreddy committed Aug 23, 2024
1 parent 9d1041c commit acfd78c
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 87 deletions.
72 changes: 33 additions & 39 deletions internal/cf/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ const (
type organizationClient struct {
organizationName string
client cfclient.Client
//resourcesCache *facade.Cache
//resourceCache *facade.Cache
}

type spaceClient struct {
spaceGuid string
client cfclient.Client
resourcesCache *facade.Cache
spaceGuid string
client cfclient.Client
resourceCache *facade.ResourceCache
}

type clientIdentifier struct {
Expand All @@ -56,10 +56,10 @@ type clientCacheEntry struct {
}

var (
cacheMutex = &sync.Mutex{}
clientCache = make(map[clientIdentifier]*clientCacheEntry)
cfCache *facade.Cache
refreshCacheMutex = &sync.Mutex{}
clientCacheMutex = &sync.Mutex{}
clientCache = make(map[clientIdentifier]*clientCacheEntry)
cfResourceCache *facade.ResourceCache
refreshResourceCacheMutex = &sync.Mutex{}
)

func newOrganizationClient(organizationName string, url string, username string, password string) (*organizationClient, error) {
Expand Down Expand Up @@ -90,11 +90,7 @@ func newOrganizationClient(organizationName string, url string, username string,
if err != nil {
return nil, err
}
// isResourceCacheEnabled, _ := strconv.ParseBool(os.Getenv("RESOURCE_CACHE_ENABLED"))
// if isResourceCacheEnabled && c.resourcesCache == nil {
// c.resourcesCache = InitResourcesCache()
// c.populateResourcesCache()
// }
// TODO:Populate resource cache for ORg client

return &organizationClient{organizationName: organizationName, client: *c}, nil
}
Expand Down Expand Up @@ -128,14 +124,12 @@ func newSpaceClient(spaceGuid string, url string, username string, password stri
return nil, err
}

spcClient := &spaceClient{spaceGuid: spaceGuid, client: *c}

return spcClient, nil
return &spaceClient{spaceGuid: spaceGuid, client: *c}, nil
}

func NewOrganizationClient(organizationName string, url string, username string, password string) (facade.OrganizationClient, error) {
cacheMutex.Lock()
defer cacheMutex.Unlock()
clientCacheMutex.Lock()
defer clientCacheMutex.Unlock()

// look up CF client in cache
identifier := clientIdentifier{url: url, username: username}
Expand Down Expand Up @@ -166,18 +160,18 @@ func NewOrganizationClient(organizationName string, url string, username string,
}

func NewSpaceClient(spaceGuid string, url string, username string, password string, config config.Config) (facade.SpaceClient, error) {
cacheMutex.Lock()
defer cacheMutex.Unlock()
clientCacheMutex.Lock()
defer clientCacheMutex.Unlock()

// look up CF client in cache
identifier := clientIdentifier{url: url, username: username}
cacheEntry, isInCache := clientCache[identifier]

var err error = nil
var spcClient *spaceClient = nil
var client *spaceClient = nil
if isInCache {
// re-use CF client from cache and wrap it as spaceClient
spcClient = &spaceClient{spaceGuid: spaceGuid, client: cacheEntry.client, resourcesCache: cfCache}
client = &spaceClient{spaceGuid: spaceGuid, client: cacheEntry.client, resourceCache: cfResourceCache}
if cacheEntry.password != password {
// password was rotated => delete client from cache and create a new one below
delete(clientCache, identifier)
Expand All @@ -187,27 +181,27 @@ func NewSpaceClient(spaceGuid string, url string, username string, password stri

if !isInCache {
// create new CF client and wrap it as spaceClient
spcClient, err = newSpaceClient(spaceGuid, url, username, password)
client, err = newSpaceClient(spaceGuid, url, username, password)
if err == nil {
// add CF client to cache
clientCache[identifier] = &clientCacheEntry{url: url, username: username, password: password, client: spcClient.client}
clientCache[identifier] = &clientCacheEntry{url: url, username: username, password: password, client: client.client}
}
}

if config.IsResourceCacheEnabled && spcClient.resourcesCache == nil {
spcClient.resourcesCache = facade.InitResourcesCache()
spcClient.resourcesCache.SetResourceCacheEnabled(config.IsResourceCacheEnabled)
spcClient.resourcesCache.SetCacheTimeOut(config.CacheTimeOut)
spcClient.populateResourcesCache()
if config.IsResourceCacheEnabled && client.resourceCache == nil {
client.resourceCache = facade.InitResourceCache()
client.resourceCache.SetResourceCacheEnabled(config.IsResourceCacheEnabled)
client.resourceCache.SetCacheTimeOut(config.CacheTimeOut)
client.populateResourceCache()
}

return spcClient, err
return client, err

}

func NewSpaceHealthChecker(spaceGuid string, url string, username string, password string) (facade.SpaceHealthChecker, error) {
cacheMutex.Lock()
defer cacheMutex.Unlock()
clientCacheMutex.Lock()
defer clientCacheMutex.Unlock()

// look up CF client in cache
identifier := clientIdentifier{url: url, username: username}
Expand Down Expand Up @@ -237,12 +231,12 @@ func NewSpaceHealthChecker(spaceGuid string, url string, username string, passwo
return client, err
}

func (c *spaceClient) populateResourcesCache() {
func (c *spaceClient) populateResourceCache() {
// TODO: Create the space options
// TODO: Add for loop for space
refreshCacheMutex.Lock()
defer refreshCacheMutex.Unlock()
if c.resourcesCache.IsCacheExpired() {
refreshResourceCacheMutex.Lock()
defer refreshResourceCacheMutex.Unlock()
if c.resourceCache.IsCacheExpired() {
instanceOptions := cfclient.NewServiceInstanceListOptions()
instanceOptions.ListOptions.LabelSelector.EqualTo(labelOwner)
instanceOptions.Page = 1
Expand All @@ -263,7 +257,7 @@ func (c *spaceClient) populateResourcesCache() {
instance, err := InitInstance(serviceInstance)
// instance is added to cache only if error is nil
if err == nil {
c.resourcesCache.AddInstanceInCache(*serviceInstance.Metadata.Labels[labelOwner], instance)
c.resourceCache.AddInstanceInCache(*serviceInstance.Metadata.Labels[labelOwner], instance)
}
}

Expand All @@ -274,8 +268,8 @@ func (c *spaceClient) populateResourcesCache() {

pager.NextPage(instanceOptions)
}
c.resourcesCache.SetLastCacheTime()
cfCache = c.resourcesCache
c.resourceCache.SetLastCacheTime()
cfResourceCache = c.resourceCache
}
// TODO: Add for loop for bindings
}
17 changes: 9 additions & 8 deletions internal/cf/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ func (io *instanceFilterOwner) getListOptions() *cfclient.ServiceInstanceListOpt
// The function add the parameter values to the orphan cf instance, so that can be adopted.
func (c *spaceClient) GetInstance(ctx context.Context, instanceOpts map[string]string) (*facade.Instance, error) {

if c.resourcesCache.IsResourceCacheEnabled() {
if c.resourceCache.IsResourceCacheEnabled() {
// Ensure resourcesCache is initialized
if c.resourcesCache == nil {
c.resourcesCache = facade.InitResourcesCache()
if c.resourceCache == nil {
c.resourceCache = facade.InitResourceCache()
}

// Attempt to retrieve instance from Cache
var instanceInCache bool
var instance *facade.Instance

if len(c.resourcesCache.GetCachedInstances()) != 0 {
if c.resourcesCache.IsCacheExpired() {
if len(c.resourceCache.GetCachedInstances()) != 0 {
if c.resourceCache.IsCacheExpired() {

c.populateResourcesCache()
c.populateResourceCache()
}

instance, instanceInCache = c.resourcesCache.GetInstanceFromCache(instanceOpts["owner"])
instance, instanceInCache = c.resourceCache.GetInstanceFromCache(instanceOpts["owner"])
}

if instanceInCache {
Expand Down Expand Up @@ -163,9 +163,10 @@ func (c *spaceClient) UpdateInstance(ctx context.Context, guid string, name stri
return err
}

func (c *spaceClient) DeleteInstance(ctx context.Context, guid string) error {
func (c *spaceClient) DeleteInstance(ctx context.Context, guid string, owner string) error {
// TODO: return jobGUID to enable querying the job deletion status
_, err := c.client.ServiceInstances.Delete(ctx, guid)
c.resourceCache.RemoveInstanceFromCache(owner)
return err
}

Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type Config struct {

//Resource cache is enabled or disabled
//TODO change it back to false after testing
IsResourceCacheEnabled bool `env:"RESOURCE_CACHE_ENABLED" envDefault:"true"`

//cache timeout in seconds,minutes or hours
Expand Down
4 changes: 2 additions & 2 deletions internal/controllers/serviceinstance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (r *ServiceInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Requ
} else if recreateOnCreationFailure && (cfinstance.State == facade.InstanceStateCreatedFailed || cfinstance.State == facade.InstanceStateDeleteFailed) {
// Re-create instance
log.V(1).Info("Deleting instance for later re-creation")
if err := client.DeleteInstance(ctx, cfinstance.Guid); err != nil {
if err := client.DeleteInstance(ctx, cfinstance.Guid, cfinstance.Owner); err != nil {
return ctrl.Result{}, RetryError
}
status.LastModifiedAt = &[]metav1.Time{metav1.Now()}[0]
Expand Down Expand Up @@ -425,7 +425,7 @@ func (r *ServiceInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Requ
} else {
if cfinstance.State != facade.InstanceStateDeleting {
log.V(1).Info("Deleting instance")
if err := client.DeleteInstance(ctx, cfinstance.Guid); err != nil {
if err := client.DeleteInstance(ctx, cfinstance.Guid, cfinstance.Owner); err != nil {
return ctrl.Result{}, err
}
status.LastModifiedAt = &[]metav1.Time{metav1.Now()}[0]
Expand Down
Loading

0 comments on commit acfd78c

Please sign in to comment.