Skip to content

Commit

Permalink
refactor: more move things around
Browse files Browse the repository at this point in the history
  • Loading branch information
tenstad committed Feb 7, 2025
1 parent 309bb06 commit 1be3be6
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 78 deletions.
50 changes: 1 addition & 49 deletions apis/user/v1alpha1/groupqualityofservicelimits_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,12 @@ package v1alpha1
import (
"reflect"

resource "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
)

// +kubebuilder:validation:Pattern=`^(0|((0|[1-9][0-9]*)[KMGT]i))$`
type Quantity string

func (q *Quantity) ToKiB() (*int64, error) {
if q == nil {
return nil, nil
}

rq, err := resource.ParseQuantity(string(*q))
if err != nil {
return nil, err
}

i := rq.ScaledValue(0) / 1024
return &i, nil
}

// QualityOfService configures data limits. The value -1 indicates unlimited.
type QualityOfServiceLimits struct {
// StorageQuotaBytes is the limit for total stored data in bytes.
// +optional
// +nullable
StorageQuotaBytes *Quantity `json:"storageQuotaBytes"`
// StorageQuotaCount is the limit for total number of objects.
// +optional
// +nullable
StorageQuotaCount *uint32 `json:"storageQuotaCount"`
// RequestsPerMin is the limit for number of HTTP requests per minute.
// +optional
// +nullable
RequestsPerMin *uint32 `json:"requestsPerMin"`
// InboundBytesPerMin is the limit for inbound data per minute in bytes.
// +optional
// +nullable
InboundBytesPerMin *Quantity `json:"inboundBytesPerMin"`
// OutboundKiBsPerMin is the limit for outbound data per minute in bytes.
// +optional
// +nullable
OutboundBytesPerMin *Quantity `json:"outboundBytesPerMin"`
}

// GroupQualityOfServiceLimitsParameters are the configurable fields of a GroupQualityOfServiceLimits.
type GroupQualityOfServiceLimitsParameters struct {
// GroupID of the quality of service limits.
Expand All @@ -87,13 +45,7 @@ type GroupQualityOfServiceLimitsParameters struct {
// +optional
Region string `json:"region,omitempty"`

// Warning is the soft limit that triggers a warning.
// +optional
Warning *QualityOfServiceLimits `json:"warning,omitempty"`

// Hard is the hard limit.
// +optional
Hard *QualityOfServiceLimits `json:"hard,omitempty"`
QOS `json:",inline"`
}

// GroupQualityOfServiceLimitsObservation are the observable fields of a GroupQualityOfServiceLimits.
Expand Down
54 changes: 54 additions & 0 deletions apis/user/v1alpha1/qos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package v1alpha1

import resource "k8s.io/apimachinery/pkg/api/resource"

// +kubebuilder:validation:Pattern=`^(0|((0|[1-9][0-9]*)[KMGT]i))$`
type Quantity string

func (q *Quantity) ToKiB() (*int64, error) {
if q == nil {
return nil, nil
}

rq, err := resource.ParseQuantity(string(*q))
if err != nil {
return nil, err
}

i := rq.ScaledValue(0) / 1024
return &i, nil
}

// QualityOfService configures data limits. The value -1 indicates unlimited.
type QualityOfServiceLimits struct {
// StorageQuotaBytes is the limit for total stored data in bytes.
// +optional
// +nullable
StorageQuotaBytes *Quantity `json:"storageQuotaBytes"`
// StorageQuotaCount is the limit for total number of objects.
// +optional
// +nullable
StorageQuotaCount *uint32 `json:"storageQuotaCount"`
// RequestsPerMin is the limit for number of HTTP requests per minute.
// +optional
// +nullable
RequestsPerMin *uint32 `json:"requestsPerMin"`
// InboundBytesPerMin is the limit for inbound data per minute in bytes.
// +optional
// +nullable
InboundBytesPerMin *Quantity `json:"inboundBytesPerMin"`
// OutboundKiBsPerMin is the limit for outbound data per minute in bytes.
// +optional
// +nullable
OutboundBytesPerMin *Quantity `json:"outboundBytesPerMin"`
}

type QOS struct {
// Warning is the soft limit that triggers a warning.
// +optional
Warning *QualityOfServiceLimits `json:"warning,omitempty"`

// Hard is the hard limit.
// +optional
Hard *QualityOfServiceLimits `json:"hard,omitempty"`
}
36 changes: 26 additions & 10 deletions apis/user/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

"github.com/statnett/provider-cloudian/apis/user/v1alpha1"
apisv1alpha1 "github.com/statnett/provider-cloudian/apis/v1alpha1"
qosapi "github.com/statnett/provider-cloudian/internal/controller/qualityofservicelimits"
qoslimits "github.com/statnett/provider-cloudian/internal/controller/qualityofservicelimits"
"github.com/statnett/provider-cloudian/internal/features"
"github.com/statnett/provider-cloudian/internal/sdk/cloudian"
)
Expand Down Expand Up @@ -166,7 +166,7 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex

cr.SetConditions(xpv1.Available())

expected, err := qosapi.ToCloudianQOS(cr.Spec.ForProvider.Warning, cr.Spec.ForProvider.Hard)
expected, err := qoslimits.ToCloudianQOS(cr.Spec.ForProvider.QOS)
if err != nil {
return managed.ExternalObservation{}, err
}
Expand All @@ -180,8 +180,8 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
// Return false when the external resource exists, but it not up to date
// with the desired managed resource state. This lets the managed
// resource reconciler know that it needs to call Update.
ResourceUpToDate: qosapi.LimitsEqual(expected.Warning, qos.Warning) &&
qosapi.LimitsEqual(expected.Hard, qos.Hard),
ResourceUpToDate: expected.Warning.Equal(qos.Warning) &&
expected.Hard.Equal(qos.Hard),

// Return any details that may be required to connect to the external
// resource. These will be stored as the connection secret.
Expand All @@ -195,7 +195,7 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
return managed.ExternalCreation{}, errors.New(errNotGroupQualityOfServiceLimits)
}

qos, err := qosapi.ToCloudianQOS(cr.Spec.ForProvider.Warning, cr.Spec.ForProvider.Hard)
qos, err := qoslimits.ToCloudianQOS(cr.Spec.ForProvider.QOS)
if err != nil {
return managed.ExternalCreation{}, err
}
Expand All @@ -221,7 +221,7 @@ func (c *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext
return managed.ExternalUpdate{}, errors.New(errNotGroupQualityOfServiceLimits)
}

qos, err := qosapi.ToCloudianQOS(cr.Spec.ForProvider.Warning, cr.Spec.ForProvider.Hard)
qos, err := qoslimits.ToCloudianQOS(cr.Spec.ForProvider.QOS)
if err != nil {
return managed.ExternalUpdate{}, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
"k8s.io/utils/ptr"
)

func ToCloudianQOS(warning *v1alpha1.QualityOfServiceLimits, hard *v1alpha1.QualityOfServiceLimits) (cloudian.QualityOfService, error) {
func ToCloudianQOS(qos v1alpha1.QOS) (cloudian.QualityOfService, error) {
var err error
qos := cloudian.QualityOfService{}
cQOS := cloudian.QualityOfService{}

if qos.Warning, err = ToCloudianLimits(warning); err != nil {
if cQOS.Warning, err = ToCloudianLimits(qos.Warning); err != nil {
return cloudian.QualityOfService{}, err
}
if qos.Hard, err = ToCloudianLimits(hard); err != nil {
if cQOS.Hard, err = ToCloudianLimits(qos.Hard); err != nil {
return cloudian.QualityOfService{}, err
}
return qos, nil
return cQOS, nil
}

func ToCloudianLimits(limits *v1alpha1.QualityOfServiceLimits) (cloudian.QualityOfServiceLimits, error) {
Expand Down Expand Up @@ -46,11 +46,3 @@ func ToCloudianLimits(limits *v1alpha1.QualityOfServiceLimits) (cloudian.Quality

return qosl, nil
}

func LimitsEqual(a cloudian.QualityOfServiceLimits, b cloudian.QualityOfServiceLimits) bool {
return ptr.Equal(a.InboundKiBsPerMin, b.InboundKiBsPerMin) &&
ptr.Equal(a.OutboundKiBsPerMin, b.OutboundKiBsPerMin) &&
ptr.Equal(a.RequestsPerMin, b.RequestsPerMin) &&
ptr.Equal(a.StorageQuotaCount, b.StorageQuotaCount) &&
ptr.Equal(a.StorageQuotaKiBs, b.StorageQuotaKiBs)
}
18 changes: 18 additions & 0 deletions internal/sdk/cloudian/qos.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ type QualityOfServiceLimits struct {
OutboundKiBsPerMin *int64
}

func (a QualityOfServiceLimits) Equal(b QualityOfServiceLimits) bool {
// k8s.io/utils/ptr Equal
eq := func(a, b *int64) bool {
if (a == nil) != (b == nil) {
return false
}
if a == nil {
return true
}
return *a == *b
}
return eq(a.InboundKiBsPerMin, b.InboundKiBsPerMin) &&
eq(a.OutboundKiBsPerMin, b.OutboundKiBsPerMin) &&
eq(a.RequestsPerMin, b.RequestsPerMin) &&
eq(a.StorageQuotaCount, b.StorageQuotaCount) &&
eq(a.StorageQuotaKiBs, b.StorageQuotaKiBs)
}

func (qos *QualityOfService) allMinusOne() bool {
return qos.Warning.allMinusOne() && qos.Hard.allMinusOne()
}
Expand Down

0 comments on commit 1be3be6

Please sign in to comment.