From 6b6b01228b991822d6b927f7f088571317afd1dd Mon Sep 17 00:00:00 2001 From: Rodrigo Campos Date: Thu, 5 Mar 2020 19:59:16 -0300 Subject: [PATCH] packet: Move terraform logic to new function This makes Install() more readable and terraform tricks (more will come in future patches) can be easily added to that function. --- pkg/platform/packet/packet.go | 67 +++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/pkg/platform/packet/packet.go b/pkg/platform/packet/packet.go index bc0fb01c1..d46b8dfcb 100644 --- a/pkg/platform/packet/packet.go +++ b/pkg/platform/packet/packet.go @@ -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 { @@ -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