github.com/openshift/installer@v1.4.17/pkg/asset/machines/powervs/powervsmachines.go (about)

     1  // Package powervs generates Machine objects for powerVS.
     2  package powervs
     3  
     4  import (
     5  	"fmt"
     6  
     7  	v1 "k8s.io/api/core/v1"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  	"k8s.io/utils/ptr"
    10  	capibm "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
    11  	capi "sigs.k8s.io/cluster-api/api/v1beta1"
    12  
    13  	"github.com/openshift/installer/pkg/asset"
    14  	"github.com/openshift/installer/pkg/asset/manifests/capiutils"
    15  	"github.com/openshift/installer/pkg/types"
    16  	"github.com/openshift/installer/pkg/types/powervs"
    17  )
    18  
    19  // GenerateMachines returns manifests and runtime objects to provision the control plane (including bootstrap, if applicable) nodes using CAPI.
    20  func GenerateMachines(clusterID string, ic *types.InstallConfig, pool *types.MachinePool, role string) ([]*asset.RuntimeFile, error) {
    21  	if poolPlatform := pool.Platform.Name(); poolPlatform != powervs.Name {
    22  		return nil, fmt.Errorf("non-Power VS machine-pool: %q", poolPlatform)
    23  	}
    24  	mpool := pool.Platform.PowerVS
    25  
    26  	total := int64(1)
    27  	if pool.Replicas != nil {
    28  		total = *pool.Replicas
    29  	}
    30  
    31  	var (
    32  		result         []*asset.RuntimeFile
    33  		image          string
    34  		service        capibm.IBMPowerVSResourceReference
    35  		name           string
    36  		powerVSMachine *capibm.IBMPowerVSMachine
    37  		dataSecret     string
    38  		machine        *capi.Machine
    39  	)
    40  
    41  	// Note: This will be created later
    42  	image = fmt.Sprintf("rhcos-%s", clusterID)
    43  
    44  	if ic.PowerVS.ServiceInstanceGUID == "" {
    45  		serviceName := fmt.Sprintf("%s-power-iaas", clusterID)
    46  
    47  		service = capibm.IBMPowerVSResourceReference{
    48  			Name: &serviceName,
    49  		}
    50  	} else {
    51  		service = capibm.IBMPowerVSResourceReference{
    52  			ID: &ic.PowerVS.ServiceInstanceGUID,
    53  		}
    54  	}
    55  
    56  	for idx := int64(0); idx < total; idx++ {
    57  		name = fmt.Sprintf("%s-%s-%d", clusterID, pool.Name, idx)
    58  
    59  		powerVSMachine = GenerateMachine(ic, service, mpool, name, image)
    60  
    61  		result = append(result, &asset.RuntimeFile{
    62  			File:   asset.File{Filename: fmt.Sprintf("10_inframachine_%s.yaml", powerVSMachine.Name)},
    63  			Object: powerVSMachine,
    64  		})
    65  
    66  		dataSecret = fmt.Sprintf("%s-%s", clusterID, "master")
    67  		machine = GenerateCAPIMachine(clusterID, powerVSMachine.Name, dataSecret)
    68  
    69  		result = append(result, &asset.RuntimeFile{
    70  			File:   asset.File{Filename: fmt.Sprintf("10_machine_%s.yaml", machine.Name)},
    71  			Object: machine,
    72  		})
    73  	}
    74  
    75  	name = fmt.Sprintf("%s-bootstrap", clusterID)
    76  	powerVSMachine = GenerateMachine(ic, service, mpool, name, image)
    77  	powerVSMachine.Labels["install.openshift.io/bootstrap"] = ""
    78  
    79  	result = append(result, &asset.RuntimeFile{
    80  		File:   asset.File{Filename: fmt.Sprintf("10_inframachine_%s.yaml", powerVSMachine.Name)},
    81  		Object: powerVSMachine,
    82  	})
    83  
    84  	dataSecret = fmt.Sprintf("%s-%s", clusterID, "bootstrap")
    85  	machine = GenerateCAPIMachine(clusterID, powerVSMachine.Name, dataSecret)
    86  	machine.Labels["install.openshift.io/bootstrap"] = ""
    87  
    88  	result = append(result, &asset.RuntimeFile{
    89  		File:   asset.File{Filename: fmt.Sprintf("10_machine_%s.yaml", machine.Name)},
    90  		Object: machine,
    91  	})
    92  
    93  	return result, nil
    94  }
    95  
    96  // GenerateMachine creates a capibm.IBMPowerVSMachine struct.
    97  func GenerateMachine(ic *types.InstallConfig, service capibm.IBMPowerVSResourceReference, mpool *powervs.MachinePool, name string, image string) *capibm.IBMPowerVSMachine {
    98  	return &capibm.IBMPowerVSMachine{
    99  		TypeMeta: metav1.TypeMeta{
   100  			APIVersion: capibm.GroupVersion.String(),
   101  			Kind:       "IBMPowerVSMachine",
   102  		},
   103  		ObjectMeta: metav1.ObjectMeta{
   104  			Name:      name,
   105  			Namespace: capiutils.Namespace,
   106  			Labels: map[string]string{
   107  				"cluster.x-k8s.io/control-plane": "",
   108  			},
   109  		},
   110  		Spec: capibm.IBMPowerVSMachineSpec{
   111  			ServiceInstanceID: ic.PowerVS.ServiceInstanceGUID,
   112  			ServiceInstance:   &service,
   113  			SSHKey:            "",
   114  			ImageRef: &v1.LocalObjectReference{
   115  				Name: image,
   116  			},
   117  			SystemType:    mpool.SysType,
   118  			ProcessorType: capibm.PowerVSProcessorType(mpool.ProcType),
   119  			Processors:    mpool.Processors,
   120  			MemoryGiB:     mpool.MemoryGiB,
   121  		},
   122  	}
   123  }
   124  
   125  // GenerateCAPIMachine creates a capi.Machine struct.
   126  func GenerateCAPIMachine(clusterID string, name string, dataSecret string) *capi.Machine {
   127  	return &capi.Machine{
   128  		TypeMeta: metav1.TypeMeta{
   129  			Kind:       "Machine",
   130  			APIVersion: capi.GroupVersion.String(),
   131  		},
   132  		ObjectMeta: metav1.ObjectMeta{
   133  			Name: name,
   134  			Labels: map[string]string{
   135  				"cluster.x-k8s.io/control-plane": "",
   136  			},
   137  		},
   138  		Spec: capi.MachineSpec{
   139  			ClusterName: clusterID,
   140  			Bootstrap: capi.Bootstrap{
   141  				DataSecretName: ptr.To(dataSecret),
   142  			},
   143  			InfrastructureRef: v1.ObjectReference{
   144  				APIVersion: capibm.GroupVersion.String(),
   145  				Kind:       "IBMPowerVSMachine",
   146  				Name:       name,
   147  			},
   148  		},
   149  	}
   150  }