Skip to content

Commit

Permalink
Merge pull request #59 from yamamoto-febc/fix/temporary-file-handling
Browse files Browse the repository at this point in the history
Fix where to create temporary files
  • Loading branch information
yamamoto-febc authored Feb 16, 2019
2 parents 753e227 + ee05a02 commit bb5fe0d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
12 changes: 7 additions & 5 deletions rke/resource_rke_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ func clusterUp(d *schema.ResourceData) error {

apiURL, caCrt, clientCert, clientKey, clusterUpErr := realClusterUp(context.Background(),
rkeConfig, nil, nil, nil,
clusterFilePath, tempDir, false, disablePortCheck)
clusterFilePath, "", false, disablePortCheck)
if clusterUpErr != nil {
return clusterUpErr
}
Expand Down Expand Up @@ -1511,7 +1511,7 @@ func clusterRemove(d *schema.ResourceData) error {
}

return realClusterRemove(context.Background(),
rkeConfig, nil, nil, clusterFilePath, tempDir)
rkeConfig, nil, nil, clusterFilePath, "")
}

func realClusterUp( // nolint: gocyclo
Expand Down Expand Up @@ -1723,7 +1723,7 @@ func readClusterState(d *schema.ResourceData) (*cluster.Cluster, error) {

ctx := context.Background()
kubeCluster, err := cluster.ParseCluster(ctx, rkeConfig, clusterFilePath,
tempDir, nil, nil, nil)
"", nil, nil, nil)
if err != nil {
return nil, err
}
Expand All @@ -1737,7 +1737,8 @@ func readClusterState(d *schema.ResourceData) (*cluster.Cluster, error) {
}

func readKubeConfig(dir string) (string, error) {
localKubeConfigPath := pki.GetLocalKubeConfig(pki.ClusterConfig, dir)
configPath := filepath.Join(dir, pki.ClusterConfig)
localKubeConfigPath := pki.GetLocalKubeConfig(configPath, "")
if _, err := os.Stat(localKubeConfigPath); err == nil {
var data []byte
if data, err = ioutil.ReadFile(localKubeConfigPath); err != nil {
Expand All @@ -1752,7 +1753,8 @@ func writeKubeConfigFile(dir string, d *schema.ResourceData) error {
if rawKubeConfig, ok := d.GetOk("kube_config_yaml"); ok {
strConf := rawKubeConfig.(string)
if strConf != "" {
localKubeConfigPath := pki.GetLocalKubeConfig(pki.ClusterConfig, dir)
configPath := filepath.Join(dir, pki.ClusterConfig)
localKubeConfigPath := pki.GetLocalKubeConfig(configPath, "")
if err := ioutil.WriteFile(localKubeConfigPath, []byte(strConf), 0640); err != nil {
return err
}
Expand Down
21 changes: 20 additions & 1 deletion rke/resource_rke_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/rancher/rke/pki"
"github.com/rancher/types/apis/management.cattle.io/v3"
"gopkg.in/yaml.v2"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -58,6 +59,7 @@ func TestAccResourceRKECluster(t *testing.T) {
"rke_cluster.cluster", "nodes.0.address", nodeIP),
resource.TestMatchResourceAttr(
"rke_cluster.cluster", "kube_config_yaml", regexp.MustCompile(".+")), // should be not empty
testAccCheckTempFilesExists(),
),
},
{
Expand All @@ -77,6 +79,7 @@ func TestAccResourceRKECluster(t *testing.T) {
"rke_cluster.cluster", "nodes.0.labels.foo", "foo"),
resource.TestCheckResourceAttr(
"rke_cluster.cluster", "nodes.0.labels.bar", "bar"),
testAccCheckTempFilesExists(),
),
},
},
Expand Down Expand Up @@ -150,6 +153,21 @@ func TestAccResourceRKECluster_NodeCountUpAndDown(t *testing.T) {
})
}

func testAccCheckTempFilesExists() resource.TestCheckFunc {
return func(s *terraform.State) error {

if _, err := os.Stat(pki.ClusterConfig); err == nil {
return fmt.Errorf("temporary file %q is still exists", pki.ClusterConfig)
}

kubeClusterYAML := fmt.Sprintf("%s%s", pki.KubeAdminConfigPrefix, pki.ClusterConfig)
if _, err := os.Stat(kubeClusterYAML); err == nil {
return fmt.Errorf("temporary file %q is still exists", kubeClusterYAML)
}
return nil
}
}

func testAccCheckRKENodeExists(n string, nodeIPs ...string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -299,7 +317,8 @@ func testAccCheckRKEClusterDestroy(s *terraform.State) error {
}
}

return nil
// check tmp files
return testAccCheckTempFilesExists()(s)
}

func testAccCheckRKEConfigBasic(ip, user, sshKey string) string {
Expand Down

0 comments on commit bb5fe0d

Please sign in to comment.