-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP]nri-plugin: Modify nri plugin r3 & transmit prefetchfiles to sna…
…pshotter
- Loading branch information
Showing
8 changed files
with
277 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,204 @@ | ||
/* | ||
* Copyright (c) 2023. Nydus Developers. All rights reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log/syslog" | ||
"net" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/containerd/nydus-snapshotter/config" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
"github.com/urfave/cli/v2" | ||
|
||
"github.com/containerd/nri/pkg/api" | ||
"github.com/containerd/nri/pkg/stub" | ||
"github.com/containerd/nydus-snapshotter/pkg/errdefs" | ||
"github.com/containerd/nydus-snapshotter/version" | ||
) | ||
|
||
const ( | ||
endpointPrefetch = "/api/v1/prefetch" | ||
defaultEvents = "RunPodSandbox" | ||
defaultSystemControllerAddress = "/run/containerd-nydus/system.sock" | ||
nydusPrefetchAnnotation = "containerd.io/snapshot/nydus-prefetch" | ||
imageAddressAnnotation = "containerd.io/snapshot/nydus-image-address" | ||
) | ||
|
||
type PluginArgs struct { | ||
PluginName string | ||
PluginIdx string | ||
SocketAddress string | ||
} | ||
|
||
type Flags struct { | ||
Args *PluginArgs | ||
Flag []cli.Flag | ||
} | ||
|
||
func buildFlags(args *PluginArgs) []cli.Flag { | ||
return []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "name", | ||
Usage: "plugin name to register to NRI", | ||
Destination: &args.PluginName, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "idx", | ||
Usage: "plugin index to register to NRI", | ||
Destination: &args.PluginIdx, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "socket-addr", | ||
Value: defaultSystemControllerAddress, | ||
Usage: "default unix domain socket address", | ||
Destination: &args.SocketAddress, | ||
}, | ||
} | ||
} | ||
|
||
func NewPluginFlags() *Flags { | ||
var args PluginArgs | ||
return &Flags{ | ||
Args: &args, | ||
Flag: buildFlags(&args), | ||
} | ||
} | ||
|
||
type plugin struct { | ||
stub stub.Stub | ||
mask stub.EventMask | ||
} | ||
|
||
var ( | ||
globalSocket string | ||
log *logrus.Logger | ||
logWriter *syslog.Writer | ||
) | ||
|
||
func sendDataOverHTTP(data config.PrefetchMessage, endpoint, sock string) error { | ||
url := fmt.Sprintf("http://unix%s", endpoint) | ||
|
||
body, err := json.Marshal(data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(body)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client := &http.Client{ | ||
Transport: &http.Transport{ | ||
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { | ||
return net.Dial("unix", sock) | ||
}, | ||
}, | ||
} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("failed to send data, status code: %d", resp.StatusCode) | ||
} | ||
defer resp.Body.Close() | ||
|
||
return nil | ||
} | ||
|
||
func (p *plugin) RunPodSandbox(pod *api.PodSandbox) error { | ||
prefetchList, ok := pod.Annotations[nydusPrefetchAnnotation] | ||
if !ok { | ||
return nil | ||
} | ||
imageAddress, ok := pod.Annotations[imageAddressAnnotation] | ||
if !ok { | ||
return nil | ||
} | ||
msg := config.PrefetchMessage{ | ||
ImageAddress: imageAddress, | ||
PrefetchFiles: prefetchList, | ||
} | ||
|
||
err := sendDataOverHTTP(msg, endpointPrefetch, globalSocket) | ||
if err != nil { | ||
log.Errorf("failed to send data: %v\n", err) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func main() { | ||
|
||
flags := NewPluginFlags() | ||
app := &cli.App{ | ||
Name: "prefetch-nri-plugin", | ||
Usage: "NRI plugin for obtaining and transmitting prefetch files path", | ||
Version: version.Version, | ||
Flags: flags.Flag, | ||
HideVersion: true, | ||
Action: func(c *cli.Context) error { | ||
var ( | ||
opts []stub.Option | ||
err error | ||
) | ||
|
||
globalSocket = flags.Args.SocketAddress | ||
|
||
log = logrus.StandardLogger() | ||
log.SetFormatter(&logrus.TextFormatter{ | ||
PadLevelText: true, | ||
}) | ||
logWriter, err = syslog.New(syslog.LOG_INFO, "prefetch-nri-plugin") | ||
|
||
if err == nil { | ||
log.SetOutput(io.MultiWriter(os.Stdout, logWriter)) | ||
} | ||
|
||
if flags.Args.PluginName != "" { | ||
opts = append(opts, stub.WithPluginName(flags.Args.PluginName)) | ||
} | ||
if flags.Args.PluginIdx != "" { | ||
opts = append(opts, stub.WithPluginIdx(flags.Args.PluginIdx)) | ||
} | ||
|
||
p := &plugin{} | ||
|
||
if p.mask, err = api.ParseEventMask(defaultEvents); err != nil { | ||
log.Fatalf("failed to parse events: %v", err) | ||
} | ||
|
||
if p.stub, err = stub.New(p, opts...); err != nil { | ||
log.Fatalf("failed to create plugin stub: %v", err) | ||
} | ||
|
||
err = p.stub.Run(context.Background()) | ||
if err != nil { | ||
return errors.Wrap(err, "plugin exited") | ||
} | ||
return nil | ||
}, | ||
} | ||
if err := app.Run(os.Args); err != nil { | ||
|
||
if errdefs.IsConnectionClosed(err) { | ||
log.Info("prefetch NRI plugin exited") | ||
} else { | ||
log.WithError(err).Fatal("failed to start prefetch NRI 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
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
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