Skip to content

Commit

Permalink
Remove configuration files even if TF resources are already removed (#…
Browse files Browse the repository at this point in the history
…192)

* pkg/cluster: Remove terraform resource only if tf state exists

Signed-off-by: Din Music <din.music@din-cloud.com>

* scripts: Add cluster destroy script

Signed-off-by: Din Music <din.music@din-cloud.com>

* .github/workflows: Test cluster destroy

Signed-off-by: Din Music <din.music@din-cloud.com>

---------

Signed-off-by: Din Music <din.music@din-cloud.com>
  • Loading branch information
MusicDin authored Apr 15, 2024
1 parent 553fa62 commit 4795451
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/tests-deploy-node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup environment
uses: ./.github/actions/runner-setup

- name: Deploy single node
- name: Deploy cluster
run: |
./scripts/deploy-node.sh k8s \
${{ matrix.distro }} \
Expand All @@ -51,6 +51,10 @@ jobs:
run: |
./scripts/test-cluster.sh
- name: Destroy cluster
run: |
./scripts/destroy-cluster.sh k8s
# Test most combinations of Kubernetes versions, distros,
# and network plugins. Run this only on push.
test-single-node-all:
Expand Down
32 changes: 17 additions & 15 deletions pkg/cluster/action_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,36 @@ import (
"os"

"github.com/MusicDin/kubitect/pkg/ui"
"github.com/MusicDin/kubitect/pkg/utils/file"
)

// Destroy destroys an active cluster and removes cluster's
// directory. If cluster does not exist or does not contain
// a terraform state file (is inactive), an error is returned.
// Destroy destroys the cluster and removes cluster's directory.
// Terraform resources are wiped if terraform state file is found.
func (c *ClusterMeta) Destroy() error {
if !c.ContainsTfStateConfig() {
return fmt.Errorf("cluster '%s' is already destroyed (or not yet initialized).", c.Name)
if !file.Exists(c.Path) {
return fmt.Errorf("cluster %q does not exist", c.Name)
}

ui.Printf(ui.INFO, "Cluster '%s' will be destroyed.\n", c.Name)
ui.Printf(ui.INFO, "Cluster %q will be destroyed.\n", c.Name)
if err := ui.Ask(); err != nil {
return err
}

ui.Println(ui.INFO, "Destroying cluster...")
if err := c.Provisioner().Destroy(); err != nil {
return err
if c.ContainsTfStateConfig() {
ui.Println(ui.INFO, "Removing cluster resources...")
if err := c.Provisioner().Destroy(); err != nil {
return err
}
}

ui.Println(ui.INFO, "Cleaning up cluster directory...")
ui.Println(ui.INFO, "Removing cluster cache...")
_ = os.RemoveAll(c.CacheDir())

ui.Println(ui.INFO, "Removing cluster directory...")
if err := os.RemoveAll(c.Path); err != nil {
return fmt.Errorf("failed to remove directory of the cluster '%s': %v", c.Name, err)
return fmt.Errorf("failed to remove directory of the cluster %q: %v", c.Name, err)
}

ui.Println(ui.INFO, "Cleaning up cluster cache...")
os.RemoveAll(c.CacheDir())

ui.Printf(ui.INFO, "Cluster '%s' has been successfully destroyed.\n", c.Name)
ui.Printf(ui.INFO, "Cluster %q has been successfully destroyed.\n", c.Name)
return nil
}
5 changes: 2 additions & 3 deletions pkg/cluster/action_destroy_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cluster

import (
"io/ioutil"
"os"
"path"
"testing"
Expand All @@ -17,13 +16,13 @@ func TestDestroy(t *testing.T) {
err := os.MkdirAll(path.Dir(c.TfStatePath()), os.ModePerm)
require.NoError(t, err)

err = ioutil.WriteFile(c.TfStatePath(), []byte(""), os.ModePerm)
err = os.WriteFile(c.TfStatePath(), []byte(""), os.ModePerm)
require.NoError(t, err)

assert.NoError(t, c.Destroy())
}

func TestDestroy_NoTfStateFile(t *testing.T) {
c := MockCluster(t)
assert.EqualError(t, c.Destroy(), "cluster 'cluster-mock' is already destroyed (or not yet initialized).")
assert.EqualError(t, c.Destroy(), `cluster "cluster-mock" does not exist`)
}
19 changes: 19 additions & 0 deletions scripts/destroy-cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh
set -eu

if [ "${1:-}" = "" ]; then
echo "Usage: ${0} <cluster_name>"
exit 1
fi

CLUSTER="${1}"

echo "==> DESTROY: Cluster ${CLUSTER}"
kubitect destroy --cluster "${CLUSTER}"

if [ -d "${HOME}/.kubitect/clusters/${CLUSTER}" ]; then
echo "==> FAIL: Cluster directory still exists"
exit 1
fi

echo "==> PASS"

0 comments on commit 4795451

Please sign in to comment.