Files
cozystack/cmd/gpu-node-agent/main.go
Aleksei Sviridkin 0cb44dc919 feat(gpu-node-agent): add the agent command, image and chart
The inventory has to run next to kubelet: a DaemonSet reading the
host's sysfs and kubelet's pod-resources socket, both mounted
read-only. Connecting to the socket needs root, so the agent runs as
root but with no capabilities and no privilege escalation.

The manager's cached client would watch every pod in the cluster, so
the agent uses a direct client and lists only its own node's pods.
RBAC cannot name the caller's node, so the role is cluster-wide; it
holds only the verbs the agent uses, and the DaemonSet selects the
nodes node-feature-discovery marks as carrying an NVIDIA device, so
the token does not sit on nodes with nothing to report.

The image is in the root build list so that every build path stamps
its reference; the committed tag is replaced by the first build.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
2026-10-21 06:33:45 +03:00

133 lines
3.9 KiB
Go

/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"errors"
"flag"
"os"
"time"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
gpuv1alpha1 "github.com/cozystack/cozystack/api/gpu/v1alpha1"
"github.com/cozystack/cozystack/internal/gpu/agent"
)
var scheme = runtime.NewScheme()
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(gpuv1alpha1.AddToScheme(scheme))
}
type options struct {
nodeName string
sysfs string
socket string
interval time.Duration
probeAddr string
}
func parseFlags(args []string) (options, error) {
var o options
fs := flag.NewFlagSet("gpu-node-agent", flag.ContinueOnError)
fs.StringVar(&o.nodeName, "node-name", os.Getenv("NODE_NAME"), "Name of this node; the agent writes only the GPUNode of that name.")
fs.StringVar(&o.sysfs, "sysfs", "/sys", "Where the host's sysfs is mounted.")
fs.StringVar(&o.socket, "pod-resources-socket", agent.PodResourcesSocket, "kubelet's pod-resources socket.")
fs.DurationVar(&o.interval, "interval", 30*time.Second, "Time between inventory syncs.")
fs.StringVar(&o.probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
if err := fs.Parse(args); err != nil {
return o, err
}
if o.nodeName == "" {
return o, errors.New("--node-name or NODE_NAME is required")
}
if o.interval < time.Second {
return o, errors.New("--interval must be at least 1s")
}
return o, nil
}
func main() {
ctrl.SetLogger(zap.New())
setupLog := ctrl.Log.WithName("setup")
o, err := parseFlags(os.Args[1:])
if err != nil {
setupLog.Error(err, "invalid flags")
os.Exit(2)
}
cfg := ctrl.GetConfigOrDie()
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{BindAddress: "0"},
HealthProbeBindAddress: o.probeAddr,
})
if err != nil {
setupLog.Error(err, "unable to create manager")
os.Exit(1)
}
// The manager's client would cache and so watch every pod in the
// cluster; the agent lists only its own node's.
c, err := client.New(cfg, client.Options{Scheme: scheme})
if err != nil {
setupLog.Error(err, "unable to create client")
os.Exit(1)
}
podResources, conn, err := agent.Dial(o.socket)
if err != nil {
setupLog.Error(err, "invalid pod-resources socket")
os.Exit(1)
}
defer func() { _ = conn.Close() }()
a := &agent.Agent{
NodeName: o.nodeName,
Sysfs: o.sysfs,
PodResources: podResources,
Client: c,
Interval: o.interval,
}
if err := mgr.Add(a); err != nil {
setupLog.Error(err, "unable to add agent")
os.Exit(1)
}
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("synced", a.Ready); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}
setupLog.Info("starting GPU node agent", "node", o.nodeName)
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "agent stopped")
os.Exit(1)
}
}