Skip to content

Commit

Permalink
pkg: add ClusterMetadata asset,type that can be used for destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavdahiya committed Sep 25, 2018
1 parent 008b619 commit 78c3118
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/asset/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand Down
1 change: 1 addition & 0 deletions pkg/asset/cluster/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
9 changes: 6 additions & 3 deletions pkg/asset/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions pkg/asset/metadata/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
],
)
2 changes: 2 additions & 0 deletions pkg/asset/metadata/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package metadata contains asset targets that generates the metadata.yaml
package metadata
74 changes: 74 additions & 0 deletions pkg/asset/metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 31 additions & 0 deletions pkg/asset/metadata/stock.go
Original file line number Diff line number Diff line change
@@ -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 }
1 change: 1 addition & 0 deletions pkg/asset/stock/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
8 changes: 7 additions & 1 deletion pkg/asset/stock/stock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -17,6 +18,7 @@ type Stock struct {
ignitionStock
clusterStock
manifestsStock
metadataStock
}

type installConfigStock struct {
Expand All @@ -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.
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion pkg/asset/store.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package asset

import (
"github.com/Sirupsen/logrus"
"github.com/sirupsen/logrus"
)

// Store is a store for the states of assets.
Expand Down
1 change: 1 addition & 0 deletions pkg/types/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 26 additions & 0 deletions pkg/types/clustermetadata.go
Original file line number Diff line number Diff line change
@@ -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"`
}

0 comments on commit 78c3118

Please sign in to comment.