github.com/openshift/installer@v1.4.17/pkg/asset/installconfig/installconfigbase.go (about)

     1  package installconfig
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	"github.com/pkg/errors"
     8  	"github.com/sirupsen/logrus"
     9  	"sigs.k8s.io/yaml"
    10  
    11  	"github.com/openshift/installer/pkg/asset"
    12  	"github.com/openshift/installer/pkg/types"
    13  	"github.com/openshift/installer/pkg/types/conversion"
    14  	"github.com/openshift/installer/pkg/types/defaults"
    15  )
    16  
    17  // AssetBase is the base structure for the separate InstallConfig assets used
    18  // in the agent-based and IPI/UPI installation methods.
    19  type AssetBase struct {
    20  	Config *types.InstallConfig `json:"config"`
    21  	File   *asset.File          `json:"file"`
    22  }
    23  
    24  // Files returns the files generated by the asset.
    25  func (a *AssetBase) Files() []*asset.File {
    26  	if a.File != nil {
    27  		return []*asset.File{a.File}
    28  	}
    29  	return []*asset.File{}
    30  }
    31  
    32  // Name returns the human-friendly name of the asset.
    33  func (a *AssetBase) Name() string {
    34  	return "Install Config"
    35  }
    36  
    37  // LoadFromFile returns the installconfig from disk.
    38  func (a *AssetBase) LoadFromFile(f asset.FileFetcher) (found bool, err error) {
    39  	file, err := f.FetchByName(installConfigFilename)
    40  	if err != nil {
    41  		if os.IsNotExist(err) {
    42  			return false, nil
    43  		}
    44  		return false, errors.Wrap(err, asset.InstallConfigError)
    45  	}
    46  
    47  	config := &types.InstallConfig{}
    48  	if err := yaml.UnmarshalStrict(file.Data, config, yaml.DisallowUnknownFields); err != nil {
    49  		err = errors.Wrapf(err, "failed to unmarshal %s", installConfigFilename)
    50  		if !strings.Contains(err.Error(), "unknown field") {
    51  			return false, errors.Wrap(err, asset.InstallConfigError)
    52  		}
    53  		err = errors.Wrapf(err, "failed to parse first occurrence of unknown field")
    54  		logrus.Warnf(err.Error())
    55  		logrus.Info("Attempting to unmarshal while ignoring unknown keys because strict unmarshaling failed")
    56  		if err = yaml.Unmarshal(file.Data, config); err != nil {
    57  			err = errors.Wrapf(err, "failed to unmarshal %s", installConfigFilename)
    58  			return false, errors.Wrap(err, asset.InstallConfigError)
    59  		}
    60  	}
    61  	a.Config = config
    62  
    63  	// Upconvert any deprecated fields
    64  	if err := conversion.ConvertInstallConfig(a.Config); err != nil {
    65  		return false, errors.Wrap(errors.Wrap(err, "failed to upconvert install config"), asset.InstallConfigError)
    66  	}
    67  
    68  	defaults.SetInstallConfigDefaults(a.Config)
    69  
    70  	return true, nil
    71  }
    72  
    73  // RecordFile generates the asset manifest file from the config CR.
    74  func (a *AssetBase) RecordFile() error {
    75  	data, err := yaml.Marshal(a.Config)
    76  	if err != nil {
    77  		return errors.Wrap(err, "failed to Marshal InstallConfig")
    78  	}
    79  	a.File = &asset.File{
    80  		Filename: installConfigFilename,
    81  		Data:     data,
    82  	}
    83  	return nil
    84  }