Skip to content

Commit c38053a

Browse files
authored
Merge pull request #18 from ucloud/v0.2.0
V0.2.0
2 parents d91fe97 + 8e42f50 commit c38053a

File tree

85 files changed

+4473
-1201
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+4473
-1201
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,6 @@ tags
7777
# End of https://www.gitignore.io/api/go,vim,emacs,visualstudiocode
7878
.idea/*
7979
vendor/*
80-
manager
81-
main
80+
/main
81+
/Dockerfile-withvendor
82+
Dockerfile-TZSH

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.12.9-alpine3.9 as go-builder
1+
FROM golang:1.13.3-alpine as go-builder
22

33
RUN apk update && apk upgrade && \
44
apk add --no-cache ca-certificates git mercurial
@@ -19,7 +19,7 @@ RUN go mod download
1919
COPY pkg ./ cmd ./ version ./
2020

2121
RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o ${GOBIN}/${PROJECT_NAME} \
22-
-ldflags "-X ${REPO_PATH}/pkg/version.Version=${VERSION} -X ${REPO_PATH}/pkg/version.GitSHA=${GIT_SHA}" \
22+
-ldflags "-X ${REPO_PATH}/version.Version=${VERSION} -X ${REPO_PATH}/version.GitSHA=${GIT_SHA}" \
2323
$BUILD_PATH
2424

2525
# =============================================================================

Dockerfile-withvendor

Lines changed: 0 additions & 32 deletions
This file was deleted.

Makefile

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
SHELL=/bin/bash -o pipefail
2+
3+
PROJECT_NAME=redis-cluster-operator
4+
REPO=ucloud/$(PROJECT_NAME)
5+
6+
# replace with your public registry
7+
ALTREPO=$(DOCKER_REGISTRY)/$(PROJECT_NAME)
8+
E2EALTREPO=$(DOCKER_REGISTRY)/$(PROJECT_NAME)-e2e
9+
10+
VERSION=$(shell git describe --always --tags --dirty | sed "s/\(.*\)-g`git rev-parse --short HEAD`/\1/")
11+
GIT_SHA=$(shell git rev-parse --short HEAD)
12+
BIN_DIR=build/bin
13+
.PHONY: all build check clean test login build-e2e push push-e2e build-tools
14+
15+
all: check build
16+
17+
build: test build-go build-image
18+
19+
build-go:
20+
GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
21+
-ldflags "-X github.com/$(REPO)/version.Version=$(VERSION) -X github.com/$(REPO)/version.GitSHA=$(GIT_SHA)" \
22+
-o $(BIN_DIR)/$(PROJECT_NAME)-linux-amd64 cmd/manager/main.go
23+
GO111MODULE=on CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build \
24+
-ldflags "-X github.com/$(REPO)/version.Version=$(VERSION) -X github.com/$(REPO)/version.GitSHA=$(GIT_SHA)" \
25+
-o $(BIN_DIR)/$(PROJECT_NAME)-darwin-amd64 cmd/manager/main.go
26+
27+
build-image:
28+
docker build --build-arg VERSION=$(VERSION) --build-arg GIT_SHA=$(GIT_SHA) -t $(ALTREPO):$(VERSION) .
29+
docker tag $(ALTREPO):$(VERSION) $(ALTREPO):latest
30+
31+
build-e2e:
32+
docker build -t $(E2EALTREPO):$(VERSION) -f test/e2e/Dockerfile .
33+
34+
build-tools:
35+
bash hack/docker/redis-tools/make.sh build
36+
37+
test:
38+
GO111MODULE=on go test $$(go list ./... | grep -v /vendor/) -race -coverprofile=coverage.txt -covermode=atomic
39+
40+
login:
41+
@docker login -u "$(DOCKER_USER)" -p "$(DOCKER_PASS)"
42+
43+
push: build-image
44+
docker push $(ALTREPO):$(VERSION)
45+
docker push $(ALTREPO):latest
46+
47+
push-e2e: build-e2e
48+
docker push $(E2EALTREPO):$(VERSION)
49+
50+
clean:
51+
rm -f $(BIN_DIR)/$(PROJECT_NAME)*
52+
53+
check: check-format
54+
55+
check-format:
56+
@test -z "$$(gofmt -s -l . 2>&1 | grep -v -e vendor/ | tee /dev/stderr)"

README.md

Lines changed: 85 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# redis-operator
1+
# redis-cluster-operator
22

33
## Overview
44

@@ -8,10 +8,39 @@ The operator itself is built with the [Operator framework](https://github.com/op
88

99
![Redis Cluster atop Kubernetes](/static/redis-cluster.png)
1010

11+
Each master node and its slave nodes is managed by a statefulSet, create a headless svc for each statefulSet,
12+
and create a clusterIP service for all nodes.
13+
14+
Each statefulset uses PodAntiAffinity to ensure that the master and slaves are dispersed on different nodes.
15+
At the same time, when the operator selects the master in each statefulset, it preferentially select the pod
16+
with different k8s nodes as master.
17+
18+
Table of Contents
19+
=================
20+
21+
* [redis-cluster-operator](#redis-cluster-operator)
22+
* [Overview](#overview)
23+
* [Prerequisites](#prerequisites)
24+
* [Features](#features)
25+
* [Quick Start](#quick-start)
26+
* [Deploy redis cluster operator](#deploy-redis-cluster-operator)
27+
* [Deploy a sample Redis Cluster](#deploy-a-sample-redis-cluster)
28+
* [Scaling Up the Redis Cluster](#scaling-up-the-redis-cluster)
29+
* [Scaling Down the Redis Cluster](#scaling-down-the-redis-cluster)
30+
* [Backup and Restore](#backup-and-restore)
31+
* [Prometheus Discovery](#prometheus-discovery)
32+
* [Create Redis Cluster with password](#create-redis-cluster-with-password)
33+
* [Persistent Volume](#persistent-volume)
34+
* [Custom Configuration](#custom-configuration)
35+
* [Custom Service](#custom-service)
36+
* [Custom Resource](#custom-resource)
37+
* [ValidatingWebhook](#validatingwebhook)
38+
* [End to end tests](#end-to-end-tests)
39+
1140
## Prerequisites
1241

1342
* go version v1.13+.
14-
* Access to a Kubernetes v1.13.3 cluster.
43+
* Access to a Kubernetes v1.13.10 cluster.
1544

1645
## Features
1746

@@ -29,7 +58,6 @@ The operator itself is built with the [Operator framework](https://github.com/op
2958

3059
- __Prometheus Discovery__
3160

32-
3361
## Quick Start
3462

3563
### Deploy redis cluster operator
@@ -76,33 +104,41 @@ NAME MASTERSIZE STATUS AGE
76104
example-distributedrediscluster 3 Scaling 11s
77105
78106
$ kubectl get all -l redis.kun/name=example-distributedrediscluster
79-
NAME READY STATUS RESTARTS AGE
80-
pod/drc-example-distributedrediscluster-0 1/1 Running 0 4m5s
81-
pod/drc-example-distributedrediscluster-1 1/1 Running 0 3m31s
82-
pod/drc-example-distributedrediscluster-2 1/1 Running 0 2m54s
83-
pod/drc-example-distributedrediscluster-3 1/1 Running 0 2m20s
84-
pod/drc-example-distributedrediscluster-4 1/1 Running 0 103s
85-
pod/drc-example-distributedrediscluster-5 1/1 Running 0 62s
86-
87-
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
88-
service/example-distributedrediscluster ClusterIP None <none> 6379/TCP,16379/TCP 4m5s
89-
90-
NAME READY AGE
91-
statefulset.apps/drc-example-distributedrediscluster 6/6 4m5s
107+
NAME READY STATUS RESTARTS AGE
108+
pod/drc-example-distributedrediscluster-0-0 1/1 Running 0 2m48s
109+
pod/drc-example-distributedrediscluster-0-1 1/1 Running 0 2m8s
110+
pod/drc-example-distributedrediscluster-1-0 1/1 Running 0 2m48s
111+
pod/drc-example-distributedrediscluster-1-1 1/1 Running 0 2m13s
112+
pod/drc-example-distributedrediscluster-2-0 1/1 Running 0 2m48s
113+
pod/drc-example-distributedrediscluster-2-1 1/1 Running 0 2m15s
114+
115+
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
116+
service/example-distributedrediscluster ClusterIP 172.17.132.71 <none> 6379/TCP,16379/TCP 2m48s
117+
service/example-distributedrediscluster-0 ClusterIP None <none> 6379/TCP,16379/TCP 2m48s
118+
service/example-distributedrediscluster-1 ClusterIP None <none> 6379/TCP,16379/TCP 2m48s
119+
service/example-distributedrediscluster-2 ClusterIP None <none> 6379/TCP,16379/TCP 2m48s
120+
121+
NAME READY AGE
122+
statefulset.apps/drc-example-distributedrediscluster-0 2/2 2m48s
123+
statefulset.apps/drc-example-distributedrediscluster-1 2/2 2m48s
124+
statefulset.apps/drc-example-distributedrediscluster-2 2/2 2m48s
92125
93126
$ kubectl get distributedrediscluster
94127
NAME MASTERSIZE STATUS AGE
95128
example-distributedrediscluster 3 Healthy 4m
96129
```
97130

98-
#### Scaling the Redis Cluster
131+
#### Scaling Up the Redis Cluster
99132

100-
Increase the masterSize to trigger the scaling.
133+
Increase the masterSize to trigger the scaling up.
101134

102135
```
103136
apiVersion: redis.kun/v1alpha1
104137
kind: DistributedRedisCluster
105138
metadata:
139+
annotations:
140+
# if your operator run as cluster-scoped, add this annotations
141+
redis.kun/scope: cluster-scoped
106142
name: example-distributedrediscluster
107143
spec:
108144
# Increase the masterSize to trigger the scaling.
@@ -111,8 +147,29 @@ spec:
111147
image: redis:5.0.4-alpine
112148
```
113149

150+
#### Scaling Down the Redis Cluster
151+
152+
Decrease the masterSize to trigger the scaling down.
153+
154+
```
155+
apiVersion: redis.kun/v1alpha1
156+
kind: DistributedRedisCluster
157+
metadata:
158+
annotations:
159+
# if your operator run as cluster-scoped, add this annotations
160+
redis.kun/scope: cluster-scoped
161+
name: example-distributedrediscluster
162+
spec:
163+
# Increase the masterSize to trigger the scaling.
164+
masterSize: 3
165+
ClusterReplicas: 1
166+
image: redis:5.0.4-alpine
167+
```
168+
114169
#### Backup and Restore
115170

171+
**Only Ceph object storage is supported now**
172+
116173
Backup
117174
```
118175
$ kubectl create -f deploy/example/backup-restore/redisclusterbackup_cr.yaml
@@ -147,7 +204,7 @@ $ kubectl create -f deploy/example/persistent.yaml
147204
$ kubectl create -f deploy/example/custom-config.yaml
148205
```
149206

150-
#### Custom Headless Service
207+
#### Custom Service
151208

152209
```
153210
$ kubectl create -f deploy/example/custom-service.yaml
@@ -157,4 +214,12 @@ $ kubectl create -f deploy/example/custom-service.yaml
157214

158215
```
159216
$ kubectl create -f deploy/example/custom-resources.yaml
160-
```
217+
```
218+
219+
## ValidatingWebhook
220+
221+
see [ValidatingWebhook](/hack/webhook/README.md)
222+
223+
## End to end tests
224+
225+
see [e2e](/test/e2e/README.md)

cmd/manager/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ import (
2727
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
2828

2929
"github.com/ucloud/redis-cluster-operator/pkg/apis"
30+
redisv1alpha1 "github.com/ucloud/redis-cluster-operator/pkg/apis/redis/v1alpha1"
3031
config2 "github.com/ucloud/redis-cluster-operator/pkg/config"
3132
"github.com/ucloud/redis-cluster-operator/pkg/controller"
33+
"github.com/ucloud/redis-cluster-operator/pkg/controller/distributedrediscluster"
34+
"github.com/ucloud/redis-cluster-operator/pkg/controller/redisclusterbackup"
35+
"github.com/ucloud/redis-cluster-operator/pkg/utils"
3236
"github.com/ucloud/redis-cluster-operator/version"
3337
)
3438

@@ -52,6 +56,9 @@ func main() {
5256
// be added before calling pflag.Parse().
5357
pflag.CommandLine.AddFlagSet(zap.FlagSet())
5458

59+
pflag.CommandLine.AddFlagSet(distributedrediscluster.FlagSet())
60+
pflag.CommandLine.AddFlagSet(redisclusterbackup.FlagSet())
61+
5562
// Add flags registered by imported packages (e.g. glog and
5663
// controller-runtime)
5764
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
@@ -78,6 +85,8 @@ func main() {
7885
os.Exit(1)
7986
}
8087

88+
utils.SetClusterScoped(namespace)
89+
8190
// Get a config to talk to the apiserver
8291
cfg, err := config.GetConfig()
8392
if err != nil {
@@ -146,6 +155,17 @@ func main() {
146155
}
147156
}
148157

158+
if os.Getenv("ENABLE_WEBHOOKS") == "true" {
159+
log.Info("Starting the WebHook.")
160+
ws := mgr.GetWebhookServer()
161+
ws.CertDir = "/etc/webhook/certs"
162+
ws.Port = 7443
163+
if err = (&redisv1alpha1.DistributedRedisCluster{}).SetupWebhookWithManager(mgr); err != nil {
164+
log.Error(err, "unable to create webHook", "webHook", "DistributedRedisCluster")
165+
os.Exit(1)
166+
}
167+
}
168+
149169
log.Info("Starting the Cmd.")
150170

151171
// Start the Cmd

deploy/cluster/cluster_role.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ rules:
1414
- get
1515
- list
1616
- watch
17+
- delete
1718
- apiGroups:
1819
- ""
1920
resources:
@@ -29,6 +30,7 @@ rules:
2930
- patch
3031
- update
3132
- watch
33+
- delete
3234
- apiGroups:
3335
- ""
3436
resources:
@@ -48,6 +50,7 @@ rules:
4850
- patch
4951
- update
5052
- watch
53+
- delete
5154
- apiGroups:
5255
- apps
5356
resources:
@@ -61,6 +64,7 @@ rules:
6164
- patch
6265
- update
6366
- watch
67+
- delete
6468
- apiGroups:
6569
- policy
6670
resources:
@@ -72,6 +76,7 @@ rules:
7276
- patch
7377
- update
7478
- watch
79+
- delete
7580
- apiGroups:
7681
- apps
7782
resourceNames:

0 commit comments

Comments
 (0)