Skip to content

Commit f04628b

Browse files
authored
Merge pull request #999 from k8s-infra-cherrypick-robot/cherry-pick-998-to-release-1.2
[release-1.2] fix: use `os.Lstat` to resolve `os.Stat` issue in windows
2 parents c7abc27 + e558fc0 commit f04628b

File tree

6 files changed

+44
-7
lines changed

6 files changed

+44
-7
lines changed

pkg/secrets-store/provider_client.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
internalerrors "sigs.k8s.io/secrets-store-csi-driver/pkg/errors"
3232
"sigs.k8s.io/secrets-store-csi-driver/pkg/util/fileutil"
33+
"sigs.k8s.io/secrets-store-csi-driver/pkg/util/runtimeutil"
3334
"sigs.k8s.io/secrets-store-csi-driver/provider/v1alpha1"
3435

3536
"google.golang.org/grpc"
@@ -134,6 +135,15 @@ func (p *PluginClientBuilder) Get(ctx context.Context, provider string) (v1alpha
134135
socketPath = tryPath
135136
break
136137
}
138+
// TODO: This is a workaround for Windows 20H2 issue for os.Stat(). See
139+
// microsoft/Windows-Containers#97 for details.
140+
// Once the issue is resolved, the following os.Lstat() is not needed.
141+
if runtimeutil.IsRuntimeWindows() {
142+
if _, err := os.Lstat(tryPath); err == nil {
143+
socketPath = tryPath
144+
break
145+
}
146+
}
137147
}
138148

139149
if socketPath == "" {

pkg/secrets-store/server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ import (
2121
"fmt"
2222
"net"
2323
"os"
24-
"runtime"
2524
"strings"
2625
"sync"
2726
"time"
2827

28+
"sigs.k8s.io/secrets-store-csi-driver/pkg/util/runtimeutil"
29+
2930
"github.com/container-storage-interface/spec/lib/go/csi"
3031
pbSanitizer "github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
3132
"google.golang.org/grpc"
@@ -86,7 +87,7 @@ func (s *nonBlockingGRPCServer) serve(ctx context.Context, endpoint string, ids
8687
}
8788

8889
if proto == "unix" {
89-
if runtime.GOOS != "windows" {
90+
if !runtimeutil.IsRuntimeWindows() {
9091
addr = "/" + addr
9192
}
9293
if err := os.Remove(addr); err != nil && !os.IsNotExist(err) {

pkg/secrets-store/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import (
2020
"context"
2121
"fmt"
2222
"os"
23-
"runtime"
2423
"strings"
2524

2625
secretsstorev1 "sigs.k8s.io/secrets-store-csi-driver/apis/v1"
26+
"sigs.k8s.io/secrets-store-csi-driver/pkg/util/runtimeutil"
2727
"sigs.k8s.io/secrets-store-csi-driver/pkg/util/spcpsutil"
2828

2929
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -57,7 +57,7 @@ func (ns *nodeServer) ensureMountPoint(target string) (bool, error) {
5757
return !notMnt, err
5858
}
5959

60-
if runtime.GOOS == "windows" {
60+
if runtimeutil.IsRuntimeWindows() {
6161
// IsLikelyNotMountPoint always returns notMnt=true for windows as the
6262
// target path is not a soft link to the global mount
6363
// instead check if the dir exists for windows and if it's not empty

pkg/util/fileutil/atomic_writer.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ import (
2727
"os"
2828
"path"
2929
"path/filepath"
30-
"runtime"
3130
"strings"
3231
"time"
3332

33+
"sigs.k8s.io/secrets-store-csi-driver/pkg/util/runtimeutil"
34+
3435
"k8s.io/apimachinery/pkg/util/sets"
3536
"k8s.io/klog/v2"
3637
)
@@ -195,7 +196,7 @@ func (w *AtomicWriter) Write(payload map[string]FileProjection) error {
195196
}
196197

197198
// (9)
198-
if runtime.GOOS == "windows" {
199+
if runtimeutil.IsRuntimeWindows() {
199200
os.Remove(dataDirPath)
200201
err = os.Symlink(tsDirName, dataDirPath)
201202
os.Remove(newDataDirPath)

pkg/util/fileutil/writer_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"testing"
2929

3030
"sigs.k8s.io/secrets-store-csi-driver/pkg/test_utils/tmpdir"
31+
"sigs.k8s.io/secrets-store-csi-driver/pkg/util/runtimeutil"
3132
"sigs.k8s.io/secrets-store-csi-driver/provider/v1alpha1"
3233
)
3334

@@ -442,7 +443,7 @@ func readPayloads(path string, payloads []*v1alpha1.File) error {
442443
if err != nil {
443444
return err
444445
}
445-
if runtime.GOOS == "windows" {
446+
if runtimeutil.IsRuntimeWindows() {
446447
// on windows only the 0200 bitmask is used by chmod
447448
// https://golang.org/src/os/file.go?s=15847:15891#L522
448449
if (info.Mode() & 0200) != (fs.FileMode(p.Mode) & 0200) {

pkg/util/runtimeutil/runtime.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package runtimeutil
18+
19+
import "runtime"
20+
21+
// IsRuntimeWindows returns true if the runtime is windows.
22+
func IsRuntimeWindows() bool {
23+
return runtime.GOOS == "windows"
24+
}

0 commit comments

Comments
 (0)