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

Set controller reference for both create and update deployment operation in UPF controller. #42

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 8 additions & 4 deletions controllers/upf/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ func (r *UPFDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}

if deployment, err := createDeployment(log, configMapVersion, upfDeployment); err == nil {
// Set the controller reference, specifying that UPFDeployment controls the underlying Deployment
if err := ctrl.SetControllerReference(upfDeployment, deployment, r.Scheme); err != nil {
log.Error(err, "Got error while setting Owner reference on Deployment", "Deployment.namespace", deployment.Name, "Deployment.name", deployment.Name)
}
if !deploymentFound {
// Only create Deployment in case all required NADs are present. Otherwise Requeue in 10 sec.
if ok := controllers.ValidateNetworkAttachmentDefinitions(ctx, r.Client, log, upfDeployment.Kind, deployment); ok {
// Set the controller reference, specifying that UPFDeployment controls the underlying Deployment
if err := ctrl.SetControllerReference(upfDeployment, deployment, r.Scheme); err != nil {
log.Error(err, "Got error while setting Owner reference on Deployment", "Deployment.namespace", deployment.Name, "Deployment.name", deployment.Name)
}

log.Info("Creating Deployment", "Deployment.namespace", deployment.Namespace, "Deployment.name", deployment.Name)
if err := r.Client.Create(ctx, deployment); err != nil {
Expand All @@ -164,6 +164,10 @@ func (r *UPFDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Reques
log.Info("Not all NetworkAttachDefinitions available in current namespace, requeuing")
return reconcile.Result{RequeueAfter: time.Duration(10) * time.Second}, nil
}
} else {
if err = r.Client.Update(ctx, deployment); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Does this actually work? Because since deployment came out of createDeployment, I don't think it will have resourceVersion set, so API server will reject the Update, won't it?

Copy link
Member

Choose a reason for hiding this comment

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

That is, before this call, deployment is either the current deployment, or an empty deployment. After this call, deployment is the new object created in createDeployment. Instead, you should set the namespace and name when you create the "empty" deployment, and pass that pointer into createDeployment, which should just set the Spec in the passed in object.

Copy link
Member

Choose a reason for hiding this comment

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

To be clear, we shouldn't be touching the metadata of an existing deployment, so we need to reuse that deployment object in the Update. Otherwise api server will reject the Update - this is how optimistic concurrency happens.

Copy link
Member Author

Choose a reason for hiding this comment

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

Then I can use the currentDeployment object in the update

currentDeployment := new(appsv1.Deployment)
if err := r.Client.Get(ctx, types.NamespacedName{Name: deploymentName, Namespace: namespace}, currentDeployment); err == nil {
		deploymentFound = true
}
...
     if !deploymentFound {
...
     } else {
         if err = r.Client.Update(ctx, currentDeployment); err != nil {
....

Before the update call, I check if Spec.Capacity.MaxDownlinkTroughput changed. If true then call createResourceRequirements and update currentDeployment.spec.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@denysaleksandrov

Last night when I tried it (as you know, I found that the deletion of UPFDeployment does not remove the corresponding Deployment while doing Client.Update), what I found was that the reconciliation loop went crazy (keeps on reconciling)... I will need to do a test on your fix to ensure that doesn't happen

log.Error(err, "Failed to update Deployment", "Deployment.namespace", deployment.Namespace, "Deployment.name", deployment.Name)
}
}
} else {
log.Error(err, "Failed to create Deployment")
Expand Down