Skip to content

Commit b773de3

Browse files
authored
255 rearrange helper functions (#259)
* move hasHostnameBasedNodes to configmap helper as receiver method #255 * log error when nodes key is empty #255
1 parent 992cae7 commit b773de3

4 files changed

Lines changed: 32 additions & 22 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ OPERATOR_SDK_VERSION ?= v1.39.0
5252
# Image URL to use all building/pushing image targets
5353
DOCKER_HUB_NAME ?= quay.io/akyriako#$(shell docker info | sed '/Username:/!d;s/.* //')
5454
IMG_NAME ?= typesense-operator
55-
IMG_TAG ?= 0.4.0
55+
IMG_TAG ?= 0.4.1-dev.13
5656
IMG ?= $(DOCKER_HUB_NAME)/$(IMG_NAME):$(IMG_TAG)
5757

5858
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.

internal/controller/typesensecluster_configmap.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,20 @@ func (r *TypesenseClusterReconciler) getShortName(raftNodeEndpoint string) strin
380380

381381
return host
382382
}
383+
384+
func (r *TypesenseClusterReconciler) hasBootstrapValues(ts *tsv1alpha1.TypesenseCluster, cm *v1.ConfigMap) (bool, error) {
385+
rawNodeslist, ok := cm.Data["nodes"]
386+
if !ok || rawNodeslist == "" {
387+
err := fmt.Errorf("configmap is missing 'nodes' key")
388+
return false, err
389+
}
390+
391+
nodeslist := strings.Split(rawNodeslist, ",")
392+
for _, node := range nodeslist {
393+
if strings.Contains(node, fmt.Sprintf(ClusterStatefulSet, ts.Name)) {
394+
return true, nil
395+
}
396+
}
397+
398+
return false, nil
399+
}

internal/controller/typesensecluster_quorum.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,15 @@ func (r *TypesenseClusterReconciler) ReconcileQuorum(ctx context.Context, ts *ts
116116
r.logger.V(debugLevel).Info("reporting cluster status", "status", clusterStatus)
117117

118118
if clusterStatus == ClusterStatusSplitBrain {
119-
rawNodeslist, ok := quorum.NodesListConfigMap.Data["nodes"]
120-
if !ok || rawNodeslist == "" {
121-
return ConditionReasonQuorumNotReady, 0, fmt.Errorf("configmap %s is missing 'nodes' key", quorum.NodesListConfigMap.Name)
119+
hbv, err := r.hasBootstrapValues(ts, quorum.NodesListConfigMap)
120+
if err != nil {
121+
return ConditionReasonQuorumNotReady, 0, err
122122
}
123-
nodeslist := strings.Split(rawNodeslist, ",")
124-
if hasHostnameBasedNodes(nodeslist, fmt.Sprintf(ClusterStatefulSet, ts.Name)) {
123+
124+
if hbv {
125125
return ConditionReasonQuorumNotReadyWaitATerm, 0, nil
126126
}
127+
127128
return r.downgradeQuorum(ctx, ts, quorum.NodesListConfigMap, stsObjectKey, sts.Status.ReadyReplicas, int32(quorum.MinRequiredNodes))
128129
}
129130

@@ -174,14 +175,15 @@ func (r *TypesenseClusterReconciler) ReconcileQuorum(ctx context.Context, ts *ts
174175
}
175176

176177
if clusterStatus == ClusterStatusElectionDeadlock {
177-
rawNodeslist, ok := quorum.NodesListConfigMap.Data["nodes"]
178-
if !ok || rawNodeslist == "" {
179-
return ConditionReasonQuorumNotReady, 0, fmt.Errorf("configmap %s is missing 'nodes' key", quorum.NodesListConfigMap.Name)
178+
hbv, err := r.hasBootstrapValues(ts, quorum.NodesListConfigMap)
179+
if err != nil {
180+
return ConditionReasonQuorumNotReady, 0, err
180181
}
181-
nodeslist := strings.Split(rawNodeslist, ",")
182-
if hasHostnameBasedNodes(nodeslist, fmt.Sprintf(ClusterStatefulSet, ts.Name)) {
182+
183+
if hbv {
183184
return ConditionReasonQuorumNotReadyWaitATerm, 0, nil
184185
}
186+
185187
return r.downgradeQuorum(ctx, ts, quorum.NodesListConfigMap, stsObjectKey, int32(healthyNodes), int32(minRequiredNodes))
186188
}
187189

@@ -389,13 +391,3 @@ func (r *TypesenseClusterReconciler) updatePodReadinessGate(ctx context.Context,
389391
//r.logger.V(debugLevel).Info("updating pod readiness gate condition", "pod", pod.Name, "condition", condition.Type, "conditionStatus", condition.Status)
390392
return nil
391393
}
392-
393-
func hasHostnameBasedNodes(nodes []string, statefulSetName string) bool {
394-
for _, node := range nodes {
395-
if strings.Contains(node, statefulSetName) {
396-
return true
397-
}
398-
}
399-
400-
return false
401-
}

internal/controller/typesensecluster_quorum_helpers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ func (r *TypesenseClusterReconciler) getQuorum(ctx context.Context, ts *tsv1alph
161161

162162
rawNodes, ok := cm.Data["nodes"]
163163
if !ok || strings.TrimSpace(rawNodes) == "" {
164-
return &Quorum{}, fmt.Errorf("configmap %s is missing 'nodes' key", cm.Name)
164+
err := fmt.Errorf("configmap %s is missing 'nodes' key", configMapName)
165+
return &Quorum{}, err
165166
}
166167
nodes := strings.Split(rawNodes, ",")
167168
availableNodes := len(nodes)

0 commit comments

Comments
 (0)