Skip to content

Commit

Permalink
Fix NAD handling on create/update
Browse files Browse the repository at this point in the history
Currently only NAD creatin was handled for provided
nicMappings, this patch allows update to the nads.

Also Added missing ownership to the managed net-attach-defs
so they get's controlled with OVNController reconciler if
deleted/updated explicitly.

Closes-Issue: OSPRH-6894
  • Loading branch information
karelyatin committed Jun 12, 2024
1 parent 62d4b4e commit 3a00657
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion controllers/ovncontroller_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func (r *OVNControllerReconciler) reconcileNormal(ctx context.Context, instance
}

// Create additional Physical Network Attachments
networkAttachments, err := ovncontroller.CreateAdditionalNetworks(ctx, helper, instance, ovsServiceLabels)
networkAttachments, err := ovncontroller.CreateOrUpdateAdditionalNetworks(ctx, helper, instance, ovsServiceLabels)
if err != nil {
Log.Info(fmt.Sprintf("Failed to create additional networks: %s", err))
return ctrl.Result{}, err
Expand Down
38 changes: 28 additions & 10 deletions pkg/ovncontroller/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
ovnv1 "github.com/openstack-k8s-operators/ovn-operator/api/v1beta1"
)

// CreateAdditionalNetworks - creates network attachement definitions based on the provided mappings
func CreateAdditionalNetworks(
// CreateOrUpdateAdditionalNetworks - creates network attachement definitions based on the provided mappings
func CreateOrUpdateAdditionalNetworks(
ctx context.Context,
h *helper.Helper,
instance *ovnv1.OVNController,
Expand All @@ -37,6 +37,11 @@ func CreateAdditionalNetworks(
var networkAttachments []string

for physNet, interfaceName := range instance.Spec.NicMappings {
nadSpec := netattdefv1.NetworkAttachmentDefinitionSpec{
Config: fmt.Sprintf(
`{"cniVersion": "0.3.1", "name": "%s", "type": "host-device", "device": "%s"}`,
physNet, interfaceName),
}
nad = &netattdefv1.NetworkAttachmentDefinition{}
err := h.GetClient().Get(
ctx,
Expand All @@ -52,23 +57,36 @@ func CreateAdditionalNetworks(
physNet, interfaceName, err)
}

ownerRef := metav1.NewControllerRef(instance, instance.GroupVersionKind())
nad = &netattdefv1.NetworkAttachmentDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: physNet,
Namespace: instance.Namespace,
Labels: labels,
},
Spec: netattdefv1.NetworkAttachmentDefinitionSpec{
Config: fmt.Sprintf(
`{"cniVersion": "0.3.1", "name": "%s", "type": "host-device", "device": "%s"}`,
physNet, interfaceName),
Name: physNet,
Namespace: instance.Namespace,
Labels: labels,
OwnerReferences: []metav1.OwnerReference{*ownerRef},
},
Spec: nadSpec,
}
// Request object not found, lets create it
if err := h.GetClient().Create(ctx, nad); err != nil {
return nil, fmt.Errorf("can not create NetworkAttachmentDefinition %s/%s: %w",
physNet, interfaceName, err)
}
} else {
owned := false
for _, owner := range nad.GetOwnerReferences() {
if owner.Name == instance.Name {
owned = true
break
}
}
if owned {
nad.Spec = nadSpec
if err := h.GetClient().Update(ctx, nad); err != nil {
return nil, fmt.Errorf("can not update NetworkAttachmentDefinition %s/%s: %w",
physNet, interfaceName, err)
}
}
}

networkAttachments = append(networkAttachments, physNet)
Expand Down

0 comments on commit 3a00657

Please sign in to comment.