github.com/openshift/installer@v1.4.17/pkg/asset/imagebased/configimage/imagedigestsources.go (about)

     1  package configimage
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"errors"
     7  	"fmt"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	k8sjson "sigs.k8s.io/json"
    12  
    13  	"github.com/openshift/installer/pkg/asset"
    14  	"github.com/openshift/installer/pkg/types"
    15  )
    16  
    17  var (
    18  	imageDigestSourcesFilename = filepath.Join(manifestsDir, "image-digest-sources.json")
    19  
    20  	_ asset.WritableAsset = (*ImageDigestSources)(nil)
    21  )
    22  
    23  // ImageDigestSources generates the image-based installer image digest sources asset.
    24  type ImageDigestSources struct {
    25  	File   *asset.File
    26  	Config []types.ImageDigestSource
    27  }
    28  
    29  // Name returns a human friendly name for the asset.
    30  func (*ImageDigestSources) Name() string {
    31  	return "Image-based installer image digest sources"
    32  }
    33  
    34  // Dependencies returns all of the dependencies directly needed to generate
    35  // the asset.
    36  func (*ImageDigestSources) Dependencies() []asset.Asset {
    37  	return []asset.Asset{
    38  		&InstallConfig{},
    39  	}
    40  }
    41  
    42  // Generate generates the Image-based Installer ImageDigestSources manifest.
    43  func (ids *ImageDigestSources) Generate(_ context.Context, dependencies asset.Parents) error {
    44  	installConfig := &InstallConfig{}
    45  
    46  	dependencies.Get(installConfig)
    47  
    48  	if installConfig.Config == nil || installConfig.Config.ImageDigestSources == nil {
    49  		return nil
    50  	}
    51  
    52  	ids.Config = installConfig.Config.ImageDigestSources
    53  
    54  	imageDigestSourcesData, err := json.Marshal(ids.Config)
    55  	if err != nil {
    56  		return fmt.Errorf("failed to marshal image-based installer ImageDigestSources: %w", err)
    57  	}
    58  
    59  	ids.File = &asset.File{
    60  		Filename: imageDigestSourcesFilename,
    61  		Data:     imageDigestSourcesData,
    62  	}
    63  
    64  	return ids.finish()
    65  }
    66  
    67  // Files returns the files generated by the asset.
    68  func (ids *ImageDigestSources) Files() []*asset.File {
    69  	if ids.File != nil {
    70  		return []*asset.File{ids.File}
    71  	}
    72  	return []*asset.File{}
    73  }
    74  
    75  // Load returns ImageDigestSources asset from the disk.
    76  func (ids *ImageDigestSources) Load(f asset.FileFetcher) (bool, error) {
    77  	file, err := f.FetchByName(imageDigestSourcesFilename)
    78  	if err != nil {
    79  		if os.IsNotExist(err) {
    80  			return false, nil
    81  		}
    82  		return false, fmt.Errorf("failed to load %s file: %w", imageDigestSourcesFilename, err)
    83  	}
    84  
    85  	config := []types.ImageDigestSource{}
    86  	strErrs, err := k8sjson.UnmarshalStrict(file.Data, &config)
    87  	if len(strErrs) > 0 {
    88  		return false, fmt.Errorf("failed to unmarshal %s: %w", imageDigestSourcesFilename, errors.Join(strErrs...))
    89  	}
    90  	if err != nil {
    91  		return false, fmt.Errorf("failed to unmarshal %s: invalid JSON syntax", imageDigestSourcesFilename)
    92  	}
    93  
    94  	ids.File, ids.Config = file, config
    95  	if err = ids.finish(); err != nil {
    96  		return false, err
    97  	}
    98  
    99  	return true, nil
   100  }
   101  
   102  func (ids *ImageDigestSources) finish() error {
   103  	if ids.Config == nil {
   104  		return errors.New("missing configuration or manifest file")
   105  	}
   106  	return nil
   107  }