Skip to content

Commit

Permalink
Integrate IBMPowerVSImage with MachineSpec (#552)
Browse files Browse the repository at this point in the history
Signed-off-by: Prajyot-Parab <[email protected]>
  • Loading branch information
Prajyot-Parab authored Feb 13, 2022
1 parent 2c13255 commit bdcbc3d
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 45 deletions.
65 changes: 61 additions & 4 deletions api/v1alpha4/ibmpowervs_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1alpha4
import (
apiconversion "k8s.io/apimachinery/pkg/conversion"
"sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta1"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

Expand Down Expand Up @@ -49,13 +50,36 @@ func (dst *IBMPowerVSClusterList) ConvertFrom(srcRaw conversion.Hub) error {
func (src *IBMPowerVSMachine) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*v1beta1.IBMPowerVSMachine)

return Convert_v1alpha4_IBMPowerVSMachine_To_v1beta1_IBMPowerVSMachine(src, dst, nil)
if err := Convert_v1alpha4_IBMPowerVSMachine_To_v1beta1_IBMPowerVSMachine(src, dst, nil); err != nil {
return err
}

dst.Spec.Image = (*v1beta1.IBMPowerVSResourceReference)(&src.Spec.Image)

return nil
}

func (dst *IBMPowerVSMachine) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*v1beta1.IBMPowerVSMachine)

return Convert_v1beta1_IBMPowerVSMachine_To_v1alpha4_IBMPowerVSMachine(src, dst, nil)
if err := Convert_v1beta1_IBMPowerVSMachine_To_v1alpha4_IBMPowerVSMachine(src, dst, nil); err != nil {
return err
}

// Preserve Hub data on down-conversion except for metadata
if err := utilconversion.MarshalData(src, dst); err != nil {
return err
}

if src.Spec.Image == nil && src.Spec.ImageRef != nil {
dst.Spec.Image.Name = &src.Spec.ImageRef.Name
}

if src.Spec.Image != nil && src.Spec.ImageRef == nil {
dst.Spec.Image = IBMPowerVSResourceReference(*src.Spec.Image)
}

return nil
}

func (src *IBMPowerVSMachineList) ConvertTo(dstRaw conversion.Hub) error {
Expand All @@ -73,13 +97,36 @@ func (dst *IBMPowerVSMachineList) ConvertFrom(srcRaw conversion.Hub) error {
func (src *IBMPowerVSMachineTemplate) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*v1beta1.IBMPowerVSMachineTemplate)

return Convert_v1alpha4_IBMPowerVSMachineTemplate_To_v1beta1_IBMPowerVSMachineTemplate(src, dst, nil)
if err := Convert_v1alpha4_IBMPowerVSMachineTemplate_To_v1beta1_IBMPowerVSMachineTemplate(src, dst, nil); err != nil {
return err
}

dst.Spec.Template.Spec.Image = (*v1beta1.IBMPowerVSResourceReference)(&src.Spec.Template.Spec.Image)

return nil
}

func (dst *IBMPowerVSMachineTemplate) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*v1beta1.IBMPowerVSMachineTemplate)

return Convert_v1beta1_IBMPowerVSMachineTemplate_To_v1alpha4_IBMPowerVSMachineTemplate(src, dst, nil)
if err := Convert_v1beta1_IBMPowerVSMachineTemplate_To_v1alpha4_IBMPowerVSMachineTemplate(src, dst, nil); err != nil {
return err
}

// Preserve Hub data on down-conversion except for metadata
if err := utilconversion.MarshalData(src, dst); err != nil {
return err
}

if src.Spec.Template.Spec.Image == nil && src.Spec.Template.Spec.ImageRef != nil {
dst.Spec.Template.Spec.Image.Name = &src.Spec.Template.Spec.ImageRef.Name
}

if src.Spec.Template.Spec.Image != nil && src.Spec.Template.Spec.ImageRef == nil {
dst.Spec.Template.Spec.Image = IBMPowerVSResourceReference(*src.Spec.Template.Spec.Image)
}

return nil
}

func (src *IBMPowerVSMachineTemplateList) ConvertTo(dstRaw conversion.Hub) error {
Expand All @@ -99,3 +146,13 @@ func (dst *IBMPowerVSMachineTemplateList) ConvertFrom(srcRaw conversion.Hub) err
func Convert_v1beta1_IBMPowerVSMachineStatus_To_v1alpha4_IBMPowerVSMachineStatus(in *v1beta1.IBMPowerVSMachineStatus, out *IBMPowerVSMachineStatus, s apiconversion.Scope) error {
return autoConvert_v1beta1_IBMPowerVSMachineStatus_To_v1alpha4_IBMPowerVSMachineStatus(in, out, s)
}

// Convert_v1beta1_IBMPowerVSMachineSpec_To_v1alpha4_IBMPowerVSMachineSpec is an autogenerated conversion function.
// Requires manual conversion as ImageRef does not exist in v1alpha4 version of IBMPowerVSMachineSpec.
func Convert_v1beta1_IBMPowerVSMachineSpec_To_v1alpha4_IBMPowerVSMachineSpec(in *v1beta1.IBMPowerVSMachineSpec, out *IBMPowerVSMachineSpec, s apiconversion.Scope) error {
return autoConvert_v1beta1_IBMPowerVSMachineSpec_To_v1alpha4_IBMPowerVSMachineSpec(in, out, s)
}

func Convert_v1alpha4_IBMPowerVSMachineSpec_To_v1beta1_IBMPowerVSMachineSpec(in *IBMPowerVSMachineSpec, out *v1beta1.IBMPowerVSMachineSpec, s apiconversion.Scope) error {
return autoConvert_v1alpha4_IBMPowerVSMachineSpec_To_v1beta1_IBMPowerVSMachineSpec(in, out, s)
}
63 changes: 35 additions & 28 deletions api/v1alpha4/zz_generated.conversion.go

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

5 changes: 5 additions & 0 deletions api/v1beta1/conditions_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ const (
InstanceReadyCondition clusterv1.ConditionType = "InstanceReady"
)

const (
// WaitingForIBMPowerVSImageReason used when machine is waiting for powervs image to be ready before proceeding.
WaitingForIBMPowerVSImageReason = "WaitingForIBMPowerVSImage"
)

const (
// ImageNotReadyReason used when the image is in a queued state.
ImageNotReadyReason = "ImageNotReady"
Expand Down
8 changes: 7 additions & 1 deletion api/v1beta1/ibmpowervsmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ type IBMPowerVSMachineSpec struct {
SSHKey string `json:"sshKey,omitempty"`

// Image is the reference to the Image from which to create the machine instance.
Image IBMPowerVSResourceReference `json:"image"`
// +optional
Image *IBMPowerVSResourceReference `json:"image,omitempty"`

// ImageRef is an optional reference to a provider-specific resource that holds
// the details for provisioning the Image for a Cluster.
// +optional
ImageRef *v1.ObjectReference `json:"imageRef,omitempty"`

// SysType is the System type used to host the vsi
// +optional
Expand Down
14 changes: 12 additions & 2 deletions api/v1beta1/ibmpowervsmachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,18 @@ func (r *IBMPowerVSMachine) validateIBMPowerVSMachineNetwork() *field.Error {
}

func (r *IBMPowerVSMachine) validateIBMPowerVSMachineImage() *field.Error {
if res, err := validateIBMPowerVSResourceReference(r.Spec.Image, "Image"); !res {
return err
if r.Spec.Image == nil && r.Spec.ImageRef == nil {
return field.Invalid(field.NewPath(""), "", "One of - Image or ImageRef must be specified")
}

if r.Spec.Image != nil && r.Spec.ImageRef != nil {
return field.Invalid(field.NewPath(""), "", "Only one of - Image or ImageRef maybe be specified")
}

if r.Spec.Image != nil {
if res, err := validateIBMPowerVSResourceReference(*r.Spec.Image, "Image"); !res {
return err
}
}
return nil
}
17 changes: 15 additions & 2 deletions api/v1beta1/ibmpowervsmachinetemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,21 @@ func (r *IBMPowerVSMachineTemplate) validateIBMPowerVSMachineTemplateNetwork() *
}

func (r *IBMPowerVSMachineTemplate) validateIBMPowerVSMachineTemplateImage() *field.Error {
if res, err := validateIBMPowerVSResourceReference(r.Spec.Template.Spec.Image, "Image"); !res {
return err
mt := r.Spec.Template

if mt.Spec.Image == nil && mt.Spec.ImageRef == nil {
return field.Invalid(field.NewPath(""), "", "One of - Image or ImageRef must be specified")
}

if mt.Spec.Image != nil && mt.Spec.ImageRef != nil {
return field.Invalid(field.NewPath(""), "", "Only one of - Image or ImageRef maybe be specified")
}

if mt.Spec.Image != nil {
if res, err := validateIBMPowerVSResourceReference(*mt.Spec.Image, "Image"); !res {
return err
}
}

return nil
}
11 changes: 10 additions & 1 deletion api/v1beta1/zz_generated.deepcopy.go

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

18 changes: 13 additions & 5 deletions cloud/scope/powervs_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type PowerVSMachineScopeParams struct {
Machine *clusterv1.Machine
IBMPowerVSCluster *v1beta1.IBMPowerVSCluster
IBMPowerVSMachine *v1beta1.IBMPowerVSMachine
IBMPowerVSImage *v1beta1.IBMPowerVSImage
}

// PowerVSMachineScope defines a scope defined around a Power VS Machine.
Expand All @@ -69,6 +70,7 @@ type PowerVSMachineScope struct {
Machine *clusterv1.Machine
IBMPowerVSCluster *v1beta1.IBMPowerVSCluster
IBMPowerVSMachine *v1beta1.IBMPowerVSMachine
IBMPowerVSImage *v1beta1.IBMPowerVSImage
}

// NewPowerVSMachineScope creates a new PowerVSMachineScope from the supplied parameters.
Expand All @@ -94,11 +96,12 @@ func NewPowerVSMachineScope(params PowerVSMachineScopeParams) (scope *PowerVSMac
scope.Cluster = params.Cluster

if params.IBMPowerVSMachine == nil {
err = errors.New("aws machine is required when creating a MachineScope")
err = errors.New("PowerVS machine is required when creating a MachineScope")
return
}
scope.IBMPowerVSMachine = params.IBMPowerVSMachine
scope.IBMPowerVSCluster = params.IBMPowerVSCluster
scope.IBMPowerVSImage = params.IBMPowerVSImage

if params.Logger == (logr.Logger{}) {
params.Logger = klogr.New()
Expand Down Expand Up @@ -203,9 +206,14 @@ func (m *PowerVSMachineScope) CreateMachine() (*models.PVMInstanceReference, err
return nil, fmt.Errorf("failed to convert Processors(%s) to float64", s.Processors)
}

imageID, err := getImageID(s.Image, m)
if err != nil {
return nil, fmt.Errorf("error getting image ID: %v", err)
var imageID *string
if m.IBMPowerVSImage != nil {
imageID = &m.IBMPowerVSImage.Status.ImageID
} else {
imageID, err = getImageID(s.Image, m)
if err != nil {
return nil, fmt.Errorf("error getting image ID: %v", err)
}
}

networkID, err := getNetworkID(s.Network, m)
Expand Down Expand Up @@ -273,7 +281,7 @@ func (m *PowerVSMachineScope) GetBootstrapData() (string, error) {
return base64.StdEncoding.EncodeToString(value), nil
}

func getImageID(image v1beta1.IBMPowerVSResourceReference, m *PowerVSMachineScope) (*string, error) {
func getImageID(image *v1beta1.IBMPowerVSResourceReference, m *PowerVSMachineScope) (*string, error) {
if image.ID != nil {
return image.ID, nil
} else if image.Name != nil {
Expand Down
Loading

0 comments on commit bdcbc3d

Please sign in to comment.