1 package hooks 2 3 import ( 4 "fmt" 5 6 oci "github.com/opencontainers/runtime-spec/specs-go" 7 ) 8 9 // Note: The below type definition as well as constants have been copied from 10 // https://github.com/opencontainers/runc/blob/master/libcontainer/configs/config.go. 11 // This is done to not introduce a direct dependency on runc, which would complicate 12 // integration with windows. 13 type HookName string 14 15 const ( 16 17 // Prestart commands are executed after the container namespaces are created, 18 // but before the user supplied command is executed from init. 19 // Note: This hook is now deprecated 20 // Prestart commands are called in the Runtime namespace. 21 Prestart HookName = "prestart" 22 23 // CreateRuntime commands MUST be called as part of the create operation after 24 // the runtime environment has been created but before the pivot_root has been executed. 25 // CreateRuntime is called immediately after the deprecated Prestart hook. 26 // CreateRuntime commands are called in the Runtime Namespace. 27 CreateRuntime HookName = "createRuntime" 28 ) 29 30 // NewOCIHook creates a new oci.Hook with given parameters 31 func NewOCIHook(path string, args, env []string) oci.Hook { 32 return oci.Hook{ 33 Path: path, 34 Args: args, 35 Env: env, 36 } 37 } 38 39 // AddOCIHook adds oci.Hook of the given hook name to spec 40 func AddOCIHook(spec *oci.Spec, hn HookName, hk oci.Hook) error { 41 if spec.Hooks == nil { 42 spec.Hooks = &oci.Hooks{} 43 } 44 switch hn { 45 case Prestart: 46 spec.Hooks.Prestart = append(spec.Hooks.Prestart, hk) 47 case CreateRuntime: 48 spec.Hooks.CreateRuntime = append(spec.Hooks.CreateRuntime, hk) 49 default: 50 return fmt.Errorf("hook %q is not supported", hn) 51 } 52 return nil 53 } 54