Files
ilkerandClaude Opus 5 6dc2826c17 hpcod: the life of an application, carried past the moment it is published
Publishing worked; everything after it did not. Four things were wrong
and each was silent in its own way.

Versions were compared as text, so asking for "gromacs" with 2024.9 and
2024.10 published returned 2024.9. The whole meaning of an unversioned
reference is "the newest one", and it was a wrong answer rather than a
missing one - invisible until somebody noticed they had spent a week on
last year's build.

A recipe could hold one version while the catalogue could hold many, so
an update either destroyed what it replaced or grew a parallel
directory sharing nothing with the first. Recipes now carry a list, and
the single-version form is read into the same shape, so nothing
downstream knows which was written.

An image was named by a mutable tag. Whoever repushes gromacs:2024.4
changed what every rebuilt cluster received, silently, and the record
meant to freeze the cluster froze only its opinion of it. Two hashes
are now recorded because they are checkable in different places: the
manifest digest answers "has this moved" from the control plane, and
the SIF hash is the only one a head can check with no network and no
credential - which matters, because the head is where a wrong image
does harm. Pulling by digest is deliberately not done: whether this
Apptainer accepts an @sha256: reference is not established, and a
provision failing at its last task over untested syntax would be worse
than the problem.

Module and binaries had been in the catalogue since it was written and
were read by nothing - Module was referenced nowhere in the program. So
a tenant was handed /opt/apps/gromacs.sif and left to work out
Apptainer. They now become a wrapper per binary and an Lmod modulefile,
and the answer is `module load gromacs`. Whether to pass --nv is
decided on the node at run time: passing it without a driver fails
outright, and omitting it where there is one leaves the job on the CPU
saying nothing at all, which is the dangerous one.

Four states replace `published:`. A withdrawn entry stays in the
catalogue file, because dropping it would make it indistinguishable
from one that never existed - and that difference is what an operator
needs when a cluster is found still holding it. A pinned reference to
one is refused with the recorded reason instead of "no application",
which would send the user hunting for a typo in a correct name.

The portal re-reads the catalogue when it changes, so publishing an
application no longer needs a restart - the step that is invisible when
forgotten. C20 ships the catalogue, which nothing did.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-22 10:25:54 +02:00

243 lines
8.4 KiB
Go

// Package catalog is the list of applications a tenant can ask for.
//
// The container registry stores the bytes; it does not store what they mean.
// Nothing in an OCI manifest says that gromacs-gpu needs a GPU, how large the
// image is, or what module name it exposes once mounted. That is what this
// file carries, and it is what lets a user ask for an application rather than
// for hardware: someone who wants GROMACS on GPUs should not also have to
// know to tick the GPU box, and if they forget, placement should not put them
// on a site that cannot run it.
//
// A YAML file rather than a service, deliberately. It is read, not written,
// and only the platform team publishes to it. It becomes a service the day
// someone else needs to write to it, and not before.
package catalog
import (
"fmt"
"os"
"sort"
"strings"
"gopkg.in/yaml.v3"
"github.com/ilkermanap/hpcod/internal/model"
"github.com/ilkermanap/hpcod/internal/spec"
)
// App is one publishable application image.
type App struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
Description string `yaml:"description,omitempty"`
// Image is where the SIF lives, as Singularity would be given it -
// oras://host/namespace/name:tag for a registry-held SIF.
Image string `yaml:"image"`
// Requires is what the application needs from the hardware. It is the
// reason this file exists: these become placement constraints without
// the user restating them.
Requires []model.Capability `yaml:"requires,omitempty"`
// SizeGB decides how the image reaches a tenant. Small ones can be
// pulled while the cluster provisions; a large one belongs either in the
// template or in a fetch that happens after the cluster is already
// usable, so it does not sit on the critical path that took a day to
// shorten.
SizeGB float64 `yaml:"size_gb,omitempty"`
// Binaries are the commands a user expects on PATH once the module is
// loaded. Each becomes a wrapper on the head that runs it inside the
// image, which is the difference between shipping a file and shipping
// software - before this a tenant got a SIF and was left to work out
// Apptainer.
//
// It is also what the build checks: an image that does not contain
// every one of these is refused before it is pushed, rather than found
// out inside a job that has already queued and started.
Binaries []string `yaml:"binaries,omitempty"`
// Module is the Lmod name the application appears under, when it
// provides one.
Module string `yaml:"module,omitempty"`
// Digest is the manifest digest the tag pointed at when the catalogue
// was generated. A tag is mutable: whoever repushes gromacs:2024.4
// changes what every cluster rebuilt afterwards receives, silently, and
// the record that was meant to freeze a cluster freezes nothing. This is
// what lets a head node say the tag has moved under it.
Digest string `yaml:"digest,omitempty"`
// SIFSHA256 is the hash of the file itself, recorded when it was
// published. The head can check it with sha256sum and no network, which
// the manifest digest would need a registry round trip and a credential
// for. Empty for an image published before this was recorded.
SIFSHA256 string `yaml:"sif_sha256,omitempty"`
// StatusField is where the application is in its life: available,
// deprecated or withdrawn. Read through Status(), which supplies the
// default; the raw field is exported only because YAML needs it.
StatusField string `yaml:"status,omitempty"`
// StatusNote says why, for the two states that need a reason. A
// deprecation nobody explained is one nobody acts on.
StatusNote string `yaml:"status_note,omitempty"`
// Signed records whether the image carries a Singularity signature.
// Running an unsigned third-party binary on tenant nodes is a decision,
// so it is recorded rather than assumed.
Signed bool `yaml:"signed"`
}
// Ref is how an app is named in a request: "openfoam" or "openfoam:12".
func (a App) Ref() string {
if a.Version == "" {
return a.Name
}
return a.Name + ":" + a.Version
}
// Catalog is the loaded file.
type Catalog struct {
Apps []App `yaml:"apps"`
}
// Load reads a catalog. A missing file is an empty catalog, not an error: a
// site that publishes no applications is a legitimate configuration, and the
// control plane must still start.
func Load(path string) (*Catalog, error) {
raw, err := os.ReadFile(path)
if os.IsNotExist(err) {
return &Catalog{}, nil
}
if err != nil {
return nil, err
}
var c Catalog
if err := yaml.Unmarshal(raw, &c); err != nil {
return nil, fmt.Errorf("%s: %w", path, err)
}
for i, a := range c.Apps {
if a.Name == "" || a.Image == "" {
return nil, fmt.Errorf("%s: app %d needs at least a name and an image", path, i)
}
}
return &c, nil
}
// Lookup resolves a reference. Without a version it returns the highest one
// present, so "openfoam" means the newest published OpenFOAM rather than
// whichever happens to be first in the file.
func (c *Catalog) Lookup(ref string) (App, bool) {
name, version, hasVersion := strings.Cut(ref, ":")
var candidates []App
for _, a := range c.Apps {
if a.Name != name {
continue
}
if hasVersion {
if a.Version != version {
continue
}
// A pinned reference resolves to exactly that entry, whatever
// its state. Resolve is what refuses a withdrawn one, and it
// can only say why if it is handed the entry rather than
// "no such application".
candidates = append(candidates, a)
continue
}
// Unversioned means "the newest one anybody should start on", so a
// withdrawn or draft version is not a candidate - otherwise
// withdrawing the newest build would silently be undone by the
// next person who typed the bare name.
if a.Offered() {
candidates = append(candidates, a)
}
}
if len(candidates) == 0 {
return App{}, false
}
sort.Slice(candidates, func(i, j int) bool {
return compareVersions(candidates[i].Version, candidates[j].Version) < 0
})
return candidates[len(candidates)-1], true
}
// Resolve turns the application list from a request into the applications
// themselves and the capabilities they imply.
//
// An unknown reference is an error rather than a warning. Provisioning a
// fourteen-day cluster and only then discovering that the application the
// user came for was never published is the wrong order to find out.
func (c *Catalog) Resolve(refs []string) ([]App, []model.Capability, error) {
var apps []App
seen := map[model.Capability]bool{}
var caps []model.Capability
for _, ref := range refs {
a, ok := c.Lookup(ref)
if !ok {
return nil, nil, fmt.Errorf("no application %q in the catalog", ref)
}
if !a.Offered() {
// Named, found, and refused - with the reason. "No such
// application" would send the user looking for a typo in a
// name that is perfectly correct.
why := string(a.Status())
if a.StatusNote != "" {
why += ": " + a.StatusNote
}
return nil, nil, fmt.Errorf("application %q is %s, so a new cluster cannot be built on it", ref, why)
}
apps = append(apps, a)
for _, cap := range a.Requires {
if !seen[cap] {
seen[cap] = true
caps = append(caps, cap)
}
}
}
return apps, caps, nil
}
// Expand folds the capabilities an application needs into the request's own
// constraints, and returns the applications that were resolved.
//
// It runs before placement rather than inside it. Placement's job is to match
// constraints against hardware; knowing that a particular build of GROMACS
// wants a GPU is catalogue knowledge, and mixing the two would mean the
// placement tests had to carry an application list to say anything about
// hardware.
//
// A capability the request already states is left alone: an explicit count of
// four GPUs is not overwritten by an application that merely needs one.
func (c *Catalog) Expand(req spec.Request) (spec.Request, []App, error) {
apps, caps, err := c.Resolve(req.Stack.Apps)
if err != nil {
return req, nil, err
}
stated := map[model.Capability]bool{}
for _, con := range req.Compute.Require {
stated[con.Capability] = true
}
// Copy before appending: the caller's slice may be shared, and a request
// that quietly grows constraints every time it is examined would be a
// bad surprise in a dry-run.
out := req
out.Compute.Require = append([]spec.Constraint(nil), req.Compute.Require...)
for _, cap := range caps {
if stated[cap] {
continue
}
out.Compute.Require = append(out.Compute.Require, spec.Constraint{
Capability: cap,
Count: 1,
})
}
return out, apps, nil
}