mirror of
https://github.com/projectcapsule/capsule.git
synced 2026-02-14 18:09:58 +00:00
* chore(deps): update dependency golangci/golangci-lint to v2.8.0 * chore(deps): update dependency golangci/golangci-lint to v2.8.0 Signed-off-by: Hristo Hristov <me@hhristov.info> * chore(deps): update dependency golangci/golangci-lint to v2.8.0 Signed-off-by: Hristo Hristov <me@hhristov.info> * chore(deps): update dependency golangci/golangci-lint to v2.8.0 Signed-off-by: Hristo Hristov <me@hhristov.info> --------- Signed-off-by: Hristo Hristov <me@hhristov.info> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Hristo Hristov <me@hhristov.info>
60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
// Copyright 2020-2026 Project Capsule Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package v1beta2
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
)
|
|
|
|
const (
|
|
ResourceQuotaAnnotationPrefix = "quota.resources.capsule.clastix.io"
|
|
ResourceUsedAnnotationPrefix = "used.resources.capsule.clastix.io"
|
|
)
|
|
|
|
func UsedAnnotationForResource(kindGroup string) string {
|
|
return fmt.Sprintf("%s/%s", ResourceUsedAnnotationPrefix, kindGroup)
|
|
}
|
|
|
|
func LimitAnnotationForResource(kindGroup string) string {
|
|
return fmt.Sprintf("%s/%s", ResourceQuotaAnnotationPrefix, kindGroup)
|
|
}
|
|
|
|
func GetUsedResourceFromTenant(tenant Tenant, kindGroup string) (int64, error) {
|
|
usedStr, ok := tenant.GetAnnotations()[UsedAnnotationForResource(kindGroup)]
|
|
if !ok {
|
|
usedStr = "0"
|
|
}
|
|
|
|
used, _ := strconv.ParseInt(usedStr, 10, 10)
|
|
|
|
return used, nil
|
|
}
|
|
|
|
type NonLimitedResourceError struct {
|
|
kindGroup string
|
|
}
|
|
|
|
func NewNonLimitedResourceError(kindGroup string) *NonLimitedResourceError {
|
|
return &NonLimitedResourceError{kindGroup: kindGroup}
|
|
}
|
|
|
|
func (n NonLimitedResourceError) Error() string {
|
|
return fmt.Sprintf("resource %s is not limited for the current tenant", n.kindGroup)
|
|
}
|
|
|
|
func GetLimitResourceFromTenant(tenant Tenant, kindGroup string) (int64, error) {
|
|
limitStr, ok := tenant.GetAnnotations()[LimitAnnotationForResource(kindGroup)]
|
|
if !ok {
|
|
return 0, NewNonLimitedResourceError(kindGroup)
|
|
}
|
|
|
|
limit, err := strconv.ParseInt(limitStr, 10, 10)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("resource %s limit cannot be parsed, %w", kindGroup, err)
|
|
}
|
|
|
|
return limit, nil
|
|
}
|