Repository navigation
STS 537: wipe pvcs of hbase when requested #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4172e38
STS 537: wipe pvcs of hbase when requested
fvlankvelt 05a47eb
STS 537: abort restore when deleting pvcs fails, add some logging
fvlankvelt ff1b72d
Merge branch 'main' into sts-537-wipe-pvcs
fvlankvelt 7d118ea
STS 537: no else needed
fvlankvelt e178795
STS 537: bow to astra
fvlankvelt 23b8f1c
Merge branch 'main' into sts-537-wipe-pvcs
fvlankvelt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,125 @@ | ||
| package restore | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/go-zookeeper/zk" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/clients/k8s" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/foundation/logger" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/orchestration/portforward" | ||
| "github.com/stackvista/stackstate-backup-cli/internal/orchestration/scale" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| const zkSessionTimeout = 2 * time.Second | ||
|
|
||
| func deleteRecursive(zkConn *zk.Conn, path string, logger *logger.Logger) error { | ||
| children, _, err := zkConn.Children(path) | ||
| switch { | ||
| case errors.Is(err, zk.ErrNoNode): | ||
| return nil | ||
| case err != nil: | ||
| logger.Warningf("Unable to list znode for %s: %v", path, err) | ||
| default: | ||
| for _, child := range children { | ||
| if err := deleteRecursive(zkConn, path+"/"+child, logger); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
| if err := zkConn.Delete(path, -1); err != nil && !errors.Is(err, zk.ErrNoNode) { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func scaleDownHBase(k8sClient *k8s.Client, namespace string, logger *logger.Logger) error { | ||
| logger.Println() | ||
| logger.Infof("Scaling down HBase") | ||
| _, err := scale.ScaleDown(k8sClient, namespace, "app.kubernetes.io/name=hbase", logger) | ||
| return err | ||
| } | ||
|
|
||
| func WipePVCs(ctx context.Context, k8sClient *k8s.Client, namespace, zkQuorum string, logger *logger.Logger) error { | ||
| if !strings.Contains(zkQuorum, "-stackgraph") { | ||
| zkHost, zkPortStr, portSpecified := strings.Cut(zkQuorum, ":") | ||
| port := 2181 | ||
| if portSpecified { | ||
| if zkPort, err := strconv.Atoi(zkPortStr); err != nil { | ||
| logger.Warningf("Unable to parse port %s, falling back to %d", zkPortStr, port) | ||
| } else { | ||
| port = zkPort | ||
| } | ||
| } | ||
| pf, err := portforward.SetupPortForward(k8sClient, namespace, zkHost, port, logger) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer close(pf.StopChan) | ||
|
|
||
| zkEndpoint := fmt.Sprintf("localhost:%d", pf.LocalPort) | ||
| zkConn, _, err := zk.Connect( | ||
| []string{zkEndpoint}, | ||
| zkSessionTimeout, | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer zkConn.Close() | ||
|
|
||
| if err = scaleDownHBase(k8sClient, namespace, logger); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| logger.Println() | ||
| logger.Infof("Removing HBase and Tephra nodes from Zookeeper") | ||
|
|
||
| if err := deleteRecursive(zkConn, "/hbase", logger); err != nil { | ||
| return err | ||
| } | ||
| if err := deleteRecursive(zkConn, "/tx.service", logger); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| logger.Successf("Removed nodes from Zookeeper") | ||
| } else { | ||
| if err := scaleDownHBase(k8sClient, namespace, logger); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| defer func() { | ||
| err := scale.ScaleUpFromAnnotations(k8sClient, namespace, "app.kubernetes.io/name=hbase", logger) | ||
| if err != nil { | ||
| logger.Warningf("Failed to scale up HBase: %v", err) | ||
| } | ||
| }() | ||
|
|
||
| // List pvcs in the hbase application | ||
| pvcs, err := k8sClient.Clientset().CoreV1().PersistentVolumeClaims(namespace).List(ctx, metav1.ListOptions{ | ||
| LabelSelector: "app.kubernetes.io/name=hbase", | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| logger.Println() | ||
| logger.Infof("Deleting PVCs") | ||
| for _, pvc := range pvcs.Items { | ||
| if err := k8sClient.DeletePVC(pvc.Namespace, pvc.Name); err != nil { | ||
|
fvlankvelt marked this conversation as resolved.
|
||
| return err | ||
| } | ||
| logger.Infof(" - %s", pvc.Name) | ||
| } | ||
| logger.Successf("Deleted PVCs") | ||
|
|
||
| logger.Println() | ||
| logger.Infof("Scaled up HBase after deleting PVCs") | ||
|
|
||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.