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 df74182
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 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

0 comments on commit df74182

Please sign in to comment.