Skip to content

Commit

Permalink
adapt ephemeral storage limit range (#4456)
Browse files Browse the repository at this point in the history
* repair calculation ratio

* adapt limit range
  • Loading branch information
bxy4543 committed Jan 3, 2024
1 parent b4daa2d commit e87219f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
34 changes: 21 additions & 13 deletions controllers/account/controllers/account_controller.go
Expand Up @@ -26,6 +26,7 @@ import (
"strings"
"time"

corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/rest"

"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -214,6 +215,9 @@ func (r *AccountReconciler) syncAccount(ctx context.Context, owner, accountNames
if err := r.syncResourceQuotaAndLimitRange(ctx, userNamespace); err != nil {
r.Logger.Error(err, "sync resource resourceQuota and limitRange failed")
}
if err := r.adaptEphemeralStorageLimitRange(ctx, userNamespace); err != nil {
r.Logger.Error(err, "adapt ephemeral storage limitRange failed")
}
account := accountv1.Account{
ObjectMeta: metav1.ObjectMeta{
Name: owner,
Expand Down Expand Up @@ -298,18 +302,22 @@ func (r *AccountReconciler) syncResourceQuotaAndLimitRange(ctx context.Context,
return nil
}

//func (r *AccountReconciler) adaptNodePortCountQuota(ctx context.Context, nsName string) error {
// quota := resources.GetDefaultResourceQuota(nsName, ResourceQuotaPrefix+nsName)
// return retry.Retry(10, 1*time.Second, func() error {
// _, err := controllerutil.CreateOrUpdate(ctx, r.Client, quota, func() error {
// if _, ok := quota.Spec.Hard[corev1.ResourceServicesNodePorts]; !ok {
// quota.Spec.Hard[corev1.ResourceServicesNodePorts] = resource.MustParse(env.GetEnvWithDefault(resources.QuotaLimitsNodePorts, resources.DefaultQuotaLimitsNodePorts))
// }
// return nil
// })
// return err
// })
//}
func (r *AccountReconciler) adaptEphemeralStorageLimitRange(ctx context.Context, nsName string) error {
limit := resources.GetDefaultLimitRange(nsName, nsName)
return retry.Retry(10, 1*time.Second, func() error {
_, err := controllerutil.CreateOrUpdate(ctx, r.Client, limit, func() error {
if len(limit.Spec.Limits) == 0 {
limit = resources.GetDefaultLimitRange(nsName, nsName)
}
limit.Spec.Limits[0].DefaultRequest[corev1.ResourceEphemeralStorage] = resources.LimitRangeDefault[corev1.ResourceEphemeralStorage]
limit.Spec.Limits[0].Default[corev1.ResourceEphemeralStorage] = resources.LimitRangeDefault[corev1.ResourceEphemeralStorage]
//if _, ok := limit.Spec.Limits[0].Default[corev1.ResourceEphemeralStorage]; !ok {
//}
return nil
})
return err
})
}

func (r *AccountReconciler) syncRoleAndRoleBinding(ctx context.Context, name, namespace string) error {
role := rbacv1.Role{
Expand Down Expand Up @@ -568,5 +576,5 @@ func getAmountWithDiscount(amount int64, discount pkgtypes.RechargeDiscount) int
break
}
}
return int64(math.Ceil(float64(amount)*r)) + amount
return int64(math.Ceil(float64(amount)*r/100)) + amount
}
14 changes: 9 additions & 5 deletions controllers/pkg/resources/resources.go
Expand Up @@ -487,15 +487,19 @@ func DefaultResourceQuotaHard() corev1.ResourceList {
func DefaultLimitRangeLimits() []corev1.LimitRangeItem {
return []corev1.LimitRangeItem{
{
Type: corev1.LimitTypeContainer,
Default: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("64Mi"),
},
Type: corev1.LimitTypeContainer,
Default: LimitRangeDefault,
DefaultRequest: LimitRangeDefault,
},
}
}

var LimitRangeDefault = corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("64Mi"),
corev1.ResourceEphemeralStorage: resource.MustParse("100Mi"),
}

//
//// MiB
//func GetInfraCPUQuantity(flavor string, count int) *resource.Quantity {
Expand Down

0 comments on commit e87219f

Please sign in to comment.