Skip to content

Commit

Permalink
feat: delete container on failed hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
nixpig committed Dec 27, 2024
1 parent cf1d51b commit 345b39d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ This is a personal project for me to explore and better understand the OCI Runti
- [ ] Implement [Cgroups v2](https://github.com/opencontainers/runtime-spec/blob/main/config-linux.md#control-groups)
- [ ] Implement optional [Seccomp](https://github.com/opencontainers/runtime-spec/blob/main/config-linux.md#seccomp)
- [ ] Implement optional [AppArmor](https://github.com/opencontainers/runtime-spec/blob/main/config.md#linux-process)
- [ ] Rollback (step 12)
- [ ] Build, version and package
- [ ] Refactor and tidy-up

Expand Down
8 changes: 8 additions & 0 deletions container/container_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ import (

func (c *Container) Init(reexecCmd string, reexecArgs []string) error {
if err := c.ExecHooks("createRuntime"); err != nil {
if err := c.Delete(true); err != nil {
return fmt.Errorf("delete container: %w", err)
}

return fmt.Errorf("execute createRuntime hooks: %w", err)
}

if err := c.ExecHooks("createContainer"); err != nil {
if err := c.Delete(true); err != nil {
return fmt.Errorf("delete container: %w", err)
}

return fmt.Errorf("execute createContainer hooks: %w", err)
}

Expand Down
4 changes: 4 additions & 0 deletions container/container_reexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ func (c *Container) Reexec() error {
}

if err := c.ExecHooks("startContainer"); err != nil {
if err := c.Delete(true); err != nil {
return fmt.Errorf("delete container: %w", err)
}

return fmt.Errorf("execute startContainer hooks: %w", err)
}

Expand Down
4 changes: 2 additions & 2 deletions container/container_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func (c *Container) Start() error {
}

if err := c.ExecHooks("prestart"); err != nil {
if err := c.ExecHooks("poststop"); err != nil {
fmt.Println("Warning: failed to execute poststop hooks: ", err)
if err := c.Delete(true); err != nil {
return fmt.Errorf("delete container: %w", err)
}

return fmt.Errorf("execute prestart hooks: %w", err)
Expand Down

0 comments on commit 345b39d

Please sign in to comment.