G15b: add Kubernetes static PV manifests

This commit is contained in:
pingqiu committed 2026-05-03 08:50:26 -07:00
1 parent da0098a7d6
commit b3299796ce
6 files changed
+439

No files matched your search

@@ -0,0 +1,217 @@
package main
import (
"bytes"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"gopkg.in/yaml.v3"
)
func TestG15b_Manifest_CSIDriverRequiresAttach(t *testing.T) {
doc := g15bFindKind(t, "csi-driver.yaml", "CSIDriver")
spec := g15bMap(t, doc, "spec")
if got, ok := spec["attachRequired"].(bool); !ok || !got {
t.Fatalf("CSIDriver.spec.attachRequired=%v (%T), want true", spec["attachRequired"], spec["attachRequired"])
}
}
func TestG15b_Manifest_ControllerUsesAttacherNotProvisioner(t *testing.T) {
body := g15bReadManifest(t, "csi-controller.yaml")
if !strings.Contains(body, "csi-attacher") {
t.Fatalf("controller manifest must include csi-attacher")
}
if strings.Contains(body, "csi-provisioner") {
t.Fatalf("G15b static PV controller must not include csi-provisioner")
}
if !strings.Contains(body, "--master=$(BLOCKMASTER_ADDR)") {
t.Fatalf("controller block-csi args must wire read-only blockmaster lookup")
}
}
func TestG15b_Manifest_StaticPVDoesNotEmbedTargetFacts(t *testing.T) {
body := g15bReadManifest(t, "static-pv-pvc-pod.yaml")
for _, forbidden := range []string{"iscsiAddr", "iqn", "nqn", "endpointVersion", "epoch"} {
if strings.Contains(body, forbidden) {
t.Fatalf("static PV close-path manifest must not embed %q", forbidden)
}
}
}
func TestG15b_Manifest_NodePluginPrivilegedShape(t *testing.T) {
doc := g15bFindKind(t, "csi-node.yaml", "DaemonSet")
spec := g15bMap(t, doc, "spec")
tpl := g15bMap(t, spec, "template")
podSpec := g15bMap(t, tpl, "spec")
if got, ok := podSpec["hostNetwork"].(bool); !ok || !got {
t.Fatalf("node DaemonSet hostNetwork=%v, want true", podSpec["hostNetwork"])
}
containers := g15bSlice(t, podSpec, "containers")
blockCSI := g15bContainer(t, containers, "block-csi")
sec := g15bMap(t, blockCSI, "securityContext")
if got, ok := sec["privileged"].(bool); !ok || !got {
t.Fatalf("block-csi privileged=%v, want true", sec["privileged"])
}
mounts := g15bSlice(t, blockCSI, "volumeMounts")
wantMounts := map[string]bool{
"/var/lib/kubelet": false,
"/dev": false,
"/etc/iscsi": false,
}
for _, raw := range mounts {
m := raw.(map[string]any)
path, _ := m["mountPath"].(string)
if _, ok := wantMounts[path]; ok {
wantMounts[path] = true
}
if path == "/var/lib/kubelet" && m["mountPropagation"] != "Bidirectional" {
t.Fatalf("/var/lib/kubelet mountPropagation=%v, want Bidirectional", m["mountPropagation"])
}
}
for path, found := range wantMounts {
if !found {
t.Fatalf("node plugin missing mountPath %s", path)
}
}
}
func TestG15b_Manifest_StaticPVUsesBlockCSIDriver(t *testing.T) {
doc := g15bFindKind(t, "static-pv-pvc-pod.yaml", "PersistentVolume")
spec := g15bMap(t, doc, "spec")
csi := g15bMap(t, spec, "csi")
if got := csi["driver"]; got != "block.csi.seaweedfs.com" {
t.Fatalf("PV csi.driver=%v, want block.csi.seaweedfs.com", got)
}
if got := csi["volumeHandle"]; got != "v1" {
t.Fatalf("PV csi.volumeHandle=%v, want v1", got)
}
}
func TestG15b_Manifest_NoAuthorityShapedFields(t *testing.T) {
for _, file := range []string{"csi-driver.yaml", "csi-controller.yaml", "csi-node.yaml", "static-pv-pvc-pod.yaml"} {
body := strings.ToLower(g15bStripYAMLComments(g15bReadManifest(t, file)))
for _, forbidden := range []string{"endpointversion", "authorityepoch", "assignmentfact", "assignmentask"} {
if strings.Contains(body, forbidden) {
t.Fatalf("%s contains authority-shaped field %q", file, forbidden)
}
}
}
}
func g15bReadManifest(t *testing.T, name string) string {
t.Helper()
path := filepath.Join(g15bRepoRoot(t), "deploy", "k8s", "g15b", name)
b, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read %s: %v", path, err)
}
return string(b)
}
func g15bFindKind(t *testing.T, name, kind string) map[string]any {
t.Helper()
for _, doc := range g15bDecodeDocs(t, g15bReadManifest(t, name)) {
if doc["kind"] == kind {
return doc
}
}
t.Fatalf("%s: kind %s not found", name, kind)
return nil
}
func g15bDecodeDocs(t *testing.T, body string) []map[string]any {
t.Helper()
dec := yaml.NewDecoder(strings.NewReader(body))
var out []map[string]any
for {
var doc map[string]any
err := dec.Decode(&doc)
if err != nil {
if err.Error() == "EOF" {
break
}
t.Fatalf("decode yaml: %v", err)
}
if len(doc) > 0 {
out = append(out, doc)
}
}
return out
}
func g15bRepoRoot(t *testing.T) string {
t.Helper()
_, file, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("runtime.Caller failed")
}
dir := filepath.Dir(file)
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}
parent := filepath.Dir(dir)
if parent == dir {
t.Fatal("repo root not found")
}
dir = parent
}
}
func g15bMap(t *testing.T, m map[string]any, key string) map[string]any {
t.Helper()
v, ok := m[key]
if !ok {
t.Fatalf("missing key %q in %v", key, m)
}
out, ok := v.(map[string]any)
if !ok {
t.Fatalf("key %q has type %T, want map", key, v)
}
return out
}
func g15bSlice(t *testing.T, m map[string]any, key string) []any {
t.Helper()
v, ok := m[key]
if !ok {
t.Fatalf("missing key %q in %v", key, m)
}
out, ok := v.([]any)
if !ok {
t.Fatalf("key %q has type %T, want slice", key, v)
}
return out
}
func g15bContainer(t *testing.T, containers []any, name string) map[string]any {
t.Helper()
for _, raw := range containers {
c, ok := raw.(map[string]any)
if !ok {
t.Fatalf("container has type %T, want map", raw)
}
if c["name"] == name {
return c
}
}
t.Fatalf("container %q not found", name)
return nil
}
func g15bStripYAMLComments(body string) string {
var out bytes.Buffer
for _, line := range strings.Split(body, "\n") {
if idx := strings.Index(line, "#"); idx >= 0 {
line = line[:idx]
}
out.WriteString(line)
out.WriteByte('\n')
}
return out.String()
}
@@ -0,0 +1,46 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: sw-block-csi-controller
namespace: kube-system
spec:
replicas: 1
selector:
matchLabels:
app: sw-block-csi-controller
template:
metadata:
labels:
app: sw-block-csi-controller
spec:
serviceAccountName: sw-block-csi
containers:
- name: block-csi
image: sw-block-csi:local
imagePullPolicy: Never
args:
- "--endpoint=unix:///csi/csi.sock"
- "--master=$(BLOCKMASTER_ADDR)"
- "--node-id=$(NODE_NAME)"
env:
- name: BLOCKMASTER_ADDR
value: "blockmaster.kube-system.svc.cluster.local:9333"
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
volumeMounts:
- name: socket-dir
mountPath: /csi
- name: csi-attacher
image: registry.k8s.io/sig-storage/csi-attacher:v4.8.1
args:
- "--csi-address=/csi/csi.sock"
- "--timeout=30s"
- "--leader-election=true"
volumeMounts:
- name: socket-dir
mountPath: /csi
volumes:
- name: socket-dir
emptyDir: {}
@@ -0,0 +1,9 @@
apiVersion: storage.k8s.io/v1
kind: CSIDriver
metadata:
name: block.csi.seaweedfs.com
spec:
attachRequired: true
podInfoOnMount: false
volumeLifecycleModes:
- Persistent
@@ -0,0 +1,72 @@
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: sw-block-csi-node
namespace: kube-system
spec:
selector:
matchLabels:
app: sw-block-csi-node
template:
metadata:
labels:
app: sw-block-csi-node
spec:
serviceAccountName: sw-block-csi
hostNetwork: true
hostPID: true
containers:
- name: block-csi
image: sw-block-csi:local
imagePullPolicy: Never
args:
- "--endpoint=unix:///csi/csi.sock"
- "--node-id=$(NODE_NAME)"
env:
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
securityContext:
privileged: true
volumeMounts:
- name: socket-dir
mountPath: /csi
- name: kubelet-dir
mountPath: /var/lib/kubelet
mountPropagation: Bidirectional
- name: dev
mountPath: /dev
- name: iscsi-dir
mountPath: /etc/iscsi
- name: csi-node-driver-registrar
image: registry.k8s.io/sig-storage/csi-node-driver-registrar:v2.13.0
args:
- "--csi-address=/csi/csi.sock"
- "--kubelet-registration-path=/var/lib/kubelet/plugins/block.csi.seaweedfs.com/csi.sock"
volumeMounts:
- name: socket-dir
mountPath: /csi
- name: registration-dir
mountPath: /registration
volumes:
- name: socket-dir
hostPath:
path: /var/lib/kubelet/plugins/block.csi.seaweedfs.com
type: DirectoryOrCreate
- name: kubelet-dir
hostPath:
path: /var/lib/kubelet
type: Directory
- name: dev
hostPath:
path: /dev
type: Directory
- name: iscsi-dir
hostPath:
path: /etc/iscsi
type: DirectoryOrCreate
- name: registration-dir
hostPath:
path: /var/lib/kubelet/plugins_registry
type: Directory
+39
View File
@@ -0,0 +1,39 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: sw-block-csi
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: sw-block-csi-attacher
rules:
- apiGroups: [""]
resources: ["persistentvolumes", "nodes"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["events"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
- apiGroups: ["storage.k8s.io"]
resources: ["volumeattachments"]
verbs: ["get", "list", "watch", "update", "patch"]
- apiGroups: ["storage.k8s.io"]
resources: ["volumeattachments/status"]
verbs: ["patch"]
- apiGroups: ["coordination.k8s.io"]
resources: ["leases"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: sw-block-csi-attacher
subjects:
- kind: ServiceAccount
name: sw-block-csi
namespace: kube-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: sw-block-csi-attacher
@@ -0,0 +1,56 @@
apiVersion: v1
kind: PersistentVolume
metadata:
name: sw-block-static-v1
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: sw-block-static
csi:
driver: block.csi.seaweedfs.com
volumeHandle: v1
fsType: ext4
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: sw-block-static-v1
namespace: default
spec:
accessModes:
- ReadWriteOnce
storageClassName: sw-block-static
volumeName: sw-block-static-v1
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Pod
metadata:
name: sw-block-static-smoke
namespace: default
spec:
restartPolicy: Never
containers:
- name: writer
image: busybox:1.36
command:
- /bin/sh
- -c
- |
set -eu
dd if=/dev/urandom of=/data/payload.bin bs=4096 count=1
sha256sum /data/payload.bin > /data/payload.sha256
sync
sha256sum -c /data/payload.sha256
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: sw-block-static-v1