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

UDN, IPAM: Use v1.multus-cni.io/default-network #69

Closed
wants to merge 1 commit into from
Closed
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
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 @@ -32,6 +32,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 @@ -137,6 +139,7 @@ var _ = Describe("KubeVirt IPAM launcher pod mutato machine", Serial, func() {
inputNADs: []*nadv1.NetworkAttachmentDefinition{
dummyNAD(nadName),
dummyPrimaryNetworkNAD(nadName),
dummyDefaultNetworkNAD(),
},
inputPod: dummyPodForVM(nadName, vmName),
expectedAdmissionResponse: admissionv1.AdmissionResponse{
Expand All @@ -146,8 +149,8 @@ var _ = Describe("KubeVirt IPAM launcher pod mutato machine", Serial, func() {
expectedAdmissionPatches: ConsistOf([]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 @@ -162,6 +165,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: admissionv1.AdmissionResponse{
Expand All @@ -171,8 +175,8 @@ var _ = Describe("KubeVirt IPAM launcher pod mutato machine", Serial, func() {
expectedAdmissionPatches: Equal([]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 @@ -334,6 +338,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
Loading