mirror of
https://github.com/mariadb-operator/mariadb-operator.git
synced 2025-08-18 08:17:37 +00:00
Avoid sending nil body in bootstrap request
This commit is contained in:
@ -89,7 +89,7 @@ func (r *GaleraReconciler) recoverCluster(ctx context.Context, mariadb *mariadbv
|
||||
if err := r.bootstrap(ctx, src, rs, mariadb, clientSet, logger); err != nil {
|
||||
return fmt.Errorf("error forcefully bootstrapping: %v", err)
|
||||
}
|
||||
return nil
|
||||
return r.patchRecoveryStatus(ctx, mariadb, rs)
|
||||
}
|
||||
|
||||
logger.V(1).Info("Get Galera state")
|
||||
|
@ -85,15 +85,15 @@ func (b *Bootstrap) Disable(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (b *Bootstrap) decodeAndValidateBootstrap(r *http.Request) (*recovery.Bootstrap, error) {
|
||||
if r.Body == nil || r.ContentLength == 0 {
|
||||
if r.Body == nil || r.ContentLength <= 0 {
|
||||
return nil, nil
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
var bootstrap recovery.Bootstrap
|
||||
if err := json.NewDecoder(r.Body).Decode(&bootstrap); err != nil {
|
||||
return nil, galeraErrors.NewAPIErrorf("error decoding bootstrap: %v", err)
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
if err := bootstrap.Validate(); err != nil {
|
||||
return nil, galeraErrors.NewAPIErrorf("invalid bootstrap: %v", err)
|
||||
|
@ -9,6 +9,8 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
mdbreflect "github.com/mariadb-operator/mariadb-operator/pkg/reflect"
|
||||
)
|
||||
|
||||
func (c *Client) NewRequestWithContext(ctx context.Context, method string, path string, body interface{},
|
||||
@ -28,7 +30,7 @@ func (c *Client) NewRequestWithContext(ctx context.Context, method string, path
|
||||
}
|
||||
|
||||
var bodyReader io.Reader
|
||||
if body != nil {
|
||||
if !mdbreflect.IsNil(body) {
|
||||
bodyBytes, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error encoding body: %v", err)
|
||||
|
17
pkg/reflect/reflect.go
Normal file
17
pkg/reflect/reflect.go
Normal file
@ -0,0 +1,17 @@
|
||||
package reflect
|
||||
|
||||
import "reflect"
|
||||
|
||||
func IsNil(i interface{}) bool {
|
||||
if i == nil {
|
||||
return true
|
||||
}
|
||||
v := reflect.ValueOf(i)
|
||||
|
||||
switch v.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.UnsafePointer, reflect.Slice, reflect.Interface:
|
||||
return v.IsNil()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
40
pkg/reflect/reflect_test.go
Normal file
40
pkg/reflect/reflect_test.go
Normal file
@ -0,0 +1,40 @@
|
||||
package reflect
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func TestIsNil(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input interface{}
|
||||
want bool
|
||||
}{
|
||||
{"nil interface", nil, true},
|
||||
{"nil pointer", (*int)(nil), true},
|
||||
{"nil slice", ([]int)(nil), true},
|
||||
{"nil map", (map[string]int)(nil), true},
|
||||
{"nil channel", (chan int)(nil), true},
|
||||
{"nil func", (func())(nil), true},
|
||||
{"nil unsafe pointer", (unsafe.Pointer)(nil), true},
|
||||
{"non-nil pointer", new(int), false},
|
||||
{"non-nil slice", []int{}, false},
|
||||
{"non-nil map", map[string]int{}, false},
|
||||
{"non-nil channel", make(chan int), false},
|
||||
{"non-nil func", func() {}, false},
|
||||
{"non-nil unsafe pointer", unsafe.Pointer(new(int)), false},
|
||||
{"non-nil interface", interface{}(0), false},
|
||||
{"non-nil int", 123, false},
|
||||
{"non-nil string", "hello", false},
|
||||
{"nil interface containing nil pointer", interface{}((*int)(nil)), true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsNil(tt.input); got != tt.want {
|
||||
t.Errorf("IsNil(%v) = %v, want %v", tt.input, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user