|
| 1 | +// Copyright 2020-2023 Project Capsule Authors. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package v1beta2 |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "sort" |
| 9 | + |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + "k8s.io/apimachinery/pkg/api/resource" |
| 12 | + |
| 13 | + "github.com/projectcapsule/capsule/pkg/api" |
| 14 | +) |
| 15 | + |
| 16 | +func (r *ResourcePool) AssignNamespaces(namespaces []corev1.Namespace) { |
| 17 | + var l []string |
| 18 | + |
| 19 | + for _, ns := range namespaces { |
| 20 | + if ns.Status.Phase == corev1.NamespaceActive && ns.DeletionTimestamp == nil { |
| 21 | + l = append(l, ns.GetName()) |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + sort.Strings(l) |
| 26 | + |
| 27 | + r.Status.NamespaceSize = uint(len(l)) |
| 28 | + r.Status.Namespaces = l |
| 29 | +} |
| 30 | + |
| 31 | +func (r *ResourcePool) AssignClaims() { |
| 32 | + var size uint |
| 33 | + |
| 34 | + for _, claims := range r.Status.Claims { |
| 35 | + for range claims { |
| 36 | + size++ |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + r.Status.ClaimSize = size |
| 41 | +} |
| 42 | + |
| 43 | +func (r *ResourcePool) GetClaimFromStatus(cl *ResourcePoolClaim) *ResourcePoolClaimsItem { |
| 44 | + ns := cl.Namespace |
| 45 | + |
| 46 | + claims := r.Status.Claims[ns] |
| 47 | + if claims == nil { |
| 48 | + return nil |
| 49 | + } |
| 50 | + |
| 51 | + for _, claim := range claims { |
| 52 | + if claim.UID == cl.UID { |
| 53 | + return claim |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +func (r *ResourcePool) AddClaimToStatus(claim *ResourcePoolClaim) { |
| 61 | + ns := claim.Namespace |
| 62 | + |
| 63 | + if r.Status.Claims == nil { |
| 64 | + r.Status.Claims = ResourcePoolNamespaceClaimsStatus{} |
| 65 | + } |
| 66 | + |
| 67 | + if r.Status.Allocation.Claimed == nil { |
| 68 | + r.Status.Allocation.Claimed = corev1.ResourceList{} |
| 69 | + } |
| 70 | + |
| 71 | + claims := r.Status.Claims[ns] |
| 72 | + if claims == nil { |
| 73 | + claims = ResourcePoolClaimsList{} |
| 74 | + } |
| 75 | + |
| 76 | + scl := &ResourcePoolClaimsItem{ |
| 77 | + StatusNameUID: api.StatusNameUID{ |
| 78 | + UID: claim.UID, |
| 79 | + Name: api.Name(claim.Name), |
| 80 | + }, |
| 81 | + Claims: claim.Spec.ResourceClaims, |
| 82 | + } |
| 83 | + |
| 84 | + // Try to update existing entry if UID matches |
| 85 | + exists := false |
| 86 | + |
| 87 | + for i, cl := range claims { |
| 88 | + if cl.UID == claim.UID { |
| 89 | + claims[i] = scl |
| 90 | + |
| 91 | + exists = true |
| 92 | + |
| 93 | + break |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if !exists { |
| 98 | + claims = append(claims, scl) |
| 99 | + } |
| 100 | + |
| 101 | + r.Status.Claims[ns] = claims |
| 102 | + |
| 103 | + r.CalculateClaimedResources() |
| 104 | +} |
| 105 | + |
| 106 | +func (r *ResourcePool) RemoveClaimFromStatus(claim *ResourcePoolClaim) { |
| 107 | + newClaims := ResourcePoolClaimsList{} |
| 108 | + |
| 109 | + claims, ok := r.Status.Claims[claim.Namespace] |
| 110 | + if !ok { |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + for _, cl := range claims { |
| 115 | + if cl.UID != claim.UID { |
| 116 | + newClaims = append(newClaims, cl) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + r.Status.Claims[claim.Namespace] = newClaims |
| 121 | + |
| 122 | + if len(newClaims) == 0 { |
| 123 | + delete(r.Status.Claims, claim.Namespace) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func (r *ResourcePool) CalculateClaimedResources() { |
| 128 | + usage := corev1.ResourceList{} |
| 129 | + |
| 130 | + for res := range r.Status.Allocation.Hard { |
| 131 | + usage[res] = resource.MustParse("0") |
| 132 | + } |
| 133 | + |
| 134 | + for _, claims := range r.Status.Claims { |
| 135 | + for _, claim := range claims { |
| 136 | + for resourceName, qt := range claim.Claims { |
| 137 | + amount, exists := usage[resourceName] |
| 138 | + if !exists { |
| 139 | + amount = resource.MustParse("0") |
| 140 | + } |
| 141 | + |
| 142 | + amount.Add(qt) |
| 143 | + usage[resourceName] = amount |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + r.Status.Allocation.Claimed = usage |
| 149 | + |
| 150 | + r.CalculateAvailableResources() |
| 151 | +} |
| 152 | + |
| 153 | +func (r *ResourcePool) CalculateAvailableResources() { |
| 154 | + available := corev1.ResourceList{} |
| 155 | + |
| 156 | + for res, qt := range r.Status.Allocation.Hard { |
| 157 | + amount, exists := r.Status.Allocation.Claimed[res] |
| 158 | + if exists { |
| 159 | + qt.Sub(amount) |
| 160 | + } |
| 161 | + |
| 162 | + available[res] = qt |
| 163 | + } |
| 164 | + |
| 165 | + r.Status.Allocation.Available = available |
| 166 | +} |
| 167 | + |
| 168 | +func (r *ResourcePool) CanClaimFromPool(claim corev1.ResourceList) []error { |
| 169 | + claimable := r.GetAvailableClaimableResources() |
| 170 | + errs := []error{} |
| 171 | + |
| 172 | + for resourceName, req := range claim { |
| 173 | + available, exists := claimable[resourceName] |
| 174 | + if !exists || available.IsZero() || available.Cmp(req) < 0 { |
| 175 | + errs = append(errs, errors.New("not enough resources"+string(resourceName)+"available")) |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return errs |
| 180 | +} |
| 181 | + |
| 182 | +func (r *ResourcePool) GetAvailableClaimableResources() corev1.ResourceList { |
| 183 | + hard := r.Status.Allocation.Hard.DeepCopy() |
| 184 | + |
| 185 | + for resourceName, qt := range hard { |
| 186 | + claimed, exists := r.Status.Allocation.Claimed[resourceName] |
| 187 | + if !exists { |
| 188 | + claimed = resource.MustParse("0") |
| 189 | + } |
| 190 | + |
| 191 | + qt.Sub(claimed) |
| 192 | + |
| 193 | + hard[resourceName] = qt |
| 194 | + } |
| 195 | + |
| 196 | + return hard |
| 197 | +} |
| 198 | + |
| 199 | +// Gets the Hard specification for the resourcequotas |
| 200 | +// This takes into account the default resources being used. However they don't count towards the claim usage |
| 201 | +// This can be changed in the future, the default is not calculated as usage because this might interrupt the namespace management |
| 202 | +// As we would need to verify if a new namespace with it's defaults still has place in the Pool. Same with attempting to join existing namespaces. |
| 203 | +func (r *ResourcePool) GetResourceQuotaHardResources(namespace string) corev1.ResourceList { |
| 204 | + _, claimed := r.GetNamespaceClaims(namespace) |
| 205 | + |
| 206 | + for resourceName, amount := range claimed { |
| 207 | + if amount.IsZero() { |
| 208 | + delete(claimed, resourceName) |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + // Only Consider Default, when enabled |
| 213 | + for resourceName, amount := range r.Spec.Defaults { |
| 214 | + usedValue := claimed[resourceName] |
| 215 | + usedValue.Add(amount) |
| 216 | + |
| 217 | + claimed[resourceName] = usedValue |
| 218 | + } |
| 219 | + |
| 220 | + return claimed |
| 221 | +} |
| 222 | + |
| 223 | +// Gets the total amount of claimed resources for a namespace. |
| 224 | +func (r *ResourcePool) GetNamespaceClaims(namespace string) (claims map[string]*ResourcePoolClaimsItem, claimedResources corev1.ResourceList) { |
| 225 | + claimedResources = corev1.ResourceList{} |
| 226 | + claims = map[string]*ResourcePoolClaimsItem{} |
| 227 | + |
| 228 | + // First, check if quota exists in the status |
| 229 | + for ns, cl := range r.Status.Claims { |
| 230 | + if ns != namespace { |
| 231 | + continue |
| 232 | + } |
| 233 | + |
| 234 | + for _, claim := range cl { |
| 235 | + for resourceName, claimed := range claim.Claims { |
| 236 | + usedValue, usedExists := claimedResources[resourceName] |
| 237 | + if !usedExists { |
| 238 | + usedValue = resource.MustParse("0") // Default to zero if no used value is found |
| 239 | + } |
| 240 | + |
| 241 | + // Combine with claim |
| 242 | + usedValue.Add(claimed) |
| 243 | + claimedResources[resourceName] = usedValue |
| 244 | + } |
| 245 | + |
| 246 | + claims[string(claim.UID)] = claim |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + return |
| 251 | +} |
| 252 | + |
| 253 | +// Calculate usage for each namespace. |
| 254 | +func (r *ResourcePool) GetClaimedByNamespaceClaims() (claims map[string]corev1.ResourceList) { |
| 255 | + claims = map[string]corev1.ResourceList{} |
| 256 | + |
| 257 | + // First, check if quota exists in the status |
| 258 | + for ns, cl := range r.Status.Claims { |
| 259 | + claims[ns] = corev1.ResourceList{} |
| 260 | + nsScope := claims[ns] |
| 261 | + |
| 262 | + for _, claim := range cl { |
| 263 | + for resourceName, claimed := range claim.Claims { |
| 264 | + usedValue, usedExists := nsScope[resourceName] |
| 265 | + if !usedExists { |
| 266 | + usedValue = resource.MustParse("0") |
| 267 | + } |
| 268 | + |
| 269 | + usedValue.Add(claimed) |
| 270 | + nsScope[resourceName] = usedValue |
| 271 | + } |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + return |
| 276 | +} |
0 commit comments