-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MULTIARCH-4096: PowerVS: Add GenerateClusterAssets
Generate the cluster assets for the PowerVS CAPI provider.
- Loading branch information
Showing
2 changed files
with
177 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package powervs | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/sirupsen/logrus" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
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 | ||
resourceGroupName string | ||
vpcName string | ||
vpcRegion string | ||
transitGatewayName string | ||
cosName string | ||
cosRegion string | ||
err error | ||
powerVSCluster *capibm.IBMPowerVSCluster | ||
) | ||
|
||
defer func() { | ||
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) | ||
logrus.Debugf("GenerateClusterAssets: resourceGroupName = %v", resourceGroupName) | ||
logrus.Debugf("GenerateClusterAssets: vpcName = %v", vpcName) | ||
logrus.Debugf("GenerateClusterAssets: vpcRegion = %v", vpcRegion) | ||
logrus.Debugf("GenerateClusterAssets: transitGatewayName = %v", transitGatewayName) | ||
logrus.Debugf("GenerateClusterAssets: cosName = %v", cosName) | ||
logrus.Debugf("GenerateClusterAssets: cosRegion = %v", cosRegion) | ||
logrus.Debugf("GenerateClusterAssets: powerVSCluster.Spec.ControlPlaneEndpoint.Host = %v", powerVSCluster.Spec.ControlPlaneEndpoint.Host) | ||
}() | ||
|
||
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? | ||
resourceGroupName = installConfig.Config.Platform.PowerVS.PowerVSResourceGroup | ||
} | ||
|
||
vpcName = fmt.Sprintf("%s-vpc", clusterID.InfraID) | ||
|
||
if installConfig.Config.PowerVS.VPCRegion == "" { | ||
if vpcRegion, err = powervstypes.VPCRegionForPowerVSRegion(installConfig.Config.PowerVS.Region); err != nil { | ||
return nil, fmt.Errorf("Unable to derive vpcRegion from region: %s %w", installConfig.Config.PowerVS.Region, err) | ||
} | ||
} else { | ||
vpcRegion = installConfig.Config.Platform.PowerVS.VPCRegion | ||
} | ||
|
||
transitGatewayName = 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") | ||
} | ||
|
||
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, | ||
}, | ||
ServiceInstance: &service, | ||
Zone: &installConfig.Config.Platform.PowerVS.Zone, | ||
ResourceGroup: &capibm.IBMPowerVSResourceReference{ | ||
Name: &resourceGroupName, | ||
}, | ||
VPC: &capibm.VPCResourceReference{ | ||
Name: &vpcName, | ||
Region: &vpcRegion, | ||
}, | ||
TransitGateway: &capibm.TransitGateway{ | ||
Name: &transitGatewayName, | ||
}, | ||
// LoadBalancers:, | ||
CosInstance: &capibm.CosInstance{ | ||
Name: cosName, | ||
BucketName: bucket, | ||
BucketRegion: vpcRegion, | ||
}, | ||
}, | ||
} | ||
|
||
// 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)) | ||
} | ||
|
||
// Another avoidence | ||
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 | ||
} |