Skip to content

Commit bafb6d9

Browse files
committed
default-validator: provide external plugin version.
Provide an externally comilable standalone version of the default validator plugin. In addition to an illustrative purpose, this provides an easier way of experimenting and developing new validation features. Instead of having to recompile the runtime, one can simply modify and run the externally compiled version. Signed-off-by: Krisztian Litkey <[email protected]>
1 parent 278aa24 commit bafb6d9

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ PLUGINS := \
5656
$(BIN_PATH)/ulimit-adjuster \
5757
$(BIN_PATH)/v010-adapter \
5858
$(BIN_PATH)/template \
59+
$(BIN_PATH)/default-validator \
5960
$(BIN_PATH)/wasm
6061

6162

@@ -134,6 +135,11 @@ $(BIN_PATH)/template: $(wildcard plugins/template/*.go)
134135
$(Q)echo "Building $@..."; \
135136
cd $(dir $<) && $(GO_BUILD) -o $@ .
136137

138+
$(BIN_PATH)/default-validator: $(wildcard plugins/default-validator/external/*.go) $(wildcard plugins/default-validator/*.go)
139+
$(Q)echo "Building $@..."; \
140+
cd $(dir $<) && $(GO_BUILD) -o $@ .
141+
142+
137143
ifneq ($(TINYGO_DOCKER),1)
138144
$(BIN_PATH)/wasm: $(wildcard plugins/wasm/*.go)
139145
$(Q)echo "Building $@..."; \
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"context"
21+
"flag"
22+
"fmt"
23+
"os"
24+
25+
"github.com/sirupsen/logrus"
26+
"gopkg.in/yaml.v3"
27+
28+
"github.com/containerd/nri/pkg/stub"
29+
validator "github.com/containerd/nri/plugins/default-validator"
30+
)
31+
32+
type plugin struct {
33+
*validator.DefaultValidator
34+
stub stub.Stub
35+
}
36+
37+
var (
38+
log *logrus.Logger
39+
)
40+
41+
func (p *plugin) Configure(_ context.Context, config, _, _ string) (stub.EventMask, error) {
42+
if config == "" {
43+
log.Infof("No configuration provided, using defaults...")
44+
return 0, nil
45+
}
46+
47+
cfg := &validator.DefaultValidatorConfig{}
48+
err := yaml.Unmarshal([]byte(config), cfg)
49+
if err != nil {
50+
return 0, fmt.Errorf("failed to parse configuration: %w", err)
51+
}
52+
53+
if !cfg.Enable {
54+
log.Infof("Validation disabled, exiting....")
55+
os.Exit(0)
56+
}
57+
58+
log.Infof("Using configuration %+v...", cfg)
59+
p.SetConfig(cfg)
60+
61+
return 0, nil
62+
}
63+
64+
func (p *plugin) onClose() {
65+
log.Infof("Connection to the runtime lost, exiting...")
66+
os.Exit(0)
67+
}
68+
69+
func main() {
70+
var (
71+
pluginName string
72+
pluginIdx string
73+
verbose bool
74+
err error
75+
)
76+
77+
log = logrus.StandardLogger()
78+
log.SetFormatter(&logrus.TextFormatter{
79+
PadLevelText: true,
80+
})
81+
82+
flag.StringVar(&pluginName, "name", "", "plugin name to register to NRI")
83+
flag.StringVar(&pluginIdx, "idx", "", "plugin index to register to NRI")
84+
flag.BoolVar(&verbose, "verbose", false, "use verbose (debug) logging")
85+
flag.Parse()
86+
87+
if verbose {
88+
log.SetLevel(logrus.DebugLevel)
89+
}
90+
91+
p := &plugin{
92+
DefaultValidator: validator.NewDefaultValidator(
93+
&validator.DefaultValidatorConfig{
94+
Enable: true,
95+
},
96+
),
97+
}
98+
99+
opts := []stub.Option{
100+
stub.WithOnClose(p.onClose),
101+
}
102+
if pluginName != "" {
103+
opts = append(opts, stub.WithPluginName(pluginName))
104+
}
105+
if pluginIdx != "" {
106+
opts = append(opts, stub.WithPluginIdx(pluginIdx))
107+
}
108+
109+
if p.stub, err = stub.New(p, opts...); err != nil {
110+
log.Fatalf("failed to create plugin stub: %v", err)
111+
}
112+
113+
if err = p.stub.Run(context.Background()); err != nil {
114+
log.Errorf("plugin exited (%v)", err)
115+
os.Exit(1)
116+
}
117+
}

0 commit comments

Comments
 (0)