github.com/openshift/installer@v1.4.17/pkg/asset/ignition/machine/master.go (about)

     1  package machine
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"os"
     7  
     8  	igntypes "github.com/coreos/ignition/v2/config/v3_2/types"
     9  	"github.com/pkg/errors"
    10  
    11  	"github.com/openshift/installer/pkg/asset"
    12  	"github.com/openshift/installer/pkg/asset/ignition"
    13  	"github.com/openshift/installer/pkg/asset/installconfig"
    14  	"github.com/openshift/installer/pkg/asset/tls"
    15  )
    16  
    17  const (
    18  	masterIgnFilename = "master.ign"
    19  )
    20  
    21  // Master is an asset that generates the ignition config for master nodes.
    22  type Master struct {
    23  	Config *igntypes.Config
    24  	File   *asset.File
    25  }
    26  
    27  var _ asset.WritableAsset = (*Master)(nil)
    28  
    29  // Dependencies returns the assets on which the Master asset depends.
    30  func (a *Master) Dependencies() []asset.Asset {
    31  	return []asset.Asset{
    32  		&installconfig.InstallConfig{},
    33  		&tls.RootCA{},
    34  	}
    35  }
    36  
    37  // Generate generates the ignition config for the Master asset.
    38  func (a *Master) Generate(_ context.Context, dependencies asset.Parents) error {
    39  	installConfig := &installconfig.InstallConfig{}
    40  	rootCA := &tls.RootCA{}
    41  	dependencies.Get(installConfig, rootCA)
    42  
    43  	a.Config = pointerIgnitionConfig(installConfig.Config, rootCA.Cert(), "master")
    44  
    45  	data, err := ignition.Marshal(a.Config)
    46  	if err != nil {
    47  		return errors.Wrap(err, "failed to marshal Ignition config")
    48  	}
    49  	a.File = &asset.File{
    50  		Filename: masterIgnFilename,
    51  		Data:     data,
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  // Name returns the human-friendly name of the asset.
    58  func (a *Master) Name() string {
    59  	return "Master Ignition Config"
    60  }
    61  
    62  // Files returns the files generated by the asset.
    63  func (a *Master) Files() []*asset.File {
    64  	if a.File != nil {
    65  		return []*asset.File{a.File}
    66  	}
    67  	return []*asset.File{}
    68  }
    69  
    70  // Load returns the master ignitions from disk.
    71  func (a *Master) Load(f asset.FileFetcher) (found bool, err error) {
    72  	file, err := f.FetchByName(masterIgnFilename)
    73  	if err != nil {
    74  		if os.IsNotExist(err) {
    75  			return false, nil
    76  		}
    77  		return false, err
    78  	}
    79  
    80  	config := &igntypes.Config{}
    81  	if err := json.Unmarshal(file.Data, config); err != nil {
    82  		return false, errors.Wrapf(err, "failed to unmarshal %s", masterIgnFilename)
    83  	}
    84  
    85  	a.File, a.Config = file, config
    86  	return true, nil
    87  }