Skip to content

Commit

Permalink
controller: update status via the net-attach-def-client lib
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Duarte Barroso <[email protected]>
  • Loading branch information
maiqueb committed Nov 7, 2022
1 parent 5a51952 commit 57a60f4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
14 changes: 5 additions & 9 deletions pkg/annotations/dynamic-network-status.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
multusapi "gopkg.in/k8snetworkplumbingwg/multus-cni.v3/pkg/server/api"
)

func AddDynamicIfaceToStatus(currentPod *corev1.Pod, networkSelectionElement *nettypes.NetworkSelectionElement, response *multusapi.Response) (string, error) {
func AddDynamicIfaceToStatus(currentPod *corev1.Pod, networkSelectionElement *nettypes.NetworkSelectionElement, response *multusapi.Response) ([]nettypes.NetworkStatus, error) {
currentIfaceStatus, err := podDynamicNetworkStatus(currentPod)
if err != nil {
return "", err
return nil, err
}

if response != nil && response.Result != nil {
Expand All @@ -25,16 +25,12 @@ func AddDynamicIfaceToStatus(currentPod *corev1.Pod, networkSelectionElement *ne
nil,
)
if err != nil {
return "", fmt.Errorf("failed to create NetworkStatus from the response: %v", err)
return nil, fmt.Errorf("failed to create NetworkStatus from the response: %v", err)
}

newIfaceString, err := json.Marshal(append(currentIfaceStatus, *newIfaceStatus))
if err != nil {
return "", fmt.Errorf("failed to marshall the dynamic networks status after interface creation")
}
return string(newIfaceString), nil
return append(currentIfaceStatus, *newIfaceStatus), nil
}
return "", fmt.Errorf("got an empty response from multus: %+v", response)
return nil, fmt.Errorf("got an empty response from multus: %+v", response)
}

func DeleteDynamicIfaceFromStatus(currentPod *corev1.Pod, networkSelectionElement *nettypes.NetworkSelectionElement) (string, error) {
Expand Down
60 changes: 52 additions & 8 deletions pkg/annotations/dynamic-network-status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ import (
var _ = Describe("NetworkStatusFromResponse", func() {
const (
ifaceName = "ens32"
ifaceToAdd = "newiface"
macAddr = "02:03:04:05:06:07"
namespace = "ns1"
networkName = "tenantnetwork"
podName = "tpod"
)

DescribeTable("add dynamic interface to network status", func(initialNetStatus []nadv1.NetworkStatus, resultIPs []string, expectedNetworkStatus string) {
const (
ifaceToAdd = "newiface"
macAddr = "02:03:04:05:06:07"
)
DescribeTable("add dynamic interface to network status", func(initialNetStatus []nadv1.NetworkStatus, resultIPs []string, expectedNetworkStatus []nadv1.NetworkStatus) {
Expect(
AddDynamicIfaceToStatus(
newPod(podName, namespace, initialNetStatus...),
Expand All @@ -37,23 +35,69 @@ var _ = Describe("NetworkStatusFromResponse", func() {
),
).To(Equal(expectedNetworkStatus))
},
Entry("initial empty pod", []nadv1.NetworkStatus{}, nil, `[{"name":"ns1/tenantnetwork","interface":"newiface","mac":"02:03:04:05:06:07","dns":{}}]`),
Entry("initial empty pod", []nadv1.NetworkStatus{}, nil, []nadv1.NetworkStatus{
{
Name: NamespacedName(namespace, networkName),
Interface: ifaceToAdd,
Mac: macAddr,
DNS: nadv1.DNS{
Nameservers: []string{},
Domain: "",
Search: []string{},
Options: []string{},
},
}}),
Entry("pod with a network present in the network status", []nadv1.NetworkStatus{
{
Name: "net1",
Interface: "iface1",
Mac: "00:00:00:20:10:00",
}},
nil,
`[{"name":"net1","interface":"iface1","mac":"00:00:00:20:10:00","dns":{}},{"name":"ns1/tenantnetwork","interface":"newiface","mac":"02:03:04:05:06:07","dns":{}}]`),
[]nadv1.NetworkStatus{
{
Name: "net1",
Interface: "iface1",
Mac: "00:00:00:20:10:00",
},
{
Name: NamespacedName(namespace, networkName),
Interface: ifaceToAdd,
Mac: macAddr,
DNS: nadv1.DNS{
Nameservers: []string{},
Domain: "",
Search: []string{},
Options: []string{},
},
}},
),
Entry("result with IPs", []nadv1.NetworkStatus{
{
Name: "net1",
Interface: "iface1",
Mac: "00:00:00:20:10:00",
}},
[]string{"10.10.10.10/24"},
`[{"name":"net1","interface":"iface1","mac":"00:00:00:20:10:00","dns":{}},{"name":"ns1/tenantnetwork","interface":"newiface","ips":["10.10.10.10"],"mac":"02:03:04:05:06:07","dns":{}}]`))
[]nadv1.NetworkStatus{
{
Name: "net1",
Interface: "iface1",
Mac: "00:00:00:20:10:00",
},
{
Name: NamespacedName(namespace, networkName),
Interface: ifaceToAdd,
Mac: macAddr,
IPs: []string{"10.10.10.10"},
DNS: nadv1.DNS{
Nameservers: []string{},
Domain: "",
Search: []string{},
Options: []string{},
},
}},
))

DescribeTable("remove an interface to the current network status", func(initialNetStatus []nadv1.NetworkStatus, networkName, ifaceToRemove, expectedNetworkStatus string) {
Expect(
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func (pnc *PodNetworksController) addNetworks(dynamicAttachmentRequest *DynamicA
return fmt.Errorf("failed to compute the updated network status: %v", err)
}

if err := pnc.updatePodNetworkStatus(pod, newIfaceStatus); err != nil {
if err := nadutils.SetNetworkStatus(pnc.k8sClientSet, pod, newIfaceStatus); err != nil {
return err
}

Expand Down

0 comments on commit 57a60f4

Please sign in to comment.