Last active
March 1, 2023 16:50
-
-
Save harshanarayana/0544e95aa62a20248dae5d2bbbad0858 to your computer and use it in GitHub Desktop.
NRI based Container ID Injection
This file contains hidden or 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
package main | |
import ( | |
"context" | |
"flag" | |
"fmt" | |
"os" | |
"github.com/sirupsen/logrus" | |
"sigs.k8s.io/yaml" | |
"github.com/containerd/nri/pkg/api" | |
"github.com/containerd/nri/pkg/stub" | |
) | |
type config struct { | |
CfgParam1 string `json:"cfgParam1"` | |
} | |
type plugin struct { | |
stub stub.Stub | |
mask stub.EventMask | |
} | |
var ( | |
cfg config | |
log *logrus.Logger | |
) | |
func (p *plugin) Configure(config, runtime, version string) (stub.EventMask, error) { | |
log.Infof("Connected to %s/%s...", runtime, version) | |
if config == "" { | |
return 0, nil | |
} | |
err := yaml.Unmarshal([]byte(config), &cfg) | |
if err != nil { | |
return 0, fmt.Errorf("failed to parse configuration: %w", err) | |
} | |
log.Info("Got configuration data %+v...", cfg) | |
return 0, nil | |
} | |
func (p *plugin) Synchronize(pods []*api.PodSandbox, containers []*api.Container) ([]*api.ContainerUpdate, error) { | |
log.Info("Synchronizing state with the runtime...") | |
return nil, nil | |
} | |
func (p *plugin) Shutdown() { | |
log.Info("Runtime shutting down...") | |
} | |
func (p *plugin) CreateContainer(pod *api.PodSandbox, ctr *api.Container) (*api.ContainerAdjustment, []*api.ContainerUpdate, error) { | |
os.WriteFile("/tmp/container-id", []byte(ctr.GetId()), os.FileMode(0755)) | |
adjustment := &api.ContainerAdjustment{} | |
updates := []*api.ContainerUpdate{} | |
adjustment.Env = append(adjustment.Env, &api.KeyValue{ | |
Key: "CONTAINER_ID", | |
Value: ctr.GetId(), | |
}) | |
adjustment.Mounts = append(adjustment.Mounts, &api.Mount{ | |
Source: "/tmp/container-id", | |
Destination: "/run/container-id", | |
Type: "bind", | |
Options: []string{ | |
"rbind", | |
"rprivate", | |
"rw", | |
}, | |
}) | |
return adjustment, updates, nil | |
} | |
func (p *plugin) onClose() { | |
log.Infof("Connection to the runtime lost, exiting...") | |
os.Exit(0) | |
} | |
func main() { | |
var ( | |
pluginName string | |
pluginIdx string | |
err error | |
) | |
log = logrus.StandardLogger() | |
log.SetFormatter(&logrus.TextFormatter{ | |
PadLevelText: true, | |
}) | |
flag.StringVar(&pluginName, "name", "", "plugin name to register to NRI") | |
flag.StringVar(&pluginIdx, "idx", "", "plugin index to register to NRI") | |
flag.Parse() | |
p := &plugin{} | |
opts := []stub.Option{ | |
stub.WithOnClose(p.onClose), | |
} | |
if pluginName != "" { | |
opts = append(opts, stub.WithPluginName(pluginName)) | |
} | |
if pluginIdx != "" { | |
opts = append(opts, stub.WithPluginIdx(pluginIdx)) | |
} | |
if p.stub, err = stub.New(p, opts...); err != nil { | |
log.Fatalf("failed to create plugin stub: %v", err) | |
} | |
if err = p.stub.Run(context.Background()); err != nil { | |
log.Errorf("plugin exited (%v)", err) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add following to
/etc/containerd/config.toml
And do the following
echo "disableConnections: false" > /etc/nri/nri.conf
usingcontainerd - v1.7.0-beta.2+