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

     1  package openshiftinstall
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/pkg/errors"
     9  	corev1 "k8s.io/api/core/v1"
    10  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  	"sigs.k8s.io/yaml"
    12  
    13  	"github.com/openshift/installer/pkg/asset"
    14  	"github.com/openshift/installer/pkg/version"
    15  )
    16  
    17  var (
    18  	configPath = filepath.Join("openshift", "openshift-install-manifests.yaml")
    19  )
    20  
    21  // Config generates the openshift-install ConfigMap.
    22  type Config struct {
    23  	File *asset.File
    24  }
    25  
    26  var _ asset.WritableAsset = (*Config)(nil)
    27  
    28  // Name returns a human friendly name for the asset.
    29  func (*Config) Name() string {
    30  	return "OpenShift Install (Manifests)"
    31  }
    32  
    33  // Dependencies returns all of the dependencies directly needed to generate
    34  // the asset.
    35  func (*Config) Dependencies() []asset.Asset {
    36  	return []asset.Asset{}
    37  }
    38  
    39  // Generate generates the openshift-install ConfigMap.
    40  func (i *Config) Generate(_ context.Context, dependencies asset.Parents) error {
    41  	cm, err := CreateInstallConfigMap("openshift-install-manifests")
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	i.File = &asset.File{
    47  		Filename: configPath,
    48  		Data:     []byte(cm),
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  // Files returns the files generated by the asset.
    55  func (i *Config) Files() []*asset.File {
    56  	if i.File != nil {
    57  		return []*asset.File{i.File}
    58  	}
    59  	return []*asset.File{}
    60  }
    61  
    62  // Load loads the already-rendered files back from disk.
    63  func (i *Config) Load(f asset.FileFetcher) (bool, error) {
    64  	file, err := f.FetchByName(configPath)
    65  	if os.IsNotExist(err) {
    66  		return false, nil
    67  	} else if err != nil {
    68  		return false, err
    69  	}
    70  	i.File = file
    71  	return true, nil
    72  }
    73  
    74  // CreateInstallConfigMap creates an openshift-install ConfigMap from the
    75  // OPENSHIFT_INSTALL_INVOKER environment variable and the given name for the
    76  // ConfigMap. This returns an error if marshalling to YAML fails.
    77  func CreateInstallConfigMap(name string) (string, error) {
    78  	var invoker string
    79  	if env := os.Getenv("OPENSHIFT_INSTALL_INVOKER"); env != "" {
    80  		invoker = env
    81  	} else {
    82  		invoker = "user"
    83  	}
    84  
    85  	cm := &corev1.ConfigMap{
    86  		TypeMeta: metav1.TypeMeta{
    87  			APIVersion: corev1.SchemeGroupVersion.String(),
    88  			Kind:       "ConfigMap",
    89  		},
    90  		ObjectMeta: metav1.ObjectMeta{
    91  			Namespace: "openshift-config",
    92  			Name:      name,
    93  		},
    94  		Data: map[string]string{
    95  			"version": version.Raw,
    96  			"invoker": invoker,
    97  		},
    98  	}
    99  
   100  	cmData, err := yaml.Marshal(cm)
   101  	if err != nil {
   102  		return "", errors.Wrapf(err, "failed to create %q ConfigMap", name)
   103  	}
   104  
   105  	return string(cmData), nil
   106  }