Skip to content
Merged
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
16 changes: 16 additions & 0 deletions crd/nodenetworkconfig/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nodenetworkconfig

import (
"context"
"fmt"
"reflect"

"github.com/Azure/azure-container-networking/crd"
Expand All @@ -16,6 +17,7 @@ import (
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrlcli "sigs.k8s.io/controller-runtime/pkg/client"
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

// Scheme is a runtime scheme containing the client-go scheme and the NodeNetworkConfig scheme.
Expand Down Expand Up @@ -136,3 +138,17 @@ func (c *Client) UpdateSpec(ctx context.Context, key types.NamespacedName, spec
}
return nnc, nil
}

// SetOwnerRef sets the owner of the NodeNetworkConfig to the given object, using HTTP Patch
func (c *Client) SetOwnerRef(ctx context.Context, nnc *v1alpha.NodeNetworkConfig, owner metav1.Object) (*v1alpha.NodeNetworkConfig, error) {
newNNC := nnc.DeepCopy()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should either return the newNNC or mutate the passed nnc, since the patch call will write the resultant object to the ref it gets passed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, I'll change that

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decided to return instead of mutate since the nnccli.Patch() could fail and we don't want to mutate the caller if we dn't actually make a successful change

if err := ctrlutil.SetControllerReference(owner, newNNC, Scheme); err != nil {
return nil, fmt.Errorf("could not set controller reference for NNC %s/%s: %w", nnc.Namespace, nnc.Name, err)
}

if err := c.nnccli.Patch(ctx, newNNC, ctrlcli.MergeFrom(nnc)); err != nil {
return nil, fmt.Errorf("could not patch NNC %s/%s: %w", nnc.Namespace, nnc.Name, err)
}

return newNNC, nil
}