-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from hustcat/devel
Move 'VF/MAC/VLAN' from NetConf to NetArgs by following the CNI spec
- Loading branch information
Showing
7 changed files
with
247 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package config | ||
|
||
import ( | ||
"encoding" | ||
"fmt" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// UnmarshallableBool typedef for builtin bool | ||
// because builtin type's methods can't be declared | ||
type UnmarshallableBool bool | ||
|
||
// UnmarshalText implements the encoding.TextUnmarshaler interface. | ||
// Returns boolean true if the string is "1" or "[Tt]rue" | ||
// Returns boolean false if the string is "0" or "[Ff]alse" | ||
func (b *UnmarshallableBool) UnmarshalText(data []byte) error { | ||
s := strings.ToLower(string(data)) | ||
switch s { | ||
case "1", "true": | ||
*b = true | ||
case "0", "false": | ||
*b = false | ||
default: | ||
return fmt.Errorf("Boolean unmarshal error: invalid input %s", s) | ||
} | ||
return nil | ||
} | ||
|
||
// UnmarshallableString typedef for builtin string | ||
type UnmarshallableString string | ||
|
||
// UnmarshalText implements the encoding.TextUnmarshaler interface. | ||
// Returns the string | ||
func (s *UnmarshallableString) UnmarshalText(data []byte) error { | ||
*s = UnmarshallableString(data) | ||
return nil | ||
} | ||
|
||
// CommonArgs contains the IgnoreUnknown argument | ||
// and must be embedded by all Arg structs | ||
type CommonArgs struct { | ||
IgnoreUnknown UnmarshallableBool `json:"ignoreunknown,omitempty"` | ||
} | ||
|
||
// GetKeyField is a helper function to receive Values | ||
// Values that represent a pointer to a struct | ||
func GetKeyField(keyString string, v reflect.Value) reflect.Value { | ||
return v.Elem().FieldByName(keyString) | ||
} | ||
|
||
// LoadSriovArgs parses args from a string in the form "K=V;K2=V2;..." | ||
func LoadSriovArgs(args string, container interface{}) error { | ||
if args == "" { | ||
return nil | ||
} | ||
|
||
containerValue := reflect.ValueOf(container) | ||
|
||
pairs := strings.Split(args, ";") | ||
unknownArgs := []string{} | ||
for _, pair := range pairs { | ||
kv := strings.Split(pair, "=") | ||
if len(kv) != 2 { | ||
return fmt.Errorf("ARGS: invalid pair %q", pair) | ||
} | ||
keyString := kv[0] | ||
valueString := kv[1] | ||
keyField := GetKeyField(keyString, containerValue) | ||
if !keyField.IsValid() { | ||
unknownArgs = append(unknownArgs, pair) | ||
continue | ||
} | ||
|
||
switch keyField.Kind() { | ||
case reflect.Int: | ||
vi, err := strconv.Atoi(valueString) | ||
if err != nil { | ||
return fmt.Errorf("Convert value %s for field %s failed", valueString, keyString) | ||
} | ||
keyField.SetInt(int64(vi)) | ||
case reflect.String: | ||
keyField.SetString(valueString) | ||
case reflect.Slice: | ||
u := keyField.Addr().Interface().(encoding.TextUnmarshaler) | ||
err := u.UnmarshalText([]byte(valueString)) | ||
if err != nil { | ||
return fmt.Errorf("ARGS: error parsing value of pair %q: %v)", pair, err) | ||
} | ||
default: | ||
return fmt.Errorf("Unsupport type %s for %s", keyField.Kind().String(), keyString) | ||
} | ||
} | ||
|
||
isIgnoreUnknown := GetKeyField("IgnoreUnknown", containerValue).Bool() | ||
if len(unknownArgs) > 0 && !isIgnoreUnknown { | ||
return fmt.Errorf("ARGS: unknown args %q", unknownArgs) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestLoadArgs(t *testing.T) { | ||
s := "VF=1;VLAN=10;MAC=AA:BB:CC:DD:EE:FF;IP=192.168.1.2" | ||
args := &NetArgs{} | ||
err := LoadSriovArgs(s, args) | ||
if err != nil { | ||
t.Errorf("error %v", err) | ||
} | ||
if args.IP.String() != "192.168.1.2" { | ||
t.Errorf("failed to parse IP") | ||
} | ||
if args.VF != 1 { | ||
t.Errorf("failed to parse VF") | ||
} | ||
if args.MAC != "AA:BB:CC:DD:EE:FF" { | ||
t.Errorf("failed to parse MAC") | ||
} | ||
if args.VLAN != 10 { | ||
t.Errorf("failed to parse VLAN") | ||
} | ||
|
||
//fmt.Printf("%#v\n%s\n", args, args.IP.String()) | ||
} | ||
|
||
// go test -run LoadArgs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package config | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/containernetworking/cni/pkg/types" | ||
) | ||
|
||
type NetConf struct { | ||
types.NetConf | ||
Master string `json:"master"` | ||
} | ||
|
||
type NetArgs struct { | ||
types.CommonArgs | ||
VF int `json:"vf,omitempty"` | ||
VLAN int `json:"vlan,omitempty"` | ||
MAC string `json:"mac,omitempty"` | ||
IP net.IP `json:"ip,omitempty"` | ||
} | ||
|
||
type SriovConf struct { | ||
Net *NetConf | ||
Args *NetArgs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.