|
| 1 | +/* |
| 2 | +Copyright 2023 IBM Corporation. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "strings" |
| 21 | + |
| 22 | + mcadv1beta1 "github.com/project-codeflare/mcad/api/v1beta1" |
| 23 | + v1 "k8s.io/api/core/v1" |
| 24 | +) |
| 25 | + |
| 26 | +// Should be defined in api/core/v1/types.go |
| 27 | +const DefaultResourceLimitsPrefix = "limits." |
| 28 | + |
| 29 | +// A tracker of allocated quota, mapped by namespace |
| 30 | +type QuotaTracker struct { |
| 31 | + // state of quotas, used, and allocated amounts |
| 32 | + state map[string]*QuotaState |
| 33 | + |
| 34 | + // used amounts by dispatched AppWrappers partially unaccounted by the ResourceQuota, |
| 35 | + // as some pods may have not passed the ResourceQuota admission controller |
| 36 | + unAdmittedWeightsMap map[string]*WeightsPair |
| 37 | +} |
| 38 | + |
| 39 | +// Create a new QuotaTracker |
| 40 | +func NewQuotaTracker() *QuotaTracker { |
| 41 | + return &QuotaTracker{ |
| 42 | + state: map[string]*QuotaState{}, |
| 43 | + unAdmittedWeightsMap: map[string]*WeightsPair{}, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// State includes total quota, used quota, and currently allocated quota |
| 48 | +type QuotaState struct { |
| 49 | + // quota enforced in the ResourceQuota object |
| 50 | + quota *WeightsPair |
| 51 | + // used amount in the status of the ResourceQuota object |
| 52 | + used *WeightsPair |
| 53 | + // allocated amount by dispatched AppWrappers in the current dispatching cycle |
| 54 | + allocated *WeightsPair |
| 55 | +} |
| 56 | + |
| 57 | +// Create a QuotaState from a ResourceQuota object |
| 58 | +func NewQuotaStateFromResourceQuota(resourceQuota *v1.ResourceQuota) *QuotaState { |
| 59 | + quotaWeights, usedWeights := getQuotaAndUsedWeightsPairsForResourceQuota(resourceQuota) |
| 60 | + return &QuotaState{ |
| 61 | + quota: quotaWeights, |
| 62 | + used: usedWeights, |
| 63 | + allocated: NewWeightsPair(Weights{}, Weights{}), |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Account for all in-flight AppWrappers with their resource demand not yet reflected in |
| 68 | +// the Used status of any ResourceQuota object in their corresponding namespace |
| 69 | +func (tracker *QuotaTracker) Init(weightsPairMap map[string]*WeightsPair) { |
| 70 | + tracker.unAdmittedWeightsMap = weightsPairMap |
| 71 | +} |
| 72 | + |
| 73 | +// Check if the resource demand of an AppWrapper satisfies a ResourceQuota, |
| 74 | +// without changing the current quota allocation, returning resource names with insufficient quota |
| 75 | +func (tracker *QuotaTracker) Satisfies(appWrapperAskWeights *WeightsPair, resourceQuota *v1.ResourceQuota) (bool, []v1.ResourceName) { |
| 76 | + namespace := resourceQuota.GetNamespace() |
| 77 | + var quotaState *QuotaState |
| 78 | + var exists bool |
| 79 | + if quotaState, exists = tracker.state[namespace]; !exists { |
| 80 | + quotaState = NewQuotaStateFromResourceQuota(resourceQuota) |
| 81 | + tracker.state[namespace] = quotaState |
| 82 | + } |
| 83 | + // check if both appwrapper requests and limits fit available resource quota |
| 84 | + quotaWeights := quotaState.quota.Clone() |
| 85 | + quotaWeights.Sub(quotaState.used) |
| 86 | + quotaWeights.Sub(quotaState.allocated) |
| 87 | + var unAdmittedWeights *WeightsPair |
| 88 | + if unAdmittedWeights, exists = tracker.unAdmittedWeightsMap[namespace]; exists { |
| 89 | + quotaWeights.Sub(unAdmittedWeights) |
| 90 | + } |
| 91 | + quotaFits, insufficientResources := appWrapperAskWeights.Fits(quotaWeights) |
| 92 | + |
| 93 | + // mcadLog.Info("QuotaTracker.Satisfies():", "namespace", namespace, |
| 94 | + // "QuotaWeights", quotaState.quota, "UsedWeights", quotaState.used, |
| 95 | + // "AllocatedWeights", quotaState.allocated, "unAdmittedWeights", unAdmittedWeights, |
| 96 | + // "AvailableWeights", quotaWeights, "appWrapperAskWeights", appWrapperAskWeights, |
| 97 | + // "quotaFits", quotaFits) |
| 98 | + return quotaFits, insufficientResources |
| 99 | +} |
| 100 | + |
| 101 | +// Update the QuotaState by the allocated weights of an AppWrapper in a namespace, |
| 102 | +// fails if QuotaState does not exist in the QuotaTracker |
| 103 | +func (tracker *QuotaTracker) Allocate(namespace string, appWrapperAskWeights *WeightsPair) bool { |
| 104 | + if state, exists := tracker.state[namespace]; exists && appWrapperAskWeights != nil { |
| 105 | + state.allocated.Add(appWrapperAskWeights) |
| 106 | + return true |
| 107 | + } |
| 108 | + return false |
| 109 | +} |
| 110 | + |
| 111 | +// Get requests and limits from AppWrapper specs |
| 112 | +func getWeightsPairForAppWrapper(appWrapper *mcadv1beta1.AppWrapper) *WeightsPair { |
| 113 | + requests := aggregateRequests(appWrapper) |
| 114 | + limits := aggregateLimits(appWrapper) |
| 115 | + return NewWeightsPair(requests, limits) |
| 116 | +} |
| 117 | + |
| 118 | +// Get requests and limits for both quota and used from ResourceQuota object |
| 119 | +func getQuotaAndUsedWeightsPairsForResourceQuota(resourceQuota *v1.ResourceQuota) (quotaWeights *WeightsPair, |
| 120 | + usedWeights *WeightsPair) { |
| 121 | + quotaWeights = getWeightsPairForResourceList(&resourceQuota.Status.Hard) |
| 122 | + usedWeights = getWeightsPairForResourceList(&resourceQuota.Status.Used) |
| 123 | + return quotaWeights, usedWeights |
| 124 | +} |
| 125 | + |
| 126 | +// Create a pair of Weights for requests and limits |
| 127 | +// given in a ResourceList of a ResourceQuota |
| 128 | +func getWeightsPairForResourceList(r *v1.ResourceList) *WeightsPair { |
| 129 | + requests := Weights{} |
| 130 | + limits := Weights{} |
| 131 | + for k, v := range *r { |
| 132 | + if strings.HasPrefix(k.String(), DefaultResourceLimitsPrefix) { |
| 133 | + trimmedName := strings.Replace(k.String(), DefaultResourceLimitsPrefix, "", 1) |
| 134 | + if _, exists := limits[v1.ResourceName(trimmedName)]; !exists { |
| 135 | + limits[v1.ResourceName(trimmedName)] = v.AsDec() |
| 136 | + } |
| 137 | + continue |
| 138 | + } |
| 139 | + if strings.HasPrefix(k.String(), v1.DefaultResourceRequestsPrefix) { |
| 140 | + trimmedName := strings.Replace(k.String(), v1.DefaultResourceRequestsPrefix, "", 1) |
| 141 | + k = v1.ResourceName(trimmedName) |
| 142 | + } |
| 143 | + // in case of two keys: requests.xxx and xxx, take the minimum quota of the two |
| 144 | + if value, exists := requests[k]; !exists || value.Cmp(v.AsDec()) > 0 { |
| 145 | + requests[k] = v.AsDec() |
| 146 | + } |
| 147 | + } |
| 148 | + return NewWeightsPair(requests, limits) |
| 149 | +} |
0 commit comments