Skip to content

Commit

Permalink
UDN, IPAM: Use v1.multus-cni.io/default-network
Browse files Browse the repository at this point in the history
In order to specify ipam-claim-reference for the primary network,
use v1.multus-cni.io/default-network instead
k8s.ovn.org/primary-udn-ipamclaim.

Signed-off-by: Or Shoval <[email protected]>
  • Loading branch information
oshoval committed Sep 22, 2024
1 parent 49ee8d7 commit 23d4035
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
5 changes: 4 additions & 1 deletion pkg/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ const (
NetworkRolePrimary NetworkRole = "primary"
)

const OVNPrimaryNetworkIPAMClaimAnnotation = "k8s.ovn.org/primary-udn-ipamclaim"
const (
MultusDefaultNetwork = "v1.multus-cni.io/default-network"
DefaultNetworkName = "ovn-kubernetes"
)

type RelevantConfig struct {
Name string `json:"name"`
Expand Down
38 changes: 32 additions & 6 deletions pkg/ipamclaimswebhook/podmutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ func (a *IPAMClaimsValet) Handle(ctx context.Context, request admission.Request)
if newPod == nil {
newPod = pod.DeepCopy()
}
updatePodWithOVNPrimaryNetworkIPAMClaimAnnotation(newPod, newPrimaryNetworkIPAMClaimName)
if err = updatePodWithDefaultNetworkAnnotation(a.Client, newPod, newPrimaryNetworkIPAMClaimName); err != nil {
return admission.Errored(http.StatusInternalServerError,
fmt.Errorf("failed updating default network annotation: %v", err))
}
}

if newPod != nil {
Expand Down Expand Up @@ -159,8 +162,33 @@ func updatePodSelectionElements(pod *corev1.Pod, networks []*v1.NetworkSelection
return nil
}

func updatePodWithOVNPrimaryNetworkIPAMClaimAnnotation(pod *corev1.Pod, primaryNetworkIPAMClaimName string) {
pod.Annotations[config.OVNPrimaryNetworkIPAMClaimAnnotation] = primaryNetworkIPAMClaimName
func updatePodWithDefaultNetworkAnnotation(cli client.Client, pod *corev1.Pod, ipamClaimName string) error {
nadKey := types.NamespacedName{
Namespace: "default",
Name: config.DefaultNetworkName,
}

nad := v1.NetworkAttachmentDefinition{}
if err := cli.Get(context.Background(), nadKey, &nad); err != nil {
return err
}

networkAnnotation := []v1.NetworkSelectionElement{
{
Namespace: "default",
Name: config.DefaultNetworkName,
IPAMClaimReference: ipamClaimName,
},
}

annotationBytes, err := json.Marshal(networkAnnotation)
if err != nil {
return err
}

pod.Annotations[config.MultusDefaultNetwork] = string(annotationBytes)

return nil
}

func ensureIPAMClaimRefAtNetworkSelectionElements(ctx context.Context,
Expand Down Expand Up @@ -236,9 +264,7 @@ func ensureIPAMClaimRefAtNetworkSelectionElements(ctx context.Context,
func findNewPrimaryNetworkIPAMClaimName(ctx context.Context,
cli client.Client, pod *corev1.Pod, vmName string) (string, error) {
log := logf.FromContext(ctx)
if pod.Annotations[config.OVNPrimaryNetworkIPAMClaimAnnotation] != "" {
return "", nil
}

primaryNetworkNAD, err := udn.FindPrimaryNetwork(ctx, cli, pod.Namespace)
if err != nil {
return "", err
Expand Down
16 changes: 12 additions & 4 deletions pkg/ipamclaimswebhook/podmutator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (

ipamclaimsapi "github.com/k8snetworkplumbingwg/ipamclaims/pkg/crd/ipamclaims/v1alpha1"
nadv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"

"github.com/kubevirt/ipam-extensions/pkg/config"
)

type testConfig struct {
Expand Down Expand Up @@ -136,6 +138,7 @@ var _ = Describe("KubeVirt IPAM launcher pod mutato machine", Serial, func() {
inputNADs: []*nadv1.NetworkAttachmentDefinition{
dummyNAD(nadName),
dummyPrimaryNetworkNAD(nadName),
dummyDefaultNetworkNAD(),
},
inputPod: dummyPodForVM(nadName, vmName),
expectedAdmissionResponse: admission.Response{
Expand All @@ -146,8 +149,8 @@ var _ = Describe("KubeVirt IPAM launcher pod mutato machine", Serial, func() {
Patches: []jsonpatch.JsonPatchOperation{
{
Operation: "add",
Path: "/metadata/annotations/k8s.ovn.org~1primary-udn-ipamclaim",
Value: "vm1.podnet",
Path: "/metadata/annotations/v1.multus-cni.io~1default-network",
Value: "[{\"name\":\"ovn-kubernetes\",\"namespace\":\"default\",\"ipam-claim-reference\":\"vm1.podnet\"}]",
},
{
Operation: "replace",
Expand All @@ -163,6 +166,7 @@ var _ = Describe("KubeVirt IPAM launcher pod mutato machine", Serial, func() {
inputVMI: dummyVMI(nadName),
inputNADs: []*nadv1.NetworkAttachmentDefinition{
dummyPrimaryNetworkNAD(nadName),
dummyDefaultNetworkNAD(),
},
inputPod: dummyPodForVM("" /*without network selection element*/, vmName),
expectedAdmissionResponse: admission.Response{
Expand All @@ -173,8 +177,8 @@ var _ = Describe("KubeVirt IPAM launcher pod mutato machine", Serial, func() {
Patches: []jsonpatch.JsonPatchOperation{
{
Operation: "add",
Path: "/metadata/annotations/k8s.ovn.org~1primary-udn-ipamclaim",
Value: "vm1.podnet",
Path: "/metadata/annotations/v1.multus-cni.io~1default-network",
Value: "[{\"name\":\"ovn-kubernetes\",\"namespace\":\"default\",\"ipam-claim-reference\":\"vm1.podnet\"}]",
},
},
},
Expand Down Expand Up @@ -349,6 +353,10 @@ func dummyNADWithoutPersistentIPs(nadName string) *nadv1.NetworkAttachmentDefini
return dummyNADWithConfig(nadName, `{"name": "goodnet"}`)
}

func dummyDefaultNetworkNAD() *nadv1.NetworkAttachmentDefinition {
return dummyNADWithConfig("default/"+config.DefaultNetworkName, "")
}

func podAdmissionRequest(pod *corev1.Pod) admission.Request {
rawPod, err := json.Marshal(pod)
if err != nil {
Expand Down

0 comments on commit 23d4035

Please sign in to comment.