-
Notifications
You must be signed in to change notification settings - Fork 8
/
config.go
74 lines (64 loc) · 2.64 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package plugin
import (
"encoding/json"
"fmt"
"github.com/containernetworking/cni/pkg/types"
cniv1 "github.com/containernetworking/cni/pkg/types/100"
"github.com/containernetworking/cni/pkg/version"
)
// Kubernetes a K8s specific struct to hold config
type Kubernetes struct {
K8sAPIRoot string `json:"k8s_api_root"`
Kubeconfig string `json:"kubeconfig"`
InterceptRuleMgrType string `json:"intercept_type"`
NodeName string `json:"node_name"`
ExcludeNamespaces []string `json:"exclude_namespaces"`
CNIBinDir string `json:"cni_bin_dir"`
}
// Config is whatever you expect your configuration json to be. This is whatever
// is passed in on stdin. Your plugin may wish to expose its functionality via
// runtime args, see CONVENTIONS.md in the CNI spec.
type Config struct {
types.NetConf // You may wish to not nest this type
RuntimeConfig *struct { // SampleConfig map[string]interface{} `json:"sample"`
} `json:"runtimeConfig"`
// This is the previous result, when called in the context of a chained
// plugin. Because this plugin supports multiple versions, we'll have to
// parse this in two passes. If your plugin is not chained, this can be
// removed (though you may wish to error if a non-chainable plugin is
// chained.
// If you need to modify the result before returning it, you will need
// to actually convert it to a concrete versioned struct.
RawPrevResult *map[string]any `json:"prevResult"`
PrevResult *cniv1.Result `json:"-"`
// Add plugin-specific flags here
LogLevel string `json:"log_level"`
LogUDSAddress string `json:"log_uds_address"`
Kubernetes Kubernetes `json:"kubernetes"`
HostNSEnterExec bool `json:"hostNSEnterExec"`
}
// parseConfig parses the supplied configuration (and prevResult) from stdin.
func parseConfig(stdin []byte) (*Config, error) {
conf := Config{}
if err := json.Unmarshal(stdin, &conf); err != nil {
return nil, fmt.Errorf("failed to parse network configuration: %v", err)
}
// Parse previous result. Remove this if your plugin is not chained.
if conf.RawPrevResult != nil {
resultBytes, err := json.Marshal(conf.RawPrevResult)
if err != nil {
return nil, fmt.Errorf("could not serialize prevResult: %v", err)
}
res, err := version.NewResult(conf.CNIVersion, resultBytes)
if err != nil {
return nil, fmt.Errorf("could not parse prevResult: %v", err)
}
conf.RawPrevResult = nil
conf.PrevResult, err = cniv1.NewResultFromResult(res)
if err != nil {
return nil, fmt.Errorf("could not convert result to current version: %v", err)
}
}
// End previous result parsing
return &conf, nil
}