/
delete.go
60 lines (47 loc) · 1.86 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package cluster
import (
"fmt"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/errors"
"github.com/grtl/mysql-operator/pkg/cmd/util/config"
"github.com/grtl/mysql-operator/pkg/cmd/util/fail"
"github.com/grtl/mysql-operator/pkg/cmd/util/options"
)
var removePVC bool
var clusterDeleteCmd = &cobra.Command{
Use: "delete [cluster names]",
Short: "Deletes MySQL clusters",
Long: `Deletes MySQL clusters and resources associated with them.
Unless explicitly specified, does not remove PersistentVolumeClaims.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
options := options.ExtractOptions(cmd)
for _, arg := range args {
err := deleteCluster(arg, options)
fail.ErrorOrForceContinue(err, options)
}
},
}
func init() {
clusterDeleteCmd.PersistentFlags().BoolVarP(&removePVC, "remove-pvc", "r", false, "remove PersistentVolumeClaims along with the cluster")
Cmd.AddCommand(clusterDeleteCmd)
}
func deleteCluster(clusterName string, options *options.Options) error {
fmt.Printf("Deleting: %s/%s\n", options.Namespace, clusterName)
clustersInterface := config.GetConfig().Clientset().CrV1().MySQLClusters(options.Namespace)
err := clustersInterface.Delete(clusterName, &v1.DeleteOptions{})
if removePVC && (err == nil || options.Force) {
deleteErr := deletePVC(clusterName, options)
return errors.NewAggregate([]error{err, deleteErr})
}
return err
}
func deletePVC(clusterName string, options *options.Options) error {
fmt.Printf("Deleting PVCs for: %s/%s\n", options.Namespace, clusterName)
pvcInterface := config.GetConfig().KubeClientset().CoreV1().PersistentVolumeClaims(options.Namespace)
return pvcInterface.DeleteCollection(&v1.DeleteOptions{}, v1.ListOptions{
LabelSelector: labels.Set{"app": clusterName}.AsSelector().String(),
})
}