-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #763 from imeoer/nydusify-hook-plugin
nydusify: support hook plugin
- Loading branch information
Showing
9 changed files
with
361 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ tmp | |
cmd/nydusify | ||
output | ||
nydusify-smoke | ||
nydus-hook-plugin |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,86 @@ | ||
// Copyright 2022 Nydus Developers. All rights reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package converter | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
|
||
"github.com/dragonflyoss/image-service/contrib/nydusify/pkg/hook" | ||
"github.com/dragonflyoss/image-service/contrib/nydusify/pkg/utils" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func (cvt *Converter) newHookInfo(ctx context.Context, buildLayers []*buildLayer) (*hook.Info, error) { | ||
blobs := []hook.Blob{} | ||
for _, layer := range buildLayers { | ||
record := layer.GetCacheRecord() | ||
if record.NydusBlobDesc != nil { | ||
blobs = append(blobs, hook.Blob{ | ||
ID: record.NydusBlobDesc.Digest.Hex(), | ||
Size: record.NydusBlobDesc.Size, | ||
}) | ||
} | ||
} | ||
bootstrapLayer := buildLayers[len(buildLayers)-1] | ||
bootstrapPath := bootstrapLayer.bootstrapPath | ||
|
||
if bootstrapPath == "" { | ||
cache := bootstrapLayer.cacheGlue | ||
// If we can't find bootstrap file in local, try to pull | ||
// it from cache image. | ||
if cache != nil && cache.remote != nil { | ||
record := bootstrapLayer.GetCacheRecord() | ||
reader, err := cache.remote.Pull(ctx, *record.NydusBootstrapDesc, true) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Pull cached bootstrap layer") | ||
} | ||
defer reader.Close() | ||
|
||
bootstrapPath = filepath.Join(cvt.WorkDir, "bootstraps", "bootstrap_for_hook") | ||
if err := utils.UnpackFile(reader, utils.BootstrapFileNameInLayer, bootstrapPath); err != nil { | ||
return nil, errors.Wrap(err, "Unpack cached bootstrap layer") | ||
} | ||
} | ||
} | ||
|
||
info := hook.Info{ | ||
BootstrapPath: bootstrapPath, | ||
SourceRef: cvt.SourceRemote.Ref, | ||
TargetRef: cvt.TargetRemote.Ref, | ||
Blobs: blobs, | ||
} | ||
|
||
return &info, nil | ||
} | ||
|
||
func (cvt *Converter) hookBeforePushManifest(ctx context.Context, info *hook.Info) error { | ||
if hook.Caller == nil { | ||
return nil | ||
} | ||
|
||
logrus.Info("[HOOK] Call hook 'BeforePushManifest'") | ||
|
||
if err := hook.Caller.BeforePushManifest(info); err != nil { | ||
return errors.Wrap(err, "Failed to call hook 'BeforePushManifest'") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (cvt *Converter) hookAfterPushManifest(ctx context.Context, info *hook.Info) error { | ||
if hook.Caller == nil { | ||
return nil | ||
} | ||
|
||
logrus.Info("[HOOK] Call hook 'AfterPushManifest'") | ||
|
||
if err := hook.Caller.AfterPushManifest(info); err != nil { | ||
return errors.Wrap(err, "Failed to call hook 'AfterPushManifest'") | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// Copyright 2022 Nydus Developers. All rights reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package hook | ||
|
||
import ( | ||
"net/rpc" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/go-plugin" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var hookPluginPath = "./nydus-hook-plugin" | ||
|
||
func init() { | ||
envPath := os.Getenv("NYDUS_HOOK_PLUGIN_PATH") | ||
if envPath != "" { | ||
hookPluginPath = envPath | ||
} | ||
} | ||
|
||
type Blob struct { | ||
ID string `json:"id"` | ||
Size int64 `json:"size"` | ||
} | ||
|
||
type Info struct { | ||
BootstrapPath string `json:"bootstrap_path"` | ||
SourceRef string `json:"source_ref"` | ||
TargetRef string `json:"target_ref"` | ||
Blobs []Blob `json:"blobs"` | ||
} | ||
|
||
type Hook interface { | ||
BeforePushManifest(info *Info) error | ||
AfterPushManifest(info *Info) error | ||
} | ||
|
||
type RPC struct{ client *rpc.Client } | ||
|
||
func (h *RPC) BeforePushManifest(info *Info) error { | ||
var resp error | ||
err := h.client.Call("Plugin.BeforePushManifest", info, &resp) | ||
if err != nil { | ||
return err | ||
} | ||
return resp | ||
} | ||
|
||
func (h *RPC) AfterPushManifest(info *Info) error { | ||
var resp error | ||
err := h.client.Call("Plugin.AfterPushManifest", info, &resp) | ||
if err != nil { | ||
return err | ||
} | ||
return resp | ||
} | ||
|
||
type RPCServer struct { | ||
Impl Hook | ||
} | ||
|
||
func (s *RPCServer) BeforePushManifest(info Info, resp *error) error { | ||
*resp = s.Impl.BeforePushManifest(&info) | ||
return *resp | ||
} | ||
|
||
func (s *RPCServer) AfterPushManifest(info Info, resp *error) error { | ||
*resp = s.Impl.AfterPushManifest(&info) | ||
return *resp | ||
} | ||
|
||
type Plugin struct { | ||
Impl Hook | ||
} | ||
|
||
func (p *Plugin) Server(*plugin.MuxBroker) (interface{}, error) { | ||
return &RPCServer{Impl: p.Impl}, nil | ||
} | ||
|
||
func (Plugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) { | ||
return &RPC{client: c}, nil | ||
} | ||
|
||
var Caller Hook | ||
|
||
var handshakeConfig = plugin.HandshakeConfig{ | ||
ProtocolVersion: 1, | ||
MagicCookieKey: "NYDUS_HOOK_PLUGIN", | ||
MagicCookieValue: "nydus-hook-plugin", | ||
} | ||
|
||
func NewPlugin(pluginImpl Hook) { | ||
plugin.Serve(&plugin.ServeConfig{ | ||
HandshakeConfig: handshakeConfig, | ||
Plugins: map[string]plugin.Plugin{ | ||
"hook": &Plugin{Impl: pluginImpl}, | ||
}, | ||
}) | ||
} | ||
|
||
var client *plugin.Client | ||
|
||
func Init() { | ||
if Caller != nil { | ||
return | ||
} | ||
|
||
if _, err := os.Stat(hookPluginPath); err != nil { | ||
if errors.Is(err, os.ErrNotExist) { | ||
return | ||
} | ||
logrus.Errorln(errors.Wrapf(err, "try load hook plugin %s", hookPluginPath)) | ||
return | ||
} | ||
|
||
var pluginMap = map[string]plugin.Plugin{ | ||
"hook": &Plugin{}, | ||
} | ||
|
||
client = plugin.NewClient(&plugin.ClientConfig{ | ||
HandshakeConfig: handshakeConfig, | ||
Plugins: pluginMap, | ||
Cmd: exec.Command(hookPluginPath), | ||
Logger: hclog.New(&hclog.LoggerOptions{ | ||
Output: hclog.DefaultOutput, | ||
Level: hclog.Error, | ||
Name: "plugin", | ||
}), | ||
}) | ||
|
||
rpcClient, err := client.Client() | ||
if err != nil { | ||
logrus.WithError(err).Error("Failed to create rpc client") | ||
return | ||
} | ||
|
||
raw, err := rpcClient.Dispense("hook") | ||
if err != nil { | ||
logrus.WithError(err).Error("Failed to dispense hook") | ||
return | ||
} | ||
|
||
logrus.Infof("[HOOK] Loaded hook plugin %s", hookPluginPath) | ||
|
||
Caller = raw.(Hook) | ||
} | ||
|
||
func Close() { | ||
if client != nil { | ||
defer client.Kill() | ||
} | ||
} |
Oops, something went wrong.