This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hook: Move OCI hooks handling to the CLI
The CLI being the implementation of the OCI specification, and the hooks being OCI specific, it makes sense to move the handling of any OCI hooks to the CLI level. This changes allows the Kata API to become OCI agnostic. Signed-off-by: Sebastien Boeuf <[email protected]>
- Loading branch information
Sebastien Boeuf
committed
Aug 17, 2018
1 parent
3333f8b
commit a4b81e8
Showing
11 changed files
with
199 additions
and
504 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright (c) 2018 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/kata-containers/runtime/virtcontainers/pkg/oci" | ||
"github.com/opencontainers/runtime-spec/specs-go" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Logger returns a logrus logger appropriate for logging hook messages | ||
func hookLogger() *logrus.Entry { | ||
return kataLog.WithField("subsystem", "hook") | ||
} | ||
|
||
func runHook(hook specs.Hook, cid, bundlePath string) error { | ||
state := specs.State{ | ||
Pid: os.Getpid(), | ||
Bundle: bundlePath, | ||
ID: cid, | ||
} | ||
|
||
stateJSON, err := json.Marshal(state) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var stdout, stderr bytes.Buffer | ||
cmd := &exec.Cmd{ | ||
Path: hook.Path, | ||
Args: hook.Args, | ||
Env: hook.Env, | ||
Stdin: bytes.NewReader(stateJSON), | ||
Stdout: &stdout, | ||
Stderr: &stderr, | ||
} | ||
|
||
if err := cmd.Start(); err != nil { | ||
return err | ||
} | ||
|
||
if hook.Timeout == nil { | ||
if err := cmd.Wait(); err != nil { | ||
return fmt.Errorf("%s: stdout: %s, stderr: %s", err, stdout.String(), stderr.String()) | ||
} | ||
} else { | ||
done := make(chan error, 1) | ||
go func() { | ||
done <- cmd.Wait() | ||
close(done) | ||
}() | ||
|
||
select { | ||
case err := <-done: | ||
if err != nil { | ||
return fmt.Errorf("%s: stdout: %s, stderr: %s", err, stdout.String(), stderr.String()) | ||
} | ||
case <-time.After(time.Duration(*hook.Timeout) * time.Second): | ||
if err := syscall.Kill(cmd.Process.Pid, syscall.SIGKILL); err != nil { | ||
return err | ||
} | ||
|
||
return fmt.Errorf("Hook timeout") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func preStartHooks(spec oci.CompatOCISpec, cid, bundlePath string) error { | ||
// If no hook available, nothing needs to be done. | ||
if spec.Hooks == nil { | ||
return nil | ||
} | ||
|
||
for _, hook := range spec.Hooks.Prestart { | ||
if err := runHook(hook, cid, bundlePath); err != nil { | ||
hookLogger().WithFields(logrus.Fields{ | ||
"hook-type": "pre-start", | ||
"error": err, | ||
}).Error("hook error") | ||
|
||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func postStartHooks(spec oci.CompatOCISpec, cid, bundlePath string) error { | ||
// If no hook available, nothing needs to be done. | ||
if spec.Hooks == nil { | ||
return nil | ||
} | ||
|
||
for _, hook := range spec.Hooks.Poststart { | ||
if err := runHook(hook, cid, bundlePath); err != nil { | ||
hookLogger().WithFields(logrus.Fields{ | ||
"hook-type": "post-start", | ||
"error": err, | ||
}).Error("hook error") | ||
|
||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func postStopHooks(spec oci.CompatOCISpec, cid, bundlePath string) error { | ||
// If no hook available, nothing needs to be done. | ||
if spec.Hooks == nil { | ||
return nil | ||
} | ||
|
||
for _, hook := range spec.Hooks.Poststop { | ||
if err := runHook(hook, cid, bundlePath); err != nil { | ||
hookLogger().WithFields(logrus.Fields{ | ||
"hook-type": "post-stop", | ||
"error": err, | ||
}).Error("hook error") | ||
|
||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.