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

     1  package machine
     2  
     3  import (
     4  	"context"
     5  	"path/filepath"
     6  
     7  	"github.com/pkg/errors"
     8  	"github.com/sirupsen/logrus"
     9  	"sigs.k8s.io/yaml"
    10  
    11  	mcfgv1 "github.com/openshift/api/machineconfiguration/v1"
    12  	"github.com/openshift/installer/pkg/asset"
    13  	"github.com/openshift/installer/pkg/asset/ignition"
    14  	"github.com/openshift/installer/pkg/asset/installconfig"
    15  	"github.com/openshift/installer/pkg/asset/tls"
    16  )
    17  
    18  var (
    19  	workerMachineConfigFileName = filepath.Join(directory, "99_openshift-installer-ignition_worker.yaml")
    20  )
    21  
    22  // WorkerIgnitionCustomizations is an asset that checks for any customizations a user might
    23  // have made to the pointer ignition configs before creating the cluster. If customizations
    24  // are made, then the updates are reconciled as a MachineConfig file
    25  type WorkerIgnitionCustomizations struct {
    26  	File *asset.File
    27  }
    28  
    29  var _ asset.WritableAsset = (*WorkerIgnitionCustomizations)(nil)
    30  
    31  // Dependencies returns the dependencies for WorkerIgnitionCustomizations
    32  func (a *WorkerIgnitionCustomizations) Dependencies() []asset.Asset {
    33  	return []asset.Asset{
    34  		&installconfig.InstallConfig{},
    35  		&tls.RootCA{},
    36  		&Worker{},
    37  	}
    38  }
    39  
    40  // Generate queries for input from the user.
    41  func (a *WorkerIgnitionCustomizations) Generate(_ context.Context, dependencies asset.Parents) error {
    42  	installConfig := &installconfig.InstallConfig{}
    43  	rootCA := &tls.RootCA{}
    44  	worker := &Worker{}
    45  	dependencies.Get(installConfig, rootCA, worker)
    46  
    47  	defaultPointerIgnition := pointerIgnitionConfig(installConfig.Config, rootCA.Cert(), "worker")
    48  	savedPointerIgnition := worker.Config
    49  
    50  	// Create a machineconfig if the ignition has been modified
    51  	savedPointerIgnitionJSON, err := ignition.Marshal(savedPointerIgnition)
    52  	if err != nil {
    53  		return errors.Wrap(err, "failed Marshal savedPointerIgnition")
    54  	}
    55  	defaultPointerIgnitionJSON, err := ignition.Marshal(defaultPointerIgnition)
    56  	if err != nil {
    57  		return errors.Wrap(err, "failed Marshal defaultPointerIgnition")
    58  	}
    59  	if string(savedPointerIgnitionJSON) != string(defaultPointerIgnitionJSON) {
    60  		logrus.Infof("Worker pointer ignition was modified. Saving contents to a machineconfig")
    61  		mc := &mcfgv1.MachineConfig{}
    62  		mc, err = generatePointerMachineConfig(*savedPointerIgnition, "worker")
    63  		if err != nil {
    64  			return errors.Wrap(err, "failed to generate worker installer machineconfig")
    65  		}
    66  		configData, err := yaml.Marshal(mc)
    67  		if err != nil {
    68  			return errors.Wrap(err, "failed to marshal worker installer machineconfig")
    69  		}
    70  		a.File = &asset.File{
    71  			Filename: workerMachineConfigFileName,
    72  			Data:     configData,
    73  		}
    74  	}
    75  
    76  	return nil
    77  }
    78  
    79  // Name returns the human-friendly name of the asset.
    80  func (a *WorkerIgnitionCustomizations) Name() string {
    81  	return "Worker Ignition Customization Check"
    82  }
    83  
    84  // Files returns the files generated by the asset.
    85  func (a *WorkerIgnitionCustomizations) Files() []*asset.File {
    86  	if a.File != nil {
    87  		return []*asset.File{a.File}
    88  	}
    89  	return []*asset.File{}
    90  }
    91  
    92  // Load does nothing, since we consume the ignition-configs
    93  func (a *WorkerIgnitionCustomizations) Load(f asset.FileFetcher) (found bool, err error) {
    94  	return false, nil
    95  }