-
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.
nri-plugin: add a configuration file and use a lock to ensure concurr…
…ency-safe, and so on
- Loading branch information
Showing
12 changed files
with
914 additions
and
33 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,222 @@ | ||
/* | ||
* 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/nri/pkg/api" | ||
"github.com/containerd/nri/pkg/stub" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/viper" | ||
"github.com/urfave/cli/v2" | ||
|
||
"github.com/containerd/nydus-snapshotter/pkg/errdefs" | ||
"github.com/containerd/nydus-snapshotter/pkg/prefetch" | ||
"github.com/containerd/nydus-snapshotter/version" | ||
) | ||
|
||
const ( | ||
endpointPrefetch = "/api/v1/prefetch" | ||
defaultEvents = "RunPodSandbox" | ||
defaultSystemControllerAddress = "/run/containerd-nydus/system.sock" | ||
nydusPrefetchAnnotation = "containerd.io/nydus-prefetch" | ||
imageAddressAnnotation = "containerd.io/nydus-image-reference" | ||
) | ||
|
||
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: "unix domain socket address. If defined in the configuration file, there is no need to add ", | ||
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 | ||
) | ||
|
||
// sendDataOverHTTP sends the prefetch data to the specified endpoint over HTTP using a Unix socket. | ||
func sendDataOverHTTP(data prefetch.MessagePrefetch, 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) | ||
} | ||
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 := prefetch.MessagePrefetch{ | ||
ImageReference: 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 | ||
) | ||
|
||
log = logrus.StandardLogger() | ||
|
||
viper.SetConfigName("prefetchConfig") | ||
viper.SetConfigType("toml") | ||
viper.AddConfigPath(".") | ||
|
||
err = viper.ReadInConfig() | ||
if err != nil { | ||
log.Warnf("failed to reading config file: %v", err) | ||
} | ||
|
||
if configSocketAddr := viper.GetString("file_prefetch.socket_address"); configSocketAddr != "" { | ||
globalSocket = configSocketAddr | ||
} else { | ||
globalSocket = flags.Args.SocketAddress | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[file_prefetch] | ||
# This is used to configure the socket address for the file prefetch. | ||
socket_address = "/run/containerd-nydus/system.sock" |
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.