|
| 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