Skip to content

Commit

Permalink
fix: fix znode subset deletion requiring parent path and recursive de…
Browse files Browse the repository at this point in the history
…letion algorithm (#74)

Addressed the following specific issues:
1. Deleting a subset of znodes required the full parent path, resulting in a "zk: invalid path" error if not provided.
2. The recursive deletion algorithm only deleted the last-level path, failing to handle intermediate-level child nodes.
  • Loading branch information
lwpk110 authored Aug 2, 2024
1 parent dfc8f3f commit 8723abb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/zookeepercluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type ImageSpec struct {
}

type ClusterConfigSpec struct {
// +kubebuilder:validation:required
// +kubebuilder:validation:optional
// +kubebuilder:validation:Enum="cluster-internal";"external-unstable"
// +kubebuilder:default="cluster-internal"
ListenerClass string `json:"listenerClass"`
Expand Down
17 changes: 16 additions & 1 deletion internal/znodecontroller/zkclient.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package znodecontroller

import (
"errors"
"fmt"
"github.com/samuel/go-zookeeper/zk"
ctrl "sigs.k8s.io/controller-runtime"
"time"
Expand Down Expand Up @@ -67,6 +69,10 @@ func (z ZkClient) Delete(path string) error {
// check if the znode exists children and delete them
children, _, err := z.Client.Children(path)
if err != nil {
if errors.Is(err, zk.ErrNoNode) {
logger.V(1).Info("current znode no exists", "path", path)
return nil
}
return err
}
if len(children) == 0 {
Expand All @@ -80,11 +86,20 @@ func (z ZkClient) Delete(path string) error {
// if exists children, delete them
logger.V(1).Info("current znode has children, should delete all children first", "path", path)
for _, child := range children {
err = z.Delete(child)
err = z.Delete(fmt.Sprintf("%s/%s", path, child))
if err != nil {
return err
}
}
// delete parent path
err = z.Client.Delete(path, -1)
if err != nil {
if errors.Is(err, zk.ErrNoNode) {
logger.V(1).Info("current znode no exists", "path", path)
return nil
}
return err
}
return nil
}

Expand Down

0 comments on commit 8723abb

Please sign in to comment.