mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
Brings seaweed-block main (v1.1.1 plus the docs and a test fix after it) into seaweed-block/ as one commit. Only files under seaweed-block/ change, and the subtree equals seaweed-block main exactly. The module's full history stays in the seaweed-block repository. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
325 lines
12 KiB
Go
325 lines
12 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"text/template"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// renderChartTemplate renders one chart template for a new release (lookup
|
|
// answers nothing) and fails the test if the template refuses to render.
|
|
func renderChartTemplate(t *testing.T, name string, edit func(values map[string]any)) []map[string]any {
|
|
t.Helper()
|
|
docs, refused := renderChart(t, name, edit, nil)
|
|
if refused != "" {
|
|
t.Fatalf("render %s refused: %s", name, refused)
|
|
}
|
|
return docs
|
|
}
|
|
|
|
// renderChart renders one chart template (see renderChartText) and returns
|
|
// its documents decoded as YAML, or the template's fail message when it
|
|
// refuses to render.
|
|
func renderChart(t *testing.T, name string, edit func(values map[string]any), running map[string]any) ([]map[string]any, string) {
|
|
t.Helper()
|
|
text, refused := renderChartText(t, name, edit, running)
|
|
if refused != "" {
|
|
return nil, refused
|
|
}
|
|
var docs []map[string]any
|
|
for _, part := range strings.Split(text, "\n---") {
|
|
doc := map[string]any{}
|
|
if err := yaml.Unmarshal([]byte(part), &doc); err != nil {
|
|
t.Fatalf("rendered %s is not YAML: %v\n%s", name, err, part)
|
|
}
|
|
if len(doc) > 0 {
|
|
docs = append(docs, doc)
|
|
}
|
|
}
|
|
return docs, ""
|
|
}
|
|
|
|
// renderChartText renders one chart template, with the chart's helpers, from
|
|
// its values.yaml changed by edit, using the Helm functions the chart uses.
|
|
// running is the release's existing blockmaster Deployment, which lookup
|
|
// answers: nil renders an install (.Release.IsInstall), anything else an
|
|
// upgrade. It returns the rendered text, or the template's fail message.
|
|
func renderChartText(t *testing.T, name string, edit func(values map[string]any), running map[string]any) (string, string) {
|
|
t.Helper()
|
|
chart := filepath.Join("..", "..", "charts", "seaweed-block")
|
|
read := func(p ...string) []byte {
|
|
b, err := os.ReadFile(filepath.Join(append([]string{chart}, p...)...))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return b
|
|
}
|
|
values, meta := map[string]any{}, map[string]any{}
|
|
if err := yaml.Unmarshal(read("values.yaml"), &values); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := yaml.Unmarshal(read("Chart.yaml"), &meta); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
edit(values)
|
|
empty := func(v any) bool {
|
|
if v == nil {
|
|
return true
|
|
}
|
|
r := reflect.ValueOf(v)
|
|
switch r.Kind() {
|
|
case reflect.Map, reflect.Slice, reflect.String:
|
|
return r.Len() == 0
|
|
case reflect.Bool:
|
|
return !r.Bool()
|
|
}
|
|
return r.IsZero()
|
|
}
|
|
var tp *template.Template
|
|
funcs := template.FuncMap{
|
|
"fail": func(msg string) (string, error) { return "", &templateFail{msg} },
|
|
"include": func(name string, data any) (string, error) {
|
|
var b bytes.Buffer
|
|
err := tp.ExecuteTemplate(&b, name, data)
|
|
return b.String(), err
|
|
},
|
|
"toYaml": func(v any) (string, error) {
|
|
b, err := yaml.Marshal(v)
|
|
return strings.TrimSuffix(string(b), "\n"), err
|
|
},
|
|
"nindent": func(n int, s string) string {
|
|
pad := strings.Repeat(" ", n)
|
|
return "\n" + pad + strings.ReplaceAll(s, "\n", "\n"+pad)
|
|
},
|
|
"quote": func(v any) string {
|
|
if v == nil {
|
|
return `""`
|
|
}
|
|
return strconv.Quote(fmt.Sprint(v))
|
|
},
|
|
"default": func(d any, given ...any) any {
|
|
if len(given) == 0 || empty(given[0]) {
|
|
return d
|
|
}
|
|
return given[0]
|
|
},
|
|
"dict": func() map[string]any { return map[string]any{} },
|
|
"empty": empty,
|
|
"int": func(v any) int {
|
|
switch x := v.(type) {
|
|
case int:
|
|
return x
|
|
case float64:
|
|
return int(x)
|
|
case string:
|
|
n, _ := strconv.Atoi(x)
|
|
return n
|
|
}
|
|
return 0
|
|
},
|
|
"lookup": func(apiVersion, kind, namespace, name string) map[string]any {
|
|
if apiVersion != "apps/v1" || kind != "Deployment" || namespace != "sw" || name != "sw-blockmaster" || running == nil {
|
|
return map[string]any{} // Helm answers an absent object with an empty map
|
|
}
|
|
return running
|
|
},
|
|
"clean": path.Clean,
|
|
"base": path.Base,
|
|
"dir": path.Dir,
|
|
"replace": func(old, new, s string) string { return strings.ReplaceAll(s, old, new) },
|
|
"hasPrefix": func(prefix, s string) bool { return strings.HasPrefix(s, prefix) },
|
|
"trimSuffix": func(suffix, s string) string { return strings.TrimSuffix(s, suffix) },
|
|
"trunc": func(n int, s string) string {
|
|
if len(s) > n {
|
|
return s[:n]
|
|
}
|
|
return s
|
|
},
|
|
}
|
|
tp = template.New(name).Funcs(funcs)
|
|
if _, err := tp.New("_helpers.tpl").Parse(string(read("templates", "_helpers.tpl"))); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := tp.New(name).Parse(string(read("templates", name))); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out bytes.Buffer
|
|
data := map[string]any{
|
|
"Values": values,
|
|
"Release": map[string]any{"Name": "sw-block", "Namespace": "sw", "Service": "Helm", "IsInstall": running == nil, "IsUpgrade": running != nil},
|
|
"Chart": map[string]any{"Name": meta["name"], "Version": meta["version"]},
|
|
}
|
|
if err := tp.ExecuteTemplate(&out, name, data); err != nil {
|
|
var f *templateFail
|
|
if errors.As(err, &f) {
|
|
return "", f.msg
|
|
}
|
|
t.Fatalf("render %s: %v", name, err)
|
|
}
|
|
return out.String(), ""
|
|
}
|
|
|
|
// blockmaster uses the host network and, with a state path, is pinned to one
|
|
// node by hostname: a rolling update's new pod can never be scheduled beside
|
|
// the old one (same host ports, only that node), so every upgrade that
|
|
// changes its pod would wait forever. Its Deployment is replaced, not rolled:
|
|
// rendered with the default state path and with ephemeral state alike.
|
|
func TestBlockmasterDeploymentIsRecreatedNotRolled(t *testing.T) {
|
|
section := func(v map[string]any, k string) map[string]any { return v[k].(map[string]any) }
|
|
pinned := func(v map[string]any) {
|
|
section(v, "blockmaster")["nodeSelector"] = map[string]any{"kubernetes.io/hostname": "n1"}
|
|
}
|
|
for _, c := range []struct {
|
|
name string
|
|
edit func(map[string]any)
|
|
}{
|
|
{"state path, pinned by hostname", pinned},
|
|
{"ephemeral state", func(v map[string]any) { section(v, "blockmaster")["stateHostPath"] = "" }},
|
|
} {
|
|
var deployment map[string]any
|
|
for _, doc := range renderChartTemplate(t, "blockmaster.yaml", c.edit) {
|
|
if md, _ := doc["metadata"].(map[string]any); doc["kind"] == "Deployment" && md["name"] == "sw-blockmaster" {
|
|
deployment = doc
|
|
}
|
|
}
|
|
if deployment == nil {
|
|
t.Fatalf("%s: no sw-blockmaster Deployment rendered", c.name)
|
|
}
|
|
spec, _ := deployment["spec"].(map[string]any)
|
|
strategy, _ := spec["strategy"].(map[string]any)
|
|
if strategy["type"] != "Recreate" {
|
|
t.Errorf("%s: blockmaster strategy %v, want Recreate (a rolling update never schedules its new pod)", c.name, spec["strategy"])
|
|
}
|
|
}
|
|
}
|
|
|
|
// blockmaster's stores and the generated blockvolume pods' state are on
|
|
// separate node paths: a holder pod mounts only its own state path, never
|
|
// blockmaster's authority and lifecycle stores. A release that sets one path
|
|
// for both (a v1.1.0 install) keeps that shared layout instead of moving
|
|
// its volumes; ephemeral state passes no path.
|
|
func TestHolderStateIsApartFromBlockmasterStores(t *testing.T) {
|
|
section := func(v map[string]any, k string) map[string]any { return v[k].(map[string]any) }
|
|
pinned := func(v map[string]any) {
|
|
section(v, "blockmaster")["nodeSelector"] = map[string]any{"kubernetes.io/hostname": "n1"}
|
|
}
|
|
// upgrade renders over a running blockmaster on a state path (an
|
|
// existing release); otherwise the render is a new install.
|
|
upgradeOf := blockmasterDeploymentWith(map[string]any{"name": "state", "hostPath": map[string]any{"path": "/var/lib/sw-block"}})
|
|
render := func(edit func(map[string]any), upgrade bool) ([]map[string]any, string) {
|
|
t.Helper()
|
|
var running map[string]any
|
|
if upgrade {
|
|
running = upgradeOf
|
|
}
|
|
return renderChart(t, "blockmaster.yaml", edit, running)
|
|
}
|
|
paths := func(edit func(map[string]any), upgrade bool) (master, holders string) {
|
|
t.Helper()
|
|
docs, refused := render(edit, upgrade)
|
|
if refused != "" {
|
|
t.Fatalf("render refused: %s", refused)
|
|
}
|
|
for _, doc := range docs {
|
|
if md, _ := doc["metadata"].(map[string]any); doc["kind"] != "Deployment" || md["name"] != "sw-blockmaster" {
|
|
continue
|
|
}
|
|
pod := doc["spec"].(map[string]any)["template"].(map[string]any)["spec"].(map[string]any)
|
|
for _, v := range pod["volumes"].([]any) {
|
|
if vol := v.(map[string]any); vol["name"] == "state" {
|
|
if hp, ok := vol["hostPath"].(map[string]any); ok {
|
|
master, _ = hp["path"].(string)
|
|
}
|
|
}
|
|
}
|
|
for _, c := range pod["containers"].([]any) {
|
|
for _, a := range c.(map[string]any)["args"].([]any) {
|
|
if s, _ := a.(string); strings.HasPrefix(s, "--launcher-state-hostpath=") {
|
|
holders = strings.TrimPrefix(s, "--launcher-state-hostpath=")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return master, holders
|
|
}
|
|
if m, h := paths(pinned, false); m != "/var/lib/sw-block/k8s/master" || h != "/var/lib/sw-block/k8s/volumes" {
|
|
t.Errorf("default paths: blockmaster %q, holders %q; want /var/lib/sw-block/k8s/master and /var/lib/sw-block/k8s/volumes", m, h)
|
|
}
|
|
v110 := map[string]func(map[string]any){
|
|
"a v1.1.0 values file that sets only blockmaster.stateHostPath": func(v map[string]any) {
|
|
pinned(v)
|
|
section(v, "blockmaster")["stateHostPath"] = "/var/lib/sw-block"
|
|
},
|
|
"a v1.1.0 generated values file (both paths the same)": func(v map[string]any) {
|
|
pinned(v)
|
|
section(v, "blockmaster")["stateHostPath"] = "/var/lib/sw-block"
|
|
section(v, "restartPersistence")["stateHostPath"] = "/var/lib/sw-block"
|
|
},
|
|
}
|
|
for name, edit := range v110 {
|
|
if m, h := paths(edit, true); m != "/var/lib/sw-block" || h != "/var/lib/sw-block" {
|
|
t.Errorf("%s: blockmaster %q, holders %q; want both /var/lib/sw-block (its volumes stay where they are)", name, m, h)
|
|
}
|
|
}
|
|
if m, h := paths(func(v map[string]any) {
|
|
pinned(v)
|
|
section(v, "blockmaster")["stateHostPath"] = "/srv/sw/master/"
|
|
}, false); m != "/srv/sw/master/" || h != "/srv/sw/volumes" {
|
|
t.Errorf("a master path ending in /master: blockmaster %q, holders %q; want the volumes directory beside it", m, h)
|
|
}
|
|
if m, h := paths(func(v map[string]any) { section(v, "blockmaster")["stateHostPath"] = "" }, false); m != "" || h != "" {
|
|
t.Errorf("ephemeral: blockmaster %q, holders %q; want no host paths", m, h)
|
|
}
|
|
|
|
// A new install never gets the shared layout silently: a custom
|
|
// blockmaster path outside the default layout needs the volumes path
|
|
// set; with it, the install renders apart.
|
|
custom := func(volumes string) func(map[string]any) {
|
|
return func(v map[string]any) {
|
|
pinned(v)
|
|
section(v, "blockmaster")["stateHostPath"] = "/data/swb"
|
|
section(v, "restartPersistence")["stateHostPath"] = volumes
|
|
}
|
|
}
|
|
if _, refused := render(custom(""), false); !strings.Contains(refused, "set restartPersistence.stateHostPath beside blockmaster.stateHostPath") {
|
|
t.Errorf("a new install with a custom blockmaster path and no volumes path: refused %q, want the beside-it refusal", refused)
|
|
}
|
|
if m, h := paths(custom("/data/swb-volumes"), false); m != "/data/swb" || h != "/data/swb-volumes" {
|
|
t.Errorf("a new install with both paths set: blockmaster %q, holders %q", m, h)
|
|
}
|
|
|
|
// Kept shared layout on an upgrade: the install notes warn and name the
|
|
// exposure; the default layout prints no warning.
|
|
notes := func(edit func(map[string]any), upgrade bool) string {
|
|
t.Helper()
|
|
var running map[string]any
|
|
if upgrade {
|
|
running = upgradeOf
|
|
}
|
|
text, refused := renderChartText(t, "NOTES.txt", edit, running)
|
|
if refused != "" {
|
|
t.Fatalf("NOTES refused: %s", refused)
|
|
}
|
|
return text
|
|
}
|
|
const shared = "WARNING: the generated blockvolume pods share blockmaster's state path"
|
|
for name, edit := range v110 {
|
|
if text := notes(edit, true); !strings.Contains(text, shared) || !strings.Contains(text, "authority and lifecycle stores") {
|
|
t.Errorf("%s: the install notes do not warn about the shared layout:\n%s", name, text)
|
|
}
|
|
}
|
|
if text := notes(pinned, false); strings.Contains(text, shared) {
|
|
t.Errorf("the default layout warns about a shared layout:\n%s", text)
|
|
}
|
|
}
|