Skip to content

Commit

Permalink
fix: mutation: use generateName for generated resources when logging (
Browse files Browse the repository at this point in the history
#2974)

Signed-off-by: Alex Pana <[email protected]>
  • Loading branch information
acpana authored Sep 13, 2023
1 parent c86ddd2 commit 83ca660
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
ResourceAPIVersion = "resource_api_version"
ResourceNamespace = "resource_namespace"
ResourceName = "resource_name"
ResourceSourceType = "resource_source_type"
RequestUsername = "request_username"
MutationApplied = "mutation_applied"
Mutator = "mutator"
Expand Down
17 changes: 15 additions & 2 deletions pkg/mutation/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func logAppliedMutations(message string, mutationUUID uuid.UUID, obj *unstructured.Unstructured, allAppliedMutations [][]types.Mutator) {
func logAppliedMutations(message string, mutationUUID uuid.UUID, obj *unstructured.Unstructured, allAppliedMutations [][]types.Mutator, source types.SourceType) {
iterations := make([]interface{}, 0, 2*len(allAppliedMutations))
for i, appliedMutations := range allAppliedMutations {
if len(appliedMutations) == 0 {
Expand All @@ -33,9 +33,22 @@ func logAppliedMutations(message string, mutationUUID uuid.UUID, obj *unstructur
logging.ResourceKind, obj.GroupVersionKind().Kind,
logging.ResourceAPIVersion, obj.GroupVersionKind().Version,
logging.ResourceNamespace, obj.GetNamespace(),
logging.ResourceName, obj.GetName(),
logging.ResourceName, getNameOrGenerateName(obj),
logging.ResourceSourceType, source,
logging.ResourceLabels, obj.GetLabels(),
}
logDetails = append(logDetails, iterations...)
log.Info(message, logDetails...)
}
}

func getNameOrGenerateName(obj *unstructured.Unstructured) string {
resourceName := obj.GetName()
// for generated resources on CREATE, like a pod from a deployment,
// the name has not been populated yet, so we use the GeneratedName instead.
if resourceName == "" {
resourceName = obj.GetGenerateName()
}

return resourceName
}
10 changes: 5 additions & 5 deletions pkg/mutation/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (s *System) mutate(mutable *types.Mutable) (int, error) {
}

if *MutationLoggingEnabled {
logAppliedMutations("Mutation applied", mutationUUID, original, allAppliedMutations)
logAppliedMutations("Mutation applied", mutationUUID, original, allAppliedMutations, mutable.Source)
}

if *MutationAnnotationsEnabled {
Expand All @@ -228,7 +228,7 @@ func (s *System) mutate(mutable *types.Mutable) (int, error) {
}

if *MutationLoggingEnabled {
logAppliedMutations("Mutation not converging", mutationUUID, original, allAppliedMutations)
logAppliedMutations("Mutation not converging", mutationUUID, original, allAppliedMutations, mutable.Source)
}

return maxIterations, fmt.Errorf("%w: mutation %s not converging for %s %s %s %s",
Expand All @@ -237,7 +237,7 @@ func (s *System) mutate(mutable *types.Mutable) (int, error) {
mutable.Object.GroupVersionKind().Group,
mutable.Object.GroupVersionKind().Kind,
mutable.Object.GetNamespace(),
mutable.Object.GetName())
getNameOrGenerateName(mutable.Object))
}

func mutateErr(err error, uid uuid.UUID, mID types.ID, obj *unstructured.Unstructured) error {
Expand All @@ -247,7 +247,7 @@ func mutateErr(err error, uid uuid.UUID, mID types.ID, obj *unstructured.Unstruc
obj.GroupVersionKind().Group,
obj.GroupVersionKind().Kind,
obj.GetNamespace(),
obj.GetName())
getNameOrGenerateName(obj))
}

func matchesErr(err error, mID types.ID, obj *unstructured.Unstructured) error {
Expand All @@ -256,5 +256,5 @@ func matchesErr(err error, mID types.ID, obj *unstructured.Unstructured) error {
obj.GroupVersionKind().Group,
obj.GroupVersionKind().Kind,
obj.GetNamespace(),
obj.GetName())
getNameOrGenerateName(obj))
}

0 comments on commit 83ca660

Please sign in to comment.