-
Notifications
You must be signed in to change notification settings - Fork 0
/
nad_types.go
84 lines (69 loc) · 1.75 KB
/
nad_types.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
75
76
77
78
79
80
81
82
83
84
package nad
import (
"encoding/json"
nadv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
)
const (
CniVersion = "0.3.1"
// cni types
WirePluginType = "wire"
TuningPluginType = "tuning"
)
type NadConfig struct {
CniVersion string `json:"cniVersion,omitempty"`
Plugins []map[string]any `json:"plugins,omitempty"`
}
// key = type; plugin should become a init fn
var Plugins = map[string]PluginConfigInterface{
"WirePlugin": WirePlugin{},
"TuningPlugin": TuningPlugin{},
}
type PluginConfigInterface interface{}
type WirePlugin struct {
PluginCniType
InterfaceName string `json:"interfaceName,omitempty"`
MTU int `json:"mtu,omitempty"`
}
type TuningPlugin struct {
PluginCniType
Capabilities
}
type Capabilities struct {
Ips bool `json:"ips,omitempty"`
Mac bool `json:"mac,omitempty"`
}
type PluginCniType struct {
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
}
func GetNadConfig(plugins []PluginConfigInterface) ([]byte, error) {
nadConfig := NadConfig{
CniVersion: CniVersion,
Plugins: []map[string]any{},
}
for _, plugin := range plugins {
b, err := json.Marshal(plugin)
if err != nil {
return nil, err
}
x := map[string]any{}
if err := json.Unmarshal(b, &x); err != nil {
return nil, err
}
nadConfig.Plugins = append(nadConfig.Plugins, x)
}
return json.Marshal(nadConfig)
}
func GetNadAnnotation(nads []*nadv1.NetworkAttachmentDefinition) ([]byte, error) {
a := []NadAnnotationEntry{}
for _, nad := range nads {
a = append(a, NadAnnotationEntry{
Name: nad.GetName(),
})
}
return json.Marshal(a)
}
type NadAnnotation []NadAnnotationEntry
type NadAnnotationEntry struct {
Name string `json:"name,omitempty"`
}