From c3dcbbdad1bb6d97e4d7ae20f52455a88f7c728c Mon Sep 17 00:00:00 2001 From: Maxim Patlasov Date: Thu, 8 May 2025 13:52:28 -0700 Subject: [PATCH 1/4] Add protosanitizer.StripSecrets(req) call --- pkg/gce-pd-csi-driver/utils.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/gce-pd-csi-driver/utils.go b/pkg/gce-pd-csi-driver/utils.go index 9a94995bd..eaad1bb47 100644 --- a/pkg/gce-pd-csi-driver/utils.go +++ b/pkg/gce-pd-csi-driver/utils.go @@ -22,6 +22,7 @@ import ( "fmt" "github.com/container-storage-interface/spec/lib/go/csi" + "github.com/kubernetes-csi/csi-lib-utils/protosanitizer" "google.golang.org/grpc" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/klog/v2" @@ -79,7 +80,7 @@ func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, h // Note that secrets are not included in any RPC message. In the past protosanitizer and other log // stripping was shown to cause a significant increase of CPU usage (see // https://github.com/kubernetes-sigs/gcp-compute-persistent-disk-csi-driver/issues/356#issuecomment-550529004). - klog.V(4).Infof("%s called with request: %s", info.FullMethod, req) + klog.V(4).Infof("%s called with request: %s", info.FullMethod, protosanitizer.StripSecrets(req)) resp, err := handler(ctx, req) if err != nil { klog.Errorf("%s returned with error: %v", info.FullMethod, err.Error()) From a5a59f6d6626ceacdbad75ca254f4a4da31f1fe4 Mon Sep 17 00:00:00 2001 From: Maxim Patlasov Date: Thu, 8 May 2025 13:53:52 -0700 Subject: [PATCH 2/4] Add unit-test for logGRPC() --- pkg/gce-pd-csi-driver/utils_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/gce-pd-csi-driver/utils_test.go b/pkg/gce-pd-csi-driver/utils_test.go index f187cc9d8..4021b5361 100644 --- a/pkg/gce-pd-csi-driver/utils_test.go +++ b/pkg/gce-pd-csi-driver/utils_test.go @@ -18,11 +18,13 @@ limitations under the License. package gceGCEDriver import ( + "context" "fmt" "testing" csi "github.com/container-storage-interface/spec/lib/go/csi" "github.com/google/go-cmp/cmp" + "google.golang.org/grpc" "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common" ) @@ -857,3 +859,24 @@ func TestGetHyperdiskAccessModeFromCapabilities(t *testing.T) { } } } + +func TestLogGRPC(t *testing.T) { + info := &grpc.UnaryServerInfo{ + FullMethod: "foo", + } + + req := struct { + Secrets map[string]string + }{map[string]string{ + "password": "pass", + }} + + handler := func(ctx context.Context, req any) (any, error) { + return nil, nil + } + + _, err := logGRPC(nil, req, info, handler) + if err != nil { + t.Fatalf("logGRPC returns error %v", err) + } +} From d4dc73f2f0a1ccd2e4430cf725e4ef470bdff929 Mon Sep 17 00:00:00 2001 From: Maxim Patlasov Date: Thu, 8 May 2025 13:55:35 -0700 Subject: [PATCH 3/4] go mod tidy && go mod vendor --- go.mod | 8 +- go.sum | 8 +- .../kubernetes-csi/csi-lib-utils/LICENSE | 201 ++++++++++++++++++ .../protosanitizer/protosanitizer.go | 124 +++++++++++ .../k8s.io/utils/clock/testing/fake_clock.go | 25 +-- vendor/modules.txt | 9 +- 6 files changed, 355 insertions(+), 20 deletions(-) create mode 100644 vendor/github.com/kubernetes-csi/csi-lib-utils/LICENSE create mode 100644 vendor/github.com/kubernetes-csi/csi-lib-utils/protosanitizer/protosanitizer.go diff --git a/go.mod b/go.mod index e900624d0..11e560427 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,7 @@ module sigs.k8s.io/gcp-compute-persistent-disk-csi-driver -go 1.23.0 +go 1.24.0 + toolchain go1.24.1 require ( @@ -14,6 +15,7 @@ require ( github.com/google/uuid v1.6.0 github.com/googleapis/gax-go/v2 v2.12.5 github.com/hashicorp/golang-lru/v2 v2.0.7 + github.com/kubernetes-csi/csi-lib-utils v0.21.0 github.com/kubernetes-csi/csi-proxy/client v1.2.1 github.com/kubernetes-csi/csi-test/v5 v5.3.1 github.com/onsi/ginkgo/v2 v2.22.2 @@ -38,7 +40,7 @@ require ( k8s.io/component-base v0.33.0 k8s.io/klog/v2 v2.130.1 k8s.io/mount-utils v0.33.0 - k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 + k8s.io/utils v0.0.0-20241210054802-24370beab758 sigs.k8s.io/boskos v0.0.0-20220711194915-6cb8a6fb2dd1 ) @@ -111,7 +113,7 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect k8s.io/test-infra v0.0.0-20210730160938-8ad9b8c53bd8 // indirect - sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect + sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect sigs.k8s.io/randfill v1.0.0 // indirect sigs.k8s.io/structured-merge-diff/v4 v4.7.0 // indirect sigs.k8s.io/yaml v1.4.0 // indirect diff --git a/go.sum b/go.sum index e288fd5c6..f57a1cf7e 100644 --- a/go.sum +++ b/go.sum @@ -1042,6 +1042,8 @@ github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/kubernetes-csi/csi-lib-utils v0.21.0 h1:dUN/iIgXLucAxyML2iPyhniIlACQumIeAJmIzsMBddc= +github.com/kubernetes-csi/csi-lib-utils v0.21.0/go.mod h1:ZCVRTYuup+bwX9tOeE5Q3LDw64QvltSwMUQ3M3g2T+Q= github.com/kubernetes-csi/csi-proxy/client v1.2.1 h1:6ApXTKp5Rhb+2PrPfnk6C+TDG7XwnWJ9/xmmq7iN5cw= github.com/kubernetes-csi/csi-proxy/client v1.2.1/go.mod h1:SfK4HVKQdMH5KrffivddAWgX5hl3P5KmnuOTBbDNboU= github.com/kubernetes-csi/csi-test/v5 v5.3.1 h1:Wiukp1In+kif+BFo6q2ExjgB+MbrAz4jZWzGfijypuY= @@ -2611,8 +2613,9 @@ k8s.io/utils v0.0.0-20200124190032-861946025e34/go.mod h1:sZAwmy6armz5eXlNoLmJcl k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= k8s.io/utils v0.0.0-20210527160623-6fdb442a123b/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= k8s.io/utils v0.0.0-20240711033017-18e509b52bc8/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= -k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 h1:M3sRQVHv7vB20Xc2ybTt7ODCeFj6JSWYFzOFnYeS6Ro= k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= +k8s.io/utils v0.0.0-20241210054802-24370beab758 h1:sdbE21q2nlQtFh65saZY+rRM6x6aJJI8IUa1AmH/qa0= +k8s.io/utils v0.0.0-20241210054802-24370beab758/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= knative.dev/caching v0.0.0-20190719140829-2032732871ff/go.mod h1:dHXFU6CGlLlbzaWc32g80cR92iuBSpsslDNBWI8C7eg= knative.dev/caching v0.0.0-20200116200605-67bca2c83dfa/go.mod h1:dHXFU6CGlLlbzaWc32g80cR92iuBSpsslDNBWI8C7eg= knative.dev/eventing-contrib v0.6.1-0.20190723221543-5ce18048c08b/go.mod h1:SnXZgSGgMSMLNFTwTnpaOH7hXDzTFtw0J8OmHflNx3g= @@ -2656,8 +2659,9 @@ sigs.k8s.io/controller-runtime v0.5.0/go.mod h1:REiJzC7Y00U+2YkMbT8wxgrsX5USpXKG sigs.k8s.io/controller-runtime v0.5.4/go.mod h1:JZUwSMVbxDupo0lTJSSFP5pimEyxGynROImSsqIOx1A= sigs.k8s.io/controller-runtime v0.9.0/go.mod h1:TgkfvrhhEw3PlI0BRL/5xM+89y3/yc0ZDfdbTl84si8= sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0= -sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 h1:/Rv+M11QRah1itp8VhT6HoVx1Ray9eB4DBr+K+/sCJ8= sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3/go.mod h1:18nIHnGi6636UCz6m8i4DhaJ65T6EruyzmoQqI2BVDo= +sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 h1:gBQPwqORJ8d8/YNZWEjoZs7npUVDpVXUUOFfW6CgAqE= +sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8/go.mod h1:mdzfpAEoE6DHQEN0uh9ZbOCuHbLK5wOm7dK4ctXE9Tg= sigs.k8s.io/kustomize v2.0.3+incompatible/go.mod h1:MkjgH3RdOWrievjo6c9T245dYlB5QeXV4WCbnt/PEpU= sigs.k8s.io/randfill v0.0.0-20250304075658-069ef1bbf016/go.mod h1:XeLlZ/jmk4i1HRopwe7/aU3H5n1zNUcX6TM94b3QxOY= sigs.k8s.io/randfill v1.0.0 h1:JfjMILfT8A6RbawdsK2JXGBR5AQVfd+9TbzrlneTyrU= diff --git a/vendor/github.com/kubernetes-csi/csi-lib-utils/LICENSE b/vendor/github.com/kubernetes-csi/csi-lib-utils/LICENSE new file mode 100644 index 000000000..8dada3eda --- /dev/null +++ b/vendor/github.com/kubernetes-csi/csi-lib-utils/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/vendor/github.com/kubernetes-csi/csi-lib-utils/protosanitizer/protosanitizer.go b/vendor/github.com/kubernetes-csi/csi-lib-utils/protosanitizer/protosanitizer.go new file mode 100644 index 000000000..253116de1 --- /dev/null +++ b/vendor/github.com/kubernetes-csi/csi-lib-utils/protosanitizer/protosanitizer.go @@ -0,0 +1,124 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package protosanitizer supports logging of gRPC messages without +// accidentally revealing sensitive fields. +package protosanitizer + +import ( + "encoding/json" + "fmt" + + "github.com/container-storage-interface/spec/lib/go/csi" + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/reflect/protoreflect" +) + +// StripSecrets returns a wrapper around the original CSI gRPC message +// which has a Stringer implementation that serializes the message +// as one-line JSON, but without including secret information. +// Instead of the secret value(s), the string "***stripped***" is +// included in the result. +// +// StripSecrets relies on an extension in CSI 1.0 and thus can only +// be used for messages based on that or a more recent spec! +// +// StripSecrets itself is fast and therefore it is cheap to pass the +// result to logging functions which may or may not end up serializing +// the parameter depending on the current log level. +func StripSecrets(msg interface{}) fmt.Stringer { + return &stripSecrets{msg} +} + +type stripSecrets struct { + msg any +} + +func (s *stripSecrets) String() string { + stripped := s.msg + + // also support scalar types like string, int, etc. + msg, ok := s.msg.(proto.Message) + if ok { + stripped = stripMessage(msg.ProtoReflect()) + } + + b, err := json.Marshal(stripped) + if err != nil { + return fmt.Sprintf("<>", s.msg, err) + } + return string(b) +} + +func stripSingleValue(field protoreflect.FieldDescriptor, v protoreflect.Value) any { + switch field.Kind() { + case protoreflect.MessageKind: + return stripMessage(v.Message()) + case protoreflect.EnumKind: + desc := field.Enum().Values().ByNumber(v.Enum()) + if desc == nil { + return v.Enum() + } + return desc.Name() + default: + return v.Interface() + } +} + +func stripValue(field protoreflect.FieldDescriptor, v protoreflect.Value) any { + if field.IsList() { + l := v.List() + res := make([]any, l.Len()) + for i := range l.Len() { + res[i] = stripSingleValue(field, l.Get(i)) + } + return res + } else if field.IsMap() { + m := v.Map() + res := make(map[string]any, m.Len()) + m.Range(func(mk protoreflect.MapKey, v protoreflect.Value) bool { + res[mk.String()] = stripSingleValue(field.MapValue(), v) + return true + }) + return res + } else { + return stripSingleValue(field, v) + } +} + +func stripMessage(msg protoreflect.Message) map[string]any { + stripped := make(map[string]any) + + // Walk through all fields and replace those with ***stripped*** that + // are marked as secret. + msg.Range(func(field protoreflect.FieldDescriptor, v protoreflect.Value) bool { + name := field.TextName() + if isCSI1Secret(field) { + stripped[name] = "***stripped***" + } else { + stripped[name] = stripValue(field, v) + } + return true + }) + return stripped +} + +// isCSI1Secret uses the csi.E_CsiSecret extension from CSI 1.0 to +// determine whether a field contains secrets. +func isCSI1Secret(desc protoreflect.FieldDescriptor) bool { + ex := proto.GetExtension(desc.Options(), csi.E_CsiSecret) + return ex.(bool) +} diff --git a/vendor/k8s.io/utils/clock/testing/fake_clock.go b/vendor/k8s.io/utils/clock/testing/fake_clock.go index 79e11deb6..462c40c2c 100644 --- a/vendor/k8s.io/utils/clock/testing/fake_clock.go +++ b/vendor/k8s.io/utils/clock/testing/fake_clock.go @@ -48,7 +48,6 @@ type fakeClockWaiter struct { stepInterval time.Duration skipIfBlocked bool destChan chan time.Time - fired bool afterFunc func() } @@ -198,12 +197,10 @@ func (f *FakeClock) setTimeLocked(t time.Time) { if w.skipIfBlocked { select { case w.destChan <- t: - w.fired = true default: } } else { w.destChan <- t - w.fired = true } if w.afterFunc != nil { @@ -305,44 +302,48 @@ func (f *fakeTimer) C() <-chan time.Time { return f.waiter.destChan } -// Stop stops the timer and returns true if the timer has not yet fired, or false otherwise. +// Stop prevents the Timer from firing. It returns true if the call stops the +// timer, false if the timer has already expired or been stopped. func (f *fakeTimer) Stop() bool { f.fakeClock.lock.Lock() defer f.fakeClock.lock.Unlock() + active := false newWaiters := make([]*fakeClockWaiter, 0, len(f.fakeClock.waiters)) for i := range f.fakeClock.waiters { w := f.fakeClock.waiters[i] if w != &f.waiter { newWaiters = append(newWaiters, w) + continue } + // If timer is found, it has not been fired yet. + active = true } f.fakeClock.waiters = newWaiters - return !f.waiter.fired + return active } -// Reset resets the timer to the fake clock's "now" + d. It returns true if the timer has not yet -// fired, or false otherwise. +// Reset changes the timer to expire after duration d. It returns true if the +// timer had been active, false if the timer had expired or been stopped. func (f *fakeTimer) Reset(d time.Duration) bool { f.fakeClock.lock.Lock() defer f.fakeClock.lock.Unlock() - active := !f.waiter.fired + active := false - f.waiter.fired = false f.waiter.targetTime = f.fakeClock.time.Add(d) - var isWaiting bool for i := range f.fakeClock.waiters { w := f.fakeClock.waiters[i] if w == &f.waiter { - isWaiting = true + // If timer is found, it has not been fired yet. + active = true break } } - if !isWaiting { + if !active { f.fakeClock.waiters = append(f.fakeClock.waiters, &f.waiter) } diff --git a/vendor/modules.txt b/vendor/modules.txt index 46769c087..476721d99 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -193,6 +193,9 @@ github.com/josharian/intern # github.com/json-iterator/go v1.1.12 ## explicit; go 1.12 github.com/json-iterator/go +# github.com/kubernetes-csi/csi-lib-utils v0.21.0 +## explicit; go 1.23.1 +github.com/kubernetes-csi/csi-lib-utils/protosanitizer # github.com/kubernetes-csi/csi-proxy/client v1.2.1 ## explicit; go 1.16 github.com/kubernetes-csi/csi-proxy/client @@ -932,7 +935,7 @@ k8s.io/test-infra/prow/config/secret k8s.io/test-infra/prow/logrusutil k8s.io/test-infra/prow/secretutil k8s.io/test-infra/prow/version -# k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 +# k8s.io/utils v0.0.0-20241210054802-24370beab758 ## explicit; go 1.18 k8s.io/utils/clock k8s.io/utils/clock/testing @@ -950,8 +953,8 @@ k8s.io/utils/strings/slices sigs.k8s.io/boskos/client sigs.k8s.io/boskos/common sigs.k8s.io/boskos/storage -# sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 -## explicit; go 1.21 +# sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 +## explicit; go 1.23 sigs.k8s.io/json sigs.k8s.io/json/internal/golang/encoding/json # sigs.k8s.io/randfill v1.0.0 From 51c1dd1e06d3a57e1f4a652a40b2ff8d0872f67f Mon Sep 17 00:00:00 2001 From: Maxim Patlasov Date: Thu, 8 May 2025 15:22:10 -0700 Subject: [PATCH 4/4] Iterate logGRPC() call 100,1000,1000 times in unit-test --- pkg/gce-pd-csi-driver/utils_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/gce-pd-csi-driver/utils_test.go b/pkg/gce-pd-csi-driver/utils_test.go index 4021b5361..28de6c3f8 100644 --- a/pkg/gce-pd-csi-driver/utils_test.go +++ b/pkg/gce-pd-csi-driver/utils_test.go @@ -875,8 +875,10 @@ func TestLogGRPC(t *testing.T) { return nil, nil } - _, err := logGRPC(nil, req, info, handler) - if err != nil { - t.Fatalf("logGRPC returns error %v", err) + for _ = range int64(100000000) { + _, err := logGRPC(nil, req, info, handler) + if err != nil { + t.Fatalf("logGRPC returns error %v", err) + } } }