Skip to content

Commit

Permalink
MULTIARCH-4096: PowerVS: Add GenerateClusterAssets
Browse files Browse the repository at this point in the history
Generate the cluster assets for the PowerVS CAPI provider.
  • Loading branch information
hamzy committed Mar 7, 2024
1 parent b713070 commit 332402a
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/asset/manifests/clusterapi/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/openshift/installer/pkg/asset/manifests/capiutils"
"github.com/openshift/installer/pkg/asset/manifests/gcp"
"github.com/openshift/installer/pkg/asset/manifests/openstack"
"github.com/openshift/installer/pkg/asset/manifests/powervs"
"github.com/openshift/installer/pkg/asset/manifests/vsphere"
"github.com/openshift/installer/pkg/asset/openshiftinstall"
"github.com/openshift/installer/pkg/asset/rhcos"
Expand All @@ -29,6 +31,7 @@ import (
azuretypes "github.com/openshift/installer/pkg/types/azure"
gcptypes "github.com/openshift/installer/pkg/types/gcp"
openstacktypes "github.com/openshift/installer/pkg/types/openstack"
powervstypes "github.com/openshift/installer/pkg/types/powervs"
vsphereplatform "github.com/openshift/installer/pkg/types/vsphere"
)

Expand Down Expand Up @@ -125,6 +128,16 @@ func (c *Cluster) Generate(dependencies asset.Parents) error {
if err != nil {
return errors.Wrap(err, "failed to generate OpenStack manifests")
}
case powervstypes.Name:
var (
err error
osImage []string
)
osImage = strings.SplitN(string(*rhcosImage), "/", 2)
out, err = powervs.GenerateClusterAssets(installConfig, clusterID, osImage[0], osImage[1])
if err != nil {
return fmt.Errorf("failed to generate PowerVS manifests %w", err)
}
default:
return fmt.Errorf("unsupported platform %q", platform)
}
Expand Down
145 changes: 145 additions & 0 deletions pkg/asset/manifests/powervs/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package powervs

import (
"fmt"
"reflect"

"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
capibm "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"

"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/asset/installconfig"
"github.com/openshift/installer/pkg/asset/manifests/capiutils"
powervstypes "github.com/openshift/installer/pkg/types/powervs"
)

// GenerateClusterAssets generates the manifests for the cluster-api.
func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID *installconfig.ClusterID, bucket string, object string) (*capiutils.GenerateClusterAssetsOutput, error) {
var (
manifests []*asset.RuntimeFile
network string
service capibm.IBMPowerVSResourceReference
resourceGroup string
transitGateway string
cosName string
cosRegion string
err error
powerVSCluster *capibm.IBMPowerVSCluster
)

logrus.Debugf("GenerateClusterAssets: installConfig = %+v, clusterID = %v, bucket = %v, object = %v", installConfig, *clusterID, bucket, object)
logrus.Debugf("GenerateClusterAssets: installConfig.Config.PowerVS = %+v", *installConfig.Config.PowerVS)

manifests = []*asset.RuntimeFile{}

network = fmt.Sprintf("%s-network", clusterID.InfraID)

if installConfig.Config.PowerVS.ServiceInstanceGUID == "" {
serviceName := fmt.Sprintf("%s-iaas", clusterID.InfraID)

service = capibm.IBMPowerVSResourceReference{
Name: &serviceName,
}
} else {
service = capibm.IBMPowerVSResourceReference{
ID: &installConfig.Config.PowerVS.ServiceInstanceGUID,
}
}

if installConfig.Config.Platform.PowerVS.PowerVSResourceGroup != "" {
// @TODO is it a name or id?
resourceGroup = installConfig.Config.Platform.PowerVS.PowerVSResourceGroup
}

transitGateway = fmt.Sprintf("%s-tg", clusterID.InfraID)

cosName = fmt.Sprintf("%s-cos", clusterID.InfraID)

if installConfig.Config.Platform.PowerVS.VPCRegion != "" {
cosRegion, err = powervstypes.COSRegionForVPCRegion(installConfig.Config.Platform.PowerVS.VPCRegion)
if err != nil {
return nil, fmt.Errorf("GenerateClusterAssets: COSRegionForVPCRegion returns %w", err)
}
} else if installConfig.Config.Platform.PowerVS.Region != "" {
cosRegion = installConfig.Config.Platform.PowerVS.Region
} else {
return nil, fmt.Errorf("GenerateClusterAssets: Region is empty")
}
logrus.Debugf("GenerateClusterAssets: cosRegion = %v", cosRegion)

powerVSCluster = &capibm.IBMPowerVSCluster{
ObjectMeta: metav1.ObjectMeta{
Name: clusterID.InfraID,
Namespace: capiutils.Namespace,
Annotations: map[string]string{
"powervs.cluster.x-k8s.io/create-infra": "true",
},
},
Spec: capibm.IBMPowerVSClusterSpec{
Network: capibm.IBMPowerVSResourceReference{
Name: &network,
},
ControlPlaneEndpoint: capiv1beta1.APIEndpoint{
Host: fmt.Sprintf("api.%s.%s", installConfig.Config.ObjectMeta.Name, installConfig.Config.BaseDomain),
Port: 6443,
},
ServiceInstance: &service,
Zone: &installConfig.Config.Platform.PowerVS.Zone,
ResourceGroup: &capibm.IBMPowerVSResourceReference{
Name: &resourceGroup,
},
TransitGateway: &capibm.TransitGateway{
Name: &transitGateway,
},
// LoadBalancers:,
CosInstance: &capibm.CosInstance{
Name: cosName,
BucketName: object,
BucketRegion: bucket,
},
},
}
logrus.Debugf("GenerateClusterAssets: powerVSCluster.Spec.ControlPlaneEndpoint.Host = %v", powerVSCluster.Spec.ControlPlaneEndpoint.Host)

// Avoid:
// vpc:
// name: ""
// region: ""
if installConfig.Config.Platform.PowerVS.VPCName != "" {
vpcResource := capibm.VPCResourceReference{
Name: &installConfig.Config.Platform.PowerVS.VPCName,
Region: &installConfig.Config.Platform.PowerVS.VPCRegion,
}
reflect.ValueOf(&powerVSCluster.Spec).Elem().FieldByName("VPC").Set(reflect.ValueOf(&vpcResource))
}

logrus.Debugf("GenerateClusterAssets: len(VPCSubnets) = %d", len(installConfig.Config.Platform.PowerVS.VPCSubnets))
if len(installConfig.Config.Platform.PowerVS.VPCSubnets) > 0 {
var subnets = make([]capibm.Subnet, len(installConfig.Config.Platform.PowerVS.VPCSubnets))

for _, vpcSubnet := range installConfig.Config.Platform.PowerVS.VPCSubnets {
subnets = append(subnets, capibm.Subnet{ID: &vpcSubnet})
}
logrus.Debugf("GenerateClusterAssets: subnets = %+v", subnets)

reflect.ValueOf(&powerVSCluster.Spec).Elem().FieldByName("VPCSubnets").Set(reflect.ValueOf(subnets))
}

manifests = append(manifests, &asset.RuntimeFile{
Object: powerVSCluster,
File: asset.File{Filename: "02_powervs-cluster.yaml"},
})

return &capiutils.GenerateClusterAssetsOutput{
Manifests: manifests,
InfrastructureRef: &corev1.ObjectReference{
APIVersion: "infrastructure.cluster.x-k8s.io/v1beta2",
Kind: "IBMPowerVSCluster",
Name: powerVSCluster.Name,
Namespace: powerVSCluster.Namespace,
},
}, nil
}

0 comments on commit 332402a

Please sign in to comment.