k8sclient: use Go struct to parse annotations and add unit tests

This commit is contained in:
Dan Williams
2018-06-19 21:27:42 -05:00
committed by Kuralamudhan Ramakrishnan
parent 591a687b42
commit 0b1e8689dc
4 changed files with 321 additions and 132 deletions

View File

@@ -28,17 +28,12 @@ const (
defaultConfDir = "/etc/cni/multus/net.d"
)
// Convert a raw delegate config map into a DelegateNetConf structure
func loadDelegateNetConf(rawConf map[string]interface{}) (*DelegateNetConf, error) {
bytes, err := json.Marshal(rawConf)
if err != nil {
return nil, fmt.Errorf("error marshalling delegate config: %v", err)
}
// Convert raw CNI JSON into a DelegateNetConf structure
func LoadDelegateNetConf(bytes []byte) (*DelegateNetConf, error) {
delegateConf := &DelegateNetConf{}
if err = json.Unmarshal(bytes, delegateConf); err != nil {
if err := json.Unmarshal(bytes, delegateConf); err != nil {
return nil, fmt.Errorf("error unmarshalling delegate config: %v", err)
}
delegateConf.RawConfig = rawConf
delegateConf.Bytes = bytes
// Do some minimal validation
@@ -90,7 +85,11 @@ func LoadNetConf(bytes []byte) (*NetConf, error) {
}
for idx, rawConf := range netconf.RawDelegates {
delegateConf, err := loadDelegateNetConf(rawConf)
bytes, err := json.Marshal(rawConf)
if err != nil {
return nil, fmt.Errorf("error marshalling delegate %d config: %v", idx, err)
}
delegateConf, err := LoadDelegateNetConf(bytes)
if err != nil {
return nil, fmt.Errorf("failed to load delegate %d config: %v", idx, err)
}
@@ -104,21 +103,6 @@ func LoadNetConf(bytes []byte) (*NetConf, error) {
return netconf, nil
}
func (d *DelegateNetConf) updateRawConfig() error {
if d.IfnameRequest != "" {
d.RawConfig["ifnameRequest"] = d.IfnameRequest
} else {
delete(d.RawConfig, "ifnameRequest")
}
bytes, err := json.Marshal(d.RawConfig)
if err != nil {
return err
}
d.Bytes = bytes
return nil
}
// AddDelegates appends the new delegates to the delegates list
func (n *NetConf) AddDelegates(newDelegates []*DelegateNetConf) error {
n.Delegates = append(n.Delegates, newDelegates...)

View File

@@ -46,9 +46,7 @@ type DelegateNetConf struct {
// MasterPlugin is only used internal housekeeping
MasterPlugin bool `json:"-"`
// Raw unmarshalled JSON
RawConfig map[string]interface{}
// Raw bytes
// Raw JSON
Bytes []byte
}
@@ -88,6 +86,26 @@ type NetworkSpec struct {
Plugin string `json:"plugin"`
}
// NetworkSelectionElement represents one element of the JSON format
// Network Attachment Selection Annotation as described in section 4.1.2
// of the CRD specification.
type NetworkSelectionElement struct {
// Name contains the name of the Network object this element selects
Name string `json:"name"`
// Namespace contains the optional namespace that the network referenced
// by Name exists in
Namespace string `json:"namespace,omitempty"`
// IPRequest contains an optional requested IP address for this network
// attachment
IPRequest string `json:"ipRequest,omitempty"`
// MacRequest contains an optional requested MAC address for this
// network attachment
MacRequest string `json:"macRequest,omitempty"`
// InterfaceRequest contains an optional requested name for the
// network interface this attachment will create in the container
InterfaceRequest string `json:"interfaceRequest,omitempty"`
}
// K8sArgs is the valid CNI_ARGS used for Kubernetes
type K8sArgs struct {
types.CommonArgs