Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ type WritableAsset interface {
Files() []*File
}

// TargetableAsset is a WritableAsset that has an Apply logic to it
type TargetableAsset interface {
WritableAsset

// Apply performs any real time actions that the asset wants to do
Apply(Parents) error
}

// File is a file for an Asset.
type File struct {
// Filename is the name of the file.
Expand Down
47 changes: 28 additions & 19 deletions pkg/asset/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Cluster struct {
FileList []*asset.File
}

var _ asset.WritableAsset = (*Cluster)(nil)
var _ asset.TargetableAsset = (*Cluster)(nil)

// Name returns the human-friendly name of the asset.
func (c *Cluster) Name() string {
Expand All @@ -55,23 +55,6 @@ func (c *Cluster) Generate(parents asset.Parents) (err error) {
adminKubeconfig := &kubeconfig.Admin{}
parents.Get(installConfig, terraformVariables, adminKubeconfig)

// Copy the terraform.tfvars to a temp directory where the terraform will be invoked within.
tmpDir, err := ioutil.TempDir(os.TempDir(), "openshift-install-")
if err != nil {
return errors.Wrap(err, "failed to create temp dir for terraform execution")
}
defer os.RemoveAll(tmpDir)

terraformVariablesFile := terraformVariables.Files()[0]
if err := ioutil.WriteFile(filepath.Join(tmpDir, terraformVariablesFile.Filename), terraformVariablesFile.Data, 0600); err != nil {
return errors.Wrap(err, "failed to write terraform.tfvars file")
}

platform := terraformVariables.Platform
if err := data.Unpack(tmpDir, platform); err != nil {
return err
}

metadata := &types.ClusterMetadata{
ClusterName: installConfig.Config.ObjectMeta.Name,
}
Expand Down Expand Up @@ -116,10 +99,36 @@ func (c *Cluster) Generate(parents asset.Parents) (err error) {
return fmt.Errorf("no known platform")
}

if err := data.Unpack(filepath.Join(tmpDir, "config.tf"), "config.tf"); err != nil {
return err
}

// Apply performs the actual action of creating the cluster
func (c *Cluster) Apply(parents asset.Parents) (err error) {
installConfig := &installconfig.InstallConfig{}
terraformVariables := &TerraformVariables{}
adminKubeconfig := &kubeconfig.Admin{}
parents.Get(installConfig, terraformVariables, adminKubeconfig)

// Copy the terraform.tfvars to a temp directory where the terraform will be invoked within.
tmpDir, err := ioutil.TempDir(os.TempDir(), "openshift-install-")
if err != nil {
return errors.Wrap(err, "failed to create temp dir for terraform execution")
}
defer os.RemoveAll(tmpDir)

terraformVariablesFile := terraformVariables.Files()[0]
if err := ioutil.WriteFile(filepath.Join(tmpDir, terraformVariablesFile.Filename), terraformVariablesFile.Data, 0600); err != nil {
return errors.Wrap(err, "failed to write terraform.tfvars file")
}

platform := terraformVariables.Platform
if err := data.Unpack(tmpDir, platform); err != nil {
return err
}

if err := data.Unpack(filepath.Join(tmpDir, "config.tf"), "config.tf"); err != nil {
return err
}
logrus.Infof("Using Terraform to create cluster...")

// This runs the terraform in a temp directory, the tfstate file will be returned
Expand Down
19 changes: 13 additions & 6 deletions pkg/asset/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ func (s *StoreImpl) Save(dir string) error {
return nil
}

func (s *StoreImpl) fetch(asset Asset, indent string) error {
func (s *StoreImpl) fetch(asset Asset, indent string) (err error) {
logrus.Debugf("%sFetching %s...", indent, asset.Name())
storedAsset, ok := s.assets[reflect.TypeOf(asset)]
if ok {
logrus.Debugf("%sFound %s...", indent, asset.Name())
reflect.ValueOf(asset).Elem().Set(reflect.ValueOf(storedAsset).Elem())
return nil
return err
}

dependencies := asset.Dependencies()
Expand All @@ -101,7 +101,7 @@ func (s *StoreImpl) fetch(asset Asset, indent string) error {
logrus.Debugf("%sGenerating dependencies of %s...", indent, asset.Name())
}
for _, d := range dependencies {
err := s.fetch(d, indent+" ")
err = s.fetch(d, indent+" ")
if err != nil {
return errors.Wrapf(err, "failed to fetch dependency for %s", asset.Name())
}
Expand All @@ -111,15 +111,15 @@ func (s *StoreImpl) fetch(asset Asset, indent string) error {
// Before generating the asset, look if we have it all ready in the state file
// if yes, then use it instead
logrus.Debugf("%sLooking up asset from state file: %s", indent, reflect.TypeOf(asset).String())
ok, err := s.GetStateAsset(asset)
ok, err = s.GetStateAsset(asset)
if err != nil {
return errors.Wrapf(err, "failed to unmarshal asset %q from state file %q", asset.Name(), stateFileName)
}
if ok {
logrus.Debugf("%sAsset found in state file", indent)
} else {
logrus.Debugf("%sAsset not found in state file. Generating %s...", indent, asset.Name())
err := asset.Generate(parents)
err = asset.Generate(parents)
if err != nil {
return errors.Wrapf(err, "failed to generate asset %s", asset.Name())
}
Expand All @@ -128,5 +128,12 @@ func (s *StoreImpl) fetch(asset Asset, indent string) error {
s.assets = make(map[reflect.Type]Asset)
}
s.assets[reflect.TypeOf(asset)] = asset
return nil

// whether we generate the asset, or pick it up from the disk, or get it from state file,
// we call 'Apply' if the asset is targetable
if target, ok := asset.(TargetableAsset); ok {
err = target.Apply(parents)
}

return err
}