github.com/openshift/installer@v1.4.17/pkg/asset/manifests/imagesourcepolicy.go (about)

     1  package manifests
     2  
     3  import (
     4  	"context"
     5  	"path/filepath"
     6  
     7  	"github.com/pkg/errors"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  	"sigs.k8s.io/yaml"
    10  
    11  	operatorv1alpha1 "github.com/openshift/api/operator/v1alpha1"
    12  	"github.com/openshift/installer/pkg/asset"
    13  	"github.com/openshift/installer/pkg/asset/installconfig"
    14  )
    15  
    16  var imageContentSourcePolicyFilename = "image-content-source-policy.yaml"
    17  
    18  // ImageContentSourcePolicy generates the image-content-source-policy.yaml file.
    19  type ImageContentSourcePolicy struct {
    20  	File *asset.File
    21  }
    22  
    23  var _ asset.WritableAsset = (*ImageContentSourcePolicy)(nil)
    24  
    25  // Name returns a human-friendly name for the asset.
    26  func (*ImageContentSourcePolicy) Name() string {
    27  	return "Image Content Source Policy"
    28  }
    29  
    30  // Dependencies returns all of the dependencies directly needed to generate
    31  // the asset.
    32  func (*ImageContentSourcePolicy) Dependencies() []asset.Asset {
    33  	return []asset.Asset{
    34  		&installconfig.InstallConfig{},
    35  	}
    36  }
    37  
    38  // Generate generates the ImageContentSourcePolicy config and its CR.
    39  func (p *ImageContentSourcePolicy) Generate(_ context.Context, dependencies asset.Parents) error {
    40  	installconfig := &installconfig.InstallConfig{}
    41  	dependencies.Get(installconfig)
    42  
    43  	if len(installconfig.Config.DeprecatedImageContentSources) > 0 {
    44  		policy := operatorv1alpha1.ImageContentSourcePolicy{
    45  			TypeMeta: metav1.TypeMeta{
    46  				APIVersion: operatorv1alpha1.SchemeGroupVersion.String(),
    47  				Kind:       "ImageContentSourcePolicy",
    48  			},
    49  			ObjectMeta: metav1.ObjectMeta{
    50  				Name: "image-policy",
    51  				// not namespaced
    52  			},
    53  		}
    54  
    55  		policy.Spec.RepositoryDigestMirrors = make([]operatorv1alpha1.RepositoryDigestMirrors, len(installconfig.Config.DeprecatedImageContentSources))
    56  		for gidx, group := range installconfig.Config.DeprecatedImageContentSources {
    57  			policy.Spec.RepositoryDigestMirrors[gidx] = operatorv1alpha1.RepositoryDigestMirrors{Source: group.Source, Mirrors: group.Mirrors}
    58  		}
    59  
    60  		policyData, err := yaml.Marshal(policy)
    61  		if err != nil {
    62  			return errors.Wrapf(err, "failed to marshal ImageContentSourcePolicy")
    63  		}
    64  		p.File = &asset.File{
    65  			Filename: filepath.Join(manifestDir, imageContentSourcePolicyFilename),
    66  			Data:     policyData,
    67  		}
    68  	}
    69  	return nil
    70  }
    71  
    72  // Files returns the files generated by the asset.
    73  func (p *ImageContentSourcePolicy) Files() []*asset.File {
    74  	if p.File == nil {
    75  		return nil
    76  	}
    77  	return []*asset.File{p.File}
    78  }
    79  
    80  // Load loads the already-rendered files back from disk.
    81  func (p *ImageContentSourcePolicy) Load(f asset.FileFetcher) (bool, error) {
    82  	return false, nil
    83  }