Skip to content

Commit

Permalink
windows: Fixes following container log rotation
Browse files Browse the repository at this point in the history
If we're following a file, that file will remain open, and we continue
to read data from it when new data becomes available.

On Windows, this can be an issue if the container logs needs to be rotated.
Log rotation is done by renaming the file, but this action may fail if
the file is already opened.

Setting the FILE_SHARE_DELETE flag when opening the file will prevent this
issue, as documented: "Delete access allows both delete and rename operations" [1].

In golang, there's no way to set this flag [2], the sharemode is always set to:
sharemode := uint32(FILE_SHARE_READ | FILE_SHARE_WRITE)

Thus, we need to open the file ourselves with the right flags.

[1] https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea?redirectedfrom=MSDN
[2] https://cs.opensource.google/go/go/+/refs/tags/go1.22.2:src/syscall/syscall_windows.go;l=366
  • Loading branch information
claudiubelu committed Apr 22, 2024
1 parent 9c9a0e3 commit 1571613
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/kubelet/kuberuntime/logs/logs.go
Expand Up @@ -291,7 +291,7 @@ func ReadLogs(ctx context.Context, path, containerID string, opts *LogOptions, r
return fmt.Errorf("failed to try resolving symlinks in path %q: %v", path, err)
}
path = evaluated
f, err := os.Open(path)
f, err := openFileShareDelete(path)
if err != nil {
return fmt.Errorf("failed to open log file %q: %v", path, err)
}
Expand Down Expand Up @@ -360,7 +360,7 @@ func ReadLogs(ctx context.Context, path, containerID string, opts *LogOptions, r
return err
}
if recreated {
newF, err := os.Open(path)
newF, err := openFileShareDelete(path)
if err != nil {
if os.IsNotExist(err) {
continue
Expand Down
29 changes: 29 additions & 0 deletions pkg/kubelet/kuberuntime/logs/logs_other.go
@@ -0,0 +1,29 @@
//go:build !windows
// +build !windows

/*
Copyright 2024 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 logs

import (
"os"
)

func openFileShareDelete(path string) (*os.File, error) {
// Noop. Only relevant for Windows.
return os.Open(path)
}
4 changes: 4 additions & 0 deletions pkg/kubelet/kuberuntime/logs/logs_test.go
Expand Up @@ -286,6 +286,10 @@ func TestReadRotatedLog(t *testing.T) {
}
}

// Finished writing into the file, close it, so we can delete it later.
err = file.Close()
assert.NoErrorf(t, err, "could not close file.")

time.Sleep(20 * time.Millisecond)
// Make the function ReadLogs end.
fakeRuntimeService.Lock()
Expand Down
49 changes: 49 additions & 0 deletions pkg/kubelet/kuberuntime/logs/logs_windows.go
@@ -0,0 +1,49 @@
//go:build windows
// +build windows

/*
Copyright 2024 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 logs

import (
"os"
"syscall"
)

// Based on Windows implementation of Windows' syscall.Open
// https://cs.opensource.google/go/go/+/refs/tags/go1.22.2:src/syscall/syscall_windows.go;l=342
// In addition to syscall.Open, this function also adds the syscall.FILE_SHARE_DELETE flag to sharemode,
// which will allow us to read from the file without blocking the file from being deleted or renamed.
// This is essential for Log Rotation which is done by renaming the open file. Without this, the file rename would fail.
func openFileShareDelete(path string) (*os.File, error) {
pathp, err := syscall.UTF16PtrFromString(path)
if err != nil {
return nil, err
}

var access uint32 = syscall.GENERIC_READ
var sharemode uint32 = syscall.FILE_SHARE_READ | syscall.FILE_SHARE_WRITE | syscall.FILE_SHARE_DELETE
var createmode uint32 = syscall.OPEN_EXISTING
var attrs uint32 = syscall.FILE_ATTRIBUTE_NORMAL

handle, err := syscall.CreateFile(pathp, access, sharemode, nil, createmode, attrs, 0)
if err != nil {
return nil, err
}

return os.NewFile(uintptr(handle), path), nil
}

0 comments on commit 1571613

Please sign in to comment.