mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
phase37: enrich operator node evidence from kubernetes
This commit is contained in:
1 parent
4252c3b4e0
commit
f4f5df3208
8 files changed
+725
-17
No files matched your search
@@ -14,6 +14,12 @@ metadata:
|
||||
labels:
|
||||
{{- include "seaweed-block.labels" . | nindent 4 }}
|
||||
rules:
|
||||
- apiGroups: [""]
|
||||
resources: ["nodes", "pods"]
|
||||
verbs: ["get", "list", "watch"]
|
||||
- apiGroups: ["storage.k8s.io"]
|
||||
resources: ["csidrivers", "csinodes"]
|
||||
verbs: ["get", "list", "watch"]
|
||||
- apiGroups: ["block.seaweedfs.com"]
|
||||
resources: ["swblockclusters", "swblockvolumes"]
|
||||
verbs: ["get", "list", "watch"]
|
||||
|
||||
@@ -28,10 +28,13 @@ func main() {
|
||||
}
|
||||
|
||||
var (
|
||||
opsStatusRunCommand = ops.DefaultRunCommand
|
||||
opsInventoryRunCommand = ops.DefaultRunCommand
|
||||
opsGenerateHelmValuesRunCommand = ops.DefaultRunCommand
|
||||
opsOperatorStatusWriterFactory = func() (ops.OperatorStatusWriter, error) {
|
||||
opsStatusRunCommand = ops.DefaultRunCommand
|
||||
opsInventoryRunCommand = ops.DefaultRunCommand
|
||||
opsGenerateHelmValuesRunCommand = ops.DefaultRunCommand
|
||||
opsOperatorStatusNodeEvidenceFactory = func() (ops.OperatorNodeEvidenceEnricher, error) {
|
||||
return ops.NewInClusterKubernetesStatusClient()
|
||||
}
|
||||
opsOperatorStatusWriterFactory = func() (ops.OperatorStatusWriter, error) {
|
||||
return ops.NewInClusterKubernetesStatusClient()
|
||||
}
|
||||
)
|
||||
@@ -143,6 +146,20 @@ func runOpsOperatorStatus(args []string, stdout, stderr io.Writer) int {
|
||||
if code != ops.VolumeStatusExitOK {
|
||||
return code
|
||||
}
|
||||
if fromBundle == "" && os.Getenv("KUBERNETES_SERVICE_HOST") != "" {
|
||||
enricher, err := opsOperatorStatusNodeEvidenceFactory()
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "sw-block ops operator-status: %v\n", err)
|
||||
return ops.VolumeStatusExitInvalid
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
cluster, err = enricher.EnrichNodeEvidence(ctx, namespace, cluster)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "sw-block ops operator-status: enrich node evidence: %v\n", err)
|
||||
return ops.VolumeStatusExitInvalid
|
||||
}
|
||||
}
|
||||
mode := "write_status"
|
||||
var writer ops.OperatorStatusWriter
|
||||
var events ops.OperatorEventSink
|
||||
|
||||
@@ -152,6 +152,8 @@ func TestPhase36D1SwBlockClusterActionabilitySchema(t *testing.T) {
|
||||
func TestPhase35D1OperatorStatusRBACIsStatusOnly(t *testing.T) {
|
||||
raw := readRepoFile(t, "charts/seaweed-block/templates/operator-status-rbac.yaml")
|
||||
required := []string{
|
||||
`resources: ["nodes", "pods"]`,
|
||||
`resources: ["csidrivers", "csinodes"]`,
|
||||
`resources: ["swblockclusters", "swblockvolumes"]`,
|
||||
`verbs: ["get", "list", "watch"]`,
|
||||
`resources: ["swblockclusters/status", "swblockvolumes/status"]`,
|
||||
@@ -170,6 +172,7 @@ func TestPhase35D1OperatorStatusRBACIsStatusOnly(t *testing.T) {
|
||||
`resources: ["deployments"`,
|
||||
`resources: ["secrets"`,
|
||||
`resources: ["storageclasses"`,
|
||||
`"create", "update", "patch"`,
|
||||
`"delete"`,
|
||||
} {
|
||||
if strings.Contains(raw, forbidden) {
|
||||
|
||||
@@ -0,0 +1,350 @@
|
||||
package ops
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"sort"
|
||||
)
|
||||
|
||||
const csiNodeAppLabel = "sw-block-csi-node"
|
||||
|
||||
func (c *KubernetesStatusClient) EnrichNodeEvidence(ctx context.Context, namespace string, cluster ClusterEvidence) (ClusterEvidence, error) {
|
||||
nodes, err := c.readKubernetesNodes(ctx)
|
||||
if err != nil {
|
||||
return cluster, err
|
||||
}
|
||||
csiPods, err := c.readCSINodePods(ctx, namespace)
|
||||
if err != nil {
|
||||
return cluster, err
|
||||
}
|
||||
driverExists, err := c.csiDriverExists(ctx)
|
||||
if err != nil {
|
||||
return cluster, err
|
||||
}
|
||||
registered, err := c.readCSINodeDriverRegistrations(ctx)
|
||||
if err != nil {
|
||||
return cluster, err
|
||||
}
|
||||
return mergeLiveNodeEvidence(namespace, cluster, nodes, csiPods, driverExists, registered), nil
|
||||
}
|
||||
|
||||
func (c *KubernetesStatusClient) readKubernetesNodes(ctx context.Context) ([]kubernetesNodeFact, error) {
|
||||
var list kubernetesNodeList
|
||||
if err := c.getJSON(ctx, "/api/v1/nodes", &list); err != nil {
|
||||
return nil, fmt.Errorf("read Kubernetes nodes: %w", err)
|
||||
}
|
||||
out := make([]kubernetesNodeFact, 0, len(list.Items))
|
||||
for _, item := range list.Items {
|
||||
fact := kubernetesNodeFact{
|
||||
Name: item.Metadata.Name,
|
||||
InternalIP: item.internalIP(),
|
||||
Ready: item.ready(),
|
||||
Schedulable: !item.Spec.Unschedulable,
|
||||
}
|
||||
out = append(out, fact)
|
||||
}
|
||||
sort.SliceStable(out, func(i, j int) bool { return out[i].Name < out[j].Name })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *KubernetesStatusClient) readCSINodePods(ctx context.Context, namespace string) (map[string]kubernetesPodFact, error) {
|
||||
var list kubernetesPodList
|
||||
path := "/api/v1/namespaces/" + pathEscape(namespace) + "/pods?labelSelector=app%3D" + pathEscape(csiNodeAppLabel)
|
||||
if err := c.getJSON(ctx, path, &list); err != nil {
|
||||
return nil, fmt.Errorf("read CSI node pods: %w", err)
|
||||
}
|
||||
out := map[string]kubernetesPodFact{}
|
||||
for _, item := range list.Items {
|
||||
nodeName := item.Spec.NodeName
|
||||
if nodeName == "" {
|
||||
continue
|
||||
}
|
||||
out[nodeName] = kubernetesPodFact{
|
||||
Name: item.Metadata.Name,
|
||||
NodeName: nodeName,
|
||||
Ready: item.ready(),
|
||||
Namespace: namespace,
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *KubernetesStatusClient) csiDriverExists(ctx context.Context) (bool, error) {
|
||||
var driver kubernetesMetadataObject
|
||||
err := c.getJSON(ctx, "/apis/storage.k8s.io/v1/csidrivers/"+pathEscape(seaweedBlockCSIDriver), &driver)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if isKubernetesNotFound(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, fmt.Errorf("read CSIDriver: %w", err)
|
||||
}
|
||||
|
||||
func (c *KubernetesStatusClient) readCSINodeDriverRegistrations(ctx context.Context) (map[string]bool, error) {
|
||||
var list kubernetesCSINodeList
|
||||
if err := c.getJSON(ctx, "/apis/storage.k8s.io/v1/csinodes", &list); err != nil {
|
||||
return nil, fmt.Errorf("read CSINodes: %w", err)
|
||||
}
|
||||
out := map[string]bool{}
|
||||
for _, item := range list.Items {
|
||||
for _, driver := range item.Spec.Drivers {
|
||||
if driver.Name == seaweedBlockCSIDriver {
|
||||
out[item.Metadata.Name] = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *KubernetesStatusClient) getJSON(ctx context.Context, path string, out any) error {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.apiURL(path), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
if c.BearerToken != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+c.BearerToken)
|
||||
}
|
||||
client := c.HTTPClient
|
||||
if client == nil {
|
||||
client = http.DefaultClient
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
||||
return json.NewDecoder(resp.Body).Decode(out)
|
||||
}
|
||||
raw, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||
return kubernetesAPIError{StatusCode: resp.StatusCode, Body: string(raw)}
|
||||
}
|
||||
|
||||
func (c *KubernetesStatusClient) apiURL(path string) string {
|
||||
return stringsTrimRightSlash(c.BaseURL) + path
|
||||
}
|
||||
|
||||
func mergeLiveNodeEvidence(namespace string, cluster ClusterEvidence, nodes []kubernetesNodeFact, csiPods map[string]kubernetesPodFact, driverExists bool, registered map[string]bool) ClusterEvidence {
|
||||
byKubernetesNode := map[string]int{}
|
||||
for i, node := range cluster.Nodes {
|
||||
if node.KubernetesNode != "" {
|
||||
byKubernetesNode[node.KubernetesNode] = i
|
||||
}
|
||||
if node.NodeName != "" {
|
||||
byKubernetesNode[node.NodeName] = i
|
||||
}
|
||||
}
|
||||
for _, fact := range nodes {
|
||||
idx, ok := byKubernetesNode[fact.Name]
|
||||
if !ok {
|
||||
cluster.Nodes = append(cluster.Nodes, NodeEvidence{NodeName: fact.Name, KubernetesNode: fact.Name})
|
||||
idx = len(cluster.Nodes) - 1
|
||||
}
|
||||
node := cluster.Nodes[idx]
|
||||
if node.NodeName == "" {
|
||||
node.NodeName = fact.Name
|
||||
}
|
||||
node.KubernetesNode = fact.Name
|
||||
node.InternalIP = firstNonEmptyString(node.InternalIP, fact.InternalIP)
|
||||
node.Ready = fact.Ready
|
||||
node.Schedulable = fact.Schedulable
|
||||
node.Conditions = mergeNodeKubernetesConditions(node.Conditions, fact)
|
||||
node.Conditions = mergeNodeCSIEvidence(node.Conditions, namespace, fact.Name, csiPods[fact.Name], driverExists, registered[fact.Name])
|
||||
cluster.Nodes[idx] = node
|
||||
}
|
||||
sort.SliceStable(cluster.Nodes, func(i, j int) bool {
|
||||
return firstNonEmptyString(cluster.Nodes[i].KubernetesNode, cluster.Nodes[i].NodeName) <
|
||||
firstNonEmptyString(cluster.Nodes[j].KubernetesNode, cluster.Nodes[j].NodeName)
|
||||
})
|
||||
return cluster
|
||||
}
|
||||
|
||||
func mergeNodeKubernetesConditions(conditions []ObservationCondition, fact kubernetesNodeFact) []ObservationCondition {
|
||||
if !fact.Ready {
|
||||
conditions = append(conditions, ObservationCondition{
|
||||
Type: "KubernetesNodeReady",
|
||||
Status: "False",
|
||||
Reason: ReasonNodeNotReady,
|
||||
Severity: "warning",
|
||||
Message: "Kubernetes node Ready condition is not True",
|
||||
EvidenceRefs: []string{"kubernetes/node/" + fact.Name},
|
||||
})
|
||||
}
|
||||
if !fact.Schedulable {
|
||||
conditions = append(conditions, ObservationCondition{
|
||||
Type: "KubernetesNodeSchedulable",
|
||||
Status: "False",
|
||||
Reason: ReasonNodeSchedulingDisabled,
|
||||
Severity: "warning",
|
||||
Message: "Kubernetes node is marked unschedulable",
|
||||
EvidenceRefs: []string{"kubernetes/node/" + fact.Name},
|
||||
})
|
||||
}
|
||||
return conditions
|
||||
}
|
||||
|
||||
func mergeNodeCSIEvidence(conditions []ObservationCondition, namespace, nodeName string, pod kubernetesPodFact, driverExists, registered bool) []ObservationCondition {
|
||||
if !driverExists || !registered {
|
||||
conditions = append(conditions, ObservationCondition{
|
||||
Type: "CSIDriverRegistered",
|
||||
Status: "False",
|
||||
Reason: ReasonCSIDriverNotRegistered,
|
||||
Severity: "warning",
|
||||
Message: "Seaweed Block CSI driver is not registered on this node",
|
||||
EvidenceRefs: []string{"kubernetes/csidriver/" + seaweedBlockCSIDriver, "kubernetes/csinode/" + nodeName},
|
||||
})
|
||||
}
|
||||
if !pod.Ready {
|
||||
conditions = append(conditions, ObservationCondition{
|
||||
Type: "CSINodePodReady",
|
||||
Status: "False",
|
||||
Reason: ReasonCSINodePodNotReady,
|
||||
Severity: "warning",
|
||||
Message: "Seaweed Block CSI node pod is not Ready on this node",
|
||||
EvidenceRefs: []string{"kubernetes/pod/" + firstNonEmptyString(pod.Namespace, namespace) + "/" + firstNonEmptyString(pod.Name, "missing")},
|
||||
})
|
||||
}
|
||||
return conditions
|
||||
}
|
||||
|
||||
func isKubernetesNotFound(err error) bool {
|
||||
apiErr, ok := err.(kubernetesAPIError)
|
||||
return ok && apiErr.StatusCode == http.StatusNotFound
|
||||
}
|
||||
|
||||
type kubernetesAPIError struct {
|
||||
StatusCode int
|
||||
Body string
|
||||
}
|
||||
|
||||
func (e kubernetesAPIError) Error() string {
|
||||
return fmt.Sprintf("http %d %s", e.StatusCode, e.Body)
|
||||
}
|
||||
|
||||
type kubernetesNodeFact struct {
|
||||
Name string
|
||||
InternalIP string
|
||||
Ready bool
|
||||
Schedulable bool
|
||||
}
|
||||
|
||||
type kubernetesPodFact struct {
|
||||
Name string
|
||||
Namespace string
|
||||
NodeName string
|
||||
Ready bool
|
||||
}
|
||||
|
||||
type kubernetesMetadataObject struct {
|
||||
Metadata kubernetesObjectMeta `json:"metadata"`
|
||||
}
|
||||
|
||||
type kubernetesObjectMeta struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type kubernetesNodeList struct {
|
||||
Items []kubernetesNodeObject `json:"items"`
|
||||
}
|
||||
|
||||
type kubernetesNodeObject struct {
|
||||
Metadata kubernetesObjectMeta `json:"metadata"`
|
||||
Spec struct {
|
||||
Unschedulable bool `json:"unschedulable"`
|
||||
} `json:"spec"`
|
||||
Status struct {
|
||||
Addresses []kubernetesNodeAddress `json:"addresses"`
|
||||
Conditions []kubernetesNodeCondition `json:"conditions"`
|
||||
} `json:"status"`
|
||||
}
|
||||
|
||||
type kubernetesNodeAddress struct {
|
||||
Type string `json:"type"`
|
||||
Address string `json:"address"`
|
||||
}
|
||||
|
||||
type kubernetesNodeCondition struct {
|
||||
Type string `json:"type"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func (n kubernetesNodeObject) internalIP() string {
|
||||
for _, address := range n.Status.Addresses {
|
||||
if address.Type == "InternalIP" {
|
||||
return address.Address
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (n kubernetesNodeObject) ready() bool {
|
||||
for _, condition := range n.Status.Conditions {
|
||||
if condition.Type == "Ready" {
|
||||
return condition.Status == "True"
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type kubernetesPodList struct {
|
||||
Items []kubernetesPodObject `json:"items"`
|
||||
}
|
||||
|
||||
type kubernetesPodObject struct {
|
||||
Metadata kubernetesObjectMeta `json:"metadata"`
|
||||
Spec struct {
|
||||
NodeName string `json:"nodeName"`
|
||||
} `json:"spec"`
|
||||
Status struct {
|
||||
Conditions []kubernetesPodCondition `json:"conditions"`
|
||||
} `json:"status"`
|
||||
}
|
||||
|
||||
type kubernetesPodCondition struct {
|
||||
Type string `json:"type"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func (p kubernetesPodObject) ready() bool {
|
||||
for _, condition := range p.Status.Conditions {
|
||||
if condition.Type == "Ready" {
|
||||
return condition.Status == "True"
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type kubernetesCSINodeList struct {
|
||||
Items []kubernetesCSINodeObject `json:"items"`
|
||||
}
|
||||
|
||||
type kubernetesCSINodeObject struct {
|
||||
Metadata kubernetesObjectMeta `json:"metadata"`
|
||||
Spec struct {
|
||||
Drivers []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"drivers"`
|
||||
} `json:"spec"`
|
||||
}
|
||||
|
||||
func firstNonEmptyString(values ...string) string {
|
||||
for _, value := range values {
|
||||
if value != "" {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func stringsTrimRightSlash(value string) string {
|
||||
for len(value) > 0 && value[len(value)-1] == '/' {
|
||||
value = value[:len(value)-1]
|
||||
}
|
||||
return value
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
package ops
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPhase37D2KubernetesNodeEvidenceEnrichesReadyAndBlockedNodes(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/api/v1/nodes":
|
||||
_, _ = w.Write([]byte(`{"items":[
|
||||
{"metadata":{"name":"m01"},"spec":{},"status":{"addresses":[{"type":"InternalIP","address":"192.168.1.181"}],"conditions":[{"type":"Ready","status":"True"}]}},
|
||||
{"metadata":{"name":"m02"},"spec":{"unschedulable":true},"status":{"addresses":[{"type":"InternalIP","address":"192.168.1.184"}],"conditions":[{"type":"Ready","status":"False"}]}}
|
||||
]}`))
|
||||
case "/api/v1/namespaces/kube-system/pods":
|
||||
if got := r.URL.Query().Get("labelSelector"); got != "app=sw-block-csi-node" {
|
||||
t.Fatalf("labelSelector=%q", got)
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"items":[
|
||||
{"metadata":{"name":"csi-m01"},"spec":{"nodeName":"m01"},"status":{"conditions":[{"type":"Ready","status":"True"}]}},
|
||||
{"metadata":{"name":"csi-m02"},"spec":{"nodeName":"m02"},"status":{"conditions":[{"type":"Ready","status":"True"}]}}
|
||||
]}`))
|
||||
case "/apis/storage.k8s.io/v1/csidrivers/block.csi.seaweedfs.com":
|
||||
_, _ = w.Write([]byte(`{"metadata":{"name":"block.csi.seaweedfs.com"}}`))
|
||||
case "/apis/storage.k8s.io/v1/csinodes":
|
||||
_, _ = w.Write([]byte(`{"items":[
|
||||
{"metadata":{"name":"m01"},"spec":{"drivers":[{"name":"block.csi.seaweedfs.com"}]}},
|
||||
{"metadata":{"name":"m02"},"spec":{"drivers":[{"name":"block.csi.seaweedfs.com"}]}}
|
||||
]}`))
|
||||
default:
|
||||
t.Fatalf("unexpected Kubernetes API path %s", r.URL.String())
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := &KubernetesStatusClient{BaseURL: server.URL, HTTPClient: server.Client()}
|
||||
cluster := ClusterEvidence{
|
||||
CapturedAt: time.Date(2026, 6, 6, 12, 0, 0, 0, time.UTC),
|
||||
Nodes: []NodeEvidence{{
|
||||
NodeName: "m01",
|
||||
KubernetesNode: "m01",
|
||||
LastHeartbeatAt: time.Date(2026, 6, 6, 11, 59, 0, 0, time.UTC),
|
||||
ReplicaCount: 1,
|
||||
}},
|
||||
}
|
||||
|
||||
enriched, err := client.EnrichNodeEvidence(context.Background(), "kube-system", cluster)
|
||||
if err != nil {
|
||||
t.Fatalf("enrich: %v", err)
|
||||
}
|
||||
if len(enriched.Nodes) != 2 {
|
||||
t.Fatalf("nodes=%+v", enriched.Nodes)
|
||||
}
|
||||
m01 := nodeByKubernetesName(t, enriched.Nodes, "m01")
|
||||
if !m01.Ready || !m01.Schedulable || m01.InternalIP != "192.168.1.181" || m01.ReplicaCount != 1 {
|
||||
t.Fatalf("m01 evidence=%+v", m01)
|
||||
}
|
||||
status, reason := classifyNodeReadiness(m01)
|
||||
if status != ManagedVolumeStatusReady || reason != ReasonNodeReady {
|
||||
t.Fatalf("m01 status=%s reason=%s", status, reason)
|
||||
}
|
||||
m02 := nodeByKubernetesName(t, enriched.Nodes, "m02")
|
||||
if m02.Ready || m02.Schedulable {
|
||||
t.Fatalf("m02 raw facts=%+v", m02)
|
||||
}
|
||||
status, reason = classifyNodeReadiness(m02)
|
||||
if status != ManagedVolumeStatusUnknown || reason != ReasonNodeNotReady {
|
||||
t.Fatalf("m02 status=%s reason=%s", status, reason)
|
||||
}
|
||||
if !conditionReason(m02.Conditions, ReasonNodeNotReady) || !conditionReason(m02.Conditions, ReasonNodeSchedulingDisabled) {
|
||||
t.Fatalf("m02 conditions=%+v", m02.Conditions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPhase37D2KubernetesNodeEvidenceProjectsCSIRegistrationBlockers(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/api/v1/nodes":
|
||||
_, _ = w.Write([]byte(`{"items":[
|
||||
{"metadata":{"name":"m01"},"spec":{},"status":{"conditions":[{"type":"Ready","status":"True"}]}},
|
||||
{"metadata":{"name":"m02"},"spec":{},"status":{"conditions":[{"type":"Ready","status":"True"}]}}
|
||||
]}`))
|
||||
case "/api/v1/namespaces/kube-system/pods":
|
||||
_, _ = w.Write([]byte(`{"items":[
|
||||
{"metadata":{"name":"csi-m01"},"spec":{"nodeName":"m01"},"status":{"conditions":[{"type":"Ready","status":"True"}]}},
|
||||
{"metadata":{"name":"csi-m02"},"spec":{"nodeName":"m02"},"status":{"conditions":[{"type":"Ready","status":"False"}]}}
|
||||
]}`))
|
||||
case "/apis/storage.k8s.io/v1/csidrivers/block.csi.seaweedfs.com":
|
||||
_, _ = w.Write([]byte(`{"metadata":{"name":"block.csi.seaweedfs.com"}}`))
|
||||
case "/apis/storage.k8s.io/v1/csinodes":
|
||||
_, _ = w.Write([]byte(`{"items":[
|
||||
{"metadata":{"name":"m01"},"spec":{"drivers":[{"name":"block.csi.seaweedfs.com"}]}},
|
||||
{"metadata":{"name":"m02"},"spec":{"drivers":[]}}
|
||||
]}`))
|
||||
default:
|
||||
t.Fatalf("unexpected Kubernetes API path %s", r.URL.String())
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := &KubernetesStatusClient{BaseURL: server.URL, HTTPClient: server.Client()}
|
||||
enriched, err := client.EnrichNodeEvidence(context.Background(), "kube-system", ClusterEvidence{})
|
||||
if err != nil {
|
||||
t.Fatalf("enrich: %v", err)
|
||||
}
|
||||
m02 := nodeByKubernetesName(t, enriched.Nodes, "m02")
|
||||
if !conditionReason(m02.Conditions, ReasonCSIDriverNotRegistered) || !conditionReason(m02.Conditions, ReasonCSINodePodNotReady) {
|
||||
t.Fatalf("m02 conditions=%+v", m02.Conditions)
|
||||
}
|
||||
status, reason := classifyNodeReadiness(m02)
|
||||
if status != ManagedVolumeStatusBlocked || reason != ReasonCSIDriverNotRegistered {
|
||||
t.Fatalf("m02 status=%s reason=%s conditions=%+v", status, reason, m02.Conditions)
|
||||
}
|
||||
crd := swBlockNodeStatuses([]NodeEvidence{m02})[0]
|
||||
if crd.Status != ManagedVolumeStatusBlocked || crd.ReasonCode != ReasonCSIDriverNotRegistered {
|
||||
t.Fatalf("CRD node status=%+v", crd)
|
||||
}
|
||||
assertCondition(t, crd.Conditions, ConditionReady, "False", ReasonCSIDriverNotRegistered)
|
||||
assertCondition(t, crd.Conditions, ConditionBlocked, "True", ReasonCSIDriverNotRegistered)
|
||||
if len(crd.EvidenceRefs) == 0 || !strings.Contains(strings.Join(crd.EvidenceRefs, ","), "kubernetes/csinode/m02") {
|
||||
t.Fatalf("missing CRD evidence refs: %+v", crd.EvidenceRefs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPhase37D2KubernetesNodeEvidenceProjectsMissingCSIDriver(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/api/v1/nodes":
|
||||
_, _ = w.Write([]byte(`{"items":[{"metadata":{"name":"m01"},"spec":{},"status":{"conditions":[{"type":"Ready","status":"True"}]}}]}`))
|
||||
case "/api/v1/namespaces/kube-system/pods":
|
||||
_, _ = w.Write([]byte(`{"items":[{"metadata":{"name":"csi-m01"},"spec":{"nodeName":"m01"},"status":{"conditions":[{"type":"Ready","status":"True"}]}}]}`))
|
||||
case "/apis/storage.k8s.io/v1/csidrivers/block.csi.seaweedfs.com":
|
||||
http.NotFound(w, r)
|
||||
case "/apis/storage.k8s.io/v1/csinodes":
|
||||
_, _ = w.Write([]byte(`{"items":[{"metadata":{"name":"m01"},"spec":{"drivers":[{"name":"block.csi.seaweedfs.com"}]}}]}`))
|
||||
default:
|
||||
t.Fatalf("unexpected Kubernetes API path %s", r.URL.String())
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := &KubernetesStatusClient{BaseURL: server.URL, HTTPClient: server.Client()}
|
||||
enriched, err := client.EnrichNodeEvidence(context.Background(), "kube-system", ClusterEvidence{})
|
||||
if err != nil {
|
||||
t.Fatalf("enrich: %v", err)
|
||||
}
|
||||
m01 := nodeByKubernetesName(t, enriched.Nodes, "m01")
|
||||
if status, reason := classifyNodeReadiness(m01); status != ManagedVolumeStatusBlocked || reason != ReasonCSIDriverNotRegistered {
|
||||
t.Fatalf("m01 status=%s reason=%s conditions=%+v", status, reason, m01.Conditions)
|
||||
}
|
||||
}
|
||||
|
||||
func nodeByKubernetesName(t *testing.T, nodes []NodeEvidence, name string) NodeEvidence {
|
||||
t.Helper()
|
||||
for _, node := range nodes {
|
||||
if node.KubernetesNode == name {
|
||||
return node
|
||||
}
|
||||
}
|
||||
t.Fatalf("missing node %s in %+v", name, nodes)
|
||||
return NodeEvidence{}
|
||||
}
|
||||
|
||||
func conditionReason(conditions []ObservationCondition, reason string) bool {
|
||||
for _, condition := range conditions {
|
||||
if condition.Reason == reason {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -14,6 +14,10 @@ type OperatorStatusSource interface {
|
||||
ClusterEvidence(ctx context.Context) (ClusterEvidence, error)
|
||||
}
|
||||
|
||||
type OperatorNodeEvidenceEnricher interface {
|
||||
EnrichNodeEvidence(ctx context.Context, namespace string, cluster ClusterEvidence) (ClusterEvidence, error)
|
||||
}
|
||||
|
||||
type OperatorStatusWriter interface {
|
||||
WriteClusterStatus(ctx context.Context, ref OperatorObjectRef, status SwBlockClusterCRDStatus) error
|
||||
WriteVolumeStatus(ctx context.Context, ref OperatorObjectRef, status SwBlockVolumeCRDStatus) error
|
||||
@@ -391,6 +395,10 @@ func swBlockNodeStatuses(nodes []NodeEvidence) []SwBlockNodeCRDStatus {
|
||||
|
||||
func classifyNodeReadiness(node NodeEvidence) (string, string) {
|
||||
switch {
|
||||
case nodeHasConditionReason(node, ReasonCSIDriverNotRegistered):
|
||||
return ManagedVolumeStatusBlocked, ReasonCSIDriverNotRegistered
|
||||
case nodeHasConditionReason(node, ReasonCSINodePodNotReady):
|
||||
return ManagedVolumeStatusBlocked, ReasonCSINodePodNotReady
|
||||
case len(node.MissingImages) > 0:
|
||||
return ManagedVolumeStatusBlocked, ReasonImageMissingOnNode
|
||||
case !node.Ready:
|
||||
@@ -402,6 +410,15 @@ func classifyNodeReadiness(node NodeEvidence) (string, string) {
|
||||
}
|
||||
}
|
||||
|
||||
func nodeHasConditionReason(node NodeEvidence, reason string) bool {
|
||||
for _, condition := range node.Conditions {
|
||||
if condition.Reason == reason {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func nodeReadinessConditions(node NodeEvidence, status, reason string) []ObservationCondition {
|
||||
conditions := append([]ObservationCondition(nil), node.Conditions...)
|
||||
switch status {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Current Plan: Phase 37 - Live Node Evidence Hardening
|
||||
|
||||
Status: active, 16% complete. Started on 2026-06-06.
|
||||
Status: active, 28% complete. Started on 2026-06-06.
|
||||
|
||||
Branch: `phase33-testops-failure-hardening`
|
||||
|
||||
@@ -93,25 +93,26 @@ internal review against control-structure-effectiveness-review.md
|
||||
Goal: populate live node readiness from Kubernetes API facts, not replay-only
|
||||
fixtures.
|
||||
|
||||
Status: pending.
|
||||
Status: dev-complete; QA pending.
|
||||
|
||||
Acceptance:
|
||||
|
||||
```text
|
||||
[ ] Ready node projects node_ready
|
||||
[ ] SchedulingDisabled node projects node_scheduling_disabled
|
||||
[ ] NotReady node projects node_not_ready
|
||||
[ ] missing CSIDriver projects csi_driver_not_registered
|
||||
[ ] missing per-node CSINode driver registration projects csi_driver_not_registered
|
||||
[ ] CSI node pod image-pull or not-ready state projects csi_node_pod_not_ready
|
||||
[ ] CRD status and operator-snapshot agree
|
||||
[ ] no workload/storage mutation verbs are added
|
||||
[x] Ready node projects node_ready
|
||||
[x] SchedulingDisabled node projects node_scheduling_disabled
|
||||
[x] NotReady node projects node_not_ready
|
||||
[x] missing CSIDriver projects csi_driver_not_registered
|
||||
[x] missing per-node CSINode driver registration projects csi_driver_not_registered
|
||||
[x] CSI node pod image-pull or not-ready state projects csi_node_pod_not_ready
|
||||
[ ] CRD status and operator-snapshot agree in live TestOps gate
|
||||
[x] no workload/storage mutation verbs are added
|
||||
```
|
||||
|
||||
Verification:
|
||||
|
||||
```text
|
||||
go test ./core/ops ./cmd/sw-block ./cmd/blockcsi
|
||||
helm lint charts/seaweed-block
|
||||
TestOps live node-readiness gate
|
||||
TestOps live CSI-registration blocker gate
|
||||
```
|
||||
@@ -237,9 +238,13 @@ QA strict rerun from clean lab
|
||||
passive read-only node fact contract and reason vocabulary for Kubernetes
|
||||
Ready/SchedulingDisabled, CSI pod/driver registration, image readiness,
|
||||
iSCSI/multipath prerequisites, and loopback cross-node blockers.
|
||||
- 28%: D2 dev-complete. `sw-block ops operator-status` enriches live master
|
||||
evidence from in-cluster read-only Kubernetes facts for Nodes, CSI node pods,
|
||||
CSIDriver, and CSINode driver registration. Chart RBAC adds only
|
||||
get/list/watch for those resources. Live QA remains pending.
|
||||
|
||||
## Next Step
|
||||
|
||||
Start D2 Kubernetes Node and CSI registration evidence. Do not implement
|
||||
finalizers, cleanup automation, rebuild/failback, backup/restore, or NVMe ANA
|
||||
parity in this phase.
|
||||
Assign QA to run D2 live node-readiness and CSI-registration gates from a clean
|
||||
lab. Do not implement finalizers, cleanup automation, rebuild/failback,
|
||||
backup/restore, or NVMe ANA parity in this phase.
|
||||
@@ -0,0 +1,134 @@
|
||||
# Phase 37 D2 QA Assignment: Live Node And CSI Evidence
|
||||
|
||||
Status: assigned.
|
||||
|
||||
## Goal
|
||||
|
||||
Validate that `sw-block ops operator-status` publishes live Kubernetes node and
|
||||
CSI registration facts into `SwBlockCluster.status.nodes[]`.
|
||||
|
||||
This is not a lifecycle or mutation test. The operator-status controller must
|
||||
remain read-only except CRD status writes and Events.
|
||||
|
||||
## Source Under Test
|
||||
|
||||
- Branch: `phase33-testops-failure-hardening`
|
||||
- Minimum commit: includes `phase37: define live node evidence contract` plus
|
||||
D2 live node evidence implementation.
|
||||
|
||||
## Required Gates
|
||||
|
||||
### G1: Healthy Live Node Path
|
||||
|
||||
Install with:
|
||||
|
||||
```bash
|
||||
helm install sw-block charts/seaweed-block \
|
||||
--namespace kube-system \
|
||||
--create-namespace \
|
||||
-f values.day1.yaml \
|
||||
--set operatorStatus.create=true \
|
||||
--set operatorStatus.dryRun=false \
|
||||
--wait --timeout 10m
|
||||
```
|
||||
|
||||
Pass criteria:
|
||||
|
||||
```text
|
||||
SwBlockCluster.status.nodes has one entry per Kubernetes node observed by the lab.
|
||||
Each healthy node has status=ready reasonCode=node_ready.
|
||||
ready=true and schedulable=true reflect the Kubernetes Node object.
|
||||
CRD status and operator-snapshot.json agree on node names, ready, schedulable,
|
||||
status, and reasonCode.
|
||||
```
|
||||
|
||||
### G2: SchedulingDisabled Node
|
||||
|
||||
Temporarily cordon one non-critical node, wait for one operator-status
|
||||
iteration, then uncordon during cleanup.
|
||||
|
||||
Pass criteria:
|
||||
|
||||
```text
|
||||
affected node has status=blocked reasonCode=node_scheduling_disabled.
|
||||
Ready condition is False with reason node_scheduling_disabled.
|
||||
Blocked condition is True with reason node_scheduling_disabled.
|
||||
No workload, PVC, PV, storage, image, or host mutation is performed by
|
||||
operator-status.
|
||||
```
|
||||
|
||||
### G3: NotReady Node
|
||||
|
||||
Use the safest lab-approved method to make one node NotReady, or mark this gate
|
||||
blocked if lab policy does not allow node readiness disruption.
|
||||
|
||||
Pass criteria:
|
||||
|
||||
```text
|
||||
affected node has status=unknown reasonCode=node_not_ready.
|
||||
Ready condition is Unknown/False with reason node_not_ready.
|
||||
No false node_ready is shown for the affected node.
|
||||
```
|
||||
|
||||
### G4: CSI Registration Blocker
|
||||
|
||||
Use a controlled CSI node pod / CSINode registration fault, preferably by
|
||||
blocking the CSI node DaemonSet image or selecting one node whose CSINode lacks
|
||||
`block.csi.seaweedfs.com`.
|
||||
|
||||
Pass criteria:
|
||||
|
||||
```text
|
||||
missing CSIDriver or missing CSINode driver registration projects
|
||||
reasonCode=csi_driver_not_registered.
|
||||
CSI node pod not Ready projects reasonCode=csi_node_pod_not_ready unless a more
|
||||
specific image blocker is present.
|
||||
CRD status, report, dashboard/operator-snapshot, and explain agree.
|
||||
No Ready=True is shown for the blocked node.
|
||||
```
|
||||
|
||||
### G5: RBAC Boundary
|
||||
|
||||
Run `kubectl auth can-i` as the operator-status service account.
|
||||
|
||||
Pass criteria:
|
||||
|
||||
```text
|
||||
Allowed:
|
||||
- get/list/watch nodes
|
||||
- get/list/watch pods
|
||||
- get/list/watch csidrivers.storage.k8s.io
|
||||
- get/list/watch csinodes.storage.k8s.io
|
||||
- get/list/watch swblockclusters,swblockvolumes
|
||||
- patch/update swblockclusters/status,swblockvolumes/status
|
||||
- create events
|
||||
|
||||
Forbidden:
|
||||
- create/update/patch/delete pods
|
||||
- create/update/patch/delete PVC/PV
|
||||
- create/update/patch/delete deployments
|
||||
- create/update/patch/delete storageclasses
|
||||
- create/update/patch/delete secrets
|
||||
- update/patch/delete swblockclusters or swblockvolumes spec
|
||||
```
|
||||
|
||||
## Cleanup
|
||||
|
||||
Run the standard Helm uninstall and cleanup verifier. Final pass requires:
|
||||
|
||||
```text
|
||||
cleanup_status=ok
|
||||
k8s_residue_count=0
|
||||
iscsi_residue_count=0
|
||||
multipath_residue_count=0
|
||||
process_residue_count=0
|
||||
hostpath_residue_count=0
|
||||
```
|
||||
|
||||
## Report
|
||||
|
||||
Write sign-off to:
|
||||
|
||||
```text
|
||||
internal/docs/qa-assignments/phase37-d2-live-node-csi-evidence-qa-signoff.md
|
||||
```
|
||||
Reference in new issue
Block a user