Something went wrong. Try again.
A Kubernetes operator that runs Claude Code sessions on long-horizon work
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103package operator
import ( "context" "errors" "log/slog" "net" "net/http" "time"
"github.com/liken-sh/liken/kubernetes/apiclient" "github.com/liken-sh/liken/kubernetes/events" "github.com/liken-sh/liken/kubernetes/informer"
"tangled.org/guid.foo/simmer/cluster" "tangled.org/guid.foo/simmer/dataplane")
// Wiring is how the operator reaches the data plane's code. The data// plane's package builds the store and the HTTP server; the command// passes them in, so this package builds and tests without them.type Wiring struct { Version string OpenStore func(dir string) (dataplane.Store, error) // DataPlane serves the data plane API with a store and the // operator's Control. client reads TokenReviews and pods. DataPlane func(store dataplane.Store, control dataplane.Control, client *apiclient.Client) http.Handler}
// Run runs the operator in its pod until ctx ends, or until one of its// servers stops on its own error, which Run answers so the pod// restarts.func Run(ctx context.Context, s Settings, w Wiring) error { ctx, fail := context.WithCancelCause(ctx) defer fail(nil) client, err := apiclient.InCluster(apiclient.InClusterOptions{}) if err != nil { return err } if s.OperatorImage == "" { if s.OperatorImage, err = ownImage(client, s.Namespace, s.PodName); err != nil { return err } } watcher, err := informer.InCluster() if err != nil { return err } store, err := w.OpenStore(s.DataDir) if err != nil { return err } bell := NewBell() o := New(store, s, w.Version, bell) client = o.Observe(client) // The recorder writes from its own goroutine, so it takes a context // that outlives the last pass, and Shutdown sends what it queued. recorder := events.New(context.Background(), client, "simmer-operator", events.Options{}) watches := cluster.Watch(ctx, watcher, cluster.Options{ Namespace: s.Namespace, Wake: bell.Ring, Reopened: o.Reopened, Deleted: o.Deleted, }) o.Attach(cluster.New(client, watches, recorder), wicketClient()) metricsListener, err := net.Listen("tcp", s.MetricsAddress) if err != nil { return err } dataplaneListener, err := net.Listen("tcp", s.DataplaneAddress) if err != nil { _ = metricsListener.Close() return err } metricsStopped := serve(ctx, metricsListener, o.Handler(), fail) dataplaneStopped := serve(ctx, dataplaneListener, o.ObserveDataPlane(w.DataPlane(store, o, client)), fail) slog.Info("the operator runs", "version", w.Version, "namespace", s.Namespace, "operator_image", s.OperatorImage) o.Loop(ctx) o.Stop() // A data plane request writes through the cluster and can post an // Event, so the recorder shuts down after the last one returns. <-dataplaneStopped <-metricsStopped watches.Done() shutdown, cancel := context.WithTimeout(context.Background(), events.ShutdownTimeout) defer cancel() err = recorder.Shutdown(shutdown) if cause := context.Cause(ctx); !errors.Is(cause, context.Canceled) { return errors.Join(cause, err) } return err}
// wicketClient is the HTTP client of the subscriptions. A stream stays// open for hours, so the client sets no overall timeout, only bounds on// the dial and on the wait for the answer's headers.func wicketClient() *http.Client { return &http.Client{Transport: &http.Transport{ DialContext: (&net.Dialer{Timeout: 10 * time.Second, KeepAlive: 30 * time.Second}).DialContext, ResponseHeaderTimeout: 30 * time.Second, }}}