github.com/openshift/installer@v1.4.17/pkg/asset/manifests/vsphere/cloudproviderconfig.go (about)

     1  package vsphere
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"strings"
     7  
     8  	yaml "gopkg.in/yaml.v2"
     9  	cloudconfig "k8s.io/cloud-provider-vsphere/pkg/common/config"
    10  
    11  	vspheretypes "github.com/openshift/installer/pkg/types/vsphere"
    12  )
    13  
    14  const (
    15  	regionTagCategory = "openshift-region"
    16  	zoneTagCategory   = "openshift-zone"
    17  )
    18  
    19  func printIfNotEmpty(buf *bytes.Buffer, k, v string) {
    20  	if v != "" {
    21  		fmt.Fprintf(buf, "%s = %q\n", k, v)
    22  	}
    23  }
    24  
    25  // CloudProviderConfigYaml generates the yaml out of tree cloud provider config for the vSphere platform.
    26  func CloudProviderConfigYaml(infraID string, p *vspheretypes.Platform) (string, error) {
    27  	vCenters := make(map[string]*cloudconfig.VirtualCenterConfigYAML)
    28  
    29  	for _, vCenter := range p.VCenters {
    30  		vCenterPort := int32(443)
    31  		if vCenter.Port != 0 {
    32  			vCenterPort = vCenter.Port
    33  		}
    34  		vCenterConfig := cloudconfig.VirtualCenterConfigYAML{
    35  			VCenterIP:    vCenter.Server,
    36  			VCenterPort:  uint(vCenterPort),
    37  			Datacenters:  vCenter.Datacenters,
    38  			InsecureFlag: true,
    39  		}
    40  		vCenters[vCenter.Server] = &vCenterConfig
    41  	}
    42  
    43  	cloudProviderConfig := cloudconfig.CommonConfigYAML{
    44  		Global: cloudconfig.GlobalYAML{
    45  			SecretName:      "vsphere-creds",
    46  			SecretNamespace: "kube-system",
    47  			InsecureFlag:    true,
    48  		},
    49  		Vcenter: vCenters,
    50  	}
    51  
    52  	if len(p.FailureDomains) > 1 {
    53  		cloudProviderConfig.Labels = cloudconfig.LabelsYAML{
    54  			Zone:   vspheretypes.TagCategoryZone,
    55  			Region: vspheretypes.TagCategoryRegion,
    56  		}
    57  	}
    58  
    59  	cloudProviderConfigYaml, err := yaml.Marshal(cloudProviderConfig)
    60  	if err != nil {
    61  		return "", err
    62  	}
    63  	return string(cloudProviderConfigYaml), nil
    64  }
    65  
    66  // CloudProviderConfigIni generates the multi-zone ini cloud provider config
    67  // for the vSphere platform. folderPath is the absolute path to the VM folder that will be
    68  // used for installation. p is the vSphere platform struct.
    69  func CloudProviderConfigIni(infraID string, p *vspheretypes.Platform) (string, error) {
    70  	buf := new(bytes.Buffer)
    71  
    72  	fmt.Fprintln(buf, "[Global]")
    73  	printIfNotEmpty(buf, "secret-name", "vsphere-creds")
    74  	printIfNotEmpty(buf, "secret-namespace", "kube-system")
    75  	printIfNotEmpty(buf, "insecure-flag", "1")
    76  	fmt.Fprintln(buf, "")
    77  
    78  	for _, vcenter := range p.VCenters {
    79  		fmt.Fprintf(buf, "[VirtualCenter %q]\n", vcenter.Server)
    80  		if vcenter.Port != 0 {
    81  			printIfNotEmpty(buf, "port", fmt.Sprintf("%d", vcenter.Port))
    82  			fmt.Fprintln(buf, "")
    83  		}
    84  		datacenters := make([]string, 0, len(vcenter.Datacenters))
    85  		datacenters = append(datacenters, vcenter.Datacenters...)
    86  		for _, failureDomain := range p.FailureDomains {
    87  			if failureDomain.Server == vcenter.Server {
    88  				failureDomainDatacenter := failureDomain.Topology.Datacenter
    89  				exists := false
    90  				for _, existingDatacenter := range datacenters {
    91  					if failureDomainDatacenter == existingDatacenter {
    92  						exists = true
    93  						break
    94  					}
    95  				}
    96  				if !exists {
    97  					datacenters = append(datacenters, failureDomainDatacenter)
    98  				}
    99  			}
   100  		}
   101  		printIfNotEmpty(buf, "datacenters", strings.Join(datacenters, ","))
   102  	}
   103  	fmt.Fprintln(buf, "")
   104  
   105  	fmt.Fprintln(buf, "[Workspace]")
   106  	printIfNotEmpty(buf, "server", p.FailureDomains[0].Server)
   107  	printIfNotEmpty(buf, "datacenter", p.FailureDomains[0].Topology.Datacenter)
   108  	printIfNotEmpty(buf, "default-datastore", p.FailureDomains[0].Topology.Datastore)
   109  
   110  	folderPath := fmt.Sprintf("/%s/vm/%s", p.FailureDomains[0].Topology.Datacenter, infraID)
   111  	if p.FailureDomains[0].Topology.Folder != "" {
   112  		folderPath = p.FailureDomains[0].Topology.Folder
   113  	}
   114  	printIfNotEmpty(buf, "folder", folderPath)
   115  	printIfNotEmpty(buf, "resourcepool-path", p.FailureDomains[0].Topology.ResourcePool)
   116  	fmt.Fprintln(buf, "")
   117  
   118  	if len(p.FailureDomains) > 1 {
   119  		fmt.Fprintln(buf, "[Labels]")
   120  		printIfNotEmpty(buf, "region", regionTagCategory)
   121  		printIfNotEmpty(buf, "zone", zoneTagCategory)
   122  	}
   123  
   124  	return buf.String(), nil
   125  }