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 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for running OCI hooks within the guest. A 'drop-in' path (guest_hook_path) is specified in the cli configuration file and if set, the agent will look for OCI hooks in this directory and inject them into the container life cycle. Fixes: #348 Replaces: #365 Co-authored-by: Edward Guzman <eguzman@nvidia.com> Co-authored-by: Felix Abecassis <fabecassis@nvidia.com> Signed-off-by: Edward Guzman <eguzman@nvidia.com> Signed-off-by: Felix Abecassis <fabecassis@nvidia.com>
- Loading branch information
Showing
7 changed files
with
571 additions
and
206 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
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,113 @@ | ||
// | ||
// Copyright (c) 2018 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
|
||
"github.com/opencontainers/runtime-spec/specs-go" | ||
) | ||
|
||
// OCI config file | ||
const ( | ||
ociConfigFile string = "config.json" | ||
ociConfigFileMode os.FileMode = 0444 | ||
) | ||
|
||
// changeToBundlePath changes the cwd to the bundle path defined in the OCI spec | ||
func changeToBundlePath(spec *specs.Spec) (string, error) { | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
return cwd, err | ||
} | ||
|
||
if spec == nil || spec.Root == nil || spec.Root.Path == "" { | ||
return cwd, errors.New("Invalid OCI spec") | ||
} | ||
|
||
rootfsPath := spec.Root.Path | ||
if !filepath.IsAbs(rootfsPath) { | ||
rootfsPath = filepath.Join(cwd, rootfsPath) | ||
} | ||
|
||
bundlePath := filepath.Dir(rootfsPath) | ||
|
||
err = os.Chdir(bundlePath) | ||
return cwd, err | ||
} | ||
|
||
// writeSpecToFile writes the container's OCI spec to 'config.json' | ||
// in the bundle path as expected by the OCI specification. | ||
func writeSpecToFile(spec *specs.Spec) error { | ||
cwd, err := os.Getwd() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
configPath := path.Join(cwd, ociConfigFile) | ||
f, err := os.OpenFile(configPath, os.O_RDWR|os.O_CREATE, ociConfigFileMode) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer f.Close() | ||
err = json.NewEncoder(f).Encode(spec) | ||
return err | ||
} | ||
|
||
func isValidHook(file os.FileInfo) (bool, error) { | ||
if file.IsDir() { | ||
return false, fmt.Errorf("is a directory") | ||
} | ||
|
||
mode := file.Mode() | ||
if (mode & os.ModeSymlink) != 0 { | ||
return false, fmt.Errorf("is a symbolic link") | ||
} | ||
|
||
perm := mode & os.ModePerm | ||
if (perm & 0111) == 0 { | ||
return false, fmt.Errorf("is not executable") | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// findHooks searches guestHookPath for any OCI hooks for a given hookType | ||
func findHooks(guestHookPath, hookType string) (hooksFound []specs.Hook) { | ||
hooksPath := path.Join(guestHookPath, hookType) | ||
|
||
files, err := ioutil.ReadDir(hooksPath) | ||
if err != nil { | ||
agentLog.WithError(err).Infof("Skipping hook type %s", hookType) | ||
return | ||
} | ||
|
||
for _, file := range files { | ||
name := file.Name() | ||
if ok, err := isValidHook(file); !ok { | ||
agentLog.WithError(err).Warnf("Skipping hook %s", name) | ||
continue | ||
} | ||
|
||
agentLog.Infof("Adding hook %s of type %s", name, hookType) | ||
hooksFound = append(hooksFound, specs.Hook{ | ||
Path: path.Join(hooksPath, name), | ||
Args: []string{name, hookType}, | ||
}) | ||
} | ||
|
||
agentLog.Infof("Added %d hooks of type %s", len(hooksFound), hookType) | ||
|
||
return | ||
} |
Oops, something went wrong.