github.com/openshift/installer@v1.4.17/pkg/infrastructure/baremetal/variables.go (about)

     1  package baremetal
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	baremetalhost "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
    10  	"github.com/sirupsen/logrus"
    11  
    12  	"github.com/openshift/installer/pkg/tfvars"
    13  	baremetaltfvars "github.com/openshift/installer/pkg/tfvars/baremetal"
    14  )
    15  
    16  const (
    17  	tfVarsFileName         = "terraform.tfvars.json"
    18  	tfPlatformVarsFileName = "terraform.platform.auto.tfvars.json"
    19  	// MastersFileName is the file where we store networking data for the control plane.
    20  	MastersFileName = ".masters.json"
    21  )
    22  
    23  type bridge struct {
    24  	Name string
    25  	MAC  string
    26  }
    27  
    28  type baremetalConfig struct {
    29  	ClusterID         string
    30  	BootstrapOSImage  string
    31  	IgnitionBootstrap string
    32  	LibvirtURI        string
    33  	Bridges           []bridge
    34  }
    35  
    36  func getConfig(dir string) (baremetalConfig, error) {
    37  	config := baremetalConfig{}
    38  	clusterConfig := &tfvars.Config{}
    39  	clusterBaremetalConfig := &baremetaltfvars.Config{}
    40  
    41  	data, err := os.ReadFile(filepath.Join(dir, tfVarsFileName))
    42  	if err == nil {
    43  		err = json.Unmarshal(data, clusterConfig)
    44  	}
    45  	if err != nil {
    46  		return config, fmt.Errorf("failed to load cluster terraform variables: %w", err)
    47  	}
    48  
    49  	config.ClusterID = clusterConfig.ClusterID
    50  	config.IgnitionBootstrap = clusterConfig.IgnitionBootstrap
    51  
    52  	data, err = os.ReadFile(filepath.Join(dir, tfPlatformVarsFileName))
    53  	if err == nil {
    54  		err = json.Unmarshal(data, clusterBaremetalConfig)
    55  	}
    56  	if err != nil {
    57  		return config, fmt.Errorf("failed to load cluster terraform variables: %w", err)
    58  	}
    59  
    60  	config.BootstrapOSImage = clusterBaremetalConfig.BootstrapOSImage
    61  	config.LibvirtURI = clusterBaremetalConfig.LibvirtURI
    62  
    63  	for _, bridgeMap := range clusterBaremetalConfig.Bridges {
    64  		mac, ok := bridgeMap["mac"]
    65  		if !ok {
    66  			return config, fmt.Errorf("bridge is missng a MAC address")
    67  		}
    68  
    69  		name, ok := bridgeMap["name"]
    70  		if !ok {
    71  			return config, fmt.Errorf("bridge is missng a name")
    72  		}
    73  
    74  		b := bridge{
    75  			Name: name,
    76  			MAC:  mac,
    77  		}
    78  
    79  		config.Bridges = append(config.Bridges, b)
    80  	}
    81  
    82  	return config, nil
    83  }
    84  
    85  func getMasterAddresses(dir string) ([]string, error) {
    86  	logrus.Debug("baremetal: getting master addresses")
    87  	masters := []string{}
    88  
    89  	data, err := os.ReadFile(filepath.Join(dir, MastersFileName))
    90  	if err != nil {
    91  		return masters, err
    92  	}
    93  
    94  	hosts := map[string]baremetalhost.BareMetalHost{}
    95  
    96  	err = json.Unmarshal(data, &hosts)
    97  	if err != nil {
    98  		return masters, err
    99  	}
   100  
   101  	for _, bmh := range hosts {
   102  		logrus.Debug("  bmh:", bmh.Name)
   103  
   104  		if bmh.Status.HardwareDetails == nil {
   105  			logrus.Debug("    HardwareDetails nil, skipping")
   106  			continue
   107  		}
   108  
   109  		for _, nic := range bmh.Status.HardwareDetails.NIC {
   110  			masters = append(masters, nic.IP)
   111  		}
   112  	}
   113  
   114  	return masters, nil
   115  }