Skip to content

Commit

Permalink
Refactor PVS Specs: change Memory to MemoryGiB
Browse files Browse the repository at this point in the history
Signed-off-by: Prajyot-Parab <prajyot.parab2@ibm.com>
  • Loading branch information
Prajyot-Parab committed Jan 17, 2023
1 parent a097ad1 commit 5fbf20c
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 252 deletions.
13 changes: 11 additions & 2 deletions api/v1beta1/ibmpowervs_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1beta1

import (
"fmt"
"strconv"
"strings"

apiconversion "k8s.io/apimachinery/pkg/conversion"
Expand Down Expand Up @@ -149,7 +151,13 @@ func (dst *IBMPowerVSImageList) ConvertFrom(srcRaw conversion.Hub) error {

func Convert_v1beta1_IBMPowerVSMachineSpec_To_v1beta2_IBMPowerVSMachineSpec(in *IBMPowerVSMachineSpec, out *infrav1beta2.IBMPowerVSMachineSpec, s apiconversion.Scope) error {
out.SystemType = in.SysType
out.Processors.StrVal = in.Processors
out.Processors = intstr.FromString(in.Processors)

memory, err := strconv.ParseFloat(in.Memory, 64)
if err != nil {
return fmt.Errorf("failed to convert memory(%s) to float64", in.Memory)
}
out.MemoryGiB = int32(memory)

switch in.ProcType {
case strings.ToLower(string(infrav1beta2.PowerVSProcessorTypeDedicated)):
Expand All @@ -165,10 +173,11 @@ func Convert_v1beta1_IBMPowerVSMachineSpec_To_v1beta2_IBMPowerVSMachineSpec(in *

func Convert_v1beta2_IBMPowerVSMachineSpec_To_v1beta1_IBMPowerVSMachineSpec(in *infrav1beta2.IBMPowerVSMachineSpec, out *IBMPowerVSMachineSpec, s apiconversion.Scope) error {
out.SysType = in.SystemType
out.Memory = strconv.FormatInt(int64(in.MemoryGiB), 10)

switch in.Processors.Type {
case intstr.Int:
out.Processors = string(in.Processors.IntVal)
out.Processors = strconv.FormatInt(int64(in.Processors.IntVal), 10)
case intstr.String:
out.Processors = in.Processors.StrVal
}
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta1/zz_generated.conversion.go

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

24 changes: 7 additions & 17 deletions api/v1beta2/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
)

func defaultIBMPowerVSMachineSpec(spec *IBMPowerVSMachineSpec) {
if spec.Memory == "" {
spec.Memory = "4"
if spec.MemoryGiB == 0 {
spec.MemoryGiB = 2
}
if spec.Processors.StrVal == "" && spec.Processors.IntVal == 0 {
spec.Processors = intstr.FromString("0.5")
spec.Processors = intstr.FromString("0.25")
}
if spec.SystemType == "" {
spec.SystemType = "s922"
Expand All @@ -38,16 +38,6 @@ func defaultIBMPowerVSMachineSpec(spec *IBMPowerVSMachineSpec) {
}
}

func validateIBMPowerVSSysType(spec IBMPowerVSMachineSpec) (bool, IBMPowerVSMachineSpec) {
sysTypes := [...]string{"s922", "e980"}
for _, st := range sysTypes {
if spec.SystemType == st {
return true, IBMPowerVSMachineSpec{}
}
}
return false, spec
}

func validateIBMPowerVSResourceReference(res IBMPowerVSResourceReference, resType string) (bool, *field.Error) {
if res.ID != nil && res.Name != nil {
return false, field.Invalid(field.NewPath("spec", resType), res, "Only one of "+resType+" - ID or Name may be specified")
Expand All @@ -62,8 +52,8 @@ func validateIBMPowerVSNetworkReference(res IBMPowerVSResourceReference) (bool,
return true, nil
}

func validateIBMPowerVSMemoryValues(resValue string) bool {
if val, err := strconv.ParseUint(resValue, 10, 64); err != nil || val < 2 {
func validateIBMPowerVSMemoryValues(resValue int32) bool {
if val := float64(resValue); val < 2 {
return false
}
return true
Expand All @@ -72,11 +62,11 @@ func validateIBMPowerVSMemoryValues(resValue string) bool {
func validateIBMPowerVSProcessorValues(resValue intstr.IntOrString) bool {
switch resValue.Type {
case intstr.Int:
if val := float64(resValue.IntVal); val < 0.5 {
if val := float64(resValue.IntVal); val < 0.25 {
return false
}
case intstr.String:
if val, err := strconv.ParseFloat(resValue.StrVal, 64); err != nil || val < 0.5 {
if val, err := strconv.ParseFloat(resValue.StrVal, 64); err != nil || val < 0.25 {
return false
}
}
Expand Down
21 changes: 8 additions & 13 deletions api/v1beta2/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

func TestValidateIBMPowerVSMemoryValues(t *testing.T) {
type args struct {
n string
n int32
}
tests := []struct {
name string
Expand All @@ -33,27 +33,22 @@ func TestValidateIBMPowerVSMemoryValues(t *testing.T) {
}{
{
name: "N is 4",
args: args{n: "4"},
args: args{n: 4},
want: true,
},
{
name: "N is 10",
args: args{n: "10"},
args: args{n: 10},
want: true,
},
{
name: "N is 1",
args: args{n: "1"},
args: args{n: 1},
want: false,
},
{
name: "N is 1.25",
args: args{n: "1.25"},
want: false,
},
{
name: "N is abc",
args: args{n: "abc"},
name: "N is -2",
args: args{n: -2},
want: false,
},
}
Expand All @@ -76,8 +71,8 @@ func TestValidateIBMPowerVSProcessorValues(t *testing.T) {
want bool
}{
{
name: "N is 0.75",
args: args{n: intstr.FromString("0.75")},
name: "N is 0.25",
args: args{n: intstr.FromString("0.25")},
want: true,
},
{
Expand Down
16 changes: 12 additions & 4 deletions api/v1beta2/ibmpowervsmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type IBMPowerVSMachineSpec struct {
// When omitted, this means that the user has no opinion and the platform is left to choose a
// reasonable default, which is subject to change over time. The current default is s922 which is generally available.
// + This is not an enum because we expect other values to be added later which should be supported implicitly.
// +kubebuilder:validation:Enum:="s922";"e880";"e980";""
// +optional
SystemType string `json:"systemType,omitempty"`

Expand All @@ -92,18 +93,25 @@ type IBMPowerVSMachineSpec struct {
// when SystemType is set to e880 or e980 maximum Processors value is 143.
// when SystemType is set to s922 maximum Processors value is 15.
// minimum value for Processors depends on the selected ProcessorType.
// when ProcessorType is set as Shared or Capped, The minimum processors is 0.5.
// when ProcessorType is set as Shared or Capped, The minimum processors is 0.25.
// when ProcessorType is set as Dedicated, The minimum processors is 1.
// When omitted, this means that the user has no opinion and the platform is left to choose a
// reasonable default, which is subject to change over time. The default is set based on the selected ProcessorType.
// when ProcessorType selected as Dedicated, the default is set to 1.
// when ProcessorType selected as Shared or Capped, the default is set to 0.5.
// when ProcessorType selected as Shared or Capped, the default is set to 0.25.
// +optional
Processors intstr.IntOrString `json:"processors,omitempty"`

// Memory is Amount of memory allocated (in GB)
// memoryGiB is the size of a virtual machine's memory, in GiB.
// maximum value for the MemoryGiB depends on the selected SystemType.
// when SystemType is set to e880 maximum MemoryGiB value is 7463 GiB.
// when SystemType is set to e980 maximum MemoryGiB value is 15307 GiB.
// when SystemType is set to s922 maximum MemoryGiB value is 942 GiB.
// The minimum memory is 2 GiB.
// When omitted, this means the user has no opinion and the platform is left to choose a reasonable
// default, which is subject to change over time. The current default is 2.
// +optional
Memory string `json:"memory,omitempty"`
MemoryGiB int32 `json:"memoryGiB,omitempty"`

// Network is the reference to the Network to use for this instance.
Network IBMPowerVSResourceReference `json:"network"`
Expand Down
16 changes: 3 additions & 13 deletions api/v1beta2/ibmpowervsmachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ func (r *IBMPowerVSMachine) ValidateDelete() error {

func (r *IBMPowerVSMachine) validateIBMPowerVSMachine() error {
var allErrs field.ErrorList
if err := r.validateIBMPowerVSMachineSysType(); err != nil {
allErrs = append(allErrs, err)
}
if err := r.validateIBMPowerVSMachineNetwork(); err != nil {
allErrs = append(allErrs, err)
}
Expand All @@ -94,13 +91,6 @@ func (r *IBMPowerVSMachine) validateIBMPowerVSMachine() error {
r.Name, allErrs)
}

func (r *IBMPowerVSMachine) validateIBMPowerVSMachineSysType() *field.Error {
if res, spec := validateIBMPowerVSSysType(r.Spec); !res {
return field.Invalid(field.NewPath("spec", "systemType"), spec.SystemType, "Invalid System Type")
}
return nil
}

func (r *IBMPowerVSMachine) validateIBMPowerVSMachineNetwork() *field.Error {
if res, err := validateIBMPowerVSNetworkReference(r.Spec.Network); !res {
return err
Expand All @@ -126,15 +116,15 @@ func (r *IBMPowerVSMachine) validateIBMPowerVSMachineImage() *field.Error {
}

func (r *IBMPowerVSMachine) validateIBMPowerVSMachineMemory() *field.Error {
if res := validateIBMPowerVSMemoryValues(r.Spec.Memory); !res {
return field.Invalid(field.NewPath("spec", "memory"), r.Spec.Memory, "Invalid Memory value - must be non-empty and a positive integer no lesser than 2")
if res := validateIBMPowerVSMemoryValues(r.Spec.MemoryGiB); !res {
return field.Invalid(field.NewPath("spec", "memoryGiB"), r.Spec.MemoryGiB, "Invalid Memory value - must a positive integer no lesser than 2")
}
return nil
}

func (r *IBMPowerVSMachine) validateIBMPowerVSMachineProcessors() *field.Error {
if res := validateIBMPowerVSProcessorValues(r.Spec.Processors); !res {
return field.Invalid(field.NewPath("spec", "processors"), r.Spec.Processors, "Invalid Processors value - must be non-empty and positive floating-point number no lesser than 0.5")
return field.Invalid(field.NewPath("spec", "processors"), r.Spec.Processors, "Invalid Processors value - must be non-empty and positive floating-point number no lesser than 0.25")
}
return nil
}
Loading

0 comments on commit 5fbf20c

Please sign in to comment.