Skip to content

Commit

Permalink
using logr key-value pairs instead of format strings in log messages
Browse files Browse the repository at this point in the history
Co-authored-by: Sam Coward <[email protected]>
  • Loading branch information
2 people authored and emmjohnson committed Oct 22, 2021
1 parent ff741d3 commit 4ded183
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
20 changes: 10 additions & 10 deletions pkg/repository/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,48 +54,48 @@ func (c *cache) Set(submitted, persisted *unstructured.Unstructured) {

func (c *cache) UnchangedSinceCached(submitted *unstructured.Unstructured, existingList []*unstructured.Unstructured) *unstructured.Unstructured {
key := getKey(submitted)
c.logger.Info("key: %s checking for changes since cached for key: %s", key)
c.logger.Info("checking for changes since cached", "key", key)
submittedCached, submittedFoundInCache := c.submittedCache[key]
submittedUnchanged := submittedFoundInCache && reflect.DeepEqual(submittedCached, *submitted)

persistedCached := c.getPersistedCached(key)

if submittedUnchanged {
c.logger.Info("key: %s no changes since last submission, checking existing objects on apiserver", key)
c.logger.Info("no changes since last submission, checking existing objects on apiserver", "key", key)
} else {
if submittedFoundInCache {
c.logger.Info("key: %s miss: submitted object in cache is different from submitted object", key)
c.logger.Info("miss: submitted object in cache is different from submitted object", "key", key)
} else {
c.logger.Info("key: %s miss: object not in cache", key)
c.logger.Info("miss: object not in cache", "key", key)
}
return nil
}

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

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

sameSame := reflect.DeepEqual(existingSpec, persistedCachedSpec)
if sameSame {
c.logger.Info("key: %s hit: persisted object in cache matches spec on apiserver", key)
c.logger.Info("hit: persisted object in cache matches spec on apiserver", "key", key)
return existing
} else {
c.logger.Info("key: %s persisted object in cache DOES NOT match spec on apiserver", key)
c.logger.Info("miss: persisted object in cache DOES NOT match spec on apiserver", "key", key)
continue
}
}

c.logger.Info("key: %s miss: no matching existing object on apiserver", key)
c.logger.Info("miss: no matching existing object on apiserver", "key", key)
return nil
}

Expand Down
13 changes: 10 additions & 3 deletions pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package repository
import (
"context"
"fmt"
"strings"

api_errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -85,7 +86,13 @@ func (r *repository) GetDelivery(name string) (*v1alpha1.ClusterDelivery, error)

func (r *repository) EnsureObjectExistsOnCluster(obj *unstructured.Unstructured, allowUpdate bool) error {
unstructuredList, err := r.ListUnstructured(obj)
r.logger.Info("considering objects from apiserver: %+v", unstructuredList)

var names []string
for _, considered := range unstructuredList {
names = append(names, considered.GetName())
}
r.logger.Info("considering objects from apiserver", "consideredList", strings.Join(names, ", "))

if err != nil {
return err
}
Expand All @@ -102,10 +109,10 @@ func (r *repository) EnsureObjectExistsOnCluster(obj *unstructured.Unstructured,
}

if outdatedObject != nil {
r.logger.Info("patching object %s (ns %s kind %s)", obj.GetName(), obj.GetNamespace(), obj.GetKind())
r.logger.Info("patching object", "name", obj.GetName(), "namespace", obj.GetNamespace(), "kind", obj.GetKind())
return r.patchUnstructured(outdatedObject, obj)
} else {
r.logger.Info("creating object %s (ns %s kind %s)", obj.GetName(), obj.GetNamespace(), obj.GetKind())
r.logger.Info("creating object", "name", obj.GetName(), "namespace", obj.GetNamespace(), "kind", obj.GetKind())
return r.createUnstructured(obj)
}
}
Expand Down

0 comments on commit 4ded183

Please sign in to comment.