-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1763 from abhinavdahiya/gather_update
gather: update the state handling for terraform 0.12
- Loading branch information
Showing
6 changed files
with
264 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package exec | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
|
||
"github.com/hashicorp/terraform/states/statefile" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// ReadState reads the terraform state from file and returns the contents in bytes | ||
// It returns an error if reading the state was unsuccessful | ||
// ReadState utilizes the terraform's internal wiring to upconvert versions of terraform state to return | ||
// the state it currently recognizes. | ||
func ReadState(file string) ([]byte, error) { | ||
f, err := os.Open(file) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to open %q", file) | ||
} | ||
defer f.Close() | ||
|
||
sf, err := statefile.Read(f) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to read statefile from %q", file) | ||
} | ||
|
||
out := bytes.Buffer{} | ||
if err := statefile.Write(sf, &out); err != nil { | ||
return nil, errors.Wrapf(err, "failed to write statefile") | ||
} | ||
return out.Bytes(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Package aws contains utilities that help gather AWS specific | ||
// information from terraform state. | ||
package aws | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||
|
||
"github.com/openshift/installer/pkg/terraform" | ||
) | ||
|
||
// BootstrapIP returns the ip address for bootstrap host. | ||
func BootstrapIP(tfs *terraform.State) (string, error) { | ||
br, err := terraform.LookupResource(tfs, "module.bootstrap", "aws_instance", "bootstrap") | ||
if err != nil { | ||
return "", errors.Wrap(err, "failed to lookup bootstrap") | ||
} | ||
if len(br.Instances) == 0 { | ||
return "", errors.New("no bootstrap instance found") | ||
} | ||
bootstrap, _, err := unstructured.NestedString(br.Instances[0].Attributes, "public_ip") | ||
if err != nil { | ||
return "", errors.New("no public_ip found for bootstrap") | ||
} | ||
return bootstrap, nil | ||
} | ||
|
||
// ControlPlaneIPs returns the ip addresses for control plane hosts. | ||
func ControlPlaneIPs(tfs *terraform.State) ([]string, error) { | ||
mrs, err := terraform.LookupResource(tfs, "module.masters", "aws_instance", "master") | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to lookup masters") | ||
} | ||
var errs []error | ||
var masters []string | ||
for idx, inst := range mrs.Instances { | ||
master, _, err := unstructured.NestedString(inst.Attributes, "private_ip") | ||
if err != nil { | ||
errs = append(errs, errors.Wrapf(err, "no private_ip for master.%d", idx)) | ||
} | ||
masters = append(masters, master) | ||
} | ||
return masters, utilerrors.NewAggregate(errs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Package libvirt contains utilities that help gather Libvirt specific | ||
// information from terraform state. | ||
package libvirt | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||
|
||
"github.com/openshift/installer/pkg/terraform" | ||
) | ||
|
||
// BootstrapIP returns the ip address for bootstrap host. | ||
func BootstrapIP(tfs *terraform.State) (string, error) { | ||
br, err := terraform.LookupResource(tfs, "module.bootstrap", "libvirt_domain", "bootstrap") | ||
if err != nil { | ||
return "", errors.Wrap(err, "failed to lookup bootstrap") | ||
} | ||
if len(br.Instances) == 0 { | ||
return "", errors.New("no bootstrap instance found") | ||
} | ||
bootstrap, err := hostnameForDomain(br.Instances[0].Attributes) | ||
if err != nil { | ||
return "", errors.Wrap(err, "failed to lookup hostname") | ||
} | ||
return bootstrap, nil | ||
} | ||
|
||
// ControlPlaneIPs returns the ip addresses for control plane hosts. | ||
func ControlPlaneIPs(tfs *terraform.State) ([]string, error) { | ||
mrs, err := terraform.LookupResource(tfs, "", "libvirt_domain", "master") | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to lookup masters") | ||
} | ||
var errs []error | ||
var masters []string | ||
for idx, inst := range mrs.Instances { | ||
master, err := hostnameForDomain(inst.Attributes) | ||
if err != nil { | ||
errs = append(errs, errors.Wrapf(err, "failed to lookup hostname for master.%d", idx)) | ||
} | ||
masters = append(masters, master) | ||
} | ||
return masters, utilerrors.NewAggregate(errs) | ||
} | ||
|
||
func hostnameForDomain(attr map[string]interface{}) (string, error) { | ||
nics, _, err := unstructured.NestedSlice(attr, "network_interface") | ||
if err != nil { | ||
return "", errors.Wrap(err, "failed to lookup network_interface") | ||
} | ||
if len(nics) == 0 { | ||
return "", errors.New("no network_interface found") | ||
} | ||
hostname, _, err := unstructured.NestedString(nics[0].(map[string]interface{}), "hostname") | ||
if err != nil { | ||
return "", errors.New("no hostname found") | ||
} | ||
return hostname, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Package openstack contains utilities that help gather Openstack specific | ||
// information from terraform state. | ||
package openstack | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||
|
||
"github.com/openshift/installer/pkg/terraform" | ||
) | ||
|
||
// BootstrapIP returns the ip address for bootstrap host. | ||
func BootstrapIP(tfs *terraform.State) (string, error) { | ||
br, err := terraform.LookupResource(tfs, "module.bootstrap", "openstack_compute_instance_v2", "bootstrap") | ||
if err != nil { | ||
return "", errors.Wrap(err, "failed to lookup bootstrap") | ||
} | ||
if len(br.Instances) == 0 { | ||
return "", errors.New("no bootstrap instance found") | ||
} | ||
bootstrap, _, err := unstructured.NestedString(br.Instances[0].Attributes, "access_ip_v4") | ||
if err != nil { | ||
return "", errors.New("no public_ip found for bootstrap") | ||
} | ||
return bootstrap, nil | ||
} | ||
|
||
// ControlPlaneIPs returns the ip addresses for control plane hosts. | ||
func ControlPlaneIPs(tfs *terraform.State) ([]string, error) { | ||
mrs, err := terraform.LookupResource(tfs, "module.masters", "openstack_compute_instance_v2", "master_conf") | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to lookup masters") | ||
} | ||
var errs []error | ||
var masters []string | ||
for idx, inst := range mrs.Instances { | ||
master, _, err := unstructured.NestedString(inst.Attributes, "access_ip_v4") | ||
if err != nil { | ||
errs = append(errs, errors.Wrapf(err, "no access_ip_v4 for master_conf.%d", idx)) | ||
} | ||
masters = append(masters, master) | ||
} | ||
return masters, utilerrors.NewAggregate(errs) | ||
} |
Oops, something went wrong.