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:
Tomofumi Hayashi 2019-08-23 00:19:06 +09:00 committed by Doug Smith
parent b2e1098ab5
commit 05df28a58c
4 changed files with 7 additions and 84 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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())
})
})
})

View File

@ -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