Skip to content

Commit 045a43d

Browse files
committed
nrt: logr: add support for logr
Move to the logr.Logger interface everywhere, instead of the global `klog` instance. This enable named logger, presetting values for simple and automatic consistency, enables pluggable loggers and comes for free since we already depend on the logr package and klog has a native logr integration. In addition, add minimal support to make it easy to replace the logr reference, to help integrators of this code. The default is still (and will still be) klog for backward compatibility and ecosystem integration. Signed-off-by: Francesco Romani <[email protected]>
1 parent 8d9a4cd commit 045a43d

28 files changed

+412
-306
lines changed

pkg/noderesourcetopology/cache/discardreserved.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ import (
2020
"context"
2121
"sync"
2222

23+
"github.com/go-logr/logr"
2324
topologyv1alpha2 "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/apis/topology/v1alpha2"
2425

2526
corev1 "k8s.io/api/core/v1"
2627
"k8s.io/apimachinery/pkg/types"
2728
"k8s.io/klog/v2"
2829

2930
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
31+
"sigs.k8s.io/scheduler-plugins/pkg/noderesourcetopology/logging"
3032
)
3133

3234
// DiscardReserved is intended to solve similiar problem as Overreserve Cache,
@@ -45,12 +47,14 @@ type DiscardReserved struct {
4547
rMutex sync.RWMutex
4648
reservationMap map[string]map[types.UID]bool // Key is NodeName, value is Pod UID : reserved status
4749
client ctrlclient.Client
50+
lh logr.Logger
4851
}
4952

50-
func NewDiscardReserved(client ctrlclient.Client) Interface {
53+
func NewDiscardReserved(lh logr.Logger, client ctrlclient.Client) Interface {
5154
return &DiscardReserved{
5255
client: client,
5356
reservationMap: make(map[string]map[types.UID]bool),
57+
lh: lh,
5458
}
5559
}
5660

@@ -74,7 +78,7 @@ func (pt *DiscardReserved) NodeMaybeOverReserved(nodeName string, pod *corev1.Po
7478
func (pt *DiscardReserved) NodeHasForeignPods(nodeName string, pod *corev1.Pod) {}
7579

7680
func (pt *DiscardReserved) ReserveNodeResources(nodeName string, pod *corev1.Pod) {
77-
klog.V(5).InfoS("nrtcache NRT Reserve", "logID", klog.KObj(pod), "UID", pod.GetUID(), "node", nodeName)
81+
pt.lh.V(5).Info("NRT Reserve", "logID", logging.PodLogID(pod), "podUID", pod.GetUID(), "node", nodeName)
7882
pt.rMutex.Lock()
7983
defer pt.rMutex.Unlock()
8084

@@ -85,14 +89,14 @@ func (pt *DiscardReserved) ReserveNodeResources(nodeName string, pod *corev1.Pod
8589
}
8690

8791
func (pt *DiscardReserved) UnreserveNodeResources(nodeName string, pod *corev1.Pod) {
88-
klog.V(5).InfoS("nrtcache NRT Unreserve", "logID", klog.KObj(pod), "UID", pod.GetUID(), "node", nodeName)
92+
pt.lh.V(5).Info("NRT Unreserve", "logID", klog.KObj(pod), "podUID", pod.GetUID(), "node", nodeName)
8993

9094
pt.removeReservationForNode(nodeName, pod)
9195
}
9296

9397
// PostBind is invoked to cleanup reservationMap
9498
func (pt *DiscardReserved) PostBind(nodeName string, pod *corev1.Pod) {
95-
klog.V(5).InfoS("nrtcache NRT PostBind", "logID", klog.KObj(pod), "UID", pod.GetUID(), "node", nodeName)
99+
pt.lh.V(5).Info("NRT PostBind", "logID", klog.KObj(pod), "podUID", pod.GetUID(), "node", nodeName)
96100

97101
pt.removeReservationForNode(nodeName, pod)
98102
}

pkg/noderesourcetopology/cache/discardreserved_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/types"
2828
podlisterv1 "k8s.io/client-go/listers/core/v1"
29+
"k8s.io/klog/v2"
2930

3031
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
3132
)
@@ -50,7 +51,7 @@ func TestDiscardReservedNodesGetCachedNRTCopy(t *testing.T) {
5051
checkGetCachedNRTCopy(
5152
t,
5253
func(client ctrlclient.Client, _ podlisterv1.PodLister) (Interface, error) {
53-
return NewDiscardReserved(client), nil
54+
return NewDiscardReserved(klog.Background(), client), nil
5455
},
5556
testCases...,
5657
)

pkg/noderesourcetopology/cache/foreign_pods.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ limitations under the License.
1717
package cache
1818

1919
import (
20+
"fmt"
21+
22+
"github.com/go-logr/logr"
2023
corev1 "k8s.io/api/core/v1"
2124
"k8s.io/apimachinery/pkg/util/sets"
2225
k8scache "k8s.io/client-go/tools/cache"
23-
"k8s.io/klog/v2"
2426

27+
"sigs.k8s.io/scheduler-plugins/pkg/noderesourcetopology/logging"
2528
"sigs.k8s.io/scheduler-plugins/pkg/noderesourcetopology/resourcerequests"
2629
)
2730

@@ -35,19 +38,19 @@ var (
3538
onlyExclusiveResources = false
3639
)
3740

38-
func SetupForeignPodsDetector(schedProfileName string, podInformer k8scache.SharedInformer, cc Interface) {
41+
func SetupForeignPodsDetector(lh logr.Logger, schedProfileName string, podInformer k8scache.SharedInformer, cc Interface) {
3942
foreignCache := func(obj interface{}) {
4043
pod, ok := obj.(*corev1.Pod)
4144
if !ok {
42-
klog.V(3).InfoS("nrtcache: foreign: unsupported object %T", obj)
45+
lh.V(3).Info("unsupported object", "kind", fmt.Sprintf("%T", obj))
4346
return
4447
}
4548
if !IsForeignPod(pod) {
4649
return
4750
}
4851

4952
cc.NodeHasForeignPods(pod.Spec.NodeName, pod)
50-
klog.V(6).InfoS("nrtcache: has foreign pods", "logID", klog.KObj(pod), "node", pod.Spec.NodeName, "podUID", pod.UID)
53+
lh.V(6).Info("detected foreign pods", "logID", logging.PodLogID(pod), "podUID", pod.GetUID(), "node", pod.Spec.NodeName)
5154
}
5255

5356
podInformer.AddEventHandler(k8scache.ResourceEventHandlerFuncs{
@@ -67,11 +70,11 @@ func TrackAllForeignPods() {
6770
onlyExclusiveResources = false
6871
}
6972

70-
func RegisterSchedulerProfileName(schedProfileName string) {
71-
klog.InfoS("nrtcache: setting up foreign pod detection", "profile", schedProfileName)
73+
func RegisterSchedulerProfileName(lh logr.Logger, schedProfileName string) {
74+
lh.Info("setting up detection", "profile", schedProfileName)
7275
schedProfileNames.Insert(schedProfileName)
7376

74-
klog.V(5).InfoS("nrtcache: registered scheduler profiles", "names", schedProfileNames.List())
77+
lh.V(5).Info("registered scheduler profiles", "names", schedProfileNames.List())
7578
}
7679

7780
func IsForeignPod(pod *corev1.Pod) bool {

pkg/noderesourcetopology/cache/foreign_pods_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
corev1 "k8s.io/api/core/v1"
2323
"k8s.io/apimachinery/pkg/api/resource"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/klog/v2"
2526
)
2627

2728
func TestIsForeignPod(t *testing.T) {
@@ -195,7 +196,7 @@ func TestIsForeignPod(t *testing.T) {
195196
for _, tt := range tests {
196197
t.Run(tt.name, func(t *testing.T) {
197198
for _, profileName := range tt.profileNames {
198-
RegisterSchedulerProfileName(profileName)
199+
RegisterSchedulerProfileName(klog.Background(), profileName)
199200
}
200201
defer CleanRegisteredSchedulerProfileNames()
201202

0 commit comments

Comments
 (0)