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

     1  package ibmcloud
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"regexp"
     7  	"sort"
     8  	"strings"
     9  	"text/template"
    10  
    11  	"github.com/sirupsen/logrus"
    12  
    13  	configv1 "github.com/openshift/api/config/v1"
    14  )
    15  
    16  // https://github.com/kubernetes/kubernetes/blob/368ee4bb8ee7a0c18431cd87ee49f0c890aa53e5/staging/src/k8s.io/legacy-cloud-providers/gce/gce.go#L188
    17  type config struct {
    18  	Global     global     `gcfg:"global"`
    19  	Kubernetes kubernetes `gcfg:"kubernetes"`
    20  	Provider   provider   `gcfg:"provider"`
    21  }
    22  
    23  type global struct {
    24  	Version string `gcfg:"version"`
    25  }
    26  
    27  type kubernetes struct {
    28  	ConfigFile string `gcfg:"config-file"`
    29  }
    30  
    31  type provider struct {
    32  	AccountID                string `gcfg:"accountID"`
    33  	ClusterID                string `gcfg:"clusterID"`
    34  	ClusterDefaultProvider   string `gcfg:"cluster-default-provider"`
    35  	Region                   string `gcfg:"region"`
    36  	G2CredentialsFilePath    string `gcfg:"g2Credentials"`
    37  	G2ResourceGroupName      string `gcfg:"g2ResourceGroupName"`
    38  	G2VPCName                string `gcfg:"g2VpcName"`
    39  	G2WorkerServiceAccountID string `gcfg:"g2workerServiceAccountID"`
    40  	G2VPCSubnetNames         string `gcfg:"g2VpcSubnetNames"`
    41  	IAMEndpointOverride      string `gcfg:"iamEndpointOverride,omitempty"`
    42  	VPCEndpointOverride      string `gcfg:"g2EndpointOverride,omitempty"`
    43  	RMEndpointOverride       string `gcfg:"rmEndpointOverride,omitempty"`
    44  }
    45  
    46  // CloudProviderConfig generates the cloud provider config for the IBMCloud platform.
    47  func CloudProviderConfig(infraID string, accountID string, region string, resourceGroupName string, vpcName string, subnets []string, controlPlaneZones []string, computeZones []string, serviceEndpoints []configv1.IBMCloudServiceEndpoint) (string, error) {
    48  	if vpcName == "" {
    49  		vpcName = fmt.Sprintf("%s-vpc", infraID)
    50  	}
    51  
    52  	var subnetNames string
    53  	if len(subnets) > 0 {
    54  		subnetNames = strings.Join(subnets, ",")
    55  	} else {
    56  		subnetNames = getVpcSubnetNames(infraID, controlPlaneZones, computeZones)
    57  	}
    58  
    59  	config := &config{
    60  		Global: global{
    61  			Version: "1.1.0",
    62  		},
    63  		Kubernetes: kubernetes{
    64  			ConfigFile: "",
    65  		},
    66  		Provider: provider{
    67  			AccountID:                accountID,
    68  			ClusterID:                infraID,
    69  			ClusterDefaultProvider:   "g2",
    70  			Region:                   region,
    71  			G2CredentialsFilePath:    "/etc/vpc/ibmcloud_api_key",
    72  			G2ResourceGroupName:      resourceGroupName,
    73  			G2VPCName:                vpcName,
    74  			G2WorkerServiceAccountID: accountID,
    75  			G2VPCSubnetNames:         subnetNames,
    76  		},
    77  	}
    78  
    79  	// Add any IBM Cloud Service Endpoint overrides as necessary
    80  	for _, endpoint := range serviceEndpoints {
    81  		switch endpoint.Name {
    82  		case configv1.IBMCloudServiceIAM:
    83  			config.Provider.IAMEndpointOverride = endpoint.URL
    84  		case configv1.IBMCloudServiceVPC:
    85  			// Trim the version suffix ('/v1', '/v21', etc.) from the VPC URL since the CCM appends it
    86  			suffixRegex := regexp.MustCompile(`(^.*)/v\d+[/]{0,1}$`)
    87  			config.Provider.VPCEndpointOverride = suffixRegex.ReplaceAllString(endpoint.URL, "${1}")
    88  		case configv1.IBMCloudServiceResourceManager:
    89  			config.Provider.RMEndpointOverride = endpoint.URL
    90  		default:
    91  			logrus.Debugf("ignoring unnecessary endpoint override for cloud provider config: %s", endpoint.Name)
    92  		}
    93  	}
    94  
    95  	buf := &bytes.Buffer{}
    96  	template := template.Must(template.New("ibmcloud cloudproviderconfig").Parse(configTmpl))
    97  	if err := template.Execute(buf, config); err != nil {
    98  		return "", err
    99  	}
   100  	return buf.String(), nil
   101  }
   102  
   103  // Generate a string of Subnet names for Control Plane and Compute based off the cluster name
   104  func getVpcSubnetNames(infraID string, controlPlaneZones []string, computeZones []string) string {
   105  	var subnetNames []string
   106  
   107  	for cpIndex := range controlPlaneZones {
   108  		// Add Control Plane subnet
   109  		subnetNames = append(subnetNames, fmt.Sprintf("%s-subnet-control-plane-%s", infraID, controlPlaneZones[cpIndex]))
   110  	}
   111  	for comIndex := range computeZones {
   112  		// Add Compute subnet
   113  		subnetNames = append(subnetNames, fmt.Sprintf("%s-subnet-compute-%s", infraID, computeZones[comIndex]))
   114  	}
   115  	sort.Strings(subnetNames)
   116  	return strings.Join(subnetNames, ",")
   117  }
   118  
   119  var configTmpl = `[global]
   120  version = {{.Global.Version}}
   121  [kubernetes]
   122  config-file = {{ if ne .Kubernetes.ConfigFile "" }}{{ .Kubernetes.ConfigFile }}{{ else }}""{{ end }}
   123  [provider]
   124  accountID = {{.Provider.AccountID}}
   125  clusterID = {{.Provider.ClusterID}}
   126  cluster-default-provider = {{.Provider.ClusterDefaultProvider}}
   127  region = {{.Provider.Region}}
   128  g2Credentials = {{.Provider.G2CredentialsFilePath}}
   129  g2ResourceGroupName = {{.Provider.G2ResourceGroupName}}
   130  g2VpcName = {{.Provider.G2VPCName}}
   131  g2workerServiceAccountID = {{.Provider.G2WorkerServiceAccountID}}
   132  g2VpcSubnetNames = {{.Provider.G2VPCSubnetNames}}
   133  {{ if ne .Provider.IAMEndpointOverride ""}}{{ printf "iamEndpointOverride = %s\n" .Provider.IAMEndpointOverride }}{{ end }}{{ if ne .Provider.VPCEndpointOverride ""}}{{ printf "g2EndpointOverride = %s\n" .Provider.VPCEndpointOverride }}{{ end }}{{ if ne .Provider.RMEndpointOverride ""}}{{ printf "rmEndpointOverride = %s\n" .Provider.RMEndpointOverride }}{{ end }}
   134  
   135  `