mirror of
https://github.com/k8snetworkplumbingwg/multus-cni.git
synced 2025-07-31 23:48:33 +00:00
Remove UnmarshalJSON() for NetworkSelectionElement (#362)
To support non-string variable in NetworkSelectionElement, remove UnmarshalJSON(). interfaceRequest json is still supported in the changes.
This commit is contained in:
parent
b2e1098ab5
commit
05df28a58c
@ -232,6 +232,10 @@ func parsePodNetworkAnnotation(podNetworks, defaultNamespace string) ([]*types.N
|
||||
if net.Namespace == "" {
|
||||
net.Namespace = defaultNamespace
|
||||
}
|
||||
// compatibility pre v3.2, will be removed in v4.0
|
||||
if net.ObsolatedInterfaceRequest != "" && net.InterfaceRequest == "" {
|
||||
net.InterfaceRequest = net.ObsolatedInterfaceRequest
|
||||
}
|
||||
}
|
||||
|
||||
return networks, nil
|
||||
|
@ -330,42 +330,3 @@ func CheckSystemNamespaces(namespace string, systemNamespaces []string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// UnmarshalJSON unmarshal Network Selection Annotation. This also supports
|
||||
// the deprecated "interfaceRequest" property.
|
||||
func (net *NetworkSelectionElement) UnmarshalJSON(bytes []byte) error {
|
||||
var networkSelectionMap map[string]string
|
||||
|
||||
if err := json.Unmarshal(bytes, &networkSelectionMap); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if name, ok := networkSelectionMap["name"]; ok {
|
||||
net.Name = name
|
||||
} else {
|
||||
return logging.Errorf(`UnmarshalJSON(): "name" was not provided`)
|
||||
}
|
||||
|
||||
if namespace, ok := networkSelectionMap["namespace"]; ok {
|
||||
net.Namespace = namespace
|
||||
}
|
||||
|
||||
if ips, ok := networkSelectionMap["ips"]; ok {
|
||||
net.IPRequest = ips
|
||||
}
|
||||
|
||||
if mac, ok := networkSelectionMap["mac"]; ok {
|
||||
net.MacRequest = mac
|
||||
}
|
||||
|
||||
// compatibility pre v3.2
|
||||
if ifName, ok := networkSelectionMap["interfaceRequest"]; ok {
|
||||
net.InterfaceRequest = ifName
|
||||
}
|
||||
|
||||
if ifName, ok := networkSelectionMap["interface"]; ok {
|
||||
net.InterfaceRequest = ifName
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -467,49 +467,4 @@ var _ = Describe("config operations", func() {
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
|
||||
Context("using UnmarshalJSON", func() {
|
||||
It("succeeds with valid json", func() {
|
||||
networkselectionelement := &NetworkSelectionElement{
|
||||
Name: "kube-system",
|
||||
Namespace: "net1",
|
||||
InterfaceRequest: "",
|
||||
}
|
||||
conf := `{
|
||||
"name": "kube-system",
|
||||
"namespace": "net1",
|
||||
"interfaceRequest": "",
|
||||
"ips": "10.18.89.129",
|
||||
"mac": "CB-32-97-FF-D6-79",
|
||||
"interface": ""
|
||||
}`
|
||||
err := networkselectionelement.UnmarshalJSON([]byte(conf))
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
||||
|
||||
It("fails to parse invalid json", func() {
|
||||
networkselectionelement := &NetworkSelectionElement{
|
||||
Name: "kube-system",
|
||||
Namespace: "net1",
|
||||
InterfaceRequest: "",
|
||||
}
|
||||
err := networkselectionelement.UnmarshalJSON([]byte("invalidjson~"))
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("fails with missing name parameter", func() {
|
||||
networkselectionelement := &NetworkSelectionElement{
|
||||
Name: "kube-system",
|
||||
Namespace: "net1",
|
||||
InterfaceRequest: "",
|
||||
}
|
||||
// conf json does not include "name"
|
||||
conf := `{
|
||||
"namespace": "net1",
|
||||
"interfaceRequest": ""
|
||||
}`
|
||||
err := networkselectionelement.UnmarshalJSON([]byte(conf))
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -140,6 +140,9 @@ type NetworkSelectionElement struct {
|
||||
// InterfaceRequest contains an optional requested name for the
|
||||
// network interface this attachment will create in the container
|
||||
InterfaceRequest string `json:"interface,omitempty"`
|
||||
// ObsoateInterfaceRequest is obsolated parameter at pre 3.2.
|
||||
// This will be removed in 4.0 release.
|
||||
ObsolatedInterfaceRequest string `json:"interfaceRequest,omitempty"`
|
||||
}
|
||||
|
||||
// K8sArgs is the valid CNI_ARGS used for Kubernetes
|
||||
|
Loading…
Reference in New Issue
Block a user