Skip to content

Commit

Permalink
feat: use deployment for sentinel (#36)
Browse files Browse the repository at this point in the history
* rebase

* rebase

* rebase

* feat: use deployment for sentinel

* support crud for deployment

* rebase

* tmp

* tmp

* tmp

* tmp

* tmp

* remove log and update the resource requirement

* update readme

* add sentinelConfig

* test done

* remove println

* add sentinel.yaml

* tmp

* update

* e2e test done

* update

---------

Co-authored-by: Colin Chamber <[email protected]>
  • Loading branch information
jiayouxujin and ColinChamber authored Aug 9, 2023
1 parent 4d7cd87 commit e51f365
Show file tree
Hide file tree
Showing 20 changed files with 638 additions and 156 deletions.
18 changes: 9 additions & 9 deletions api/v1alpha1/kvrocks_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ type KVRocksSpec struct {
Type KVRocksType `json:"type"`
KVRocksConfig map[string]string `json:"kvrocksConfig,omitempty"`
// RocksDBConfig map[string]string `json:"rocksDBConfig,omitempty"`
Replicas int32 `json:"replicas"`
Master uint `json:"master"`
Password string `json:"password"`
Resources *corev1.ResourceRequirements `json:"resources"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
Toleration []corev1.Toleration `json:"toleration,omitempty"`
Affinity *corev1.Affinity `json:"affinity,omitempty"`
Storage *KVRocksStorage `json:"storage,omitempty"`
EnableSentinel bool `json:"enableSentinel,omitempty"`
Replicas int32 `json:"replicas"`
// +optional
Master uint `json:"master"`
Password string `json:"password"`
Resources *corev1.ResourceRequirements `json:"resources"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
Toleration []corev1.Toleration `json:"toleration,omitempty"`
Affinity *corev1.Affinity `json:"affinity,omitempty"`
Storage *KVRocksStorage `json:"storage,omitempty"`
}

// KVRocksStatus defines the observed state of KVRocks
Expand Down
3 changes: 0 additions & 3 deletions config/crd/bases/kvrocks.apache.org_kvrocks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -866,8 +866,6 @@ spec:
type: array
type: object
type: object
enableSentinel:
type: boolean
image:
type: string
imagePullPolicy:
Expand Down Expand Up @@ -993,7 +991,6 @@ spec:
type: string
required:
- image
- master
- password
- replicas
- resources
Expand Down
2 changes: 1 addition & 1 deletion config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ rules:
- list
- patch
- update
- watch
- watch
1 change: 0 additions & 1 deletion deploy/crd/templates/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,6 @@ spec:
type: string
required:
- image
- master
- password
- replicas
- resources
Expand Down
25 changes: 25 additions & 0 deletions examples/sentinel.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: kvrocks.com/v1alpha1
kind: KVRocks
metadata:
name: sentinel-1
namespace: kvrocks
spec:
image: redis:6.2.4
imagePullPolicy: IfNotPresent
replicas: 3
password: c4ca4238a0b923820dcc509a6f75849b
type: sentinel
toleration:
- key: kvrocks
effect: NoSchedule
operator: Exists
- key: node
effect: NoSchedule
operator: Exists
resources:
limits:
cpu: 500m
memory: 500Mi
requests:
cpu: 500m
memory: 500Mi
8 changes: 4 additions & 4 deletions examples/standard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ metadata:
namespace: kvrocks
labels:
kvrocks/system: gt
kvrocks/monitored-by: sentinel-1
spec:
image: apache/kvrocks:nightly
imagePullPolicy: IfNotPresent
master: 1
replicas: 3
type: standard
enableSentinel: true
password: "123456"
kvrocksConfig:
bind: "0.0.0.0"
Expand Down Expand Up @@ -40,9 +40,9 @@ spec:
rocksdb.compression: "no"
rocksdb.wal_ttl_seconds: "0"
rocksdb.wal_size_limit_mb: "0"
# storage:
# size: 32Gi
# class: local-hostpath
# storage:
# size: 32Gi
# class: local-hostpath
toleration:
- key: kvrocks
effect: NoSchedule
Expand Down
49 changes: 49 additions & 0 deletions pkg/client/k8s/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package k8s

import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func (c *Client) CreateIfNotExistsDeployment(deployment *appsv1.Deployment) error {
if err := c.client.Create(ctx, deployment); err != nil && !errors.IsAlreadyExists(err) {
return err
}
c.logger.V(1).Info("create deployment successfully", "deployment", deployment.Name)
return nil
}

func (c *Client) GetDeployment(key types.NamespacedName) (*appsv1.Deployment, error) {
var deployment appsv1.Deployment
if err := c.client.Get(ctx, key, &deployment); err != nil {
return nil, err
}
return &deployment, nil
}

func (c *Client) UpdateDeployment(deployment *appsv1.Deployment) error {
if err := c.client.Update(ctx, deployment); err != nil {
return err
}
c.logger.V(1).Info("update deployment successfully", "deployment", deployment.Name)
return nil
}

func (c *Client) ListDeploymentPods(key types.NamespacedName) (*corev1.PodList, error) {
deployment, err := c.GetDeployment(key)
if err != nil {
return nil, err
}
labels := make(map[string]string)
for k, v := range deployment.Spec.Selector.MatchLabels {
labels[k] = v
}
var pods corev1.PodList
if err := c.client.List(ctx, &pods, client.InNamespace(deployment.Namespace), client.MatchingLabels(labels)); err != nil {
return nil, err
}
return &pods, nil
}
Loading

0 comments on commit e51f365

Please sign in to comment.