From 345b39d69398afbc566be347fc83c23756ebfbd0 Mon Sep 17 00:00:00 2001 From: nixpig <143995476+nixpig@users.noreply.github.com> Date: Fri, 27 Dec 2024 05:43:46 +0000 Subject: [PATCH] feat: delete container on failed hooks --- README.md | 1 - container/container_init.go | 8 ++++++++ container/container_reexec.go | 4 ++++ container/container_start.go | 4 ++-- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7e81c7a..b0d5f4d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/container/container_init.go b/container/container_init.go index c8de447..9a25cc6 100644 --- a/container/container_init.go +++ b/container/container_init.go @@ -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) } diff --git a/container/container_reexec.go b/container/container_reexec.go index 66f9e70..46bef79 100644 --- a/container/container_reexec.go +++ b/container/container_reexec.go @@ -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) } diff --git a/container/container_start.go b/container/container_start.go index 607dbd4..f7a07e0 100644 --- a/container/container_start.go +++ b/container/container_start.go @@ -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)