Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Container PreStop Hook - Unable to Move Log Files to Volume

Trying to configure a PreStop Hook which should run a script in a windows container. Observed that the prestop hook is not executing the script in OpenShift Windows Container. This is a Powershell script which moves logs to volume

Tried specifying basic hello world to the console, that is also not working.

Tried increasing terminationGracePeriodSeconds to 1001, had no luck with that approach.

My YAML file has a Prestop hook (see below), wonder if a Windows Container in OpenShift has any limitations with PreStop Hook processes?

lifecycle:
  preStop:
    exec:
      command:
        - 'C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe'
        - '-File'
        - 'C:\pathtoscriptinrepo\appscript.ps1'

I tried several ways by specifying cmd also but it does not work.

Below is the Powershell script that is executed:

$sourcePath ="C:/somefolder/logs"
$destinationPath = "C:/Data/appname/PROD "
# Get all .log files in the source folder
$files = Get-ChildItem -Path $sourcePath -Filter "*.log"
# Move each .log file to the destination folder
foreach ($file in $files) {
$destination = Join-Path -Path $destinationPath -ChildPath $file.Name
  Move-Item -Path $file.FullName -Destination $destination
  Write-Host "Moved file: $($file.Name)"
}

Found that the moving of files to a volume using a Popwershell script in PreStop is not working. Any ideas or solutions ?

The service account for the Persistent Volume Claim has Full permissions

like image 229
user804401 Avatar asked Sep 16 '25 05:09

user804401


1 Answers

Was able to fix this.

Observed that the Powershell Move-Item command does not work during PreStop executions.

Replacing it with Copy-Item command within the Powershell script resolved the issue, it was able to move the files.

It appears that during PreStop process, the Powershell Move-Item does not work and execution is prevented, it maybe that Windows will lock few files that are in use and causing the above.

like image 66
user804401 Avatar answered Sep 19 '25 07:09

user804401