Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be able to bind statefulsets to the host network #51

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0781bfb
make operator able to chose when a cluster is bind to the hosts (ex. …
nangelhotelbeds Jun 14, 2020
09ad959
export variable and remove unnecessary condition of nil bool
nangelhotelbeds Jun 14, 2020
defa632
remove typo underscore.
nangelhotelbeds Jun 14, 2020
a931b68
make configurable the port in all places but in the redis conf file.
nangelhotelbeds Aug 13, 2020
227c7de
remove gossip as is always 10000 + clientPort
nangelhotelbeds Aug 13, 2020
4babaf1
revert stringarg port configuration
nangelhotelbeds Aug 14, 2020
3dbc161
deleted done todo
nangelhotelbeds Aug 14, 2020
9e326c7
Merge pull request #1 from angel9484/feature/make_configurable_port_a…
angel9484 Aug 14, 2020
1769dfb
spread port for all operations using cli or -h
nangelhotelbeds Feb 21, 2021
54d0833
remove default port
nangelhotelbeds Feb 21, 2021
9336f4d
spread port from arguments the port.
nangelhotelbeds Feb 21, 2021
7f6f520
expose also monitor prometheus port when using hostnetwork.
nangelhotelbeds Feb 21, 2021
b515195
Merge pull request #2 from angel9484/feature/make_configurable_port_a…
angel9484 Feb 21, 2021
5c0899c
Merge remote-tracking branch 'forked/master' into feature/make_config…
nangelhotelbeds Feb 21, 2021
f34d70e
Merge pull request #3 from angel9484/feature/make_configurable_port_a…
angel9484 Feb 21, 2021
4731615
remove -p from non redis-cli command
nangelhotelbeds Feb 22, 2021
b0f468c
remove -p from non redis-cli command
nangelhotelbeds Feb 22, 2021
792deab
Merge pull request #4 from angel9484/feature/make_configurable_port_a…
angel9484 Feb 22, 2021
a82e83c
delete again check of min number of replicas as original code has
nangelhotelbeds Mar 24, 2021
20d0d60
Merge pull request #5 from angel9484/feature/make_configurable_port_a…
angel9484 Mar 24, 2021
ec08b99
use directly osm
nangelhotelbeds Apr 11, 2022
5bc679d
restore deleted const
nangelhotelbeds Apr 11, 2022
b9947ef
Merge pull request #6 from angel9484/feature/native_s3_support_for_ba…
angel9484 Apr 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions hack/docker/redis-tools/redis-tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ show_help() {
echo "-h, --help show brief help"
echo " --data-dir=DIR path to directory holding db data (default: /var/data)"
echo " --host=HOST database host"
echo " --port=PORT database port"
echo " --user=USERNAME database username"
echo " --bucket=BUCKET name of bucket"
echo " --location=LOCATION location of backend (<provider>:<bucket name>)"
Expand Down Expand Up @@ -50,6 +51,10 @@ while test $# -gt 0; do
export REDIS_HOST=$(echo $1 | sed -e 's/^[^=]*=//g')
shift
;;
--port*)
export REDIS_PORT=$(echo $1 | sed -e 's/^[^=]*=//g')
shift
;;
--user*)
export REDIS_USER=$(echo $1 | sed -e 's/^[^=]*=//g')
shift
Expand Down Expand Up @@ -108,8 +113,8 @@ case "$op" in
# cleanup data dump dir
rm -rf *

redis-cli --rdb dump.rdb -h "${REDIS_HOST}" -a "${REDIS_PASSWORD}"
redis-cli -h "${REDIS_HOST}" -a "${REDIS_PASSWORD}" CLUSTER NODES | grep myself > nodes.conf
redis-cli --rdb dump.rdb -h "${REDIS_HOST}" -p "${REDIS_PORT}" -a "${REDIS_PASSWORD}"
redis-cli -h "${REDIS_HOST}" -p "${REDIS_PORT}" -a "${REDIS_PASSWORD}" CLUSTER NODES | grep myself > nodes.conf
pwd
ls -lh "$SOURCE_DIR"
echo "Uploading dump file to the backend......."
Expand Down
27 changes: 27 additions & 0 deletions make.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM golang:1.13.3 as go-builder

RUN apt-get update && apt-get -y upgrade && \
apt-get install -y ca-certificates git mercurial

ARG PROJECT_NAME=redis-cluster-operator
ARG REPO_PATH=github.com/ucloud/$PROJECT_NAME
ARG BUILD_PATH=${REPO_PATH}/cmd/manager

# Build version and commit should be passed in when performing docker build
ARG VERSION=0.1.1
ARG GIT_SHA=0000000

WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download

COPY pkg ./ cmd ./ version ./

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

COPY . ./
RUN apt-get install -y bash git make cmake gcc openssh-client openssh-server
RUN apt-get install -y libc-dev
RUN make build
11 changes: 11 additions & 0 deletions pkg/apis/redis/v1alpha1/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
minClusterReplicas = 1
defaultRedisImage = "redis:5.0.4-alpine"
defaultMonitorImage = "oliver006/redis_exporter:latest"
defaultClientPort = 6379
)

func (in *DistributedRedisCluster) DefaultSpec(log logr.Logger) bool {
Expand All @@ -25,6 +26,16 @@ func (in *DistributedRedisCluster) DefaultSpec(log logr.Logger) bool {
update = true
}

if in.Spec.ClientPort == 0 {
in.Spec.ClientPort = defaultClientPort
update = true
}

if in.Spec.GossipPort != (in.Spec.ClientPort + 10000) {
in.Spec.GossipPort = in.Spec.ClientPort + 10000
update = true
}

if in.Spec.Image == "" {
in.Spec.Image = defaultRedisImage
update = true
Expand Down
27 changes: 15 additions & 12 deletions pkg/apis/redis/v1alpha1/distributedrediscluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ type DistributedRedisClusterSpec struct {
ServiceName string `json:"serviceName,omitempty"`
Config map[string]string `json:"config,omitempty"`
// Set RequiredAntiAffinity to force the master-slave node anti-affinity.
RequiredAntiAffinity bool `json:"requiredAntiAffinity,omitempty"`
Affinity *corev1.Affinity `json:"affinity,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
ToleRations []corev1.Toleration `json:"toleRations,omitempty"`
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
ContainerSecurityContext *corev1.SecurityContext `json:"containerSecurityContext,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Storage *RedisStorage `json:"storage,omitempty"`
Resources *corev1.ResourceRequirements `json:"resources,omitempty"`
PasswordSecret *corev1.LocalObjectReference `json:"passwordSecret,omitempty"`
Monitor *AgentSpec `json:"monitor,omitempty"`
Init *InitSpec `json:"init,omitempty"`
RequiredAntiAffinity bool `json:"requiredAntiAffinity,omitempty"`
Affinity *corev1.Affinity `json:"affinity,omitempty"`
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
ToleRations []corev1.Toleration `json:"toleRations,omitempty"`
SecurityContext *corev1.PodSecurityContext `json:"securityContext,omitempty"`
ContainerSecurityContext *corev1.SecurityContext `json:"containerSecurityContext,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Storage *RedisStorage `json:"storage,omitempty"`
Resources *corev1.ResourceRequirements `json:"resources,omitempty"`
PasswordSecret *corev1.LocalObjectReference `json:"passwordSecret,omitempty"`
Monitor *AgentSpec `json:"monitor,omitempty"`
Init *InitSpec `json:"init,omitempty"`
HostNetwork bool `json:"hostNetwork,omitempty"`
ClientPort int `json:"clientPort,omitempty"`
GossipPort int `json:"gossipPort,omitempty"`
}

type AgentSpec struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/redis/v1alpha1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,13 @@ func (r *ReconcileDistributedRedisCluster) Reconcile(request reconcile.Request)
return reconcile.Result{}, Kubernetes.Wrap(err, "getClusterPassword")
}

admin, err := newRedisAdmin(ctx.pods, password, config.RedisConf(), reqLogger)
admin, err := newRedisAdmin(ctx.pods, password, config.RedisConf(), reqLogger, instance.Spec.ClientPort)
if err != nil {
return reconcile.Result{}, Redis.Wrap(err, "newRedisAdmin")
}
defer admin.Close()

clusterInfos, err := admin.GetClusterInfos()
clusterInfos, err := admin.GetClusterInfos(ctx.cluster.Spec.ClientPort)
if err != nil {
if clusterInfos.Status == redisutil.ClusterInfosPartial {
return reconcile.Result{}, Redis.Wrap(err, "GetClusterInfos")
Expand Down Expand Up @@ -331,8 +331,7 @@ func (r *ReconcileDistributedRedisCluster) Reconcile(request reconcile.Request)
return reconcile.Result{}, err
}
}

newClusterInfos, err := admin.GetClusterInfos()
newClusterInfos, err := admin.GetClusterInfos(ctx.cluster.Spec.ClientPort)
if err != nil {
if clusterInfos.Status == redisutil.ClusterInfosPartial {
return reconcile.Result{}, Redis.Wrap(err, "GetClusterInfos")
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/distributedrediscluster/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package distributedrediscluster
import (
"fmt"
"net"
"strconv"
"time"

"github.com/go-logr/logr"
Expand All @@ -29,10 +30,10 @@ func getLabels(cluster *redisv1alpha1.DistributedRedisCluster) map[string]string
}

// newRedisAdmin builds and returns new redis.Admin from the list of pods
func newRedisAdmin(pods []*corev1.Pod, password string, cfg *config.Redis, reqLogger logr.Logger) (redisutil.IAdmin, error) {
func newRedisAdmin(pods []*corev1.Pod, password string, cfg *config.Redis, reqLogger logr.Logger, clientPort int) (redisutil.IAdmin, error) {
nodesAddrs := []string{}
for _, pod := range pods {
redisPort := redisutil.DefaultRedisPort
redisPort := strconv.Itoa(clientPort)
for _, container := range pod.Spec.Containers {
if container.Name == "redis" {
for _, port := range container.Ports {
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/distributedrediscluster/sync_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (r *ReconcileDistributedRedisCluster) initRestore(cluster *redisv1alpha1.Di
}

func (r *ReconcileDistributedRedisCluster) waitForClusterJoin(ctx *syncContext) error {
if infos, err := ctx.admin.GetClusterInfos(); err == nil {
if infos, err := ctx.admin.GetClusterInfos(ctx.cluster.Spec.ClientPort); err == nil {
ctx.reqLogger.V(6).Info("debug waitForClusterJoin", "cluster infos", infos)
return nil
}
Expand All @@ -180,7 +180,7 @@ func (r *ReconcileDistributedRedisCluster) waitForClusterJoin(ctx *syncContext)
// the config as they are still empty with unassigned slots.
time.Sleep(1 * time.Second)

_, err = ctx.admin.GetClusterInfos()
_, err = ctx.admin.GetClusterInfos(ctx.cluster.Spec.ClientPort)
if err != nil {
return Requeue.Wrap(err, "wait for cluster join")
}
Expand Down Expand Up @@ -278,7 +278,7 @@ func (r *ReconcileDistributedRedisCluster) scalingDown(ctx *syncContext, current
if len(node.Slots) > 0 {
return Redis.New(fmt.Sprintf("node %s is not empty! Reshard data away and try again", node.String()))
}
if err := admin.ForgetNode(node.ID); err != nil {
if err := admin.ForgetNode(node.ID,ctx.cluster.Spec.ClientPort); err != nil {
return Redis.Wrap(err, "ForgetNode")
}
}
Expand Down Expand Up @@ -346,7 +346,7 @@ func (r *ReconcileDistributedRedisCluster) resetClusterPassword(ctx *syncContext
}

podSet := clusterPods(redisClusterPods.Items)
admin, err := newRedisAdmin(podSet, oldPassword, config.RedisConf(), ctx.reqLogger)
admin, err := newRedisAdmin(podSet, oldPassword, config.RedisConf(), ctx.reqLogger, ctx.cluster.Spec.ClientPort)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/heal/failednodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (c *CheckAndHeal) FixFailedNodes(cluster *redisv1alpha1.DistributedRedisClu
c.Logger.Info("[FixFailedNodes] Forgetting failed node, this command might fail, this is not an error", "node", id)
if !c.DryRun {
c.Logger.Info("[FixFailedNodes] try to forget node", "nodeId", id)
if err := admin.ForgetNode(id); err != nil {
if err := admin.ForgetNode(id, cluster.Spec.ClientPort); err != nil {
errs = append(errs, err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/heal/untrustenodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *CheckAndHeal) FixUntrustedNodes(cluster *redisv1alpha1.DistributedRedis
doneAnAction = true
if !c.DryRun {
c.Logger.Info("[FixUntrustedNodes] try to forget node", "nodeId", id)
if err := admin.ForgetNode(id); err != nil {
if err := admin.ForgetNode(id,cluster.Spec.ClientPort); err != nil {
errs = append(errs, err)
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/redisclusterbackup/sync_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func (r *ReconcileRedisClusterBackup) backupContainers(backup *redisv1alpha1.Red
fmt.Sprintf(`--data-dir=%s`, redisv1alpha1.BackupDumpDir),
fmt.Sprintf(`--location=%s`, location),
fmt.Sprintf(`--host=%s`, node.IP),
fmt.Sprintf(`--port=%s`, node.Port),
fmt.Sprintf(`--folder=%s`, folderName),
fmt.Sprintf(`--snapshot=%s-%d`, backup.Name, i),
"--",
Expand Down
110 changes: 58 additions & 52 deletions pkg/osm/osm.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,69 +80,75 @@ func NewOSMContext(client client.Client, spec api.Backend, namespace string) (*o

if spec.S3 != nil {
nc.Provider = s3.Kind

keyID, foundKeyID := config[awsconst.AWS_ACCESS_KEY_ID]
key, foundKey := config[awsconst.AWS_SECRET_ACCESS_KEY]
if foundKey && foundKeyID {
if spec.S3.Endpoint == "" || spec.S3.Endpoint == "osm" {
nc.Config[s3.ConfigAccessKeyID] = string(keyID)
nc.Config[s3.ConfigSecretKey] = string(key)
nc.Config[s3.ConfigAuthType] = "accesskey"
nc.Config[s3.ConfigRegion] = spec.S3.Region
} else {
nc.Config[s3.ConfigAuthType] = "iam"
}
if spec.S3.Endpoint == "" || strings.HasSuffix(spec.S3.Endpoint, ".amazonaws.com") {
// Using s3 and not s3-compatible service like minio or rook, etc. Now, find region
var sess *session.Session
var err error
if nc.Config[s3.ConfigAuthType] == "iam" {
// The aws sdk does not currently support automatically setting the region based on an instances placement.
// This automatically sets region based on ec2 instance metadata when running on EC2.
// ref: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-region.html#setting-region-order-of-precedence
var c aws.Config
if s, e := session.NewSession(); e == nil {
if region, e := ec2metadata.New(s).Region(); e == nil {
c.WithRegion(region)
if foundKey && foundKeyID {
nc.Config[s3.ConfigAccessKeyID] = string(keyID)
nc.Config[s3.ConfigSecretKey] = string(key)
nc.Config[s3.ConfigAuthType] = "accesskey"
} else {
nc.Config[s3.ConfigAuthType] = "iam"
}
if spec.S3.Endpoint == "" || strings.HasSuffix(spec.S3.Endpoint, ".amazonaws.com") {
// Using s3 and not s3-compatible service like minio or rook, etc. Now, find region
var sess *session.Session
var err error
if nc.Config[s3.ConfigAuthType] == "iam" {
// The aws sdk does not currently support automatically setting the region based on an instances placement.
// This automatically sets region based on ec2 instance metadata when running on EC2.
// ref: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-region.html#setting-region-order-of-precedence
var c aws.Config
if s, e := session.NewSession(); e == nil {
if region, e := ec2metadata.New(s).Region(); e == nil {
c.WithRegion(region)
}
}
sess, err = session.NewSessionWithOptions(session.Options{
Config: c,
// Support MFA when authing using assumed roles.
SharedConfigState: session.SharedConfigEnable,
AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
})
} else {
sess, err = session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Credentials: credentials.NewStaticCredentials(string(keyID), string(key), ""),
Region: aws.String("us-east-1"),
},
// Support MFA when authing using assumed roles.
SharedConfigState: session.SharedConfigEnable,
AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
})
}
sess, err = session.NewSessionWithOptions(session.Options{
Config: c,
// Support MFA when authing using assumed roles.
SharedConfigState: session.SharedConfigEnable,
AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
if err != nil {
return nil, err
}
svc := _s3.New(sess)
out, err := svc.GetBucketLocation(&_s3.GetBucketLocationInput{
Bucket: types.StringP(spec.S3.Bucket),
})
if err != nil {
return nil, err
}
nc.Config[s3.ConfigRegion] = stringz.Val(types.String(out.LocationConstraint), "us-east-1")
} else {
sess, err = session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Credentials: credentials.NewStaticCredentials(string(keyID), string(key), ""),
Region: aws.String("us-east-1"),
},
// Support MFA when authing using assumed roles.
SharedConfigState: session.SharedConfigEnable,
AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
})
}
if err != nil {
return nil, err
}
svc := _s3.New(sess)
out, err := svc.GetBucketLocation(&_s3.GetBucketLocationInput{
Bucket: types.StringP(spec.S3.Bucket),
})
if err != nil {
return nil, err
}
nc.Config[s3.ConfigRegion] = stringz.Val(types.String(out.LocationConstraint), "us-east-1")
} else {
nc.Config[s3.ConfigEndpoint] = spec.S3.Endpoint
u, err := url.Parse(spec.S3.Endpoint)
if err != nil {
return nil, err
}
nc.Config[s3.ConfigDisableSSL] = strconv.FormatBool(u.Scheme == "http")
nc.Config[s3.ConfigEndpoint] = spec.S3.Endpoint
u, err := url.Parse(spec.S3.Endpoint)
if err != nil {
return nil, err
}
nc.Config[s3.ConfigDisableSSL] = strconv.FormatBool(u.Scheme == "http")

cacertData, ok := config[awsconst.CA_CERT_DATA]
if ok && u.Scheme == "https" {
nc.Config[s3.ConfigCACertData] = string(cacertData)
cacertData, ok := config[awsconst.CA_CERT_DATA]
if ok && u.Scheme == "https" {
nc.Config[s3.ConfigCACertData] = string(cacertData)
}
}
}
return nc, nil
Expand Down
Loading