func AutoFillVSphereProvider()

in internal/pkg/api/vsphere.go [22:101]


func AutoFillVSphereProvider(filename string, fillers ...VSphereFiller) ([]byte, error) {
	var etcdMachineConfig *v1alpha1.VSphereMachineConfig
	// only to get name of control plane and worker node machine configs
	clusterConfig, err := v1alpha1.GetClusterConfig(filename)
	if err != nil {
		return nil, fmt.Errorf("unable to get cluster config from file: %v", err)
	}
	if clusterConfig.Spec.ControlPlaneConfiguration.MachineGroupRef == nil {
		return nil, fmt.Errorf("no machineGroupRef defined for control plane")
	}
	if len(clusterConfig.Spec.WorkerNodeGroupConfigurations) == 0 {
		return nil, fmt.Errorf("no worker nodes defined")
	}
	if clusterConfig.Spec.WorkerNodeGroupConfigurations[0].MachineGroupRef == nil {
		return nil, fmt.Errorf("no machineGroupRef defined for worker nodes")
	}
	if clusterConfig.Spec.ExternalEtcdConfiguration != nil {
		if clusterConfig.Spec.ExternalEtcdConfiguration.MachineGroupRef == nil {
			return nil, fmt.Errorf("no machineGroupRef defined for etcd machines")
		}
	}
	cpName := clusterConfig.Spec.ControlPlaneConfiguration.MachineGroupRef.Name
	workerName := clusterConfig.Spec.WorkerNodeGroupConfigurations[0].MachineGroupRef.Name
	vsphereDatacenterConfig, err := v1alpha1.GetVSphereDatacenterConfig(filename)
	if err != nil {
		return nil, fmt.Errorf("unable to get vsphere datacenter config from file: %v", err)
	}

	vsphereMachineConfigs, err := v1alpha1.GetVSphereMachineConfigs(filename)
	if err != nil {
		return nil, fmt.Errorf("unable to get vsphere machine config from file: %v", err)
	}
	cpMachineConfig, ok := vsphereMachineConfigs[cpName]
	if !ok {
		return nil, fmt.Errorf("unable to find vsphere control plane machine config %v", cpName)
	}
	workerMachineConfig, ok := vsphereMachineConfigs[workerName]
	if !ok {
		return nil, fmt.Errorf("unable to find vsphere worker node machine config %v", workerName)
	}

	config := VSphereConfig{
		datacenterConfig:    vsphereDatacenterConfig,
		cpMachineConfig:     cpMachineConfig,
		workerMachineConfig: workerMachineConfig,
	}

	if clusterConfig.Spec.ExternalEtcdConfiguration != nil {
		etcdMachineConfig, ok = vsphereMachineConfigs[clusterConfig.Spec.ExternalEtcdConfiguration.MachineGroupRef.Name]
		if !ok {
			return nil, fmt.Errorf("unable to find vsphere etcd machine config %v", cpName)
		}
		config.etcdMachineConfig = etcdMachineConfig
	}
	for _, f := range fillers {
		f(config)
	}

	vsphereDatacenterConfigOutput, err := yaml.Marshal(vsphereDatacenterConfig)
	if err != nil {
		return nil, fmt.Errorf("error marshalling vsphere datacenter config: %v", err)
	}
	cpMachineConfigOutput, err := yaml.Marshal(cpMachineConfig)
	if err != nil {
		return nil, fmt.Errorf("error marshalling vsphere control plane machine config: %v", err)
	}
	workerMachineConfigOutput, err := yaml.Marshal(workerMachineConfig)
	if err != nil {
		return nil, fmt.Errorf("error marshalling vsphere worker node machine config: %v", err)
	}
	vsphereConfigOutput := templater.AppendYamlResources(vsphereDatacenterConfigOutput, cpMachineConfigOutput, workerMachineConfigOutput)
	if clusterConfig.Spec.ExternalEtcdConfiguration != nil {
		etcdMachineConfigOutput, err := yaml.Marshal(etcdMachineConfig)
		if err != nil {
			return nil, fmt.Errorf("error marshalling vsphere etcd machine config: %v", err)
		}
		vsphereConfigOutput = templater.AppendYamlResources(vsphereConfigOutput, etcdMachineConfigOutput)
	}
	return vsphereConfigOutput, nil
}