Skip to content

Commit

Permalink
Reduce number of patch requests
Browse files Browse the repository at this point in the history
  • Loading branch information
PBundyra committed Oct 28, 2024
1 parent 70baab2 commit 0f86c45
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
4 changes: 1 addition & 3 deletions pkg/controller/core/workload_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,10 @@ func (r *WorkloadReconciler) reconcileCheckBasedEviction(ctx context.Context, wl
// at this point we know a Workload has at least one Retry AdmissionCheck
message := "At least one admission check is false"
workload.SetEvictedCondition(wl, kueue.WorkloadEvictedByAdmissionCheck, message)
workload.SetAllChecksToPending(wl, "AdmissionCheck pending after retry")
if err := workload.ApplyAdmissionStatus(ctx, r.client, wl, true); err != nil {
return false, client.IgnoreNotFound(err)
}
if err := workload.SetAllChecksToPending(ctx, r.client, wl); err != nil {
return false, client.IgnoreNotFound(err)
}
cqName, _ := r.queues.ClusterQueueForWorkload(wl)
workload.ReportEvictedWorkload(r.recorder, wl, cqName, kueue.WorkloadEvictedByAdmissionCheck, message)
return true, nil
Expand Down
14 changes: 4 additions & 10 deletions pkg/workload/admissionchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ limitations under the License.
package workload

import (
"context"
"time"

apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/controller-runtime/pkg/client"

kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
)
Expand Down Expand Up @@ -89,20 +87,16 @@ func FindAdmissionCheck(checks []kueue.AdmissionCheckState, checkName string) *k
}

// SetAllChecksToPending sets all AdmissionChecks to Pending
func SetAllChecksToPending(ctx context.Context, c client.Client, w *kueue.Workload) error {
wlCopy := BaseSSAWorkload(w)
message := "AdmissionCheck pending after retry"
checks := make([]kueue.AdmissionCheckState, len(w.Status.AdmissionChecks))
for i := range w.Status.AdmissionChecks {
func SetAllChecksToPending(w *kueue.Workload, message string) {
checks := w.Status.AdmissionChecks
for i := range checks {
checks[i] = kueue.AdmissionCheckState{
Name: w.Status.AdmissionChecks[i].Name,
Name: checks[i].Name,
State: kueue.CheckStatePending,
LastTransitionTime: metav1.NewTime(time.Now()),
Message: message,
}
}
wlCopy.Status.AdmissionChecks = checks
return ApplyStatusPatch(ctx, c, wlCopy)
}

// SetAdmissionCheckState - adds or updates newCheck in the provided checks list.
Expand Down
24 changes: 14 additions & 10 deletions pkg/workload/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,7 @@ func PropagateResourceRequests(w *kueue.Workload, info *Info) bool {
// AdmissionStatusPatch creates a new object based on the input workload that contains
// the admission and related conditions. The object can be used in Server-Side-Apply.
// If strict is true, resourceVersion will be part of the patch.
func AdmissionStatusPatch(w *kueue.Workload, strict bool) *kueue.Workload {
wlCopy := BaseSSAWorkload(w)
func AdmissionStatusPatch(w *kueue.Workload, wlCopy *kueue.Workload, strict bool) {
wlCopy.Status.Admission = w.Status.Admission.DeepCopy()
wlCopy.Status.RequeueState = w.Status.RequeueState.DeepCopy()
if wlCopy.Status.Admission != nil {
Expand All @@ -634,27 +633,32 @@ func AdmissionStatusPatch(w *kueue.Workload, strict bool) *kueue.Workload {
wlCopy.ResourceVersion = w.ResourceVersion
}
wlCopy.Status.AccumulatedPastExexcutionTimeSeconds = w.Status.AccumulatedPastExexcutionTimeSeconds
return wlCopy
}

func AdmissionChecksStatusPatch(w *kueue.Workload, wlCopy *kueue.Workload) {
if wlCopy.Status.AdmissionChecks == nil && w.Status.AdmissionChecks != nil {
wlCopy.Status.AdmissionChecks = make([]kueue.AdmissionCheckState, 0)
}
for _, ac := range w.Status.AdmissionChecks {
SetAdmissionCheckState(&wlCopy.Status.AdmissionChecks, ac)
}
}

// ApplyAdmissionStatus updated all the admission related status fields of a workload with SSA.
// If strict is true, resourceVersion will be part of the patch, make this call fail if Workload
// was changed.
func ApplyAdmissionStatus(ctx context.Context, c client.Client, w *kueue.Workload, strict bool) error {
patch := AdmissionStatusPatch(w, strict)
return ApplyAdmissionStatusPatch(ctx, c, patch)
wlCopy := BaseSSAWorkload(w)
AdmissionStatusPatch(w, wlCopy, strict)
AdmissionChecksStatusPatch(w, wlCopy)
return ApplyAdmissionStatusPatch(ctx, c, wlCopy)
}

// ApplyAdmissionStatusPatch applies the patch of admission related status fields of a workload with SSA.
func ApplyAdmissionStatusPatch(ctx context.Context, c client.Client, patch *kueue.Workload) error {
return c.Status().Patch(ctx, patch, client.Apply, client.FieldOwner(constants.AdmissionName))
}

// ApplyStatusPatch applies the patch of status fields of a workload with SSA.
func ApplyStatusPatch(ctx context.Context, c client.Client, patch *kueue.Workload) error {
return c.Status().Patch(ctx, patch, client.Apply, client.FieldOwner(constants.WorkloadControllerName))
}

type Ordering struct {
PodsReadyRequeuingTimestamp config.RequeuingTimestamp
}
Expand Down

0 comments on commit 0f86c45

Please sign in to comment.