Skip to content

Commit 2160e6d

Browse files
committed
Rewrite glogx to work with klogv2 (+rename klogx)
1 parent 3c7727a commit 2160e6d

File tree

6 files changed

+67
-49
lines changed

6 files changed

+67
-49
lines changed

cluster-autoscaler/cloudprovider/gce/autoscaling_gce_client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"time"
2828

2929
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
30-
"k8s.io/autoscaler/cluster-autoscaler/utils/glogx"
30+
"k8s.io/autoscaler/cluster-autoscaler/utils/klogx"
3131

3232
gce "google.golang.org/api/compute/v1"
3333
klog "k8s.io/klog/v2"
@@ -212,7 +212,7 @@ func (client *autoscalingGceClientV1) FetchMigInstances(migRef GceRef) ([]cloudp
212212
}
213213
infos := []cloudprovider.Instance{}
214214
errorCodeCounts := make(map[string]int)
215-
errorLoggingQuota := glogx.NewLoggingQuota(100)
215+
errorLoggingQuota := klogx.NewLoggingQuota(100)
216216
for _, gceInstance := range gceInstances.ManagedInstances {
217217
ref, err := ParseInstanceUrlRef(gceInstance.Instance)
218218
if err != nil {
@@ -274,12 +274,12 @@ func (client *autoscalingGceClientV1) FetchMigInstances(migRef GceRef) ([]cloudp
274274
} else {
275275
gceInstanceJSON = string(gceInstanceJSONBytes)
276276
}
277-
glogx.V(4).UpTo(errorLoggingQuota).Infof("Got GCE instance which is being created and has lastAttemptErrors; gceInstance=%v; errorInfo=%#v", gceInstanceJSON, errorInfo)
277+
klogx.V(4).UpTo(errorLoggingQuota).Infof("Got GCE instance which is being created and has lastAttemptErrors; gceInstance=%v; errorInfo=%#v", gceInstanceJSON, errorInfo)
278278
}
279279
}
280280
infos = append(infos, instance)
281281
}
282-
glogx.V(4).Over(errorLoggingQuota).Infof("Got %v other GCE instances being created with lastAttemptErrors", -errorLoggingQuota.Left())
282+
klogx.V(4).Over(errorLoggingQuota).Infof("Got %v other GCE instances being created with lastAttemptErrors", -errorLoggingQuota.Left())
283283
if len(errorCodeCounts) > 0 {
284284
klog.V(4).Infof("Spotted following instance creation error codes: %#v", errorCodeCounts)
285285
}

cluster-autoscaler/core/scale_up.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
"k8s.io/autoscaler/cluster-autoscaler/processors/nodegroupset"
4040
"k8s.io/autoscaler/cluster-autoscaler/processors/status"
4141
"k8s.io/autoscaler/cluster-autoscaler/utils/errors"
42-
"k8s.io/autoscaler/cluster-autoscaler/utils/glogx"
42+
"k8s.io/autoscaler/cluster-autoscaler/utils/klogx"
4343
"k8s.io/autoscaler/cluster-autoscaler/utils/gpu"
4444
schedulerframework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
4545

@@ -320,12 +320,12 @@ func ScaleUp(context *context.AutoscalingContext, processors *ca_processors.Auto
320320

321321
now := time.Now()
322322

323-
loggingQuota := glogx.PodsLoggingQuota()
323+
loggingQuota := klogx.PodsLoggingQuota()
324324

325325
for _, pod := range unschedulablePods {
326-
glogx.V(1).UpTo(loggingQuota).Infof("Pod %s/%s is unschedulable", pod.Namespace, pod.Name)
326+
klogx.V(1).UpTo(loggingQuota).Infof("Pod %s/%s is unschedulable", pod.Namespace, pod.Name)
327327
}
328-
glogx.V(1).Over(loggingQuota).Infof("%v other pods are also unschedulable", -loggingQuota.Left())
328+
klogx.V(1).Over(loggingQuota).Infof("%v other pods are also unschedulable", -loggingQuota.Left())
329329

330330
nodesFromNotAutoscaledGroups, err := utils.FilterOutNodesFromNotAutoscaledGroups(nodes, context.CloudProvider)
331331
if err != nil {

cluster-autoscaler/simulator/cluster.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
"k8s.io/autoscaler/cluster-autoscaler/utils/drain"
2525
"k8s.io/autoscaler/cluster-autoscaler/utils/errors"
26-
"k8s.io/autoscaler/cluster-autoscaler/utils/glogx"
26+
"k8s.io/autoscaler/cluster-autoscaler/utils/klogx"
2727
"k8s.io/autoscaler/cluster-autoscaler/utils/gpu"
2828
kube_util "k8s.io/autoscaler/cluster-autoscaler/utils/kubernetes"
2929
pod_util "k8s.io/autoscaler/cluster-autoscaler/utils/pod"
@@ -295,11 +295,11 @@ func findPlaceFor(removedNode string, pods []*apiv1.Pod, nodes map[string]bool,
295295
return fmt.Sprintf("%s/%s", pod.Namespace, pod.Name)
296296
}
297297

298-
loggingQuota := glogx.PodsLoggingQuota()
298+
loggingQuota := klogx.PodsLoggingQuota()
299299

300300
tryNodeForPod := func(nodename string, pod *apiv1.Pod) bool {
301301
if err := predicateChecker.CheckPredicates(clusterSnapshot, pod, nodename); err != nil {
302-
glogx.V(4).UpTo(loggingQuota).Infof("Evaluation %s for %s/%s -> %v", nodename, pod.Namespace, pod.Name, err.VerboseMessage())
302+
klogx.V(4).UpTo(loggingQuota).Infof("Evaluation %s for %s/%s -> %v", nodename, pod.Namespace, pod.Name, err.VerboseMessage())
303303
return false
304304
}
305305

@@ -353,7 +353,7 @@ func findPlaceFor(removedNode string, pods []*apiv1.Pod, nodes map[string]bool,
353353
}
354354
}
355355
if !foundPlace {
356-
glogx.V(4).Over(loggingQuota).Infof("%v other nodes evaluated for %s/%s", -loggingQuota.Left(), pod.Namespace, pod.Name)
356+
klogx.V(4).Over(loggingQuota).Infof("%v other nodes evaluated for %s/%s", -loggingQuota.Left(), pod.Namespace, pod.Name)
357357
return fmt.Errorf("failed to find place for %s", podKey(pod))
358358
}
359359
}

cluster-autoscaler/utils/glogx/defaults.go renamed to cluster-autoscaler/utils/klogx/defaults.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package glogx
17+
package klogx
1818

1919
import klog "k8s.io/klog/v2"
2020

@@ -29,7 +29,7 @@ const (
2929

3030
// PodsLoggingQuota returns a new quota with default limit for pods at current verbosity.
3131
func PodsLoggingQuota() *quota {
32-
if klog.V(5) {
32+
if klog.V(5).Enabled() {
3333
return NewLoggingQuota(MaxPodsLoggedV5)
3434
}
3535
return NewLoggingQuota(MaxPodsLogged)

cluster-autoscaler/utils/glogx/glogx.go renamed to cluster-autoscaler/utils/klogx/klogx.go

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package glogx
17+
package klogx
1818

1919
import (
2020
klog "k8s.io/klog/v2"
@@ -40,41 +40,57 @@ func (q *quota) Reset() {
4040
q.left = q.limit
4141
}
4242

43-
// UpTo decreases quota for logging and reports whether there was any left.
44-
// The returned value is a boolean of type glogx.Verbose.
45-
func UpTo(quota *quota) klog.Verbose {
46-
quota.left--
47-
return quota.left >= 0
48-
}
49-
50-
// Over reports whether quota for logging was exceeded.
51-
// The returned value is a boolean of type glogx.Verbose.
52-
func Over(quota *quota) klog.Verbose {
53-
return quota.left < 0
54-
}
55-
5643
// V calls V from glog and wraps the result into glogx.Verbose.
5744
func V(n klog.Level) Verbose {
58-
return Verbose(klog.V(n))
45+
return Verbose{
46+
enabled: true,
47+
v: klog.V(n)}
5948
}
6049

6150
// Verbose is a wrapper for klog.Verbose that implements UpTo and Over.
62-
type Verbose klog.Verbose
51+
// It provides a subset of methods exposed by klog.Verbose.
52+
type Verbose struct {
53+
enabled bool
54+
v klog.Verbose
55+
}
56+
57+
func (v Verbose) enable(b bool) Verbose {
58+
return Verbose{
59+
enabled: b,
60+
v: v.v}
61+
}
6362

6463
// UpTo calls UpTo from this package if called on true object.
65-
// The returned value is a boolean of type klog.Verbose.
66-
func (v Verbose) UpTo(quota *quota) klog.Verbose {
67-
if v {
68-
return UpTo(quota)
64+
// The returned value is of type Verbose.
65+
func (v Verbose) UpTo(quota *quota) Verbose {
66+
if v.v.Enabled() {
67+
quota.left--
68+
return v.enable(quota.left >= 0)
6969
}
70-
return klog.Verbose(false)
70+
return v.enable(false)
7171
}
7272

7373
// Over calls Over from this package if called on true object.
74-
// The returned value is a boolean of type klog.Verbose.
75-
func (v Verbose) Over(quota *quota) klog.Verbose {
76-
if v {
77-
return Over(quota)
74+
// The returned value is of type Verbose.
75+
func (v Verbose) Over(quota *quota) Verbose {
76+
if v.v.Enabled() {
77+
return v.enable(quota.left < 0)
78+
}
79+
return v.enable(false)
80+
}
81+
82+
// Infof is a wrapper for klog.Infof that logs if the quota
83+
// allows for it.
84+
func (v Verbose) Infof(format string, args ...interface{}) {
85+
if v.enabled {
86+
v.v.Infof(format, args...)
87+
}
88+
}
89+
90+
// Info is a wrapper for klog.Info that logs if the quota
91+
// allows for it.
92+
func (v Verbose) Info(args ...interface{}) {
93+
if v.enabled {
94+
v.v.Info(args...)
7895
}
79-
return klog.Verbose(false)
8096
}

cluster-autoscaler/utils/glogx/glogx_test.go renamed to cluster-autoscaler/utils/klogx/klogx_test.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package glogx
17+
package klogx
1818

1919
import (
2020
"testing"
@@ -29,8 +29,8 @@ func TestLoggingQuota(t *testing.T) {
2929

3030
for i := 0; i < 5; i++ {
3131
assert.Equal(t, 3-i, q.Left())
32-
assert.Equal(t, i < 3, bool(UpTo(q)))
33-
assert.Equal(t, i >= 3, bool(Over(q)))
32+
assert.Equal(t, i < 3, V(0).UpTo(q).enabled)
33+
assert.Equal(t, i >= 3, V(0).Over(q).enabled)
3434
}
3535
}
3636

@@ -39,28 +39,30 @@ func TestReset(t *testing.T) {
3939
q := NewLoggingQuota(3)
4040

4141
for i := 0; i < 5; i++ {
42-
assert.Equal(t, i < 3, bool(UpTo(q)))
42+
assert.Equal(t, i < 3, V(0).UpTo(q).enabled)
4343
}
4444

4545
q.Reset()
4646

4747
assert.Equal(t, 3, q.Left())
48-
assert.False(t, bool(Over(q)))
49-
assert.True(t, bool(UpTo(q)))
48+
assert.False(t, V(0).Over(q).enabled)
49+
assert.True(t, V(0).UpTo(q).enabled)
5050
}
5151

5252
// Tests that quota isn't used up by calls limited by verbosity.
5353
func TestVFalse(t *testing.T) {
54-
v := Verbose(false)
54+
// XXX: this is a hack to get a disabled V, since klog v2 no longer
55+
// provides an easy way to create it
56+
v := V(10000)
5557
q := NewLoggingQuota(3)
5658

57-
assert.False(t, bool(v.UpTo(q)))
59+
assert.False(t, v.UpTo(q).v.Enabled())
5860
assert.Equal(t, 3, q.Left())
5961
}
6062

6163
// Tests that V limits calls based on verbosity the same way as klog.V.
6264
func TestV(t *testing.T) {
6365
for i := klog.Level(0); i <= 10; i++ {
64-
assert.Equal(t, bool(klog.V(i)), bool(V(i)))
66+
assert.Equal(t, klog.V(i).Enabled(), V(i).v.Enabled())
6567
}
6668
}

0 commit comments

Comments
 (0)