Skip to content
Permalink
Browse files

Remove UnmarshalJSON() for NetworkSelectionElement (#362)

To support non-string variable in NetworkSelectionElement, remove
UnmarshalJSON(). interfaceRequest json is still supported in the
changes.
  • Loading branch information...
s1061123 authored and dougbtv committed Aug 22, 2019
1 parent b2e1098 commit 05df28a58c96fcc9486a9b0b741856c4d6bf7aab
Showing with 7 additions and 84 deletions.
  1. +4 −0 k8sclient/k8sclient.go
  2. +0 −39 types/conf.go
  3. +0 −45 types/conf_test.go
  4. +3 −0 types/types.go
@@ -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

0 comments on commit 05df28a

Please sign in to comment.
You can’t perform that action at this time.