Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit

Permalink
Add basic support for resource quota (#143)
Browse files Browse the repository at this point in the history
* add basic support for resource quota

* fix declaration error

* add resourcequotas to RBAC

* add resourcequotas to helm cluster roles

* fix kubebuilder directive

* cleanup

* fixed deepcopy
  • Loading branch information
atantawi authored Feb 14, 2024
1 parent 5d6d5fa commit 810dc80
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 9 deletions.
2 changes: 1 addition & 1 deletion api/v1beta1/appwrapper_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ type CustomPodResource struct {
Requests v1.ResourceList `json:"requests"`

// Limits per replica
NotImplemented_Limits v1.ResourceList `json:"limits,omitempty"`
Limits v1.ResourceList `json:"limits,omitempty"`
}

// State transition
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ rules:
- patch
- update
- watch
- apiGroups:
- ""
resources:
- resourcequotas
verbs:
- get
- list
- watch
- apiGroups:
- apps
resources:
Expand Down
8 changes: 8 additions & 0 deletions deployment/mcad-controller/templates/rbac/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,11 @@ rules:
- subjectaccessreviews
verbs:
- create
- apiGroups:
- ""
resources:
- resourcequotas
verbs:
- get
- list
- watch
1 change: 1 addition & 0 deletions internal/controller/appwrapper_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
//+kubebuilder:rbac:groups=workload.codeflare.dev,resources=appwrappers,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=workload.codeflare.dev,resources=appwrappers/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=workload.codeflare.dev,resources=appwrappers/finalizers,verbs=update
//+kubebuilder:rbac:groups="",resources=resourcequotas,verbs=get;list;watch

// AppWrapperReconciler is the super type of Dispatcher and Runner reconcilers
type AppWrapperReconciler struct {
Expand Down
46 changes: 41 additions & 5 deletions internal/controller/dispatch_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (r *Dispatcher) listAppWrappers(ctx context.Context) (map[int]Weights, []*m
}

// Find next AppWrapper to dispatch in queue order
func (r *Dispatcher) selectForDispatch(ctx context.Context) ([]*mcadv1beta1.AppWrapper, error) {
func (r *Dispatcher) selectForDispatch(ctx context.Context, quotatracker *QuotaTracker) ([]*mcadv1beta1.AppWrapper, error) {
selected := []*mcadv1beta1.AppWrapper{}
logThisDispatch := time.Now().After(r.NextLoggedDispatch)
if logThisDispatch {
Expand Down Expand Up @@ -204,13 +204,38 @@ func (r *Dispatcher) selectForDispatch(ctx context.Context) ([]*mcadv1beta1.AppW
// return ordered slice of AppWrappers that fit (may be empty)
for _, appWrapper := range queue {
request := aggregateRequests(appWrapper)
// get resourceQuota in AppWrapper namespace, if any
resourceQuotas := &v1.ResourceQuotaList{}
namespace := appWrapper.GetNamespace()
if err := r.List(ctx, resourceQuotas, client.UnsafeDisableDeepCopy,
&client.ListOptions{Namespace: namespace}); err != nil {
return nil, err
}
quotaFits := true
var appWrapperAskWeights *WeightsPair
insufficientResources := []v1.ResourceName{}
if len(resourceQuotas.Items) > 0 {
appWrapperAskWeights = getWeightsPairForAppWrapper(appWrapper)
// assuming only one resourceQuota per nameSpace
quotaFits, insufficientResources = quotatracker.Satisfies(appWrapperAskWeights, &resourceQuotas.Items[0])
}
fits, gaps := request.Fits(available[int(appWrapper.Spec.Priority)])
if fits {
selected = append(selected, appWrapper.DeepCopy()) // deep copy AppWrapper
for priority, avail := range available {
if priority <= int(appWrapper.Spec.Priority) {
avail.Sub(request)
// check if appwrapper passes resource quota (if any)
if quotaFits {
quotatracker.Allocate(namespace, appWrapperAskWeights)
selected = append(selected, appWrapper.DeepCopy()) // deep copy AppWrapper
for priority, avail := range available {
if priority <= int(appWrapper.Spec.Priority) {
avail.Sub(request)
}
}
} else {
var msgBuilder strings.Builder
for _, resource := range insufficientResources {
msgBuilder.WriteString(fmt.Sprintf("Insufficient %v. ", resource))
}
r.Decisions[appWrapper.UID] = &QueuingDecision{reason: mcadv1beta1.QueuedInsufficientQuota, message: msgBuilder.String()}
}
} else {
var msgBuilder strings.Builder
Expand All @@ -237,6 +262,17 @@ func aggregateRequests(appWrapper *mcadv1beta1.AppWrapper) Weights {
return request
}

// Aggregate limits
func aggregateLimits(appWrapper *mcadv1beta1.AppWrapper) Weights {
limit := Weights{}
for _, r := range appWrapper.Spec.Resources.GenericItems {
for _, cpr := range r.CustomPodResources {
limit.AddProd(cpr.Replicas, NewWeights(cpr.Limits))
}
}
return limit
}

// Propagate reservations at all priority levels to all levels below
func assertPriorities(w map[int]Weights) {
keys := make([]int, len(w))
Expand Down
47 changes: 46 additions & 1 deletion internal/controller/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import (
"fmt"
"time"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
Expand Down Expand Up @@ -225,8 +227,14 @@ func (r *Dispatcher) triggerDispatch() {

// Attempt to select and dispatch appWrappers until either capacity is exhausted or no candidates remain
func (r *Dispatcher) dispatch(ctx context.Context) (ctrl.Result, error) {
// track quota allocation to AppWrappers during a dispatching cycle;
// used in only one cycle, does not carry from cycle to cycle
quotaTracker := NewQuotaTracker()
if weightsPairMap, err := r.getUnadmittedPodsWeights(ctx); err == nil {
quotaTracker.Init(weightsPairMap)
}
// find dispatch candidates according to priorities, precedence, and available resources
selectedAppWrappers, err := r.selectForDispatch(ctx)
selectedAppWrappers, err := r.selectForDispatch(ctx, quotaTracker)
if err != nil {
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -257,3 +265,40 @@ func (r *Dispatcher) dispatch(ctx context.Context) (ctrl.Result, error) {

return ctrl.Result{RequeueAfter: dispatchDelay}, nil
}

// Calculate resource demands of pods for appWrappers that have been dispatched but haven't
// passed through ResourceQuota admission controller yet (approximated by resources not created yet)
func (r *Dispatcher) getUnadmittedPodsWeights(ctx context.Context) (map[string]*WeightsPair, error) {
appWrappers := &mcadv1beta1.AppWrapperList{}
if err := r.List(ctx, appWrappers, client.UnsafeDisableDeepCopy); err != nil {
return nil, err
}
weightsPairMap := make(map[string]*WeightsPair)
for _, appWrapper := range appWrappers.Items {
_, step := r.getCachedAW(&appWrapper)
if step != mcadv1beta1.Idle {
namespace := appWrapper.GetNamespace()
weightsPair := weightsPairMap[namespace]
if weightsPair == nil {
weightsPair = NewWeightsPair(Weights{}, Weights{})
}
weightsPair.Add(getWeightsPairForAppWrapper(&appWrapper))

// subtract weights for admitted (created) pods for this appWrapper
// (already accounted for in the used status of the resourceQuota)
pods := &v1.PodList{}
if err := r.List(ctx, pods, client.UnsafeDisableDeepCopy,
client.MatchingLabels{namespaceLabel: namespace, nameLabel: appWrapper.Name}); err == nil {
createdPodsWeightsPair := &WeightsPair{requests: Weights{}, limits: Weights{}}
for _, pod := range pods.Items {
createdPodsWeightsPair.Add(NewWeightsPairForPod(&pod))
}
weightsPair.Sub(createdPodsWeightsPair)
}
nonNegativeWeightsPair := NewWeightsPair(Weights{}, Weights{})
nonNegativeWeightsPair.Max(weightsPair)
weightsPairMap[namespace] = nonNegativeWeightsPair
}
}
return weightsPairMap, nil
}
149 changes: 149 additions & 0 deletions internal/controller/quota_tracker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
Copyright 2023 IBM Corporation.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
"strings"

mcadv1beta1 "github.com/project-codeflare/mcad/api/v1beta1"
v1 "k8s.io/api/core/v1"
)

// Should be defined in api/core/v1/types.go
const DefaultResourceLimitsPrefix = "limits."

// A tracker of allocated quota, mapped by namespace
type QuotaTracker struct {
// state of quotas, used, and allocated amounts
state map[string]*QuotaState

// used amounts by dispatched AppWrappers partially unaccounted by the ResourceQuota,
// as some pods may have not passed the ResourceQuota admission controller
unAdmittedWeightsMap map[string]*WeightsPair
}

// Create a new QuotaTracker
func NewQuotaTracker() *QuotaTracker {
return &QuotaTracker{
state: map[string]*QuotaState{},
unAdmittedWeightsMap: map[string]*WeightsPair{},
}
}

// State includes total quota, used quota, and currently allocated quota
type QuotaState struct {
// quota enforced in the ResourceQuota object
quota *WeightsPair
// used amount in the status of the ResourceQuota object
used *WeightsPair
// allocated amount by dispatched AppWrappers in the current dispatching cycle
allocated *WeightsPair
}

// Create a QuotaState from a ResourceQuota object
func NewQuotaStateFromResourceQuota(resourceQuota *v1.ResourceQuota) *QuotaState {
quotaWeights, usedWeights := getQuotaAndUsedWeightsPairsForResourceQuota(resourceQuota)
return &QuotaState{
quota: quotaWeights,
used: usedWeights,
allocated: NewWeightsPair(Weights{}, Weights{}),
}
}

// Account for all in-flight AppWrappers with their resource demand not yet reflected in
// the Used status of any ResourceQuota object in their corresponding namespace
func (tracker *QuotaTracker) Init(weightsPairMap map[string]*WeightsPair) {
tracker.unAdmittedWeightsMap = weightsPairMap
}

// Check if the resource demand of an AppWrapper satisfies a ResourceQuota,
// without changing the current quota allocation, returning resource names with insufficient quota
func (tracker *QuotaTracker) Satisfies(appWrapperAskWeights *WeightsPair, resourceQuota *v1.ResourceQuota) (bool, []v1.ResourceName) {
namespace := resourceQuota.GetNamespace()
var quotaState *QuotaState
var exists bool
if quotaState, exists = tracker.state[namespace]; !exists {
quotaState = NewQuotaStateFromResourceQuota(resourceQuota)
tracker.state[namespace] = quotaState
}
// check if both appwrapper requests and limits fit available resource quota
quotaWeights := quotaState.quota.Clone()
quotaWeights.Sub(quotaState.used)
quotaWeights.Sub(quotaState.allocated)
var unAdmittedWeights *WeightsPair
if unAdmittedWeights, exists = tracker.unAdmittedWeightsMap[namespace]; exists {
quotaWeights.Sub(unAdmittedWeights)
}
quotaFits, insufficientResources := appWrapperAskWeights.Fits(quotaWeights)

// mcadLog.Info("QuotaTracker.Satisfies():", "namespace", namespace,
// "QuotaWeights", quotaState.quota, "UsedWeights", quotaState.used,
// "AllocatedWeights", quotaState.allocated, "unAdmittedWeights", unAdmittedWeights,
// "AvailableWeights", quotaWeights, "appWrapperAskWeights", appWrapperAskWeights,
// "quotaFits", quotaFits)
return quotaFits, insufficientResources
}

// Update the QuotaState by the allocated weights of an AppWrapper in a namespace,
// fails if QuotaState does not exist in the QuotaTracker
func (tracker *QuotaTracker) Allocate(namespace string, appWrapperAskWeights *WeightsPair) bool {
if state, exists := tracker.state[namespace]; exists && appWrapperAskWeights != nil {
state.allocated.Add(appWrapperAskWeights)
return true
}
return false
}

// Get requests and limits from AppWrapper specs
func getWeightsPairForAppWrapper(appWrapper *mcadv1beta1.AppWrapper) *WeightsPair {
requests := aggregateRequests(appWrapper)
limits := aggregateLimits(appWrapper)
return NewWeightsPair(requests, limits)
}

// Get requests and limits for both quota and used from ResourceQuota object
func getQuotaAndUsedWeightsPairsForResourceQuota(resourceQuota *v1.ResourceQuota) (quotaWeights *WeightsPair,
usedWeights *WeightsPair) {
quotaWeights = getWeightsPairForResourceList(&resourceQuota.Status.Hard)
usedWeights = getWeightsPairForResourceList(&resourceQuota.Status.Used)
return quotaWeights, usedWeights
}

// Create a pair of Weights for requests and limits
// given in a ResourceList of a ResourceQuota
func getWeightsPairForResourceList(r *v1.ResourceList) *WeightsPair {
requests := Weights{}
limits := Weights{}
for k, v := range *r {
if strings.HasPrefix(k.String(), DefaultResourceLimitsPrefix) {
trimmedName := strings.Replace(k.String(), DefaultResourceLimitsPrefix, "", 1)
if _, exists := limits[v1.ResourceName(trimmedName)]; !exists {
limits[v1.ResourceName(trimmedName)] = v.AsDec()
}
continue
}
if strings.HasPrefix(k.String(), v1.DefaultResourceRequestsPrefix) {
trimmedName := strings.Replace(k.String(), v1.DefaultResourceRequestsPrefix, "", 1)
k = v1.ResourceName(trimmedName)
}
// in case of two keys: requests.xxx and xxx, take the minimum quota of the two
if value, exists := requests[k]; !exists || value.Cmp(v.AsDec()) > 0 {
requests[k] = v.AsDec()
}
}
return NewWeightsPair(requests, limits)
}
Loading

0 comments on commit 810dc80

Please sign in to comment.