Skip to content

Commit

Permalink
Add IBM Power VS: tfvars
Browse files Browse the repository at this point in the history
For more background on IPI on Power VS, refer to the enhancement
proposal here: openshift/enhancements#736

Older discussions on some of the code here can be found in openshift#5224

Signed-off-by: Christy Norman <christy@linux.vnet.ibm.com>
  • Loading branch information
clnperez committed Mar 24, 2022
1 parent ca8aacd commit 8fc6f93
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
43 changes: 43 additions & 0 deletions pkg/asset/cluster/tfvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
ibmcloudprovider "github.com/openshift/cluster-api-provider-ibmcloud/pkg/apis/ibmcloudprovider/v1beta1"
libvirtprovider "github.com/openshift/cluster-api-provider-libvirt/pkg/apis/libvirtproviderconfig/v1beta1"
ovirtprovider "github.com/openshift/cluster-api-provider-ovirt/pkg/apis/ovirtprovider/v1beta1"
powervsprovider "github.com/openshift/machine-api-provider-powervs/pkg/apis/powervsprovider/v1alpha1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
awsprovider "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1beta1"
Expand Down Expand Up @@ -44,6 +45,7 @@ import (
libvirttfvars "github.com/openshift/installer/pkg/tfvars/libvirt"
openstacktfvars "github.com/openshift/installer/pkg/tfvars/openstack"
ovirttfvars "github.com/openshift/installer/pkg/tfvars/ovirt"
powervstfvars "github.com/openshift/installer/pkg/tfvars/powervs"
vspheretfvars "github.com/openshift/installer/pkg/tfvars/vsphere"
"github.com/openshift/installer/pkg/types"
"github.com/openshift/installer/pkg/types/alibabacloud"
Expand All @@ -56,6 +58,7 @@ import (
"github.com/openshift/installer/pkg/types/none"
"github.com/openshift/installer/pkg/types/openstack"
"github.com/openshift/installer/pkg/types/ovirt"
"github.com/openshift/installer/pkg/types/powervs"
"github.com/openshift/installer/pkg/types/vsphere"
)

Expand Down Expand Up @@ -672,6 +675,46 @@ func (t *TerraformVariables) Generate(parents asset.Parents) error {
Filename: TfPlatformVarsFileName,
Data: data,
})
case powervs.Name:
masters, err := mastersAsset.Machines()
if err != nil {
return err
}

// Get CISInstanceCRN from InstallConfig metadata
crn, err := installConfig.PowerVS.CISInstanceCRN(ctx)
if err != nil {
return err
}

masterConfigs := make([]*powervsprovider.PowerVSMachineProviderConfig, len(masters))
for i, m := range masters {
masterConfigs[i] = m.Spec.ProviderSpec.Value.Object.(*powervsprovider.PowerVSMachineProviderConfig)
}

data, err = powervstfvars.TFVars(
powervstfvars.TFVarsSources{
MasterConfigs: masterConfigs,
Region: installConfig.Config.Platform.PowerVS.Region,
Zone: installConfig.Config.Platform.PowerVS.Zone,
APIKey: os.Getenv("IC_API_KEY"),
SSHKey: installConfig.Config.SSHKey,
PowerVSResourceGroup: installConfig.Config.PowerVS.PowerVSResourceGroup,
ImageBucketFileName: string(*rhcosImage),
NetworkName: installConfig.Config.PowerVS.PVSNetworkName,
CISInstanceCRN: crn,
VPCSubnetName: installConfig.Config.PowerVS.Subnets[0],
VPCName: installConfig.Config.PowerVS.VPC,
},
)
if err != nil {
return errors.Wrapf(err, "failed to get %s Terraform variables", platform)
}
t.FileList = append(t.FileList, &asset.File{
Filename: TfPlatformVarsFileName,
Data: data,
})

case vsphere.Name:
controlPlanes, err := mastersAsset.Machines()
if err != nil {
Expand Down
89 changes: 89 additions & 0 deletions pkg/tfvars/powervs/powervs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Package powervs contains Power Virtual Servers-specific Terraform-variable logic.
package powervs

import (
"encoding/json"
"fmt"
"math/rand"
"time"

"github.com/openshift/installer/pkg/types/powervs"
"github.com/openshift/machine-api-provider-powervs/pkg/apis/powervsprovider/v1alpha1"
)

type config struct {
ServiceInstanceID string `json:"powervs_cloud_instance_id"`
APIKey string `json:"powervs_api_key"`
SSHKey string `json:"powervs_ssh_key"`
PowerVSRegion string `json:"powervs_region"`
PowerVSZone string `json:"powervs_zone"`
VPCRegion string `json:"powervs_vpc_region"`
VPCZone string `json:"powervs_vpc_zone"`
PowerVSResourceGroup string `json:"powervs_resource_group"`
CISInstanceCRN string `json:"powervs_cis_crn"`
ImageBucketFileName string `json:"powervs_image_bucket_file_name"`
NetworkName string `json:"powervs_network_name"`
VPCName string `json:"powervs_vpc_name"`
VPCSubnetName string `json:"powervs_vpc_subnet_name"`
BootstrapMemory string `json:"powervs_bootstrap_memory"`
BootstrapProcessors string `json:"powervs_bootstrap_processors"`
MasterMemory string `json:"powervs_master_memory"`
MasterProcessors string `json:"powervs_master_processors"`
ProcType string `json:"powervs_proc_type"`
SysType string `json:"powervs_sys_type"`
}

// TFVarsSources contains the parameters to be converted into Terraform variables
type TFVarsSources struct {
MasterConfigs []*v1alpha1.PowerVSMachineProviderConfig
APIKey string
SSHKey string
Region string
Zone string
ImageBucketFileName string
NetworkName string
PowerVSResourceGroup string
VPCZone string
CISInstanceCRN string
VPCName string
VPCSubnetName string
}

// TFVars generates Power VS-specific Terraform variables launching the cluster.
func TFVars(sources TFVarsSources) ([]byte, error) {
masterConfig := sources.MasterConfigs[0]
vpcRegion := powervs.Regions[sources.Region].VPCRegion

vpcZone := sources.VPCZone
if vpcZone == "" {
// Randomly select a zone in the VPC region.
// @TODO: Align this with a region later.
rand.Seed(time.Now().UnixNano())
// All supported Regions are MZRs and have Zones named "region-[1-3]"
vpcZone = fmt.Sprintf("%s-%d", vpcRegion, rand.Intn(2)+1)
}

cfg := &config{
ServiceInstanceID: masterConfig.ServiceInstanceID,
APIKey: sources.APIKey,
SSHKey: sources.SSHKey,
PowerVSRegion: sources.Region,
PowerVSZone: sources.Zone,
VPCRegion: vpcRegion,
VPCZone: vpcZone,
PowerVSResourceGroup: sources.PowerVSResourceGroup,
CISInstanceCRN: sources.CISInstanceCRN,
ImageBucketFileName: sources.ImageBucketFileName,
NetworkName: *masterConfig.Network.Name,
VPCName: sources.VPCName,
VPCSubnetName: sources.VPCSubnetName,
BootstrapMemory: masterConfig.Memory,
BootstrapProcessors: masterConfig.Processors,
MasterMemory: masterConfig.Memory,
MasterProcessors: masterConfig.Processors,
ProcType: masterConfig.ProcType,
SysType: masterConfig.SysType,
}

return json.MarshalIndent(cfg, "", " ")
}

0 comments on commit 8fc6f93

Please sign in to comment.