4

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

djmonki
  • 3,020
  • 7
  • 18
user804401
  • 1,990
  • 9
  • 38
  • 71

2 Answers2

2

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.

djmonki
  • 3,020
  • 7
  • 18
user804401
  • 1,990
  • 9
  • 38
  • 71
0

Found an issue with the .ps1 script:

$destinationPath = "C:/Data/appname/PROD "

There is a space at the end of the path, which is incorrect and can cause problems when attempting to move the files. The space at the end of the path makes it an invalid or non-existent directory.

Taking in mind the above, it can lead to unexpected directory creation and the file not being moved to the intended location.

The Move-Item cmdlet requires a valid path for the destination parameter, otherwise, it will fail to move the file.

Update the variable to ensure there is no space:

$destinationPath = "C:/Data/appname/PROD"
djmonki
  • 3,020
  • 7
  • 18
  • The issue is not related to the script. Identified that if the Powershell script contains code to move files to volume it is not working. Does anyone know how to comearound with this. There are no logs that will be shown in PreStop Hook. Container event logs do not help – user804401 Jul 03 '23 at 16:21
  • So to clarify, the preStop hook is not running this or any Powershell script, but if you ran the this or any Powershell script manually inside the container it will work ? – djmonki Jul 03 '23 at 17:46
  • yes, In Prestop hook basic powershell script is working like new-item. If I have code to copy files to volume it is not working – user804401 Jul 04 '23 at 05:37