Skip to content

Commit

Permalink
health.go uses the cache to retrieve the space
Browse files Browse the repository at this point in the history
  • Loading branch information
santiago-ventura committed Sep 20, 2024
1 parent 443f26c commit 109a262
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
9 changes: 8 additions & 1 deletion internal/cf/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func NewSpaceClient(spaceGuid string, url string, username string, password stri

}

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

Expand Down Expand Up @@ -254,6 +254,13 @@ func NewSpaceHealthChecker(spaceGuid string, url string, username string, passwo
}
}

if config.IsResourceCacheEnabled && client.resourceCache == nil {
if cfResourceCache != nil {
// It is expected cfResourceCache be already populated
client.resourceCache = cfResourceCache
}
}

return client, err
}

Expand Down
10 changes: 8 additions & 2 deletions internal/cf/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ package cf

import "context"

func (c *spaceClient) Check(ctx context.Context) error {
// TODO: Need to check if caching needed here or code can be removed
// TODO: Ask why do we have the health check with a different client than the origanization unit?
func (c *spaceClient) Check(ctx context.Context, owner string) error {
if c.resourceCache.checkResourceCacheEnabled() {
_, inCache := c.resourceCache.getSpaceFromCache(owner)
if inCache {
return nil
}
}
_, err := c.client.Spaces.Get(ctx, c.spaceGuid)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions internal/controllers/space_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,13 @@ func (r *SpaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resu
url := string(secret.Data["url"])
username := string(secret.Data["username"])
password := string(secret.Data["password"])
checker, err := r.HealthCheckerBuilder(status.SpaceGuid, url, username, password)
checker, err := r.HealthCheckerBuilder(status.SpaceGuid, url, username, password, r.Config)
if err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to build the healthchecker from secret %s", secretName)
}

log.V(1).Info("Checking space")
if err := checker.Check(ctx); err != nil {
if err := checker.Check(ctx, cfspace.Owner); err != nil {
return ctrl.Result{}, errors.Wrap(err, "healthcheck failed")
}

Expand Down
10 changes: 7 additions & 3 deletions internal/facade/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ SPDX-License-Identifier: Apache-2.0

package facade

import "context"
import (
"context"

"github.com/sap/cf-service-operator/internal/config"
)

//counterfeiter:generate . SpaceHealthChecker
type SpaceHealthChecker interface {
Check(ctx context.Context) error
Check(ctx context.Context, owner string) error
}

type SpaceHealthCheckerBuilder func(string, string, string, string) (SpaceHealthChecker, error)
type SpaceHealthCheckerBuilder func(string, string, string, string, *config.Config) (SpaceHealthChecker, error)

0 comments on commit 109a262

Please sign in to comment.