Skip to content

Commit

Permalink
k8s: Move ordinal extraction to seperated function
Browse files Browse the repository at this point in the history
(cherry picked from commit 29d348a)
  • Loading branch information
Rafal Korepta authored and joejulian committed Apr 12, 2023
1 parent 077bb64 commit 3891186
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/go/k8s/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Use all your favorite open source tooling - 10x faster.
## Getting started

Official Kubernetes quick start documentation can be found at
[https://vectorized.io/docs/](https://vectorized.io/docs/quick-start-kubernetes)
[https://docs.redpanda.com/docs/](https://docs.redpanda.com/docs/platform/quickstart/kubernetes-qs-dev/)

### Requirements

Expand Down
22 changes: 16 additions & 6 deletions src/go/k8s/controllers/redpanda/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func (r *ClusterReconciler) handlePodFinalizer(
// if it's not gone
if broker != nil {
// decommission it
log.WithValues(nodeID).Info("decommissioning broker")
log.WithValues("node-id", nodeID).Info("decommissioning broker")
if err = adminClient.DecommissionBroker(ctx, nodeID); err != nil {
return fmt.Errorf(`unable to decommission node "%d": %w`, nodeID, err)
}
Expand Down Expand Up @@ -470,7 +470,7 @@ func (r *ClusterReconciler) handlePodFinalizer(
}
continue
}
log.WithValues(key).Info("deleting PersistentVolumeClaim")
log.WithValues("persistent-volume-claim", key).Info("deleting PersistentVolumeClaim")
if err := r.Delete(ctx, &pvc, &client.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf(`unable to delete PersistentVolumeClaim "%s/%s": %w`, key.Name, key.Namespace, err)
}
Expand All @@ -488,7 +488,7 @@ func (r *ClusterReconciler) removePodFinalizer(
ctx context.Context, pod *corev1.Pod, log logr.Logger,
) error {
if controllerutil.ContainsFinalizer(pod, FinalizerKey) {
log.V(7).WithValues(pod.Namespace, pod.Name).Info("removing finalizer")
log.V(7).WithValues("namespace", pod.Namespace, "name", pod.Name).Info("removing finalizer")
controllerutil.RemoveFinalizer(pod, FinalizerKey)
if err := r.Update(ctx, pod); err != nil {
return err
Expand All @@ -501,7 +501,7 @@ func (r *ClusterReconciler) setPodFinalizer(
ctx context.Context, pod *corev1.Pod, log logr.Logger,
) error {
if !controllerutil.ContainsFinalizer(pod, FinalizerKey) {
log.V(7).WithValues(pod.Namespace, pod.Name).Info("adding finalizer")
log.V(7).WithValues("namespace", pod.Namespace, "name", pod.Name).Info("adding finalizer")
controllerutil.AddFinalizer(pod, FinalizerKey)
if err := r.Update(ctx, pod); err != nil {
return err
Expand Down Expand Up @@ -530,7 +530,7 @@ func (r *ClusterReconciler) setPodNodeIDAnnotation(
if err != nil {
return fmt.Errorf("cannot fetch node id for node-id annotation: %w", err)
}
log.WithValues(pod.Name, nodeID).Info("setting node-id annotation")
log.WithValues("pod-name", pod.Name, "node-id", nodeID).Info("setting node-id annotation")
pod.Annotations[PodAnnotationNodeIDKey] = fmt.Sprintf("%d", nodeID)
if err := r.Update(ctx, pod, &client.UpdateOptions{}); err != nil {
return fmt.Errorf(`unable to update pod "%s" with node-id annotation: %w`, pod.Name, err)
Expand All @@ -551,7 +551,7 @@ func (r *ClusterReconciler) fetchAdminNodeID(ctx context.Context, rp *redpandav1
return -1, fmt.Errorf("creating pki: %w", err)
}

ordinal, err := strconv.ParseInt(pod.Name[len(rp.Name)+1:], 10, 0)
ordinal, err := strconv.ParseInt(getPodOrdinal(pod.Name, rp.Name), 10, 0)
if err != nil {
return -1, fmt.Errorf("cluster %s: cannot convert pod name (%s) to ordinal: %w", rp.Name, pod.Name, err)
}
Expand All @@ -567,6 +567,16 @@ func (r *ClusterReconciler) fetchAdminNodeID(ctx context.Context, rp *redpandav1
return int32(cfg.NodeID), nil
}

func getPodOrdinal(podName string, clusterName string) string {
// Pod name needs to have at least 2 more characters
if len(podName) < len(clusterName)+2 {
return ""
}

// The +1 is for the separator between stateful set name and pod ordinal
return podName[len(clusterName)+1:]
}

func (r *ClusterReconciler) reportStatus(
ctx context.Context,
redpandaCluster *redpandav1alpha1.Cluster,
Expand Down
4 changes: 4 additions & 0 deletions src/go/k8s/pkg/resources/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,10 @@ func (r *StatefulSetResource) obj(
ContainerPort: int32(r.pandaCluster.Spec.Configuration.RPCServer.Port),
},
}, r.getPorts()...),
SecurityContext: &corev1.SecurityContext{
RunAsUser: pointer.Int64Ptr(userID),
RunAsGroup: pointer.Int64Ptr(groupID),
},
Resources: corev1.ResourceRequirements{
Limits: r.pandaCluster.Spec.Resources.Limits,
Requests: r.pandaCluster.Spec.Resources.Requests,
Expand Down
2 changes: 1 addition & 1 deletion src/go/k8s/tests/e2e/superusers-prefix/01-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ collectors:
- type: command
command: kubectl get secret -n superusers-prefix -o jsonpath='{.data.username}' cluster-schema-registry-sasl
- type: command
command: kubectl get secret -n superusers-prefix -o jsonpath='{.data.username}' cluster-asl
command: kubectl get secret -n superusers-prefix -o jsonpath='{.data.username}' cluster-sasl

0 comments on commit 3891186

Please sign in to comment.