Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support custom limitrange #5260

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions controllers/account/controllers/account_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (

"k8s.io/client-go/rest"

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

"github.com/go-logr/logr"
Expand Down Expand Up @@ -117,9 +116,9 @@ func (r *AccountReconciler) syncAccount(ctx context.Context, owner string, userN
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")
}
//if err := r.adaptEphemeralStorageLimitRange(ctx, userNamespace); err != nil {
// r.Logger.Error(err, "adapt ephemeral storage limitRange failed")
//}
if getUsername(userNamespace) != owner {
return nil, nil
}
Expand All @@ -146,22 +145,22 @@ func (r *AccountReconciler) syncResourceQuotaAndLimitRange(ctx context.Context,
return nil
}

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) 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
// })
//}

// SetupWithManager sets up the controller with the Manager.
func (r *AccountReconciler) SetupWithManager(mgr ctrl.Manager, rateOpts controller.Options) error {
Expand Down
46 changes: 40 additions & 6 deletions controllers/pkg/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,14 @@ const (
QuotaLimitsNodePorts = "QUOTA_LIMITS_NODE_PORTS"
QuotaObjectStorageSize = "QUOTA_OBJECT_STORAGE_SIZE"
QuotaObjectStorageBucket = "QUOTA_OBJECT_STORAGE_BUCKET"

LimitRangeCPU = "LIMIT_RANGE_CPU"
LimitRangeMemory = "LIMIT_RANGE_MEMORY"
LimitRangeEphemeralStorage = "LIMIT_RANGE_EPHEMERAL_STORAGE"

LimitRangeRepCPU = "LIMIT_RANGE_REP_CPU"
LimitRangeRepMemory = "LIMIT_RANGE_REP_MEMORY"
LimitRangeRepEphemeralStorage = "LIMIT_RANGE_REP_EPHEMERAL_STORAGE"
)

const (
Expand Down Expand Up @@ -436,14 +444,40 @@ func DefaultLimitRangeLimits() []corev1.LimitRangeItem {
return []corev1.LimitRangeItem{
{
Type: corev1.LimitTypeContainer,
Default: LimitRangeDefault,
DefaultRequest: LimitRangeDefault,
Default: defaultLimitRange,
DefaultRequest: defaultLimitRangeReq,
},
}
}

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

func getLimitRangeDefault() corev1.ResourceList {
rcList := corev1.ResourceList{}
cpu, memory, ephemeralStorage := resource.MustParse(env.GetEnvWithDefault(LimitRangeCPU, "50m")), resource.MustParse(env.GetEnvWithDefault(LimitRangeMemory, "64Mi")), resource.MustParse(env.GetEnvWithDefault(LimitRangeEphemeralStorage, "100Mi"))
if !cpu.IsZero() {
rcList[corev1.ResourceCPU] = cpu
}
if !memory.IsZero() {
rcList[corev1.ResourceMemory] = memory
}
if !ephemeralStorage.IsZero() {
rcList[corev1.ResourceEphemeralStorage] = ephemeralStorage
}
return rcList
}

func getLimitRangeReq() corev1.ResourceList {
rcList := corev1.ResourceList{}
cpu, memory, ephemeralStorage := resource.MustParse(env.GetEnvWithDefault(LimitRangeRepCPU, "50m")), resource.MustParse(env.GetEnvWithDefault(LimitRangeRepMemory, "64Mi")), resource.MustParse(env.GetEnvWithDefault(LimitRangeRepEphemeralStorage, "100Mi"))
if !cpu.IsZero() {
rcList[corev1.ResourceCPU] = cpu
}
if !memory.IsZero() {
rcList[corev1.ResourceMemory] = memory
}
if !ephemeralStorage.IsZero() {
rcList[corev1.ResourceEphemeralStorage] = ephemeralStorage
}
return rcList
}
Loading