Skip to content

Commit

Permalink
feat: add more server stats to CR status
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsxu committed Feb 19, 2022
1 parent 4468364 commit c820c7b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 30 deletions.
21 changes: 15 additions & 6 deletions api/v1alpha1/zookeepercluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ type ZookeeperClusterSpec struct {
Config map[string]int `json:"config,omitempty"`
}

// ServerState is the server state of cluster, which takes from Zookeeper AdminServer
type ServerState struct {
Address string `json:"address"`

PacketsSent int `json:"packets_sent"`

PacketsReceived int `json:"packets_received"`
}

// ZookeeperClusterStatus defines the observed state of ZookeeperCluster
type ZookeeperClusterStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
Expand All @@ -44,19 +53,19 @@ type ZookeeperClusterStatus struct {
Replicas int32 `json:"replicas,omitempty"`

// ReadyReplicas is the actual replicas count in the cluster
ReadyReplicas int32 `json:"readyReplicas,omitempty"`
ReadyReplicas int32 `json:"readyReplicas"`

// Address is the exposed service endpoint of the cluster
Address string `json:"address,omitempty"`
// Endpoint is the exposed service endpoint of the cluster
Endpoint string `json:"endpoint,omitempty"`

// Nodes is the cluster pod status, podIP and role
Nodes map[string]string `json:"nodes,omitempty"`
// Servers is the server list with state of cluster
Servers map[string][]ServerState `json:"servers,omitempty"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name="Ready",type=integer,JSONPath=`.status.readyReplicas`,description="The actual Zookeeper servers"
//+kubebuilder:printcolumn:name="Address",type=string,JSONPath=`.status.address`,description="The exposed service endpoint of the cluster"
//+kubebuilder:printcolumn:name="Endpoint",type=string,JSONPath=`.status.endpoint`,description="The exposed service endpoint of the cluster"

// ZookeeperCluster is the Schema for the zookeeperclusters API
type ZookeeperCluster struct {
Expand Down
31 changes: 27 additions & 4 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 26 additions & 9 deletions config/crd/bases/zookeeper.atmax.io_zookeeperclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ spec:
name: Ready
type: integer
- description: The exposed service endpoint of the cluster
jsonPath: .status.address
name: Address
jsonPath: .status.endpoint
name: Endpoint
type: string
name: v1alpha1
schema:
Expand Down Expand Up @@ -59,14 +59,9 @@ spec:
status:
description: ZookeeperClusterStatus defines the observed state of ZookeeperCluster
properties:
address:
description: Address is the exposed service endpoint of the cluster
endpoint:
description: Endpoint is the exposed service endpoint of the cluster
type: string
nodes:
additionalProperties:
type: string
description: Nodes is the cluster pod status, podIP and role
type: object
readyReplicas:
description: ReadyReplicas is the actual replicas count in the cluster
format: int32
Expand All @@ -75,6 +70,28 @@ spec:
description: Replicas is the desired replicas count in the cluster
format: int32
type: integer
servers:
additionalProperties:
items:
description: ServerState is the server state of cluster, which
takes from Zookeeper AdminServer
properties:
address:
type: string
packets_received:
type: integer
packets_sent:
type: integer
required:
- address
- packets_received
- packets_sent
type: object
type: array
description: Servers is the server list with state of cluster
type: object
required:
- readyReplicas
type: object
type: object
served: true
Expand Down
36 changes: 25 additions & 11 deletions controllers/zookeepercluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"strings"
"time"

"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -85,7 +86,7 @@ func (r *ZookeeperClusterReconciler) Reconcile(ctx context.Context, req ctrl.Req
} {
if err := fn(ctx, zk); err != nil {
if err == ErrResultRequeue {
return ctrl.Result{Requeue: true}, nil
return ctrl.Result{RequeueAfter: time.Second}, nil
}
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -206,14 +207,12 @@ func (r *ZookeeperClusterReconciler) reconcileZookeeperClusterStatus(ctx context
}

if len(actualPods.Items) > 0 && len(actualPods.Items[0].Status.HostIP) > 0 {
zk.Status.Address = fmt.Sprintf("%s:%d", actualPods.Items[0].Status.HostIP, actualServiceClient.Spec.Ports[0].NodePort)
zk.Status.Endpoint = fmt.Sprintf("%s:%d", actualPods.Items[0].Status.HostIP, actualServiceClient.Spec.Ports[0].NodePort)
}

if zk.Status.Nodes == nil {
zk.Status.Nodes = make(map[string]string)
}
zk.Status.ReadyReplicas = 0
zk.Status.Servers = make(map[string][]zookeeperv1alpha1.ServerState)

readyReplicas := 0
for _, pod := range actualPods.Items {
podIP := pod.Status.PodIP
if len(podIP) == 0 {
Expand All @@ -226,17 +225,28 @@ func (r *ZookeeperClusterReconciler) reconcileZookeeperClusterStatus(ctx context
continue
}

zk.Status.Nodes[podIP] = zkStat.ServerStats.ServerState
readyReplicas++
}
if zkStat.Error != nil {
r.Logger.Info(fmt.Sprintf("Get Zookeeper stat error: %v", zkStat.Error))
continue
}

zk.Status.ReadyReplicas = int32(readyReplicas)
if _, exist := zk.Status.Servers[zkStat.ServerStats.ServerState]; !exist {
zk.Status.Servers[zkStat.ServerStats.ServerState] = make([]zookeeperv1alpha1.ServerState, 0)
}
zk.Status.Servers[zkStat.ServerStats.ServerState] = append(zk.Status.Servers[zkStat.ServerStats.ServerState], zookeeperv1alpha1.ServerState{
Address: podIP,
PacketsSent: zkStat.ServerStats.PacketsSent,
PacketsReceived: zkStat.ServerStats.PacketsReceived,
})

zk.Status.ReadyReplicas++
}

if err := r.Status().Update(ctx, zk); err != nil {
return err
}

if zk.Spec.Replicas == zk.Status.ReadyReplicas && zk.Spec.Replicas == int32(len(zk.Status.Nodes)) {
if zk.Spec.Replicas == zk.Status.ReadyReplicas {
return nil
}

Expand All @@ -254,6 +264,10 @@ func (r *ZookeeperClusterReconciler) createHeadlessService(zk *zookeeperv1alpha1
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "client",
Port: 2181,
},
{
Name: "server",
Port: 2888,
Expand Down

0 comments on commit c820c7b

Please sign in to comment.