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

Package redis refactor #267

Merged
merged 17 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: "1.19"
go-version: "1.20"

- uses: actions/cache@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: "1.19"
go-version: "1.20"

- uses: actions/cache@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: "1.19"
go-version: "1.20"

- uses: actions/cache@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM golang:1.19 as builder
FROM golang:1.20 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ TEST_PKG = ./api/... ./controllers/... ./pkg/...
KUBEBUILDER_ASSETS = "$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)"

test: manifests generate fmt vet envtest assets ginkgo ## Run tests.
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) $(GINKGO) -p -r $(TEST_PKG) -coverprofile cover.out
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) $(GINKGO) -p -v -r $(TEST_PKG) -coverprofile cover.out

test-sequential: manifests generate fmt vet envtest assets ginkgo ## Run tests.
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) $(GINKGO) -r $(TEST_PKG) -coverprofile cover.out
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) $(GINKGO) -v -r $(TEST_PKG) -coverprofile cover.out

test-e2e: export KUBECONFIG = $(PWD)/kubeconfig
test-e2e: manifests ginkgo kind-create kind-deploy ## Runs e2e tests
Expand Down
51 changes: 49 additions & 2 deletions api/v1alpha1/redisshard_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ limitations under the License.
package v1alpha1

import (
"strconv"
"strings"

"github.com/3scale/saas-operator/pkg/util"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
Expand Down Expand Up @@ -61,11 +65,54 @@ type RedisShardNodes struct {
// Master is the node that acts as master role in the redis shard
// +operator-sdk:csv:customresourcedefinitions:type=status
// +optional
Master *string `json:"master,omitempty"`
Master map[string]string `json:"master,omitempty"`
// Slaves are the nodes that act as master role in the redis shard
// +operator-sdk:csv:customresourcedefinitions:type=status
// +optional
Slaves []string `json:"slaves,omitempty"`
Slaves map[string]string `json:"slaves,omitempty"`
}

func (rsn *RedisShardNodes) MasterHostPort() string {
for _, hostport := range rsn.Master {
return hostport
}
return ""
}

func (rsn *RedisShardNodes) GetNodeByPodIndex(podIndex int) (string, string) {
nodes := util.MergeMaps(map[string]string{}, rsn.Master, rsn.Slaves)

for alias, hostport := range nodes {
i := alias[strings.LastIndex(alias, "-")+1:]
index, _ := strconv.Atoi(i)
if index == podIndex {
return alias, hostport
}
}

return "", ""
}

func (rsn *RedisShardNodes) GetHostPortByPodIndex(podIndex int) string {
_, hostport := rsn.GetNodeByPodIndex(podIndex)
return hostport
}

func (rsn *RedisShardNodes) GetAliasByPodIndex(podIndex int) string {
alias, _ := rsn.GetNodeByPodIndex(podIndex)
return alias
}

func (rsn *RedisShardNodes) GetIndexByHostPort(hostport string) int {
nodes := util.MergeMaps(map[string]string{}, rsn.Master, rsn.Slaves)
for alias, hp := range nodes {
if hostport == hp {
i := alias[strings.LastIndex(alias, "-")+1:]
index, _ := strconv.Atoi(i)
return index
}
}
return -1
}

// RedisShardStatus defines the observed state of RedisShard
Expand Down
232 changes: 232 additions & 0 deletions api/v1alpha1/redisshard_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
/*
Copyright 2021.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"testing"
)

func TestRedisShardNodes_GetNodeByPodIndex(t *testing.T) {
type fields struct {
Master map[string]string
Slaves map[string]string
}
type args struct {
podIndex int
}
tests := []struct {
name string
fields fields
args args
want string
want1 string
}{
{
name: "Returns the node that has the given pod index",
fields: fields{
Master: map[string]string{
"redis-shard-rs0-0": "127.0.0.1:1000",
},
Slaves: map[string]string{
"redis-shard-rs0-1": "127.0.0.1:2000",
"redis-shard-rs0-2": "127.0.0.1:3000",
},
},
args: args{
podIndex: 2,
},
want: "redis-shard-rs0-2",
want1: "127.0.0.1:3000",
},
{
name: "Not found",
fields: fields{
Master: map[string]string{
"redis-shard-rs0-0": "127.0.0.1:1000",
},
Slaves: map[string]string{
"redis-shard-rs0-1": "127.0.0.1:2000",
"redis-shard-rs0-2": "127.0.0.1:3000",
},
},
args: args{
podIndex: 3,
},
want: "",
want1: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rsn := &RedisShardNodes{
Master: tt.fields.Master,
Slaves: tt.fields.Slaves,
}
got, got1 := rsn.GetNodeByPodIndex(tt.args.podIndex)
if got != tt.want {
t.Errorf("RedisShardNodes.GetNodeByPodIndex() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("RedisShardNodes.GetNodeByPodIndex() got1 = %v, want %v", got1, tt.want1)
}
})
}
}

func TestRedisShardNodes_MasterHostPort(t *testing.T) {
type fields struct {
Master map[string]string
Slaves map[string]string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Returns master hostport",
fields: fields{
Master: map[string]string{"rs0-0": "127.0.0.1:1000"},
},
want: "127.0.0.1:1000",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rsn := &RedisShardNodes{
Master: tt.fields.Master,
Slaves: tt.fields.Slaves,
}
if got := rsn.MasterHostPort(); got != tt.want {
t.Errorf("RedisShardNodes.MasterHostPort() = %v, want %v", got, tt.want)
}
})
}
}

func TestRedisShardNodes_GetHostPortByPodIndex(t *testing.T) {
type fields struct {
Master map[string]string
Slaves map[string]string
}
type args struct {
podIndex int
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "Returns the hosport of the node with the given replicaset index",
fields: fields{
Master: map[string]string{"rs0-0": "127.0.0.1:1000"},
Slaves: map[string]string{"rs0-1": "127.0.0.1:2000", "rs0-2": "127.0.0.1:3000"},
},
args: args{podIndex: 2},
want: "127.0.0.1:3000",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rsn := &RedisShardNodes{
Master: tt.fields.Master,
Slaves: tt.fields.Slaves,
}
if got := rsn.GetHostPortByPodIndex(tt.args.podIndex); got != tt.want {
t.Errorf("RedisShardNodes.GetHostPortByPodIndex() = %v, want %v", got, tt.want)
}
})
}
}

func TestRedisShardNodes_GetAliasByPodIndex(t *testing.T) {
type fields struct {
Master map[string]string
Slaves map[string]string
}
type args struct {
podIndex int
}
tests := []struct {
name string
fields fields
args args
want string
}{
{
name: "Returns the alias of the node with the given replicaset index",
fields: fields{
Master: map[string]string{"rs0-0": "127.0.0.1:1000"},
Slaves: map[string]string{"rs0-1": "127.0.0.1:2000", "rs0-2": "127.0.0.1:3000"},
},
args: args{podIndex: 1},
want: "rs0-1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rsn := &RedisShardNodes{
Master: tt.fields.Master,
Slaves: tt.fields.Slaves,
}
if got := rsn.GetAliasByPodIndex(tt.args.podIndex); got != tt.want {
t.Errorf("RedisShardNodes.GetAliasByPodIndex() = %v, want %v", got, tt.want)
}
})
}
}

func TestRedisShardNodes_GetIndexByHostPort(t *testing.T) {
type fields struct {
Master map[string]string
Slaves map[string]string
}
type args struct {
hostport string
}
tests := []struct {
name string
fields fields
args args
want int
}{
{
name: "Returns the index of the provided hostport",
fields: fields{
Master: map[string]string{"rs0-0": "127.0.0.1:1000"},
Slaves: map[string]string{"rs0-1": "127.0.0.1:2000", "rs0-2": "127.0.0.1:3000"},
},
args: args{
hostport: "127.0.0.1:3000",
},
want: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rsn := &RedisShardNodes{
Master: tt.fields.Master,
Slaves: tt.fields.Slaves,
}
if got := rsn.GetIndexByHostPort(tt.args.hostport); got != tt.want {
t.Errorf("RedisShardNodes.GetIndexByHostPort() = %v, want %v", got, tt.want)
}
})
}
}
Loading
Loading