Skip to content

[codex] Add configurable container pre-stop commands#570

Closed
zhangpu1211 wants to merge 1 commit into
GZTimeWalker:developfrom
zhangpu1211:codex/container-pre-stop-hook
Closed

[codex] Add configurable container pre-stop commands#570
zhangpu1211 wants to merge 1 commit into
GZTimeWalker:developfrom
zhangpu1211:codex/container-pre-stop-hook

Conversation

@zhangpu1211

Copy link
Copy Markdown
Contributor

What changed

This PR adds an optional PreStopCommand for container-based challenges.

The command is stored at the challenge level, copied into the created container record, and executed as a best-effort hook right before the platform destroys the container.

It includes:

  • persisted PreStopCommand support for game and exercise challenges
  • propagation through container creation for admin test containers, game instances, and exercise instances
  • best-effort execution in both Docker and Kubernetes container managers
  • transfer/import support
  • admin challenge edit UI support
  • unit and integration test coverage for persistence and transfer mapping

Why

A fork (mygzctf) uses a fixed /about_to_destroy hook before removing containers. That idea is valuable, but the hardcoded path is too specific for upstream.

This PR generalizes it into a configurable, optional platform capability:

  • challenge authors can opt in only when needed
  • the command is executed directly without going through a shell
  • failures are logged but do not block container deletion
  • a short timeout keeps cleanup best-effort instead of making deletion hang indefinitely

Behavior

  • If PreStopCommand is empty, container deletion behavior is unchanged.
  • If PreStopCommand is configured, the platform tries to execute it inside the container before deletion.
  • Docker and Kubernetes providers both use the same configuration field.
  • The command is stored on the container instance so destruction does not depend on reloading the originating challenge.

Validation

Local validation completed with .NET 10:

  • dotnet build src/GZCTF/GZCTF.csproj -c Release --no-restore
  • dotnet test src/GZCTF.Test/GZCTF.Test.csproj -c Release --no-restore --filter FullyQualifiedName~TransferChallengeTests
  • dotnet test src/GZCTF.Integration.Test/GZCTF.Integration.Test.csproj -c Release --no-restore --filter FullyQualifiedName~UpdateGameChallenge_ShouldPersistPreStopCommand
  • pnpm build in src/GZCTF/ClientApp

Note

This branch also includes the minimal Program.resx namespace fix in GZCTF.csproj, because the current develop branch still hits a resource-generation conflict locally without it. The same fix was already needed on the previous PR to make build and integration testing work.

@codecov

codecov Bot commented Apr 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 33.73494% with 55 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.94%. Comparing base (fb65b9b) to head (0c01c23).

Files with missing lines Patch % Lines
.../GZCTF/Services/Container/Manager/DockerManager.cs 25.64% 27 Missing and 2 partials ⚠️
...TF/Services/Container/Manager/KubernetesManager.cs 16.66% 23 Missing and 2 partials ⚠️
src/GZCTF/Models/Data/GameChallenge.cs 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #570      +/-   ##
===========================================
- Coverage    59.17%   58.94%   -0.23%     
===========================================
  Files          184      184              
  Lines         9354     9436      +82     
  Branches      1135     1144       +9     
===========================================
+ Hits          5535     5562      +27     
- Misses        3394     3447      +53     
- Partials       425      427       +2     
Files with missing lines Coverage Δ
src/GZCTF/Controllers/EditController.cs 50.77% <100.00%> (+0.12%) ⬆️
src/GZCTF/Models/Data/Challenge.cs 87.80% <100.00%> (+0.30%) ⬆️
src/GZCTF/Models/Data/Container.cs 22.58% <100.00%> (+1.26%) ⬆️
src/GZCTF/Models/Internal/ContainerConfig.cs 100.00% <100.00%> (ø)
...TF/Models/Request/Edit/ChallengeEditDetailModel.cs 98.24% <100.00%> (+0.06%) ⬆️
.../GZCTF/Models/Request/Edit/ChallengeUpdateModel.cs 91.30% <100.00%> (+0.39%) ⬆️
src/GZCTF/Models/Transfer/TransferChallenge.cs 100.00% <100.00%> (ø)
src/GZCTF/Models/Transfer/TransferExtensions.cs 87.97% <100.00%> (+4.00%) ⬆️
src/GZCTF/Repositories/GameInstanceRepository.cs 65.91% <100.00%> (+0.12%) ⬆️
src/GZCTF/Services/Transfer/GameImportService.cs 68.62% <100.00%> (+0.10%) ⬆️
... and 3 more

... and 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants