diff --git a/pkg/asset/BUILD.bazel b/pkg/asset/BUILD.bazel index 4ecfea4281d..6efbeb1c2ed 100644 --- a/pkg/asset/BUILD.bazel +++ b/pkg/asset/BUILD.bazel @@ -13,7 +13,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "//vendor/github.com/AlecAivazis/survey:go_default_library", - "//vendor/github.com/Sirupsen/logrus:go_default_library", + "//vendor/github.com/sirupsen/logrus:go_default_library", ], ) diff --git a/pkg/asset/cluster/BUILD.bazel b/pkg/asset/cluster/BUILD.bazel index c0fc52d15c5..8fa808439e1 100644 --- a/pkg/asset/cluster/BUILD.bazel +++ b/pkg/asset/cluster/BUILD.bazel @@ -17,5 +17,6 @@ go_library( "//pkg/asset/kubeconfig:go_default_library", "//pkg/terraform:go_default_library", "//pkg/types/config:go_default_library", + "//vendor/github.com/sirupsen/logrus:go_default_library", ], ) diff --git a/pkg/asset/cluster/cluster.go b/pkg/asset/cluster/cluster.go index dcf0f96a260..5764374a0e9 100644 --- a/pkg/asset/cluster/cluster.go +++ b/pkg/asset/cluster/cluster.go @@ -7,6 +7,8 @@ import ( "os" "path/filepath" + log "github.com/sirupsen/logrus" + "github.com/openshift/installer/pkg/asset" "github.com/openshift/installer/pkg/terraform" "github.com/openshift/installer/pkg/types/config" @@ -42,7 +44,7 @@ func (c *Cluster) Dependencies() []asset.Asset { func (c *Cluster) Generate(parents map[asset.Asset]*asset.State) (*asset.State, error) { dir, err := terraform.BaseLocation() if err != nil { - return nil, err + return nil, fmt.Errorf("error finding baselocation for terraform: %v", err) } state, ok := parents[c.tfvars] @@ -68,7 +70,7 @@ func (c *Cluster) Generate(parents map[asset.Asset]*asset.State) (*asset.State, templateDir, err := terraform.FindStepTemplates(dir, terraform.InfraStep, tfvars.Platform) if err != nil { - return nil, err + return nil, fmt.Errorf("error finding terraform templates: %v", err) } // This runs the terraform in a temp directory, the tfstate file will be returned @@ -79,7 +81,8 @@ func (c *Cluster) Generate(parents map[asset.Asset]*asset.State) (*asset.State, stateFile, err := terraform.Apply(tmpDir, terraform.InfraStep, templateDir) if err != nil { - return nil, err + // we should try to fetch the terraform state file. + log.Errorf("terraform failed: %v", err) } data, err := ioutil.ReadFile(stateFile) diff --git a/pkg/asset/metadata/BUILD.bazel b/pkg/asset/metadata/BUILD.bazel new file mode 100644 index 00000000000..0e9e4cbc3c8 --- /dev/null +++ b/pkg/asset/metadata/BUILD.bazel @@ -0,0 +1,18 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = [ + "doc.go", + "metadata.go", + "stock.go", + ], + importpath = "github.com/openshift/installer/pkg/asset/metadata", + visibility = ["//visibility:public"], + deps = [ + "//pkg/asset:go_default_library", + "//pkg/asset/cluster:go_default_library", + "//pkg/asset/installconfig:go_default_library", + "//pkg/types:go_default_library", + ], +) diff --git a/pkg/asset/metadata/doc.go b/pkg/asset/metadata/doc.go new file mode 100644 index 00000000000..7ca2d310e36 --- /dev/null +++ b/pkg/asset/metadata/doc.go @@ -0,0 +1,2 @@ +// Package metadata contains asset targets that generates the metadata.yaml +package metadata diff --git a/pkg/asset/metadata/metadata.go b/pkg/asset/metadata/metadata.go new file mode 100644 index 00000000000..eb1d969cd3d --- /dev/null +++ b/pkg/asset/metadata/metadata.go @@ -0,0 +1,74 @@ +package metadata + +import ( + "encoding/json" + "fmt" + + "github.com/openshift/installer/pkg/asset" + "github.com/openshift/installer/pkg/asset/installconfig" + "github.com/openshift/installer/pkg/types" +) + +const ( + // MetadataFilename is name of the file where clustermetadata is stored. + MetadataFilename = "metadata.json" + metadataAssetName = "Cluster Metadata" +) + +// Metadata depends on cluster and installconfig, +type Metadata struct { + installConfig asset.Asset + cluster asset.Asset +} + +var _ asset.Asset = (*Metadata)(nil) + +// Name returns the human-friendly name of the asset. +func (m *Metadata) Name() string { + return metadataAssetName +} + +// Dependencies returns the dependency of the MetaData. +func (m *Metadata) Dependencies() []asset.Asset { + return []asset.Asset{m.installConfig, m.cluster} +} + +// Generate generates the metadata.yaml file. +func (m *Metadata) Generate(parents map[asset.Asset]*asset.State) (*asset.State, error) { + installCfg, err := installconfig.GetInstallConfig(m.installConfig, parents) + if err != nil { + return nil, err + } + + cm := &types.ClusterMetadata{ + ClusterName: installCfg.Name, + } + switch { + case installCfg.Platform.AWS != nil: + cm.ClusterPlatformMetadata.AWS = &types.ClusterAWSPlatformMetadata{ + Region: installCfg.Platform.AWS.Region, + Identifier: map[string]string{ + "tectonicClusterID": installCfg.ClusterID, + }, + } + case installCfg.Platform.Libvirt != nil: + cm.ClusterPlatformMetadata.Libvirt = &types.ClusterLibvirtPlatformMetadata{ + URI: installCfg.Platform.Libvirt.URI, + } + default: + return nil, fmt.Errorf("no known platform") + } + + data, err := json.Marshal(cm) + if err != nil { + return nil, err + } + return &asset.State{ + Contents: []asset.Content{ + { + Name: MetadataFilename, + Data: []byte(data), + }, + }, + }, nil +} diff --git a/pkg/asset/metadata/stock.go b/pkg/asset/metadata/stock.go new file mode 100644 index 00000000000..e4e142fc902 --- /dev/null +++ b/pkg/asset/metadata/stock.go @@ -0,0 +1,31 @@ +package metadata + +import ( + "github.com/openshift/installer/pkg/asset" + "github.com/openshift/installer/pkg/asset/cluster" + "github.com/openshift/installer/pkg/asset/installconfig" +) + +// Stock is the stock of the cluster assets that can be generated. +type Stock interface { + // Metadata is the asset that generates the metadata.json file + Metadata() asset.Asset +} + +// StockImpl is the implementation of the cluster asset stock. +type StockImpl struct { + metadata asset.Asset +} + +var _ Stock = (*StockImpl)(nil) + +// EstablishStock establishes the stock of assets in the specified directory. +func (s *StockImpl) EstablishStock(installConfigStock installconfig.Stock, clusterStock cluster.Stock) { + s.metadata = &Metadata{ + installConfig: installConfigStock.InstallConfig(), + cluster: clusterStock.Cluster(), + } +} + +// Metadata returns the terraform tfvar asset. +func (s *StockImpl) Metadata() asset.Asset { return s.metadata } diff --git a/pkg/asset/stock/BUILD.bazel b/pkg/asset/stock/BUILD.bazel index 605f83aa8ee..de805fe0bc8 100644 --- a/pkg/asset/stock/BUILD.bazel +++ b/pkg/asset/stock/BUILD.bazel @@ -14,6 +14,7 @@ go_library( "//pkg/asset/installconfig:go_default_library", "//pkg/asset/kubeconfig:go_default_library", "//pkg/asset/manifests:go_default_library", + "//pkg/asset/metadata:go_default_library", "//pkg/asset/tls:go_default_library", ], ) diff --git a/pkg/asset/stock/stock.go b/pkg/asset/stock/stock.go index 7ca887cfd18..7ebda974cdd 100644 --- a/pkg/asset/stock/stock.go +++ b/pkg/asset/stock/stock.go @@ -6,6 +6,7 @@ import ( "github.com/openshift/installer/pkg/asset/installconfig" "github.com/openshift/installer/pkg/asset/kubeconfig" "github.com/openshift/installer/pkg/asset/manifests" + "github.com/openshift/installer/pkg/asset/metadata" "github.com/openshift/installer/pkg/asset/tls" ) @@ -17,6 +18,7 @@ type Stock struct { ignitionStock clusterStock manifestsStock + metadataStock } type installConfigStock struct { @@ -43,6 +45,10 @@ type manifestsStock struct { manifests.StockImpl } +type metadataStock struct { + metadata.StockImpl +} + var _ installconfig.Stock = (*Stock)(nil) // EstablishStock establishes the stock of assets. @@ -55,6 +61,6 @@ func EstablishStock() *Stock { s.ignitionStock.EstablishStock(s, s, s, s) s.clusterStock.EstablishStock(s, s, s) s.manifestsStock.EstablishStock(&s.installConfigStock, s, s) - + s.metadataStock.EstablishStock(s, s) return s } diff --git a/pkg/asset/store.go b/pkg/asset/store.go index 0964b1d084a..81106efc60b 100644 --- a/pkg/asset/store.go +++ b/pkg/asset/store.go @@ -1,7 +1,7 @@ package asset import ( - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" ) // Store is a store for the states of assets. diff --git a/pkg/types/BUILD.bazel b/pkg/types/BUILD.bazel index 05748e49200..4a6b7aaf5bb 100644 --- a/pkg/types/BUILD.bazel +++ b/pkg/types/BUILD.bazel @@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "go_default_library", srcs = [ + "clustermetadata.go", "doc.go", "installconfig.go", "machinepools.go", diff --git a/pkg/types/clustermetadata.go b/pkg/types/clustermetadata.go new file mode 100644 index 00000000000..332bdd66ab4 --- /dev/null +++ b/pkg/types/clustermetadata.go @@ -0,0 +1,26 @@ +package types + +// ClusterMetadata contains information +// regarding the cluster that was created by installer. +type ClusterMetadata struct { + ClusterName string `json:"clusterName"` + ClusterPlatformMetadata `json:",inline"` +} + +// ClusterPlatformMetadata contains metadata for platfrom. +type ClusterPlatformMetadata struct { + AWS *ClusterAWSPlatformMetadata `json:"aws,omitempty"` + Libvirt *ClusterLibvirtPlatformMetadata `json:"libvirt,omitempty"` +} + +// ClusterAWSPlatformMetadata contains AWS metadata. +type ClusterAWSPlatformMetadata struct { + Region string `json:"region"` + // Most AWS resources are tagged with these tags as identifier. + Identifier map[string]string `json:"identifier"` +} + +// ClusterLibvirtPlatformMetadata contains libvirt metadata. +type ClusterLibvirtPlatformMetadata struct { + URI string `json:"uri"` +}