Skip to content

Instantly share code, notes, and snippets.

@hamzy
Created February 25, 2024 13:46
Show Gist options
  • Save hamzy/6bb60bdf1905e05ed290deca3b368499 to your computer and use it in GitHub Desktop.
Save hamzy/6bb60bdf1905e05ed290deca3b368499 to your computer and use it in GitHub Desktop.
./pkg/asset/manifests/powervs/cluster.go
package powervs
import (
"fmt"
"reflect"
"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/asset/installconfig"
"github.com/openshift/installer/pkg/asset/manifests/capiutils"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
capibm "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
powervstypes "github.com/openshift/installer/pkg/types/powervs"
"github.com/sirupsen/logrus"
)
// 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,
},
Spec: capibm.IBMPowerVSClusterSpec{
Network: capibm.IBMPowerVSResourceReference{
Name: &network,
},
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,
},
},
}
// 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment