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 script is a powershell which copies logs to volume
Tried specifying basic hello world to the console, that is also not working.
Tried increasing terminationGracePeriodSeconds to 1001, no luck
My YAML file has prestop hook like below,
Does Windows Container in OpenShift has any limitation on PreStop Hook.
lifecycle:
preStop:
exec:
command:
- 'C:WindowsSystem32WindowsPowerShellv1.0Powershell.exe'
- '-File'
- 'C:pathtoscriptinrepoappscript.ps1'
I tried several ways by specifying cmd also but it does not work.
Below is my Powershell script
$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 copying of files to volume using a script in PreStop is not working. Any ideas or solutions ?
The service account for the Persistent Volume Claim has Full permissions
2
Answers
Was able to fix this.
Observed that Move Item does not work during PreStop Executions
Replacing it with Copy-Item worked for me. Was able to move the files.
I think during PreStop Move item will not work, maybe windows will lock few files that are in use.
Found an issue with the .ps1 script:
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: