diff --git a/rpi-nfd-discovery/README.md b/rpi-nfd-discovery/README.md index 7c35338..6c3fb3b 100644 --- a/rpi-nfd-discovery/README.md +++ b/rpi-nfd-discovery/README.md @@ -2,7 +2,7 @@ Minimal Go-based Raspberry Pi hardware discovery for the Node Feature Discovery (NFD) `local` source. -Reads the host's device-tree model and optional `/proc/cpuinfo` revision, and atomically writes an NFD feature file to `features.d/rpi`. Does **not** require Kubernetes API permissions. Does **not** update health/status labels on every sample. +Reads the host's device-tree model and optional `/proc/cpuinfo` revision, and publishes the labels as an NFD `NodeFeature` object (`nfd.k8s-sigs.io/v1alpha1`, named `rpi-discovery-`, labelled `nfd.node.kubernetes.io/node-name`) in the pod's namespace, via server-side apply with the in-cluster ServiceAccount. The NFD master turns it into node labels. The container needs no root and no hostPath. Optionally (`-output`) it still writes the legacy `features.d` file. Does **not** update health/status labels on every sample. ## Labels @@ -12,23 +12,26 @@ Reads the host's device-tree model and optional `/proc/cpuinfo` revision, and at ## Requirements -Your deployed NFD worker must have the `local` source enabled **and must mount the same host path** (`/etc/kubernetes/node-feature-discovery/features.d`) as this DaemonSet. +- Env `NODE_NAME` (downward API `spec.nodeName`). +- A ServiceAccount allowed `get`, `create`, `update`, `patch` on `nodefeatures.nfd.k8s-sigs.io` in the pod's namespace (the NFD namespace). +- NFD master v0.14+ (NodeFeature API enabled, the default). +- Legacy file mode: set `-output /features.d/rpi` and mount the NFD worker's `features.d` host path with the `local` source enabled. Example local host test: ```sh go test ./... go run . -model /proc/device-tree/model -cpuinfo /proc/cpuinfo \ - -output /tmp/rpi-nfd-features -interval 5m + -output /tmp/rpi-nfd-features -interval 5m # file mode; set NODE_NAME in-cluster for NodeFeature mode ``` Build and publish the multi-arch image before enabling the draft homelab deployment: ```sh docker buildx build --platform linux/arm/v7,linux/arm64,linux/amd64 \ - -t davidcollom/rpi-nfd-discovery:0.1.0 --push . + -t davidcollom/rpi-nfd-discovery:0.2.0 --push . ``` -Image is not published merely by committing these source files. The image is intentionally distroless/non-root. The DaemonSet needs a writable host-mounted NFD features directory. +Image is not published merely by committing these source files. The image is intentionally distroless/non-root. Throttling and undervoltage are deliberately out of scope for static NFD labels; they belong in Prometheus telemetry and alerts. A later exporter can expose `vcgencmd get_throttled` and associated events. diff --git a/rpi-nfd-discovery/VERSION b/rpi-nfd-discovery/VERSION index 6e8bf73..0ea3a94 100644 --- a/rpi-nfd-discovery/VERSION +++ b/rpi-nfd-discovery/VERSION @@ -1 +1 @@ -0.1.0 +0.2.0 diff --git a/rpi-nfd-discovery/main.go b/rpi-nfd-discovery/main.go index b8f1104..0d0ccac 100644 --- a/rpi-nfd-discovery/main.go +++ b/rpi-nfd-discovery/main.go @@ -1,9 +1,16 @@ package main import ( + "bytes" + "crypto/tls" + "crypto/x509" + "encoding/json" "flag" "fmt" + "io" "log" + "net" + "net/http" "os" "path/filepath" "regexp" @@ -29,8 +36,8 @@ func modelID(model string) string { name := strings.TrimPrefix(model, "Raspberry Pi ") name = strings.TrimSpace(strings.Split(name, " Rev ")[0]) name = strings.ToLower(name) - name = strings.ReplaceAll(name, " Model ", " ") - name = strings.ReplaceAll(name, " Plus", "+") + name = strings.ReplaceAll(name, " model ", " ") + name = strings.ReplaceAll(name, " plus", "+") name = strings.ReplaceAll(name, "+", "plus") name = strings.ReplaceAll(name, " ", "-") return strings.Trim(safeValue.ReplaceAllString(name, "-"), "-.") @@ -63,6 +70,75 @@ func featureContent(model, rev string) string { return b.String() } +func featureLabels(model, rev string) map[string]string { + l := map[string]string{ + "feature.node.kubernetes.io/rpi-present": "true", + "feature.node.kubernetes.io/rpi-model": modelID(model), + } + if rev != "" { + l["feature.node.kubernetes.io/rpi-revision"] = rev + } + return l +} + +// nodeFeatureBody builds the NFD NodeFeature object for a node. +func nodeFeatureBody(node, ns string, labels map[string]string) ([]byte, error) { + return json.Marshal(map[string]any{ + "apiVersion": "nfd.k8s-sigs.io/v1alpha1", + "kind": "NodeFeature", + "metadata": map[string]any{ + "name": "rpi-discovery-" + node, + "namespace": ns, + "labels": map[string]string{"nfd.node.kubernetes.io/node-name": node}, + }, + "spec": map[string]any{"labels": labels}, + }) +} + +// applyNodeFeature creates or updates the NodeFeature via server-side apply +// (needs create+patch on nodefeatures). +func applyNodeFeature(hc *http.Client, base, token, ns, node string, body []byte) error { + url := fmt.Sprintf("%s/apis/nfd.k8s-sigs.io/v1alpha1/namespaces/%s/nodefeatures/rpi-discovery-%s?fieldManager=rpi-nfd-discovery&force=true", base, ns, node) + req, err := http.NewRequest(http.MethodPatch, url, bytes.NewReader(body)) + if err != nil { + return err + } + req.Header.Set("Content-Type", "application/apply-patch+yaml") + req.Header.Set("Authorization", "Bearer "+token) + resp, err := hc.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + if resp.StatusCode/100 != 2 { + msg, _ := io.ReadAll(io.LimitReader(resp.Body, 512)) + return fmt.Errorf("apply NodeFeature: %s: %s", resp.Status, msg) + } + return nil +} + +const saDir = "/var/run/secrets/kubernetes.io/serviceaccount/" + +// inClusterClient returns an API client, base URL and pod namespace. +func inClusterClient() (*http.Client, string, string, error) { // 4th return unused + host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT") + if host == "" || port == "" { + return nil, "", "", fmt.Errorf("not running in a cluster") + } + ca, err := os.ReadFile(saDir + "ca.crt") + if err != nil { + return nil, "", "", err + } + pool := x509.NewCertPool() + pool.AppendCertsFromPEM(ca) + ns, err := os.ReadFile(saDir + "namespace") + if err != nil { + return nil, "", "", err + } + hc := &http.Client{Timeout: 30 * time.Second, Transport: &http.Transport{TLSClientConfig: &tls.Config{RootCAs: pool}}} + return hc, "https://" + net.JoinHostPort(host, port), strings.TrimSpace(string(ns)), nil +} + func writeAtomic(path, data string) error { dir := filepath.Dir(path) tmp, err := os.CreateTemp(dir, ".rpi-nfd-*") @@ -87,25 +163,57 @@ func writeAtomic(path, data string) error { func main() { modelPath := flag.String("model", "/host/device-tree/model", "host device-tree model file") cpuInfoPath := flag.String("cpuinfo", "/host/cpuinfo", "host /proc/cpuinfo") - output := flag.String("output", "/features.d/rpi", "NFD local feature file") + output := flag.String("output", "", "optional NFD local feature file (legacy; e.g. /features.d/rpi)") interval := flag.Duration("interval", 5*time.Minute, "reconciliation interval") flag.Parse() if *interval <= 0 { log.Fatal("interval must be positive") } + node := os.Getenv("NODE_NAME") + if node == "" && *output == "" { + log.Fatal("set NODE_NAME (NodeFeature mode) and/or -output (file mode)") + } + var ( + hc *http.Client + base, ns string + lastPublished string + ) + if node != "" { + var err error + if hc, base, ns, err = inClusterClient(); err != nil { + log.Fatal(err) + } + } for { model, err := readModel(*modelPath) if err != nil { log.Printf("discovery failed: %v", err) } else { - content := featureContent(model, revision(*cpuInfoPath)) - old, err := os.ReadFile(*output) - if err != nil || string(old) != content { - if err := writeAtomic(*output, content); err != nil { - log.Printf("feature write failed: %v", err) - } else { - log.Printf("discovered %s (%s)", model, modelID(model)) + rev := revision(*cpuInfoPath) + if *output != "" { + content := featureContent(model, rev) + old, err := os.ReadFile(*output) + if err != nil || string(old) != content { + if err := writeAtomic(*output, content); err != nil { + log.Printf("feature write failed: %v", err) + } else { + log.Printf("discovered %s (%s)", model, modelID(model)) + } + } + } + if node != "" { + // Re-apply every interval (idempotent) so a deleted object self-heals. + body, _ := nodeFeatureBody(node, ns, featureLabels(model, rev)) + token, err := os.ReadFile(saDir + "token") // re-read: projected tokens rotate + if err == nil { + err = applyNodeFeature(hc, base, strings.TrimSpace(string(token)), ns, node, body) + } + if err != nil { + log.Printf("NodeFeature update failed: %v", err) + } else if s := string(body); s != lastPublished { + lastPublished = s + log.Printf("published NodeFeature for %s (%s)", node, modelID(model)) } } } diff --git a/rpi-nfd-discovery/main_test.go b/rpi-nfd-discovery/main_test.go index 0c1fbfb..62e697a 100644 --- a/rpi-nfd-discovery/main_test.go +++ b/rpi-nfd-discovery/main_test.go @@ -1,6 +1,10 @@ package main import ( + "encoding/json" + "io" + "net/http" + "net/http/httptest" "os" "path/filepath" "strings" @@ -66,3 +70,51 @@ func TestAtomicOutput(t *testing.T) { t.Fatalf("unexpected features: %s", got) } } + +func TestNodeFeatureBody(t *testing.T) { + body, err := nodeFeatureBody("pi1", "nfd", featureLabels("Raspberry Pi 3 Model B Plus Rev 1.3", "a020d3")) + if err != nil { + t.Fatal(err) + } + var o struct { + APIVersion, Kind string + Metadata struct { + Name, Namespace string + Labels map[string]string + } + Spec struct{ Labels map[string]string } + } + if err := json.Unmarshal(body, &o); err != nil { + t.Fatal(err) + } + if o.APIVersion != "nfd.k8s-sigs.io/v1alpha1" || o.Kind != "NodeFeature" || + o.Metadata.Name != "rpi-discovery-pi1" || o.Metadata.Namespace != "nfd" || + o.Metadata.Labels["nfd.node.kubernetes.io/node-name"] != "pi1" || + o.Spec.Labels["feature.node.kubernetes.io/rpi-model"] != "3-bplus" || + o.Spec.Labels["feature.node.kubernetes.io/rpi-revision"] != "a020d3" || + o.Spec.Labels["feature.node.kubernetes.io/rpi-present"] != "true" { + t.Fatalf("unexpected object: %s", body) + } +} + +func TestApplyNodeFeature(t *testing.T) { + var method, path, ctype, auth, got string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + method, path, ctype, auth = r.Method, r.URL.Path, r.Header.Get("Content-Type"), r.Header.Get("Authorization") + b, _ := io.ReadAll(r.Body) + got = string(b) + })) + defer srv.Close() + if err := applyNodeFeature(srv.Client(), srv.URL, "tok", "nfd", "pi1", []byte("{}")); err != nil { + t.Fatal(err) + } + if method != "PATCH" || path != "/apis/nfd.k8s-sigs.io/v1alpha1/namespaces/nfd/nodefeatures/rpi-discovery-pi1" || + ctype != "application/apply-patch+yaml" || auth != "Bearer tok" || got != "{}" { + t.Fatalf("bad request: %s %s %s %s %s", method, path, ctype, auth, got) + } + bad := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(403) })) + defer bad.Close() + if err := applyNodeFeature(bad.Client(), bad.URL, "tok", "nfd", "pi1", nil); err == nil { + t.Fatal("expected error on 403") + } +}