diff --git a/cmd/openshift-install/create.go b/cmd/openshift-install/create.go index 37a5f6c2845..1db7e8c2edb 100644 --- a/cmd/openshift-install/create.go +++ b/cmd/openshift-install/create.go @@ -81,7 +81,7 @@ var ( // FIXME: add longer descriptions for our commands with examples for better UX. // Long: "", }, - assets: []asset.WritableAsset{&bootstrap.Bootstrap{}, &machine.Master{}, &machine.Worker{}, &kubeconfig.Admin{}}, + assets: []asset.WritableAsset{&bootstrap.Bootstrap{}, &machine.Master{}, &machine.Worker{}, &kubeconfig.Admin{}, &cluster.Metadata{}}, } clusterTarget = target{ @@ -118,7 +118,7 @@ var ( } }, }, - assets: []asset.WritableAsset{&cluster.TerraformVariables{}, &kubeconfig.Admin{}, &tls.JournalCertKey{}, &cluster.Cluster{}}, + assets: []asset.WritableAsset{&cluster.TerraformVariables{}, &kubeconfig.Admin{}, &tls.JournalCertKey{}, &cluster.Metadata{}, &cluster.Cluster{}}, } targets = []target{installConfigTarget, manifestTemplatesTarget, manifestsTarget, ignitionConfigsTarget, clusterTarget} diff --git a/pkg/asset/cluster/cluster.go b/pkg/asset/cluster/cluster.go index 7b6e8b223b6..6ebf997a74b 100644 --- a/pkg/asset/cluster/cluster.go +++ b/pkg/asset/cluster/cluster.go @@ -1,8 +1,6 @@ package cluster import ( - "encoding/json" - "fmt" "io/ioutil" "os" "path/filepath" @@ -11,18 +9,9 @@ import ( "github.com/sirupsen/logrus" "github.com/openshift/installer/pkg/asset" - "github.com/openshift/installer/pkg/asset/cluster/aws" - "github.com/openshift/installer/pkg/asset/cluster/libvirt" - "github.com/openshift/installer/pkg/asset/cluster/openstack" "github.com/openshift/installer/pkg/asset/installconfig" "github.com/openshift/installer/pkg/asset/password" "github.com/openshift/installer/pkg/terraform" - "github.com/openshift/installer/pkg/types" -) - -const ( - // metadataFileName is name of the file where clustermetadata is stored. - metadataFileName = "metadata.json" ) var ( @@ -78,41 +67,11 @@ func (c *Cluster) Generate(parents asset.Parents) (err error) { return errors.Wrap(err, "failed to write terraform.tfvars file") } - metadata := &types.ClusterMetadata{ - ClusterName: installConfig.Config.ObjectMeta.Name, - ClusterID: clusterID.ClusterID, - } - - defer func() { - if data, err2 := json.Marshal(metadata); err2 == nil { - c.FileList = append(c.FileList, &asset.File{ - Filename: metadataFileName, - Data: data, - }) - } else { - err2 = errors.Wrap(err2, "failed to Marshal ClusterMetadata") - if err == nil { - err = err2 - } else { - logrus.Error(err2) - } - } - c.FileList = append(c.FileList, &asset.File{ + c.FileList = []*asset.File{ + { Filename: kubeadminPasswordPath, Data: []byte(kubeadminPassword.Password), - }) - // serialize metadata and stuff it into c.FileList - }() - - switch { - case installConfig.Config.Platform.AWS != nil: - metadata.ClusterPlatformMetadata.AWS = aws.Metadata(clusterID.ClusterID, installConfig.Config) - case installConfig.Config.Platform.Libvirt != nil: - metadata.ClusterPlatformMetadata.Libvirt = libvirt.Metadata(installConfig.Config) - case installConfig.Config.Platform.OpenStack != nil: - metadata.ClusterPlatformMetadata.OpenStack = openstack.Metadata(clusterID.ClusterID, installConfig.Config) - default: - return fmt.Errorf("no known platform") + }, } logrus.Infof("Creating cluster...") @@ -154,19 +113,5 @@ func (c *Cluster) Load(f asset.FileFetcher) (found bool, err error) { return false, err } - return true, fmt.Errorf("%q already exists. There may already be a running cluster", terraform.StateFileName) -} - -// LoadMetadata loads the cluster metadata from an asset directory. -func LoadMetadata(dir string) (cmetadata *types.ClusterMetadata, err error) { - raw, err := ioutil.ReadFile(filepath.Join(dir, metadataFileName)) - if err != nil { - return nil, errors.Wrapf(err, "failed to read %s file", metadataFileName) - } - - if err = json.Unmarshal(raw, &cmetadata); err != nil { - return nil, errors.Wrapf(err, "failed to Unmarshal data from %s file to types.ClusterMetadata", metadataFileName) - } - - return cmetadata, err + return true, errors.Errorf("%q already exists. There may already be a running cluster", terraform.StateFileName) } diff --git a/pkg/asset/cluster/metadata.go b/pkg/asset/cluster/metadata.go new file mode 100644 index 00000000000..753fbe1c25f --- /dev/null +++ b/pkg/asset/cluster/metadata.go @@ -0,0 +1,109 @@ +package cluster + +import ( + "encoding/json" + "io/ioutil" + "path/filepath" + + "github.com/openshift/installer/pkg/asset" + "github.com/openshift/installer/pkg/asset/cluster/aws" + "github.com/openshift/installer/pkg/asset/cluster/libvirt" + "github.com/openshift/installer/pkg/asset/cluster/openstack" + "github.com/openshift/installer/pkg/asset/installconfig" + "github.com/openshift/installer/pkg/types" + "github.com/pkg/errors" +) + +const ( + metadataFileName = "metadata.json" +) + +// Metadata contains information needed to destroy clusters. +type Metadata struct { + file *asset.File +} + +var _ asset.WritableAsset = (*Metadata)(nil) + +// Name returns the human-friendly name of the asset. +func (m *Metadata) Name() string { + return "Metadata" +} + +// Dependencies returns the direct dependencies for the metadata +// asset. +func (m *Metadata) Dependencies() []asset.Asset { + return []asset.Asset{ + &installconfig.ClusterID{}, + &installconfig.InstallConfig{}, + } +} + +// Generate generates the metadata asset. +func (m *Metadata) Generate(parents asset.Parents) (err error) { + clusterID := &installconfig.ClusterID{} + installConfig := &installconfig.InstallConfig{} + parents.Get(clusterID, installConfig) + + if installConfig.Config.Platform.None != nil { + return nil + } + + metadata := &types.ClusterMetadata{ + ClusterName: installConfig.Config.ObjectMeta.Name, + ClusterID: clusterID.ClusterID, + } + + switch { + case installConfig.Config.Platform.AWS != nil: + metadata.ClusterPlatformMetadata.AWS = aws.Metadata(clusterID.ClusterID, installConfig.Config) + case installConfig.Config.Platform.Libvirt != nil: + metadata.ClusterPlatformMetadata.Libvirt = libvirt.Metadata(installConfig.Config) + case installConfig.Config.Platform.OpenStack != nil: + metadata.ClusterPlatformMetadata.OpenStack = openstack.Metadata(clusterID.ClusterID, installConfig.Config) + default: + return errors.Errorf("no known platform") + } + + data, err := json.Marshal(metadata) + if err != nil { + return errors.Wrap(err, "failed to Marshal ClusterMetadata") + } + + m.file = &asset.File{ + Filename: metadataFileName, + Data: data, + } + + return nil +} + +// Files returns the FileList generated by the asset. +func (m *Metadata) Files() []*asset.File { + if m.file != nil { + return []*asset.File{m.file} + } + return []*asset.File{} +} + +// Load is a no-op, because we never want to load broken metadata from +// the disk. +func (m *Metadata) Load(f asset.FileFetcher) (found bool, err error) { + return false, nil +} + +// LoadMetadata loads the cluster metadata from an asset directory. +func LoadMetadata(dir string) (*types.ClusterMetadata, error) { + path := filepath.Join(dir, metadataFileName) + raw, err := ioutil.ReadFile(path) + if err != nil { + return nil, err + } + + var metadata *types.ClusterMetadata + if err = json.Unmarshal(raw, &metadata); err != nil { + return nil, errors.Wrapf(err, "failed to Unmarshal data from %q to types.ClusterMetadata", path) + } + + return metadata, err +}