mirror of
https://github.com/projectcapsule/capsule.git
synced 2026-02-14 09:59:57 +00:00
fix: resource quota annotations key max length support
This commit is contained in:
@@ -4,14 +4,41 @@
|
||||
package v1beta2
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func UsedQuotaFor(resource fmt.Stringer) string {
|
||||
return "quota.capsule.clastix.io/used-" + strings.ReplaceAll(resource.String(), "/", "_")
|
||||
const (
|
||||
// Annotation name part must be no more than 63 characters.
|
||||
maxAnnotationLength = 63
|
||||
)
|
||||
|
||||
func createAnnotation(format string, resource fmt.Stringer) (string, error) {
|
||||
suffix := resource.String()
|
||||
|
||||
hash := md5.Sum([]byte(resource.String()))
|
||||
|
||||
hashed := hex.EncodeToString(hash[:])
|
||||
capsuleHashed := format + hashed
|
||||
capsuleAnnotation := format + suffix
|
||||
|
||||
switch {
|
||||
case len(capsuleAnnotation) <= maxAnnotationLength:
|
||||
return capsuleAnnotation, nil
|
||||
case len(capsuleHashed) <= maxAnnotationLength:
|
||||
return capsuleHashed, nil
|
||||
case len(hashed) <= maxAnnotationLength:
|
||||
return hashed, nil
|
||||
default:
|
||||
return "", fmt.Errorf("the annotation name would exceed the maximum supported length (%d), skipping", maxAnnotationLength)
|
||||
}
|
||||
}
|
||||
|
||||
func HardQuotaFor(resource fmt.Stringer) string {
|
||||
return "quota.capsule.clastix.io/hard-" + strings.ReplaceAll(resource.String(), "/", "_")
|
||||
func UsedQuotaFor(resource fmt.Stringer) (string, error) {
|
||||
return createAnnotation("quota.capsule.clastix.io/used-", resource)
|
||||
}
|
||||
|
||||
func HardQuotaFor(resource fmt.Stringer) (string, error) {
|
||||
return createAnnotation("quota.capsule.clastix.io/hard-", resource)
|
||||
}
|
||||
|
||||
@@ -237,8 +237,12 @@ func (r *Manager) resourceQuotasUpdate(ctx context.Context, resourceName corev1.
|
||||
found.Annotations = make(map[string]string)
|
||||
}
|
||||
found.Labels = rq.Labels
|
||||
found.Annotations[capsulev1beta2.UsedQuotaFor(resourceName)] = actual.String()
|
||||
found.Annotations[capsulev1beta2.HardQuotaFor(resourceName)] = limit.String()
|
||||
if actualKey, keyErr := capsulev1beta2.UsedQuotaFor(resourceName); keyErr == nil {
|
||||
found.Annotations[actualKey] = actual.String()
|
||||
}
|
||||
if limitKey, keyErr := capsulev1beta2.HardQuotaFor(resourceName); keyErr == nil {
|
||||
found.Annotations[limitKey] = limit.String()
|
||||
}
|
||||
// Updating the Resource according to the actual.Cmp result
|
||||
found.Spec.Hard = rq.Spec.Hard
|
||||
|
||||
|
||||
Reference in New Issue
Block a user