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

feat: resource quota add object storage size #4874

Merged
merged 7 commits into from
Jul 22, 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
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ status:
acceptedNames:
kind: ""
plural: ""
conditions: [ ]
storedVersions: [ ]
conditions: []
storedVersions: []
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@ status:
acceptedNames:
kind: ""
plural: ""
conditions: [ ]
storedVersions: [ ]
conditions: []
storedVersions: []
12 changes: 12 additions & 0 deletions controllers/objectstorage/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ rules:
- patch
- update
- watch
- apiGroups:
- ""
resources:
- resourcequotas
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- objectstorage.sealos.io
resources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"

"strings"
"time"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/rand"
Expand All @@ -56,7 +57,6 @@ type ObjectStorageUserReconciler struct {
ExternalEndpoint string
OSUDetectionCycle time.Duration
QuotaEnabled bool
DefaultQuota int64
}

const (
Expand All @@ -69,19 +69,24 @@ const (
OSExternalEndpointEnv = "OSExternalEndpoint"
OSNamespace = "OSNamespace"
OSAdminSecret = "OSAdminSecret"
QuotaEnabled = "QuotaEnabled"

OSKeySecret = "object-storage-key"
OSKeySecretAccessKey = "accessKey"
OSKeySecretSecretKey = "secretKey"
OSKeySecretInternal = "internal"
OSKeySecretExternal = "external"
OSKeySecretBucket = "bucket"

ResourceQuotaPrefix = "quota-"
ResourceObjectStorageSize = "objectstorage/size"
)

//+kubebuilder:rbac:groups=objectstorage.sealos.io,resources=objectstorageusers,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=objectstorage.sealos.io,resources=objectstorageusers/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=objectstorage.sealos.io,resources=objectstorageusers/finalizers,verbs=update
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=resourcequotas,verbs=get;list;watch;create;update;patch;delete

func (r *ObjectStorageUserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
username := req.Name
Expand Down Expand Up @@ -136,7 +141,15 @@ func (r *ObjectStorageUserReconciler) Reconcile(ctx context.Context, req ctrl.Re
return ctrl.Result{}, nil
}

updated := r.initObjectStorageUser(user, username)
resourceQuota := &corev1.ResourceQuota{}
if err := r.Get(ctx, client.ObjectKey{Name: ResourceQuotaPrefix + userNamespace, Namespace: userNamespace}, resourceQuota); err != nil {
r.Logger.Error(err, "failed to get resource quota", "name", ResourceQuotaPrefix+userNamespace, "namespace", userNamespace)
return ctrl.Result{}, err
}

quota := resourceQuota.Spec.Hard.Name(ResourceObjectStorageSize, resource.BinarySI)

updated := r.initObjectStorageUser(user, username, quota.Value())

accessKey := user.Status.AccessKey
secretKey := user.Status.SecretKey
Expand Down Expand Up @@ -337,11 +350,11 @@ func (r *ObjectStorageUserReconciler) deleteObjectStorageUser(ctx context.Contex
return nil
}

func (r *ObjectStorageUserReconciler) initObjectStorageUser(user *objectstoragev1.ObjectStorageUser, username string) bool {
func (r *ObjectStorageUserReconciler) initObjectStorageUser(user *objectstoragev1.ObjectStorageUser, username string, quota int64) bool {
var updated = false

if user.Status.Quota != r.DefaultQuota {
user.Status.Quota = r.DefaultQuota
if user.Status.Quota != quota {
user.Status.Quota = quota
updated = true
}

Expand Down Expand Up @@ -418,12 +431,9 @@ func (r *ObjectStorageUserReconciler) SetupWithManager(mgr ctrl.Manager) error {
return fmt.Errorf("failed to get the endpoint or namespace or admin secret env of object storage")
}

quotaEnabled := env.GetBoolWithDefault("quotaEnabled", true)
quotaEnabled := env.GetBoolWithDefault(QuotaEnabled, true)
r.QuotaEnabled = quotaEnabled

defaultQuota := env.GetInt64EnvWithDefault("defaultQuota", 107374182400)
r.DefaultQuota = defaultQuota

return ctrl.NewControllerManagedBy(mgr).
For(&objectstoragev1.ObjectStorageUser{}).
Complete(r)
Expand Down
32 changes: 20 additions & 12 deletions controllers/pkg/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,10 @@ const ResourceGPU corev1.ResourceName = gpu.NvidiaGpuKey
const ResourceNetwork = "network"

const (
ResourceRequestGpu corev1.ResourceName = "requests." + gpu.NvidiaGpuKey
ResourceLimitGpu corev1.ResourceName = "limits." + gpu.NvidiaGpuKey
ResourceRequestGpu corev1.ResourceName = "requests." + gpu.NvidiaGpuKey
ResourceLimitGpu corev1.ResourceName = "limits." + gpu.NvidiaGpuKey
ResourceObjectStorageSize corev1.ResourceName = "objectstorage/size"
ResourceObjectStorageBucket corev1.ResourceName = "objectstorage/bucket"
)

func NewGpuResource(product string) corev1.ResourceName {
Expand Down Expand Up @@ -378,19 +380,23 @@ func GetDefaultLimitRange(ns, name string) *corev1.LimitRange {
}

const (
QuotaLimitsCPU = "QUOTA_LIMITS_CPU"
QuotaLimitsMemory = "QUOTA_LIMITS_MEMORY"
QuotaLimitsStorage = "QUOTA_LIMITS_STORAGE"
QuotaLimitsGPU = "QUOTA_LIMITS_GPU"
QuotaLimitsNodePorts = "QUOTA_LIMITS_NODE_PORTS"
QuotaLimitsCPU = "QUOTA_LIMITS_CPU"
QuotaLimitsMemory = "QUOTA_LIMITS_MEMORY"
QuotaLimitsStorage = "QUOTA_LIMITS_STORAGE"
QuotaLimitsGPU = "QUOTA_LIMITS_GPU"
QuotaLimitsNodePorts = "QUOTA_LIMITS_NODE_PORTS"
QuotaObjectStorageSize = "QUOTA_OBJECT_STORAGE_SIZE"
QuotaObjectStorageBucket = "QUOTA_OBJECT_STORAGE_BUCKET"
)

const (
DefaultQuotaLimitsCPU = "16"
DefaultQuotaLimitsMemory = "64Gi"
DefaultQuotaLimitsStorage = "100Gi"
DefaultQuotaLimitsGPU = "8"
DefaultQuotaLimitsNodePorts = "3"
DefaultQuotaLimitsCPU = "16"
DefaultQuotaLimitsMemory = "64Gi"
DefaultQuotaLimitsStorage = "100Gi"
DefaultQuotaLimitsGPU = "8"
DefaultQuotaLimitsNodePorts = "3"
DefaultQuotaObjectStorageSize = "100Gi"
DefaultQuotaObjectStorageBucket = "5"
)

func DefaultResourceQuotaHard() corev1.ResourceList {
Expand All @@ -402,6 +408,8 @@ func DefaultResourceQuotaHard() corev1.ResourceList {
corev1.ResourceRequestsStorage: resource.MustParse(env.GetEnvWithDefault(QuotaLimitsStorage, DefaultQuotaLimitsStorage)),
corev1.ResourceLimitsEphemeralStorage: resource.MustParse(env.GetEnvWithDefault(QuotaLimitsStorage, DefaultQuotaLimitsStorage)),
corev1.ResourceServicesNodePorts: resource.MustParse(env.GetEnvWithDefault(QuotaLimitsNodePorts, DefaultQuotaLimitsNodePorts)),
ResourceObjectStorageSize: resource.MustParse(env.GetEnvWithDefault(QuotaObjectStorageSize, DefaultQuotaObjectStorageSize)),
ResourceObjectStorageBucket: resource.MustParse(env.GetEnvWithDefault(QuotaObjectStorageBucket, DefaultQuotaObjectStorageBucket)),
//TODO storage.diskio.read, storage.diskio.write
}
}
Expand Down
Loading