forked from GreptimeTeam/gtctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reimplement delete cmd for cluster delete (GreptimeTeam#179)
* implement k8s delete api and decouple the set values config logic Signed-off-by: sh2 <[email protected]> * implement bare-metal delete api Signed-off-by: sh2 <[email protected]> --------- Signed-off-by: sh2 <[email protected]>
- Loading branch information
Showing
18 changed files
with
354 additions
and
289 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
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,78 @@ | ||
// Copyright 2023 Greptime Team | ||
// | ||
// 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 main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
opt "github.com/GreptimeTeam/gtctl/pkg/cluster" | ||
"github.com/GreptimeTeam/gtctl/pkg/cluster/baremetal" | ||
"github.com/GreptimeTeam/gtctl/pkg/cluster/kubernetes" | ||
"github.com/GreptimeTeam/gtctl/pkg/logger" | ||
) | ||
|
||
type clusterDeleteOptions struct { | ||
Namespace string | ||
TearDownEtcd bool | ||
|
||
// The options for deleting GreptimeDB cluster in bare-metal. | ||
BareMetal bool | ||
} | ||
|
||
func NewDeleteClusterCommand(l logger.Logger) *cobra.Command { | ||
var options clusterDeleteOptions | ||
|
||
cmd := &cobra.Command{ | ||
Use: "delete", | ||
Short: "Delete a GreptimeDB cluster", | ||
Long: `Delete a GreptimeDB cluster`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
return fmt.Errorf("cluster name should be set") | ||
} | ||
|
||
clusterName := args[0] | ||
var ( | ||
cluster opt.Operations | ||
err error | ||
ctx = context.TODO() | ||
) | ||
|
||
if options.BareMetal { | ||
cluster, err = baremetal.NewCluster(l, clusterName, baremetal.WithCreateNoDirs()) | ||
} else { | ||
cluster, err = kubernetes.NewCluster(l) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
||
deleteOptions := &opt.DeleteOptions{ | ||
Namespace: options.Namespace, | ||
Name: clusterName, | ||
} | ||
return cluster.Delete(ctx, deleteOptions) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "default", "Namespace of GreptimeDB cluster.") | ||
cmd.Flags().BoolVar(&options.TearDownEtcd, "tear-down-etcd", false, "Tear down etcd cluster.") | ||
cmd.Flags().BoolVar(&options.BareMetal, "bare-metal", false, "Get the greptimedb cluster on bare-metal environment.") | ||
|
||
return cmd | ||
} |
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
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,68 @@ | ||
// Copyright 2023 Greptime Team | ||
// | ||
// 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 baremetal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"syscall" | ||
|
||
opt "github.com/GreptimeTeam/gtctl/pkg/cluster" | ||
fileutils "github.com/GreptimeTeam/gtctl/pkg/utils/file" | ||
) | ||
|
||
func (c *Cluster) Delete(ctx context.Context, options *opt.DeleteOptions) error { | ||
cluster, err := c.get(ctx, &opt.GetOptions{Name: options.Name}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
running, ferr, serr := c.isClusterRunning(cluster.ForegroundPid) | ||
if ferr != nil { | ||
return fmt.Errorf("error checking whether cluster '%s' is running: %v", options.Name, ferr) | ||
} | ||
if running || serr == nil { | ||
return fmt.Errorf("cluster '%s' is running, please stop it before deleting", options.Name) | ||
} | ||
|
||
csd := c.mm.GetClusterScopeDirs() | ||
c.logger.V(0).Infof("Deleting cluster configurations and runtime directories in %s", csd.BaseDir) | ||
if err = c.delete(ctx, csd.BaseDir); err != nil { | ||
return err | ||
} | ||
c.logger.V(0).Info("Deleted!") | ||
|
||
return nil | ||
} | ||
|
||
func (c *Cluster) delete(_ context.Context, baseDir string) error { | ||
return fileutils.DeleteDirIfExists(baseDir) | ||
} | ||
|
||
// isClusterRunning checks the current status of cluster by sending signal to process. | ||
func (c *Cluster) isClusterRunning(pid int) (runs bool, f error, s error) { | ||
p, f := os.FindProcess(pid) | ||
if f != nil { | ||
return false, f, nil | ||
} | ||
|
||
s = p.Signal(syscall.Signal(0)) | ||
if s != nil { | ||
return false, nil, s | ||
} | ||
|
||
return true, nil, 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
Oops, something went wrong.