Skip to content

Instantly share code, notes, and snippets.

@hamzy
Created March 19, 2024 19:26
Show Gist options
  • Save hamzy/cee9a0e81bdacc65a8f7bf37888c88f4 to your computer and use it in GitHub Desktop.
Save hamzy/cee9a0e81bdacc65a8f7bf37888c88f4 to your computer and use it in GitHub Desktop.
20240319 ./pkg/asset/manifests/powervs/cluster.go
package powervs
import (
"fmt"
"reflect"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
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
imageName string
bucketName string
err error
powerVSCluster *capibm.IBMPowerVSCluster
powerVSImage *capibm.IBMPowerVSImage
)
defer func() {
logrus.Debugf("GenerateClusterAssets: installConfig = %+v, clusterID = %v, bucket = %v, object = %v", installConfig, *clusterID, bucket, object)
logrus.Debugf("GenerateClusterAssets: ic.ObjectMeta = %+v", installConfig.Config.ObjectMeta.Name)
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: imageName = %v", imageName)
logrus.Debugf("GenerateClusterAssets: bucketName = %v", bucketName)
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("vpc-%s", 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)
switch {
case 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)
}
case installConfig.Config.Platform.PowerVS.Region != "":
cosRegion = installConfig.Config.Platform.PowerVS.Region
default:
return nil, fmt.Errorf("GenerateClusterAssets: Region is empty")
}
imageName = fmt.Sprintf("rhcos-%s", clusterID.InfraID)
bucketName = fmt.Sprintf("%s-bootstrap-ign", clusterID.InfraID)
powerVSCluster = &capibm.IBMPowerVSCluster{
TypeMeta: metav1.TypeMeta{
APIVersion: capibm.GroupVersion.String(),
Kind: "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: []capibm.VPCLoadBalancerSpec{
{
Name: fmt.Sprintf("%s-loadbalancer", clusterID.InfraID),
Public: ptr.To(true),
AdditionalListeners: []capibm.AdditionalListenerSpec{
{
Port: 22,
},
// @BUG We should be able to specify this:
// capibm.AdditionalListenerSpec{
// Port: 6443,
// },
},
},
{
Name: fmt.Sprintf("%s-loadbalancer-int", clusterID.InfraID),
Public: ptr.To(false),
AdditionalListeners: []capibm.AdditionalListenerSpec{
// @BUG We should be able to specify this:
// capibm.AdditionalListenerSpec{
// Port: 6443,
// },
{
Port: 22623,
},
},
},
},
CosInstance: &capibm.CosInstance{
Name: cosName,
BucketName: bucketName,
BucketRegion: vpcRegion,
},
Ignition: &capibm.Ignition{
Version: "3.4",
},
},
}
// 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 []capibm.Subnet
vpcSubnet string
)
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"},
})
powerVSImage = &capibm.IBMPowerVSImage{
TypeMeta: metav1.TypeMeta{
APIVersion: capibm.GroupVersion.String(),
Kind: "IBMPowerVSImage",
},
ObjectMeta: metav1.ObjectMeta{
Name: imageName,
Namespace: capiutils.Namespace,
},
Spec: capibm.IBMPowerVSImageSpec{
ClusterName: clusterID.InfraID,
ServiceInstanceID: installConfig.Config.PowerVS.ServiceInstanceGUID,
Bucket: &bucket,
Object: &object,
Region: &vpcRegion,
},
}
manifests = append(manifests, &asset.RuntimeFile{
Object: powerVSImage,
File: asset.File{Filename: "03_powervs-image.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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment