Skip to content

Commit

Permalink
fix: use os.Lstat to resolve os.Stat issue in windows
Browse files Browse the repository at this point in the history
This PR introduces a fallback os.Lstat check in provider client for the
provider socket. There is a known issue in windows using os.Stat() which
happens randomly in 1809, 1903 but always fails in ltsc2022.

Signed-off-by: Anish Ramasekar <anish.ramasekar@gmail.com>
  • Loading branch information
aramase committed Jul 13, 2022
1 parent 4d7491c commit 2ee77ca
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
10 changes: 10 additions & 0 deletions pkg/secrets-store/provider_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

internalerrors "sigs.k8s.io/secrets-store-csi-driver/pkg/errors"
"sigs.k8s.io/secrets-store-csi-driver/pkg/util/fileutil"
"sigs.k8s.io/secrets-store-csi-driver/pkg/util/runtimeutil"
"sigs.k8s.io/secrets-store-csi-driver/provider/v1alpha1"

"google.golang.org/grpc"
Expand Down Expand Up @@ -134,6 +135,15 @@ func (p *PluginClientBuilder) Get(ctx context.Context, provider string) (v1alpha
socketPath = tryPath
break
}
// TODO: This is a workaround for Windows 20H2 issue for os.Stat(). See
// microsoft/Windows-Containers#97 for details.
// Once the issue is resolved, the following os.Lstat() is not needed.
if runtimeutil.IsRuntimeWindows() {
if _, err := os.Lstat(tryPath); err == nil {
socketPath = tryPath
break
}
}
}

if socketPath == "" {
Expand Down
5 changes: 3 additions & 2 deletions pkg/secrets-store/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
"fmt"
"net"
"os"
"runtime"
"strings"
"sync"
"time"

"sigs.k8s.io/secrets-store-csi-driver/pkg/util/runtimeutil"

"github.com/container-storage-interface/spec/lib/go/csi"
pbSanitizer "github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
"google.golang.org/grpc"
Expand Down Expand Up @@ -86,7 +87,7 @@ func (s *nonBlockingGRPCServer) serve(ctx context.Context, endpoint string, ids
}

if proto == "unix" {
if runtime.GOOS != "windows" {
if !runtimeutil.IsRuntimeWindows() {
addr = "/" + addr
}
if err := os.Remove(addr); err != nil && !os.IsNotExist(err) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/secrets-store/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"context"
"fmt"
"os"
"runtime"
"strings"

secretsstorev1 "sigs.k8s.io/secrets-store-csi-driver/apis/v1"
"sigs.k8s.io/secrets-store-csi-driver/pkg/util/runtimeutil"
"sigs.k8s.io/secrets-store-csi-driver/pkg/util/spcpsutil"

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

if runtime.GOOS == "windows" {
if runtimeutil.IsRuntimeWindows() {
// IsLikelyNotMountPoint always returns notMnt=true for windows as the
// target path is not a soft link to the global mount
// instead check if the dir exists for windows and if it's not empty
Expand Down
5 changes: 3 additions & 2 deletions pkg/util/fileutil/atomic_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import (
"os"
"path"
"path/filepath"
"runtime"
"strings"
"time"

"sigs.k8s.io/secrets-store-csi-driver/pkg/util/runtimeutil"

"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
)
Expand Down Expand Up @@ -195,7 +196,7 @@ func (w *AtomicWriter) Write(payload map[string]FileProjection) error {
}

// (9)
if runtime.GOOS == "windows" {
if runtimeutil.IsRuntimeWindows() {
os.Remove(dataDirPath)
err = os.Symlink(tsDirName, dataDirPath)
os.Remove(newDataDirPath)
Expand Down
3 changes: 2 additions & 1 deletion pkg/util/fileutil/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"testing"

"sigs.k8s.io/secrets-store-csi-driver/pkg/test_utils/tmpdir"
"sigs.k8s.io/secrets-store-csi-driver/pkg/util/runtimeutil"
"sigs.k8s.io/secrets-store-csi-driver/provider/v1alpha1"
)

Expand Down Expand Up @@ -442,7 +443,7 @@ func readPayloads(path string, payloads []*v1alpha1.File) error {
if err != nil {
return err
}
if runtime.GOOS == "windows" {
if runtimeutil.IsRuntimeWindows() {
// on windows only the 0200 bitmask is used by chmod
// https://golang.org/src/os/file.go?s=15847:15891#L522
if (info.Mode() & 0200) != (fs.FileMode(p.Mode) & 0200) {
Expand Down
24 changes: 24 additions & 0 deletions pkg/util/runtimeutil/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright 2022 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 runtimeutil

import "runtime"

// IsRuntimeWindows returns true if the runtime is windows.
func IsRuntimeWindows() bool {
return runtime.GOOS == "windows"
}

0 comments on commit 2ee77ca

Please sign in to comment.