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

     1  // Package ovirt contains ovirt-specific Terraform-variable logic.
     2  package ovirt
     3  
     4  import (
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	"github.com/openshift/cluster-api-provider-ovirt/pkg/apis/ovirtprovider/v1beta1"
     9  	"github.com/openshift/installer/pkg/rhcos"
    10  	"github.com/openshift/installer/pkg/rhcos/cache"
    11  	"github.com/openshift/installer/pkg/types/ovirt"
    12  )
    13  
    14  // Auth is the collection of credentials that will be used by terraform.
    15  type Auth struct {
    16  	URL      string `json:"ovirt_url"`
    17  	Username string `json:"ovirt_username"`
    18  	Password string `json:"ovirt_password"`
    19  	Cafile   string `json:"ovirt_cafile"`
    20  	Cabundle string `json:"ovirt_ca_bundle"`
    21  	Insecure bool   `json:"ovirt_insecure"`
    22  }
    23  
    24  type config struct {
    25  	Auth                    `json:",inline"`
    26  	ClusterID               string                   `json:"ovirt_cluster_id"`
    27  	StorageDomainID         string                   `json:"ovirt_storage_domain_id"`
    28  	NetworkName             string                   `json:"ovirt_network_name,omitempty"`
    29  	VNICProfileID           string                   `json:"ovirt_vnic_profile_id,omitempty"`
    30  	AffinityGroups          []map[string]interface{} `json:"ovirt_affinity_groups,omitempty"`
    31  	BaseImageName           string                   `json:"ovirt_base_image_name,omitempty"`
    32  	BaseImageLocalFilePath  string                   `json:"ovirt_base_image_local_file_path,omitempty"`
    33  	MasterInstanceTypeID    string                   `json:"ovirt_master_instance_type_id"`
    34  	MasterVMType            string                   `json:"ovirt_master_vm_type,omitempty"`
    35  	MasterMemory            int32                    `json:"ovirt_master_memory"`
    36  	MasterCores             int32                    `json:"ovirt_master_cores"`
    37  	MasterSockets           int32                    `json:"ovirt_master_sockets"`
    38  	MasterThreads           int32                    `json:"ovirt_master_threads"`
    39  	MasterOsDiskGB          int64                    `json:"ovirt_master_os_disk_gb"`
    40  	MasterAffinityGroups    []string                 `json:"ovirt_master_affinity_groups"`
    41  	MasterAutoPinningPolicy string                   `json:"ovirt_master_auto_pinning_policy,omitempty"`
    42  	MasterHugePages         int32                    `json:"ovirt_master_hugepages"`
    43  	MasterClone             *bool                    `json:"ovirt_master_clone"`
    44  	MasterSparse            *bool                    `json:"ovirt_master_sparse"`
    45  	MasterFormat            string                   `json:"ovirt_master_format"`
    46  }
    47  
    48  // TFVars generates ovirt-specific Terraform variables.
    49  func TFVars(
    50  	auth Auth,
    51  	clusterID string,
    52  	storageDomainID string,
    53  	networkName string,
    54  	vnicProfileID string,
    55  	baseImage string,
    56  	infraID string,
    57  	masterSpec *v1beta1.OvirtMachineProviderSpec,
    58  	affinityGroups []ovirt.AffinityGroup,
    59  ) ([]byte, error) {
    60  	if clusterID == "" {
    61  		return nil, fmt.Errorf("storage domain ID cannot be empty")
    62  	}
    63  	if storageDomainID == "" {
    64  		return nil, fmt.Errorf("storage domain ID cannot be empty")
    65  	}
    66  	cfg := config{
    67  		Auth:                    auth,
    68  		ClusterID:               clusterID,
    69  		StorageDomainID:         storageDomainID,
    70  		NetworkName:             networkName,
    71  		VNICProfileID:           vnicProfileID,
    72  		MasterInstanceTypeID:    masterSpec.InstanceTypeId,
    73  		MasterVMType:            masterSpec.VMType,
    74  		MasterOsDiskGB:          masterSpec.OSDisk.SizeGB,
    75  		MasterMemory:            masterSpec.MemoryMB,
    76  		MasterAffinityGroups:    masterSpec.AffinityGroupsNames,
    77  		MasterAutoPinningPolicy: masterSpec.AutoPinningPolicy,
    78  		MasterHugePages:         masterSpec.Hugepages,
    79  		MasterClone:             masterSpec.Clone,
    80  		MasterSparse:            masterSpec.Sparse,
    81  		MasterFormat:            masterSpec.Format,
    82  	}
    83  	if masterSpec.CPU != nil {
    84  		cfg.MasterCores = masterSpec.CPU.Cores
    85  		cfg.MasterSockets = masterSpec.CPU.Sockets
    86  		cfg.MasterThreads = masterSpec.CPU.Threads
    87  	}
    88  
    89  	imageName, isURL := rhcos.GenerateOpenStackImageName(baseImage, infraID)
    90  	if isURL {
    91  		imageFilePath, err := cache.DownloadImageFile(baseImage, cache.InstallerApplicationName)
    92  		if err != nil {
    93  			return nil, err
    94  		}
    95  		cfg.BaseImageLocalFilePath = imageFilePath
    96  	} else {
    97  		cfg.BaseImageName = imageName
    98  	}
    99  	if len(affinityGroups) > 0 {
   100  		cfg.AffinityGroups = handleAffinityGroups(affinityGroups, infraID)
   101  	}
   102  	return json.MarshalIndent(cfg, "", "  ")
   103  }
   104  
   105  func handleAffinityGroups(ags []ovirt.AffinityGroup, infraID string) []map[string]interface{} {
   106  	tfAffinityGroups := make([]map[string]interface{}, len(ags))
   107  	for i, ag := range ags {
   108  		tfAffinityGroups[i] = map[string]interface{}{
   109  			"name":        fmt.Sprintf("%s-%s", infraID, ag.Name),
   110  			"priority":    ag.Priority,
   111  			"description": ag.Description,
   112  			"enforcing":   ag.Enforcing,
   113  		}
   114  	}
   115  	return tfAffinityGroups
   116  }