Skip to content

Commit

Permalink
Optimize the unit display of resourcequota of used.objectstorage/size (
Browse files Browse the repository at this point in the history
…#5187)

* optimize unit for objectstorage/size used
  • Loading branch information
nowinkeyy authored Nov 5, 2024
1 parent 1c0f91d commit 9ff29cd
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 15 deletions.
1 change: 0 additions & 1 deletion controllers/account/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/clbanning/mxj/v2 v2.5.7 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dinoallo/sealos-networkmanager-protoapi v0.0.0-20230928031328-cf9649d6af49 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
Expand Down
1 change: 0 additions & 1 deletion controllers/job/init/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dinoallo/sealos-networkmanager-protoapi v0.0.0-20230928031328-cf9649d6af49 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
Expand Down
1 change: 0 additions & 1 deletion controllers/license/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dinoallo/sealos-networkmanager-protoapi v0.0.0-20230928031328-cf9649d6af49 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"context"
"fmt"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -219,8 +218,10 @@ func (r *ObjectStorageUserReconciler) Reconcile(ctx context.Context, req ctrl.Re
updated = true
}

if used.Value() != size {
resourceQuota.Status.Used[ResourceObjectStorageSize] = resource.MustParse(strconv.FormatInt(size, 10))
stringSize := ConvertBytesToString(size)

if used.String() != stringSize {
resourceQuota.Status.Used[ResourceObjectStorageSize] = resource.MustParse(stringSize)
if err := r.Status().Update(ctx, resourceQuota); err != nil {
r.Logger.Error(err, "failed to update status", "name", resourceQuota.Name, "namespace", userNamespace)
return ctrl.Result{}, err
Expand Down Expand Up @@ -433,6 +434,30 @@ func (r *ObjectStorageUserReconciler) initObjectStorageKeySecret(secret *corev1.
return updated
}

func ConvertBytesToString(bytes int64) string {
var unit string
var value float64

switch {
case bytes >= 1<<40: // 1 TiB
value = float64(bytes) / (1 << 40)
unit = "Ti"
case bytes >= 1<<30: // 1 GiB
value = float64(bytes) / (1 << 30)
unit = "Gi"
case bytes >= 1<<20: // 1 MiB
value = float64(bytes) / (1 << 20)
unit = "Mi"
case bytes >= 1<<10: // 1 KiB
value = float64(bytes) / (1 << 10)
unit = "Ki"
default:
return fmt.Sprintf("%d", bytes)
}

return fmt.Sprintf("%.0f%s", value, unit)
}

// SetupWithManager sets up the controller with the Manager.
func (r *ObjectStorageUserReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.Logger = ctrl.Log.WithName("object-storage-user-controller")
Expand Down
1 change: 0 additions & 1 deletion controllers/pkg/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ replace (

require (
github.com/containers/storage v1.50.2
github.com/dinoallo/sealos-networkmanager-protoapi v0.0.0-20230928031328-cf9649d6af49
github.com/dustin/go-humanize v1.0.1
github.com/go-logr/logr v1.4.1
github.com/golang-jwt/jwt/v4 v4.5.0
Expand Down
2 changes: 0 additions & 2 deletions controllers/pkg/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dinoallo/sealos-networkmanager-protoapi v0.0.0-20230928031328-cf9649d6af49 h1:4GI5eviCwbPxDE311KryyyPUTO7IDVyHGp3Iyl+fEZY=
github.com/dinoallo/sealos-networkmanager-protoapi v0.0.0-20230928031328-cf9649d6af49/go.mod h1:sbm1DAsayX+XsXCOC2CFAAU9JZhX0SPKwnybDjSd0Ls=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
Expand Down
5 changes: 0 additions & 5 deletions controllers/resources/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dinoallo/sealos-networkmanager-protoapi v0.0.0-20230928031328-cf9649d6af49 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
Expand Down Expand Up @@ -61,7 +60,6 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/labring/sealos/controllers/account v0.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matoous/go-nanoid/v2 v2.0.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
Expand Down Expand Up @@ -98,8 +96,6 @@ require (
golang.org/x/time v0.5.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe // indirect
google.golang.org/grpc v1.61.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand All @@ -120,7 +116,6 @@ require (
)

replace (
github.com/labring/sealos/controllers/account => ../account
github.com/labring/sealos/controllers/pkg => ../pkg
github.com/labring/sealos/controllers/user => ../user
)
1 change: 0 additions & 1 deletion service/account/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dinoallo/sealos-networkmanager-protoapi v0.0.0-20230928031328-cf9649d6af49 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
Expand Down

0 comments on commit 9ff29cd

Please sign in to comment.