Skip to content

Commit

Permalink
Issue 25: Handling znode deletion (#28)
Browse files Browse the repository at this point in the history
* Issue 25: Handling znode deletion

Signed-off-by: SrishT <[email protected]>

* Issue 25: Addressing review comments

Signed-off-by: SrishT <[email protected]>

* Issue 25: Handling znode deletion

Signed-off-by: SrishT <[email protected]>

* Issue 25: Addressing review comments

Signed-off-by: SrishT <[email protected]>

* Issue 25: Addressing review comments

Signed-off-by: SrishT <[email protected]>

* Issue 25: Addressing review comments

Signed-off-by: SrishT <[email protected]>

* Issue 25: Adding comments

Signed-off-by: SrishT <[email protected]>

* Issue 25: Addressing review comments

Signed-off-by: SrishT <[email protected]>

Co-authored-by: SrishT <[email protected]>
  • Loading branch information
SrishT and SrishT authored Apr 21, 2020
1 parent cd9c6ec commit a323bbf
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 18 deletions.
5 changes: 0 additions & 5 deletions charts/bookkeeper/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,3 @@ We truncate at 63 chars because some Kubernetes name fields are limited to this
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s-configmap" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{- define "versionmap.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s-supported-upgrade-paths" .Release.Name $name -}}
{{- end -}}
2 changes: 1 addition & 1 deletion deploy/crds/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ spec:
- name: Desired Version
type: string
description: The desired bookkeeper version
JSONPath: .status.TargetVersion
JSONPath: .spec.version
- name: Desired Members
type: integer
description: The number of desired bookkeeper members
Expand Down
47 changes: 39 additions & 8 deletions pkg/controller/bookkeepercluster/bookkeepercluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package bookkeepercluster
import (
"context"
"fmt"
"strings"
"time"

bookkeeperv1alpha1 "github.com/pravega/bookkeeper-operator/pkg/apis/bookkeeper/v1alpha1"
Expand Down Expand Up @@ -233,35 +234,52 @@ func (r *ReconcileBookkeeperCluster) syncBookieSize(bk *bookkeeperv1alpha1.Bookk

func (r *ReconcileBookkeeperCluster) reconcileFinalizers(bk *bookkeeperv1alpha1.BookkeeperCluster) (err error) {
if bk.DeletionTimestamp.IsZero() {
if !util.ContainsString(bk.ObjectMeta.Finalizers, util.ZkFinalizer) {
bk.ObjectMeta.Finalizers = append(bk.ObjectMeta.Finalizers, util.ZkFinalizer)
// checks whether the slice of finalizers contains a string with the given prefix
// NOTE: we need to ensure that no two finalizer names have the same prefix
if !util.ContainsStringWithPrefix(bk.ObjectMeta.Finalizers, util.ZkFinalizer) {
finalizer := util.ZkFinalizer
configMap := &corev1.ConfigMap{}
if strings.TrimSpace(bk.Spec.EnvVars) != "" {
err = r.client.Get(context.TODO(), types.NamespacedName{Name: strings.TrimSpace(bk.Spec.EnvVars), Namespace: bk.Namespace}, configMap)
if err != nil {
return fmt.Errorf("failed to get the configmap %s: %v", bk.Spec.EnvVars, err)
}
clusterName, ok := configMap.Data["PRAVEGA_CLUSTER_NAME"]
if ok {
// appending name of pravega cluster to the name of the finalizer
// to handle zk metadata deletion
finalizer = finalizer + "_" + clusterName
}
}
bk.ObjectMeta.Finalizers = append(bk.ObjectMeta.Finalizers, finalizer)
if err = r.client.Update(context.TODO(), bk); err != nil {
return fmt.Errorf("failed to add the finalizer (%s): %v", bk.Name, err)
}
}
} else {
if util.ContainsString(bk.ObjectMeta.Finalizers, util.ZkFinalizer) {
bk.ObjectMeta.Finalizers = util.RemoveString(bk.ObjectMeta.Finalizers, util.ZkFinalizer)
// checks whether the slice of finalizers contains a string with the given prefix
if util.ContainsStringWithPrefix(bk.ObjectMeta.Finalizers, util.ZkFinalizer) {
finalizer, pravegaClusterName := getFinalizerAndClusterName(bk.ObjectMeta.Finalizers)
bk.ObjectMeta.Finalizers = util.RemoveString(bk.ObjectMeta.Finalizers, finalizer)
if err = r.client.Update(context.TODO(), bk); err != nil {
return fmt.Errorf("failed to update Bookkeeper object (%s): %v", bk.Name, err)
}
if err = r.cleanUpZookeeperMeta(bk); err != nil {
if err = r.cleanUpZookeeperMeta(bk, pravegaClusterName); err != nil {
return fmt.Errorf("failed to clean up metadata (%s): %v", bk.Name, err)
}
}
}
return nil
}

func (r *ReconcileBookkeeperCluster) cleanUpZookeeperMeta(bk *bookkeeperv1alpha1.BookkeeperCluster) (err error) {
func (r *ReconcileBookkeeperCluster) cleanUpZookeeperMeta(bk *bookkeeperv1alpha1.BookkeeperCluster, pravegaClusterName string) (err error) {
if err = util.WaitForClusterToTerminate(r.client, bk); err != nil {
return fmt.Errorf("failed to wait for cluster pods termination (%s): %v", bk.Name, err)
}

if err = util.DeleteAllZnodes(bk); err != nil {
if err = util.DeleteAllZnodes(bk, pravegaClusterName); err != nil {
return fmt.Errorf("failed to delete zookeeper znodes for (%s): %v", bk.Name, err)
}
fmt.Println("zookeeper metadata deleted")
return nil
}

Expand Down Expand Up @@ -401,3 +419,16 @@ func (r *ReconcileBookkeeperCluster) isRollbackTriggered(bk *bookkeeperv1alpha1.
}
return false
}

func getFinalizerAndClusterName(slice []string) (string, string) {
// get the modified finalizer name from the slice of finalizers with the given prefix
finalizer := util.GetStringWithPrefix(slice, util.ZkFinalizer)
// extracting pravega cluster name from the modified finalizer name
pravegaClusterName := strings.Replace(finalizer, util.ZkFinalizer, "", 1)
if pravegaClusterName == "" {
pravegaClusterName = "pravega-cluster"
} else {
pravegaClusterName = strings.Replace(pravegaClusterName, "_", "", 1)
}
return finalizer, pravegaClusterName
}
13 changes: 11 additions & 2 deletions pkg/util/bookkeepercluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ func Min(x, y int32) int32 {
return x
}

func ContainsString(slice []string, str string) bool {
func ContainsStringWithPrefix(slice []string, str string) bool {
for _, item := range slice {
if item == str {
if strings.HasPrefix(item, str) {
return true
}
}
Expand All @@ -107,6 +107,15 @@ func RemoveString(slice []string, str string) (result []string) {
return result
}

func GetStringWithPrefix(slice []string, str string) (result string) {
for _, item := range slice {
if strings.HasPrefix(item, str) {
return item
}
}
return ""
}

func GetClusterExpectedSize(p *v1alpha1.BookkeeperCluster) (size int) {
return int(p.Spec.Replicas)
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/util/zookeeper_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package util
import (
"container/list"
"fmt"
"log"
"time"

"github.com/pravega/bookkeeper-operator/pkg/apis/bookkeeper/v1alpha1"
Expand All @@ -26,15 +27,15 @@ const (
)

// Delete all znodes related to a specific Bookkeeper cluster
func DeleteAllZnodes(bk *v1alpha1.BookkeeperCluster) (err error) {
func DeleteAllZnodes(bk *v1alpha1.BookkeeperCluster, pravegaClusterName string) (err error) {
host := []string{bk.Spec.ZookeeperUri}
conn, _, err := zk.Connect(host, time.Second*5)
if err != nil {
return fmt.Errorf("failed to connect to zookeeper: %v", err)
}
defer conn.Close()

root := fmt.Sprintf("/%s/%s", PravegaPath, bk.Name)
root := fmt.Sprintf("/%s/%s", PravegaPath, pravegaClusterName)
exist, _, err := conn.Exists(root)
if err != nil {
return fmt.Errorf("failed to check if zookeeper path exists: %v", err)
Expand All @@ -54,6 +55,9 @@ func DeleteAllZnodes(bk *v1alpha1.BookkeeperCluster) (err error) {
}
tree.Remove(tree.Back())
}
log.Println("zookeeper metadata deleted")
} else {
log.Println("zookeeper metadata not found")
}
return nil
}
Expand Down

0 comments on commit a323bbf

Please sign in to comment.