Skip to content

Commit

Permalink
Change object logs to controllerRuntime style Resource reference
Browse files Browse the repository at this point in the history
  • Loading branch information
killianmuldoon committed Jul 27, 2022
1 parent f496712 commit bc79af1
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ package log
import (
"context"
"fmt"
"unicode"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand Down Expand Up @@ -85,9 +89,12 @@ type topologyReconcileLogger struct {
func (l *topologyReconcileLogger) WithObject(obj client.Object) Logger {
return &topologyReconcileLogger{
Logger: l.Logger.WithValues(
"object groupVersion", obj.GetObjectKind().GroupVersionKind().GroupVersion().String(),
"object kind", obj.GetObjectKind().GroupVersionKind().Kind,
"object", obj.GetName(),
"resource", metav1.GroupVersionResource{
Version: obj.GetObjectKind().GroupVersionKind().Version,
Group: obj.GetObjectKind().GroupVersionKind().GroupKind().Group,
Resource: obj.GetObjectKind().GroupVersionKind().Kind,
},
kindWithLowerCaseInitial(obj), klog.KObj(obj),
),
}
}
Expand All @@ -97,9 +104,12 @@ func (l *topologyReconcileLogger) WithObject(obj client.Object) Logger {
func (l *topologyReconcileLogger) WithRef(ref *corev1.ObjectReference) Logger {
return &topologyReconcileLogger{
Logger: l.Logger.WithValues(
"object groupVersion", ref.APIVersion,
"object kind", ref.Kind,
"object", ref.Name,
"resource", metav1.GroupVersionResource{
Version: ref.GetObjectKind().GroupVersionKind().Version,
Group: ref.GetObjectKind().GroupVersionKind().GroupKind().Group,
Resource: ref.GetObjectKind().GroupVersionKind().Kind,
},
kindWithLowerCaseInitial(ref), klog.KRef(ref.Namespace, ref.Name),
),
}
}
Expand All @@ -109,8 +119,8 @@ func (l *topologyReconcileLogger) WithMachineDeployment(md *clusterv1.MachineDep
topologyName := md.Labels[clusterv1.ClusterTopologyMachineDeploymentLabelName]
return &topologyReconcileLogger{
Logger: l.Logger.WithValues(
"machineDeployment name", md.GetName(),
"machineDeployment topologyName", topologyName,
"machineDeployment", klog.KObj(md),
"machineDeploymentTopology", topologyName,
),
}
}
Expand Down Expand Up @@ -166,3 +176,12 @@ func (ref KRef) String() string {
}
return fmt.Sprintf("%s/%s", ref.Ref.Kind, ref.Ref.Name)
}

func kindWithLowerCaseInitial(obj runtime.Object) string {
var kind []rune
if obj.GetObjectKind().GroupVersionKind().GroupKind().Kind != "" {
kind = []rune(obj.GetObjectKind().GroupVersionKind().GroupKind().Kind)
kind[0] = unicode.ToLower(kind[0])
}
return string(kind)
}

0 comments on commit bc79af1

Please sign in to comment.