Skip to content

Commit

Permalink
Fix a bug on assigning AdmissionChecks for a Workload
Browse files Browse the repository at this point in the history
  • Loading branch information
PBundyra committed Apr 16, 2024
1 parent fe0d4f0 commit ccc1b3a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/cache/clusterqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ func (c *ClusterQueue) updateLabelKeys(flavors map[kueue.ResourceFlavorReference
// updateWithAdmissionChecks updates a ClusterQueue based on the passed AdmissionChecks set.
func (c *ClusterQueue) updateWithAdmissionChecks(checks map[string]AdmissionCheck) {
hasMissing := false
singleInstanceControllers := sets.New[string]()
checksPerController := make(map[string]int, len(c.AdmissionChecks))
singleInstanceControllers := sets.New[string]()
for acName, _ := range c.AdmissionChecks {
if ac, found := checks[acName]; !found {
hasMissing = true
Expand Down
4 changes: 1 addition & 3 deletions pkg/cache/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,8 @@ func (c *ClusterQueue) snapshot() *ClusterQueue {
Preemption: c.Preemption,
NamespaceSelector: c.NamespaceSelector,
Status: c.Status,
AdmissionChecks: utilmaps.DeepCopySets[string](c.AdmissionChecks),
}
admissionChecks := make(map[string]sets.Set[string], len(c.AdmissionChecks))
maps.Copy(admissionChecks, c.AdmissionChecks)
c.AdmissionChecks = admissionChecks

for fName, rUsage := range c.Usage {
cc.Usage[fName] = maps.Clone(rUsage)
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/core/workload_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ func (r *WorkloadReconciler) reconcileSyncAdmissionChecks(ctx context.Context, w
}

admissionChecks := workload.AdmissionChecksForWorkload(log, wl, utilac.NewAdmissionChecks(&cq))
log.V(2).Info("PATRYK created ac", "ac", admissionChecks)
newChecks, shouldUpdate := syncAdmissionCheckConditions(wl.Status.AdmissionChecks, admissionChecks)
if shouldUpdate {
log.V(3).Info("The workload needs admission checks updates", "clusterQueue", klog.KRef("", cqName), "admissionChecks", admissionChecks)
Expand Down
11 changes: 11 additions & 0 deletions pkg/util/maps/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package maps
import (
"fmt"
"maps"

"k8s.io/apimachinery/pkg/util/sets"
)

// Merge merges a and b while resolving the conflicts by calling commonKeyValue
Expand Down Expand Up @@ -108,3 +110,12 @@ func FilterKeys[K comparable, V any, M ~map[K]V](m M, k []K) M {
}
return ret
}

// DeepCopySets create a deep copy of map[string]Set which would otherwise be referenced
func DeepCopySets[T comparable](src map[string]sets.Set[T]) map[string]sets.Set[T] {
copy := make(map[string]sets.Set[T], len(src))
for key, set := range src {
copy[key] = set.Clone()
}
return copy
}
14 changes: 14 additions & 0 deletions pkg/workload/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"sigs.k8s.io/kueue/pkg/features"
"sigs.k8s.io/kueue/pkg/util/api"
"sigs.k8s.io/kueue/pkg/util/limitrange"
utilmaps "sigs.k8s.io/kueue/pkg/util/maps"
)

var (
Expand Down Expand Up @@ -550,6 +551,19 @@ func RemoveFinalizer(ctx context.Context, c client.Client, wl *kueue.Workload) e
// AdmissionChecksForWorkload returns AdmissionChecks that should be assigned to a specific Workload based on
// ClusterQueue configuration and ResourceFlavors
func AdmissionChecksForWorkload(log logr.Logger, wl *kueue.Workload, admissionChecks map[string]sets.Set[string]) sets.Set[string] {
// If all admissionChecks should be run for all flavors we don't need to wait for Workload's Admission to be set.
// This is also the case if admissionChecks are specified with ClusterQueue.Spec.AdmissionChecks instead of
// ClusterQueue.Spec.AdmissionCheckStrategy
allFlavors := true
for _, flavors := range admissionChecks {
if len(flavors) != 0 {
allFlavors = false
}
}
if allFlavors {
return sets.New(utilmaps.Keys(admissionChecks)...)
}

// Kueue sets AdmissionChecks first based on ClusterQueue configuration and at this point Workload has no
// ResourceFlavors assigned, so we cannot match AdmissionChecks to ResourceFlavor.
// After Quota is reserved, another reconciliation happens and we can match AdmissionChecks to ResourceFlavors
Expand Down

0 comments on commit ccc1b3a

Please sign in to comment.