github.com/openshift/installer@v1.4.17/pkg/tfvars/tfvars.go (about)

     1  // Package tfvars generates Terraform variables for launching the cluster.
     2  package tfvars
     3  
     4  import (
     5  	"encoding/json"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  // Config contains the cluster data for terraform.
    13  type Config struct {
    14  	ClusterID          string   `json:"cluster_id,omitempty"`
    15  	ClusterDomain      string   `json:"cluster_domain,omitempty"`
    16  	BaseDomain         string   `json:"base_domain,omitempty"`
    17  	Masters            int      `json:"master_count,omitempty"`
    18  	MastersSchedulable bool     `json:"masters_schedulable,omitempty"`
    19  	MachineV4CIDRs     []string `json:"machine_v4_cidrs"`
    20  	MachineV6CIDRs     []string `json:"machine_v6_cidrs"`
    21  
    22  	UseIPv4 bool `json:"use_ipv4"`
    23  	UseIPv6 bool `json:"use_ipv6"`
    24  
    25  	IgnitionBootstrap     string `json:"ignition_bootstrap,omitempty"`
    26  	IgnitionBootstrapFile string `json:"ignition_bootstrap_file,omitempty"`
    27  	IgnitionMaster        string `json:"ignition_master,omitempty"`
    28  }
    29  
    30  // TFVars generates terraform.tfvar JSON for launching the cluster.
    31  func TFVars(clusterID string, clusterDomain string, baseDomain string, machineV4CIDRs []string, machineV6CIDRs []string, useIPv4, useIPv6 bool, bootstrapIgn string, bootstrapIgnSize int64, masterIgn string, masterCount int, mastersSchedulable bool) ([]byte, error) {
    32  	f, err := os.CreateTemp("", "openshift-install-bootstrap-*.ign")
    33  	if err != nil {
    34  		return nil, errors.Wrap(err, "failed to create tmp file for bootstrap ignition")
    35  	}
    36  	defer f.Close()
    37  
    38  	// In azure, if the storage account is encrypted, page blob is used instead of block blob due to lack of support.
    39  	// Page blobs file size must be a multiple of 512, hence a little padding is needed to push the file.
    40  	// Finding the nearest size divisible by 512 and adding that padding to the file.
    41  	// Since the file is json type, padding at the end results in json parsing error at bootstrap ignition.
    42  	// Adding the paddding to just before the last } in the json file to bypass the parsing error.
    43  	padding := bootstrapIgnSize - int64(len(bootstrapIgn))
    44  	bootstrapIgn = bootstrapIgn[0:len(bootstrapIgn)-1] + strings.Repeat(" ", int(padding)) + string(bootstrapIgn[len(bootstrapIgn)-1])
    45  
    46  	if _, err := f.WriteString(bootstrapIgn); err != nil {
    47  		return nil, errors.Wrap(err, "failed to write bootstrap ignition")
    48  	}
    49  
    50  	config := &Config{
    51  		ClusterID:             clusterID,
    52  		ClusterDomain:         strings.TrimSuffix(clusterDomain, "."),
    53  		BaseDomain:            strings.TrimSuffix(baseDomain, "."),
    54  		MachineV4CIDRs:        machineV4CIDRs,
    55  		MachineV6CIDRs:        machineV6CIDRs,
    56  		UseIPv4:               useIPv4,
    57  		UseIPv6:               useIPv6,
    58  		Masters:               masterCount,
    59  		MastersSchedulable:    mastersSchedulable,
    60  		IgnitionBootstrap:     bootstrapIgn,
    61  		IgnitionBootstrapFile: f.Name(),
    62  		IgnitionMaster:        masterIgn,
    63  	}
    64  
    65  	return json.MarshalIndent(config, "", "  ")
    66  }