Skip to content

Commit

Permalink
Add some logging to the repository cache
Browse files Browse the repository at this point in the history
[#226]

Co-authored-by: Todd Ritchie <tritchie@vmware.com>
  • Loading branch information
2 people authored and emmjohnson committed Oct 22, 2021
1 parent c4aff20 commit 7b92d08
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 9 deletions.
10 changes: 5 additions & 5 deletions pkg/registrar/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func RegisterControllers(mgr manager.Manager) error {
}

func registerWorkloadController(mgr manager.Manager) error {
repo := repository.NewRepository(mgr.GetClient(), repository.NewCache(cache.NewExpiring()))
repo := repository.NewRepository(mgr.GetClient(), repository.NewCache(cache.NewExpiring(), mgr.GetLogger().WithName("workload-repo-cache")))

ctrl, err := pkgcontroller.New("workload", mgr, pkgcontroller.Options{
Reconciler: workload.NewReconciler(repo, conditions.NewConditionManager, realizerworkload.NewRealizer()),
Expand Down Expand Up @@ -114,7 +114,7 @@ func registerWorkloadController(mgr manager.Manager) error {
}

func registerSupplyChainController(mgr manager.Manager) error {
repo := repository.NewRepository(mgr.GetClient(), repository.NewCache(cache.NewExpiring()))
repo := repository.NewRepository(mgr.GetClient(), repository.NewCache(cache.NewExpiring(), mgr.GetLogger().WithName("supply-chain-repo-cache")))

ctrl, err := pkgcontroller.New("supply-chain", mgr, pkgcontroller.Options{
Reconciler: supplychain.NewReconciler(repo, conditions.NewConditionManager),
Expand All @@ -134,7 +134,7 @@ func registerSupplyChainController(mgr manager.Manager) error {
}

func registerDeliveryController(mgr manager.Manager) error {
repo := repository.NewRepository(mgr.GetClient(), repository.NewCache(cache.NewExpiring()))
repo := repository.NewRepository(mgr.GetClient(), repository.NewCache(cache.NewExpiring(), mgr.GetLogger().WithName("delivery-repo-cache")))

ctrl, err := pkgcontroller.New("delivery", mgr, pkgcontroller.Options{
Reconciler: delivery.NewReconciler(repo),
Expand All @@ -154,7 +154,7 @@ func registerDeliveryController(mgr manager.Manager) error {
}

func registerDeliverableController(mgr manager.Manager) error {
repo := repository.NewRepository(mgr.GetClient(), repository.NewCache(cache.NewExpiring()))
repo := repository.NewRepository(mgr.GetClient(), repository.NewCache(cache.NewExpiring(), mgr.GetLogger().WithName("deliverable-repo-cache")))

ctrl, err := pkgcontroller.New("deliverable", mgr, pkgcontroller.Options{
Reconciler: deliverable.NewReconciler(repo, conditions.NewConditionManager, realizerdeliverable.NewRealizer()),
Expand Down Expand Up @@ -186,7 +186,7 @@ func registerDeliverableController(mgr manager.Manager) error {
}

func registerPipelineServiceController(mgr manager.Manager) error {
repo := repository.NewRepository(mgr.GetClient(), repository.NewCache(cache.NewExpiring()))
repo := repository.NewRepository(mgr.GetClient(), repository.NewCache(cache.NewExpiring(), mgr.GetLogger().WithName("pipeline-repo-cache")))

reconciler := pipeline.NewReconciler(repo, realizerpipeline.NewRealizer())
ctrl, err := pkgcontroller.New("pipeline-service", mgr, pkgcontroller.Options{
Expand Down
23 changes: 20 additions & 3 deletions pkg/repository/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ const submittedCachePrefix = "submitted"
const persistedCachePrefix = "persisted"
const CacheExpiryDuration = 1 * time.Hour

//counterfeiter:generate . Logger
type Logger interface {
Error(err error, msg string, keysAndValues ...interface{})
Info(msg string, keysAndValues ...interface{})
}

//counterfeiter:generate . ExpiringCache
type ExpiringCache interface {
Get(key interface{}) (val interface{}, ok bool)
Expand All @@ -39,14 +45,16 @@ type RepoCache interface {
Refresh(submitted *unstructured.Unstructured)
}

func NewCache(c ExpiringCache) RepoCache {
func NewCache(c ExpiringCache, l Logger) RepoCache {
return &cache{
ec: c,
ec: c,
logger: l,
}
}

type cache struct {
ec ExpiringCache
ec ExpiringCache
logger Logger
}

func (c *cache) Set(submitted, persisted *unstructured.Unstructured) {
Expand Down Expand Up @@ -76,24 +84,33 @@ func (c *cache) UnchangedSinceCached(submitted *unstructured.Unstructured, exist
persistedCached := c.getPersistedCached(persistedKey)

if !submittedUnchanged {
if submittedCached != nil {
c.logger.Info("miss: submitted object in cache is different from submitted object")
} else {
c.logger.Info("miss: object not in cache")
}
return nil
}

for _, existing := range existingList {
existingSpec, ok := existing.Object["spec"]
if !ok {
c.logger.Info("miss: object on apiserver has no spec")
continue
}

persistedCachedSpec, ok := persistedCached.Object["spec"]
if !ok {
c.logger.Info("miss: persisted object in cache has no spec")
continue
}

sameSame := reflect.DeepEqual(existingSpec, persistedCachedSpec)
if sameSame {
c.logger.Info("hit: persisted object in cache matches spec on apiserver")
return existing
} else {
c.logger.Info("miss: persisted object in cache DOES NOT match spec on apiserver")
continue
}
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/repository/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ var _ = Describe("Cache", func() {
var (
cache repository.RepoCache
fakeExpiringCache *repositoryfakes.FakeExpiringCache
fakeLogger *repositoryfakes.FakeLogger
submitted, persisted *unstructured.Unstructured
)

BeforeEach(func() {
fakeExpiringCache = &repositoryfakes.FakeExpiringCache{}
cache = repository.NewCache(fakeExpiringCache)
fakeLogger = &repositoryfakes.FakeLogger{}
cache = repository.NewCache(fakeExpiringCache, fakeLogger)

objKind := "the-kind"
objName := "its-name"
Expand Down
121 changes: 121 additions & 0 deletions pkg/repository/repositoryfakes/fake_logger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7b92d08

Please sign in to comment.