Skip to content

Commit

Permalink
Merge pull request kubernetes#2291 from tosi3k/lazy-gocmp
Browse files Browse the repository at this point in the history
Compute gocmp.Diff lazily in ClusterLoader2
  • Loading branch information
k8s-ci-robot committed Jun 20, 2023
2 parents c356da9 + a74088d commit daebee8
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ func GetIsPodUpdatedPredicateFromRuntimeObject(obj runtime.Object) (func(*corev1
}
}

// Auxiliary error type for lazy evaluation of gocmp.Diff which is known to be
// computationally expensive.
type lazySpecDiffError struct {
templateSpec corev1.PodSpec
podSpec corev1.PodSpec
}

func (lsde *lazySpecDiffError) Error() string {
return fmt.Sprintf("Not matching templates, diff: %v", gocmp.Diff(lsde.templateSpec, lsde.podSpec))
}

func getIsPodUpdatedPodPredicateFromUnstructured(obj *unstructured.Unstructured) (func(_ *corev1.Pod) error, error) {
templateMap, ok, err := unstructured.NestedMap(obj.UnstructuredContent(), "spec", "template")
if err != nil {
Expand All @@ -106,7 +117,7 @@ func getIsPodUpdatedPodPredicateFromUnstructured(obj *unstructured.Unstructured)

return func(pod *corev1.Pod) error {
if !equality.Semantic.DeepDerivative(template.Spec, pod.Spec) {
return goerrors.Errorf("Not matching templates, diff: %v", gocmp.Diff(template.Spec, pod.Spec))
return &lazySpecDiffError{template.Spec, pod.Spec}
}
return nil
}, nil
Expand Down

0 comments on commit daebee8

Please sign in to comment.