Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

controller: update status via the net-attach-def-client lib #46

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 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,22 +25,18 @@ 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) {
func DeleteDynamicIfaceFromStatus(currentPod *corev1.Pod, networkSelectionElement *nettypes.NetworkSelectionElement) ([]nettypes.NetworkStatus, error) {
currentIfaceStatus, err := podDynamicNetworkStatus(currentPod)
if err != nil {
return "", err
return nil, err
}

netName := NamespacedName(networkSelectionElement.Namespace, networkSelectionElement.Name)
Expand All @@ -52,12 +48,7 @@ func DeleteDynamicIfaceFromStatus(currentPod *corev1.Pod, networkSelectionElemen
}
newIfaceStatus = append(newIfaceStatus, currentIfaceStatus[i])
}

newIfaceString, err := json.Marshal(newIfaceStatus)
if err != nil {
return "", fmt.Errorf("failed to marshall the dynamic networks status after deleting interface")
}
return string(newIfaceString), nil
return newIfaceStatus, nil
}

func podDynamicNetworkStatus(currentPod *corev1.Pod) ([]nettypes.NetworkStatus, error) {
Expand Down
90 changes: 77 additions & 13 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,45 +35,101 @@ 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) {
DescribeTable("remove an interface to the current network status", func(initialNetStatus []nadv1.NetworkStatus, networkName string, ifaceToRemove string, expectedNetworkStatus []nadv1.NetworkStatus) {
Expect(
DeleteDynamicIfaceFromStatus(
newPod(podName, namespace, initialNetStatus...),
newNetworkSelectionElementWithIface(networkName, ifaceToRemove, namespace),
),
).To(Equal(expectedNetworkStatus))
},
Entry("when there aren't any existing interfaces", nil, "net1", "iface1", "[]"),
Entry("when there aren't any existing interfaces", nil, "net1", "iface1", []nadv1.NetworkStatus{}),
Entry("when we remove all the currently existing interfaces", []nadv1.NetworkStatus{
{
Name: NamespacedName(namespace, networkName),
Interface: "iface1",
Mac: "00:00:00:20:10:00",
}}, networkName, "iface1", "[]"),
}}, networkName, "iface1", []nadv1.NetworkStatus{}),
Entry("when there is *not* a matching interface to remove", []nadv1.NetworkStatus{
{
Name: NamespacedName(namespace, networkName),
Interface: "iface1",
Mac: "00:00:00:20:10:00",
}}, "net2", "iface1", `[{"name":"ns1/tenantnetwork","interface":"iface1","mac":"00:00:00:20:10:00","dns":{}}]`),
}},
"net2",
"iface1",
[]nadv1.NetworkStatus{
{
Name: NamespacedName(namespace, networkName),
Interface: "iface1",
Mac: "00:00:00:20:10:00",
},
},
),
Entry("when we remove one of the existing interfaces", []nadv1.NetworkStatus{
{
Name: NamespacedName(namespace, networkName),
Expand All @@ -87,7 +141,17 @@ var _ = Describe("NetworkStatusFromResponse", func() {
Interface: "iface2",
Mac: "aa:bb:cc:20:10:00",
},
}, "net2", "iface2", `[{"name":"ns1/tenantnetwork","interface":"iface1","mac":"00:00:00:20:10:00","dns":{}}]`))
},
"net2",
"iface2",
[]nadv1.NetworkStatus{
{
Name: NamespacedName(namespace, networkName),
Interface: "iface1",
Mac: "00:00:00:20:10:00",
},
},
))
})

func newPod(podName string, namespace string, netStatus ...nadv1.NetworkStatus) *corev1.Pod {
Expand Down
15 changes: 2 additions & 13 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package controller

import (
"context"
"encoding/json"
"fmt"
"reflect"
"strings"
"time"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
v1coreinformerfactory "k8s.io/client-go/informers"
Expand Down Expand Up @@ -283,7 +281,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 Expand Up @@ -334,7 +332,7 @@ func (pnc *PodNetworksController) removeNetworks(dynamicAttachmentRequest *Dynam
err,
)
}
if err := pnc.updatePodNetworkStatus(pod, newIfaceStatus); err != nil {
if err := nadutils.SetNetworkStatus(pnc.k8sClientSet, pod, newIfaceStatus); err != nil {
return err
}

Expand All @@ -344,15 +342,6 @@ func (pnc *PodNetworksController) removeNetworks(dynamicAttachmentRequest *Dynam
return nil
}

func (pnc *PodNetworksController) updatePodNetworkStatus(pod *corev1.Pod, newIfaceStatus string) error {
pod.Annotations[nadv1.NetworkStatusAnnot] = newIfaceStatus

if _, err := pnc.k8sClientSet.CoreV1().Pods(pod.GetNamespace()).Update(context.Background(), pod, metav1.UpdateOptions{}); err != nil {
return fmt.Errorf("failed to update pod's network-status annotations for %s: %v", pod.GetName(), err)
}
return nil
}

func networkSelectionElements(podAnnotations map[string]string, podNamespace string) ([]*nadv1.NetworkSelectionElement, error) {
podNetworks, ok := podAnnotations[nadv1.NetworkAttachmentAnnot]
if !ok || podNetworks == "" {
Expand Down