Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
packet: Move terraform logic to new function
Browse files Browse the repository at this point in the history
This makes Install() more readable and terraform tricks (more will come
in future patches) can be easily added to that function.
  • Loading branch information
rata committed Mar 5, 2020
1 parent 878b482 commit 6b6b012
Showing 1 changed file with 36 additions and 31 deletions.
67 changes: 36 additions & 31 deletions pkg/platform/packet/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,41 +139,11 @@ func (c *config) Install(ex *terraform.Executor) error {

c.AssetDir = assetDir

dnsProvider, err := dns.ParseDNS(&c.DNS)
if err != nil {
return errors.Wrap(err, "parsing DNS configuration failed")
}

if err := c.Initialize(ex); err != nil {
return err
}

// If the provider isn't manual, apply everything in a single step.
if dnsProvider != dns.DNSManual {
return ex.Apply()
}

arguments := []string{"apply", "-auto-approve"}

// Get DNS entries (it forces the creation of the controller nodes).
arguments = append(arguments, fmt.Sprintf("-target=module.packet-%s.null_resource.dns_entries", c.ClusterName))

// Add worker nodes to speed things up.
for index := range c.WorkerPools {
arguments = append(arguments, fmt.Sprintf("-target=module.worker-pool-%d.packet_device.nodes", index))
}

// Create controller and workers nodes.
if err := ex.Execute(arguments...); err != nil {
return errors.Wrap(err, "failed executing Terraform")
}

if err := dns.AskToConfigure(ex, &c.DNS); err != nil {
return errors.Wrap(err, "failed to configure DNS entries")
}

// Finish deployment.
return ex.Apply()
return c.terraformSmartApply(ex)
}

func (c *config) Destroy(ex *terraform.Executor) error {
Expand Down Expand Up @@ -240,6 +210,41 @@ func createTerraformConfigFile(cfg *config, terraformPath string) error {
return nil
}

func (c *config) terraformSmartApply(ex *terraform.Executor) error {

dnsProvider, err := dns.ParseDNS(&c.DNS)
if err != nil {
return errors.Wrap(err, "parsing DNS configuration failed")
}

// If the provider isn't manual, apply everything in a single step.
if dnsProvider != dns.DNSManual {
return ex.Apply()
}

arguments := []string{"apply", "-auto-approve"}

// Get DNS entries (it forces the creation of the controller nodes).
arguments = append(arguments, fmt.Sprintf("-target=module.packet-%s.null_resource.dns_entries", c.ClusterName))

// Add worker nodes to speed things up.
for index := range c.WorkerPools {
arguments = append(arguments, fmt.Sprintf("-target=module.worker-pool-%d.packet_device.nodes", index))
}

// Create controller and workers nodes.
if err := ex.Execute(arguments...); err != nil {
return errors.Wrap(err, "failed executing Terraform")
}

if err := dns.AskToConfigure(ex, &c.DNS); err != nil {
return errors.Wrap(err, "failed to configure DNS entries")
}

// Finish deployment.
return ex.Apply()
}

func (c *config) GetExpectedNodes() int {
workers := 0

Expand Down

0 comments on commit 6b6b012

Please sign in to comment.