-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use resource package for k8s values and add code for conversion for v…
…alues accepted/returned by bridge API. Co-authored-by: Chris Bandy <[email protected]>
- Loading branch information
Showing
8 changed files
with
140 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ spec: | |
provider: aws | ||
region: us-east-2 | ||
secret: crunchy-bridge-api-key | ||
storage: 10G | ||
storage: 10Gi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright 2024 Crunchy Data Solutions, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package bridge | ||
|
||
import ( | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/api/resource" | ||
) | ||
|
||
func FromCPU(n int64) *resource.Quantity { | ||
// Assume the Bridge API returns numbers that can be parsed by the | ||
// [resource] package. | ||
if q, err := resource.ParseQuantity(fmt.Sprint(n)); err == nil { | ||
return &q | ||
} | ||
|
||
return resource.NewQuantity(0, resource.DecimalSI) | ||
} | ||
|
||
// FromGibibytes returns n gibibytes as a [resource.Quantity]. | ||
func FromGibibytes(n int64) *resource.Quantity { | ||
// Assume the Bridge API returns numbers that can be parsed by the | ||
// [resource] package. | ||
if q, err := resource.ParseQuantity(fmt.Sprint(n) + "Gi"); err == nil { | ||
return &q | ||
} | ||
|
||
return resource.NewQuantity(0, resource.BinarySI) | ||
} | ||
|
||
// ToGibibytes returns q rounded up to a non-negative gibibyte. | ||
func ToGibibytes(q resource.Quantity) int64 { | ||
v := q.Value() | ||
|
||
if v <= 0 { | ||
return 0 | ||
} | ||
|
||
// https://stackoverflow.com/a/2745086 | ||
return 1 + ((v - 1) >> 30) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright 2024 Crunchy Data Solutions, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package bridge | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
) | ||
|
||
func TestFromCPU(t *testing.T) { | ||
zero := FromCPU(0) | ||
assert.Assert(t, zero.IsZero()) | ||
assert.Equal(t, zero.String(), "0") | ||
|
||
one := FromCPU(1) | ||
assert.Equal(t, one.String(), "1") | ||
|
||
negative := FromCPU(-2) | ||
assert.Equal(t, negative.String(), "-2") | ||
} | ||
|
||
func TestFromGibibytes(t *testing.T) { | ||
zero := FromGibibytes(0) | ||
assert.Assert(t, zero.IsZero()) | ||
assert.Equal(t, zero.String(), "0") | ||
|
||
one := FromGibibytes(1) | ||
assert.Equal(t, one.String(), "1Gi") | ||
|
||
negative := FromGibibytes(-2) | ||
assert.Equal(t, negative.String(), "-2Gi") | ||
} | ||
|
||
func TestToGibibytes(t *testing.T) { | ||
zero := resource.MustParse("0") | ||
assert.Equal(t, ToGibibytes(zero), int64(0)) | ||
|
||
// Negative quantities become zero. | ||
negative := resource.MustParse("-4G") | ||
assert.Equal(t, ToGibibytes(negative), int64(0)) | ||
|
||
// Decimal quantities round up. | ||
decimal := resource.MustParse("9000M") | ||
assert.Equal(t, ToGibibytes(decimal), int64(9)) | ||
|
||
// Binary quantities round up. | ||
binary := resource.MustParse("8000Mi") | ||
assert.Equal(t, ToGibibytes(binary), int64(8)) | ||
|
||
fourGi := resource.MustParse("4096Mi") | ||
assert.Equal(t, ToGibibytes(fourGi), int64(4)) | ||
|
||
moreThanFourGi := resource.MustParse("4097Mi") | ||
assert.Equal(t, ToGibibytes(moreThanFourGi), int64(5)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
pkg/apis/postgres-operator.crunchydata.com/v1beta1/zz_generated.deepcopy.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.