mirror of
https://github.com/mariadb-operator/mariadb-operator.git
synced 2025-08-19 16:24:15 +00:00
Validate replica range in forceClusterBootstrapInPod
This commit is contained in:
@ -231,7 +231,7 @@ func (g *GaleraRecovery) Validate(mdb *MariaDB) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if g.ForceClusterBootstrapInPod != nil {
|
if g.ForceClusterBootstrapInPod != nil {
|
||||||
if err := statefulset.ValidPodName(mdb.ObjectMeta, *g.ForceClusterBootstrapInPod); err != nil {
|
if err := statefulset.ValidPodName(mdb.ObjectMeta, int(mdb.Spec.Replicas), *g.ForceClusterBootstrapInPod); err != nil {
|
||||||
return fmt.Errorf("'spec.galera.recovery.forceClusterBootstrapInPod' invalid: %v", err)
|
return fmt.Errorf("'spec.galera.recovery.forceClusterBootstrapInPod' invalid: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package statefulset
|
package statefulset
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -49,10 +50,19 @@ func PodIndex(podName string) (*int, error) {
|
|||||||
return &index, nil
|
return &index, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ValidPodName(meta metav1.ObjectMeta, podName string) error {
|
func ValidPodName(meta metav1.ObjectMeta, replicas int, podName string) error {
|
||||||
if _, err := PodIndex(podName); err != nil {
|
if replicas < 0 {
|
||||||
|
return errors.New("replicas must be positive")
|
||||||
|
}
|
||||||
|
|
||||||
|
index, err := PodIndex(podName)
|
||||||
|
if err != nil {
|
||||||
return fmt.Errorf("invalid Pod index: %v", err)
|
return fmt.Errorf("invalid Pod index: %v", err)
|
||||||
}
|
}
|
||||||
|
if *index < 0 || *index >= replicas {
|
||||||
|
return fmt.Errorf("index '%d' out of replicas range", *index)
|
||||||
|
}
|
||||||
|
|
||||||
if !strings.HasPrefix(podName, meta.Name) {
|
if !strings.HasPrefix(podName, meta.Name) {
|
||||||
return fmt.Errorf("invalid Pod name: must start with '%s'", meta.Name)
|
return fmt.Errorf("invalid Pod name: must start with '%s'", meta.Name)
|
||||||
}
|
}
|
||||||
|
@ -8,56 +8,80 @@ import (
|
|||||||
|
|
||||||
func TestStatefulSetValidPodName(t *testing.T) {
|
func TestStatefulSetValidPodName(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
meta metav1.ObjectMeta
|
meta metav1.ObjectMeta
|
||||||
podName string
|
replicas int
|
||||||
wantErr bool
|
podName string
|
||||||
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "empty",
|
name: "empty",
|
||||||
meta: metav1.ObjectMeta{
|
meta: metav1.ObjectMeta{
|
||||||
Name: "",
|
Name: "",
|
||||||
},
|
},
|
||||||
podName: "",
|
replicas: 0,
|
||||||
wantErr: true,
|
podName: "",
|
||||||
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid",
|
name: "negative replicas",
|
||||||
|
meta: metav1.ObjectMeta{
|
||||||
|
Name: "",
|
||||||
|
},
|
||||||
|
replicas: -1,
|
||||||
|
podName: "",
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no index no prefix",
|
||||||
meta: metav1.ObjectMeta{
|
meta: metav1.ObjectMeta{
|
||||||
Name: "mariadb-galera",
|
Name: "mariadb-galera",
|
||||||
},
|
},
|
||||||
podName: "foo",
|
replicas: 3,
|
||||||
wantErr: true,
|
podName: "foo",
|
||||||
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "no index",
|
name: "no index",
|
||||||
meta: metav1.ObjectMeta{
|
meta: metav1.ObjectMeta{
|
||||||
Name: "mariadb-galera",
|
Name: "mariadb-galera",
|
||||||
},
|
},
|
||||||
podName: "mariadb-galera",
|
replicas: 3,
|
||||||
wantErr: true,
|
podName: "mariadb-galera",
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid index",
|
||||||
|
meta: metav1.ObjectMeta{
|
||||||
|
Name: "mariadb-galera",
|
||||||
|
},
|
||||||
|
replicas: 3,
|
||||||
|
podName: "mariadb-galera-5",
|
||||||
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "no prefix",
|
name: "no prefix",
|
||||||
meta: metav1.ObjectMeta{
|
meta: metav1.ObjectMeta{
|
||||||
Name: "mariadb-galera",
|
Name: "mariadb-galera",
|
||||||
},
|
},
|
||||||
podName: "foo-0",
|
replicas: 3,
|
||||||
wantErr: true,
|
podName: "foo-0",
|
||||||
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "valid",
|
name: "valid",
|
||||||
meta: metav1.ObjectMeta{
|
meta: metav1.ObjectMeta{
|
||||||
Name: "mariadb-galera",
|
Name: "mariadb-galera",
|
||||||
},
|
},
|
||||||
podName: "mariadb-galera-0",
|
replicas: 3,
|
||||||
wantErr: false,
|
podName: "mariadb-galera-0",
|
||||||
|
wantErr: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
err := ValidPodName(tt.meta, tt.podName)
|
err := ValidPodName(tt.meta, tt.replicas, tt.podName)
|
||||||
if !tt.wantErr && err != nil {
|
if !tt.wantErr && err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user