Skip to content

Commit

Permalink
Merge pull request #173 from Javlopez/feature/add-command-for-wipping…
Browse files Browse the repository at this point in the history
…-cluster

commands: add command to wiping cluster
  • Loading branch information
travisn authored Dec 19, 2023
2 parents 812e41c + f067748 commit b572c39
Show file tree
Hide file tree
Showing 17 changed files with 917 additions and 1 deletion.
28 changes: 28 additions & 0 deletions .github/workflows/go-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ jobs:
kubectl rook-ceph -n rook-ceph restore-deleted cephcluster my-cluster
tests/github-action-helper.sh wait_for_crd_to_be_ready_default
- name: Show Cluster State
run: |
set -ex
kubectl -n rook-ceph get all
- name: Destroy Cluster (removing CRs)
env:
ROOK_PLUGIN_SKIP_PROMPTS: true
run: |
set -ex
kubectl rook-ceph destroy-cluster
sleep 1
kubectl get deployments -n rook-ceph --no-headers| wc -l | (read n && [ $n -le 1 ] || { echo "the crs could not be deleted"; exit 1;})
- name: collect common logs
if: always()
uses: ./.github/workflows/collect-logs
Expand Down Expand Up @@ -283,6 +297,20 @@ jobs:
kubectl rook-ceph --operator-namespace test-operator -n test-cluster restore-deleted cephcluster my-cluster
tests/github-action-helper.sh wait_for_crd_to_be_ready_custom
- name: Show Cluster State
run: |
set -ex
kubectl -n test-cluster get all
- name: Destroy Cluster (removing CRs)
env:
ROOK_PLUGIN_SKIP_PROMPTS: true
run: |
set -ex
kubectl rook-ceph --operator-namespace test-operator -n test-cluster destroy-cluster
sleep 1
kubectl get deployments -n rook-ceph --no-headers| wc -l | (read n && [ $n -le 1 ] || { echo "the crs could not be deleted"; exit 1;})
- name: collect common logs
if: always()
uses: ./.github/workflows/collect-logs
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ help :
@echo "build : Create go binary."
@echo "test : Runs unit tests"
@echo "clean : Remove go binary file."

codegen:
@echo "generating mocks..."
@go generate ./...
@echo completed
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Visit docs below for complete details about each command and their flags uses.
1. [Restore mon quorum](docs/mons.md#restore-quorum)
1. [Disaster Recovery](docs/dr-health.md)
1. [Restore deleted CRs](docs/crd.md)
1. [Destroy cluster](docs/destroy-cluster.md)

## Examples

Expand Down
51 changes: 51 additions & 0 deletions cmd/commands/destroy_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2023 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package command

import (
"fmt"
"github.com/rook/kubectl-rook-ceph/pkg/crds"
"github.com/rook/kubectl-rook-ceph/pkg/logging"
"github.com/rook/kubectl-rook-ceph/pkg/mons"
"github.com/spf13/cobra"
)

const (
destroyClusterQuestion = "Are you sure you want to destroy the cluster in namespace %q? If absolutely certain, enter: " + destroyClusterAnswer
destroyClusterAnswer = "yes-really-destroy-cluster"
)

// DestroyClusterCmd represents the command for destroy cluster
var DestroyClusterCmd = &cobra.Command{
Use: "destroy-cluster",
Short: "delete ALL data in the Rook cluster and all Rook CRs",

Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
var answer string
logging.Warning(destroyClusterQuestion, cephClusterNamespace)
fmt.Scanf("%s", &answer)
err := mons.PromptToContinueOrCancel("", destroyClusterAnswer, answer)
if err != nil {
logging.Fatal(fmt.Errorf("the response %q to confirm the cluster deletion", destroyClusterAnswer))
}

logging.Info("proceeding")
clientsets := getClientsets(ctx)
crds.DeleteCustomResources(ctx, clientsets, clientsets.Kube, cephClusterNamespace)
},
}
6 changes: 6 additions & 0 deletions cmd/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package command
import (
"context"
"fmt"
"k8s.io/client-go/dynamic"
"regexp"
"strings"

Expand Down Expand Up @@ -101,6 +102,11 @@ func getClientsets(ctx context.Context) *k8sutil.Clientsets {
logging.Fatal(err)
}

clientsets.Dynamic, err = dynamic.NewForConfig(clientsets.KubeConfig)
if err != nil {
logging.Fatal(err)
}

return clientsets
}

Expand Down
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func addcommands() {
command.Health,
command.DrCmd,
command.RestoreCmd,
command.DestroyClusterCmd,
command.SubvolumeCmd,
)
}
71 changes: 71 additions & 0 deletions docs/destroy-cluster.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Destroying a Cluster

When a cluster is no longer needed and needs to be torn down, Rook has a [Cleanup guide](https://rook.io/docs/rook/latest/Getting-Started/ceph-teardown/) with instructions to tear it down cleanly. While following that document is highly preferred, there is a command that will automate the cleanup of Rook after applications are removed.
The `destroy-cluster` command destroys a Rook cluster and deletes CRs (custom resources) by force removing any finalizers that may be preventing the cleanup.

## !!! Warning !!!
**This command is not reversible**, and it will destroy your Rook cluster completely with guaranteed data loss.
Only use this command if you are 100% sure that the cluster and its data should be destroyed.

## `destroy-cluster` command

To destroy a cluster, run the command:
```bash
$ kubectl rook-ceph destroy-cluster
Warning: Are you sure you want to destroy the cluster in namespace "rook-ceph"? If absolutely certain, enter: yes-really-destroy-cluster
```

Any other response will cause the command to abort.

## Example of destroying a cluster
```bash
$ kubectl rook-ceph -n rook-ceph destroy-cluster
Warning: Are you sure you want to destroy the cluster in namespace "rook-ceph"? If absolutely certain, enter: yes-really-destroy-cluster
yes-really-destroy-cluster
Info: proceeding
Info: getting resource kind cephclusters
Info: removing resource cephclusters: my-cluster
Info: resource "my-cluster" is not yet deleted, applying patch to remove finalizer...
Info: resource my-cluster was deleted
Info: getting resource kind cephblockpoolradosnamespaces
Info: resource cephblockpoolradosnamespaces was not found on the cluster
Info: getting resource kind cephblockpools
Info: removing resource cephblockpools: builtin-mgr
Info: resource builtin-mgr was deleted
Info: getting resource kind cephbucketnotifications
Info: resource cephbucketnotifications was not found on the cluster
Info: getting resource kind cephbuckettopics
Info: resource cephbuckettopics was not found on the cluster
Info: getting resource kind cephclients
Info: resource cephclients was not found on the cluster
Info: getting resource kind cephcosidrivers
Info: resource cephcosidrivers was not found on the cluster
Info: getting resource kind cephfilesystemmirrors
Info: resource cephfilesystemmirrors was not found on the cluster
Info: getting resource kind cephfilesystems
Info: removing resource cephfilesystems: myfs
Info: resource myfs was deleted
Info: getting resource kind cephfilesystemsubvolumegroups
Info: removing resource cephfilesystemsubvolumegroups: myfs-csi
Info: resource myfs-csi was deleted
Info: getting resource kind cephnfses
Info: resource cephnfses was not found on the cluster
Info: getting resource kind cephobjectrealms
Info: resource cephobjectrealms was not found on the cluster
Info: getting resource kind cephobjectstores
Info: removing resource cephobjectstores: my-store
Info: resource my-store was deleted
Info: getting resource kind cephobjectstoreusers
Info: resource cephobjectstoreusers was not found on the cluster
Info: getting resource kind cephobjectzonegroups
Info: resource cephobjectzonegroups was not found on the cluster
Info: getting resource kind cephobjectzones
Info: resource cephobjectzones was not found on the cluster
Info: getting resource kind cephrbdmirrors
Info: resource cephrbdmirrors was not found on the cluster
Info: removing deployment rook-ceph-tools
Info: waiting to clean up resources
Info: 1 pods still alive, removing....
Info: pod rook-ceph-mds-myfs-b-58cf7bcbb7-d6rc4 still a live
Info: done
```
2 changes: 1 addition & 1 deletion docs/health.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Health commands logs have three ways of logging:

1. `Info`: This is just a logging information for the users.
2. `Warning`: which mean there is some improvement required in the cluster.
3. `Error`: This requires immediate user attentions to get the cluster in healthy state.
3. `Error`: This requires immediate user attention to get the cluster in healthy state.

## Output

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/fatih/color v1.16.0
github.com/golang/mock v1.6.0
github.com/pkg/errors v0.9.1
github.com/rook/rook v1.13.0
github.com/rook/rook/pkg/apis v0.0.0-20231204200402-5287527732f7
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v0.0.0-20161109072736-4bd1920723d7/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down
Loading

0 comments on commit b572c39

Please sign in to comment.