Files
mesh-host/internal/selfcompose/versions.go
T
jschoubben 8a0c39280d Node-engine: a want naming its version is resolved to its digests through the registry seats (A-430a)
A want of a released build names the module's version and no parts. Each artifact of the build's entry is
resolved at that version through the verb versions of its kind's registry seat: oci-artifact-registry for a
bundle or an archive, container-image-registry for an image, under <module>/<artifact> or a package's own
name. An upstream copy, which is not numbered, takes the digest its manifest pins. partsMissing, buildOf and
KeptBuildOf (and the shadow's added modules) read those digests; a want with parts is read as before.

A lookup that fails is said in the journal, naming the module, the version and the seat, and holds the module
whole: nothing of it is composed. It is asked again after a minute. The manifest invokes both verbs.

Builds: design 47 G A-430, A-430a; lead answer A-430b
2026-11-15 16:52:59 +01:00

218 lines
8.3 KiB
Go

package selfcompose
import (
"encoding/json"
"fmt"
"sort"
"strings"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry"
"git.novox.be/novox/mesh-sdk/go/buscalls"
"git.novox.be/novox/mesh-sdk/go/catalogue"
)
// **A want names its version; the node-engine resolves the digests** (design 47 G A-430, A-430a, the lead's answer A-430b).
// A want of a released build names the module's version and no parts: each artifact of the build's entry is looked up at that
// version through the verb `versions` of the registry seat of its kind (oci-artifact-registry for a bundle or an archive,
// container-image-registry for an image), under the name the builder numbered it by: `<module>/<artifact>`, or the entry's
// package name for a package. An upstream copy is not numbered (it is mirrored by its pinned digest): its digest is the one its
// manifest's `from` pins. A want that names parts (a build no version is known of) is read by its parts as before. A lookup that
// fails is said in the journal, naming the module, the version and the seat, and the module is held whole: never composed partly.
// resolveAgain is how long a failed lookup stands before it is asked again; a pass is asked then.
const resolveAgain = time.Minute
// versionsVerb is the verb a lookup asks.
const versionsVerb = "versions"
// resolvedKey names one resolution: a module's build at a version.
type resolvedKey struct{ module, fp, version string }
// versionParts is a module's build at a version as resolved: its digests by artifact reference, or why not, and when that was found.
type versionParts struct {
parts map[string]string
failed string
at time.Time
}
// registryVersions is the answer of either seat's verb `versions`: the two seats answer the same shape.
type registryVersions struct {
Versions []struct {
Digest string `json:"digest"`
Tag string `json:"tag,omitempty"`
Version string `json:"version,omitempty"`
} `json:"versions"`
}
// seatOfKind is the registry seat an artifact of kind is numbered in, and whether one is (design 51 *The seats*).
func seatOfKind(kind string) (string, bool) {
switch kind {
case catalogue.ArtifactImage:
return containerimageregistry.Seat, true
case catalogue.ArtifactBundle, catalogue.ArtifactArchive:
return ociartifactregistry.Seat, true
}
return "", false
}
// registryName is the name an artifact of module is numbered under: `<module>/<artifact>`, or a package's name in its
// ecosystem as the entry gives it.
func registryName(module string, a meshcatalog.Artifact) string {
_, artifact, _ := strings.Cut(a.Ref, ":")
if a.Kind == catalogue.ArtifactPackage && a.Package != nil && a.Package.Name != "" {
return a.Package.Name
}
return module + "/" + artifact
}
// partsOf is the digest of each artifact of e for want w, by artifact reference, and the artifacts it names no digest for ("" when
// each has one). A want with parts gives them; a want with a version and no parts is resolved through the registry seats.
func (c *Composer) partsOf(module string, e meshcatalog.CatalogEntry, w meshwants.WantsByWantNodeModuleValue) (map[string]string, string) {
parts := w.Parts
if len(parts) == 0 && w.Version != "" && len(e.Artifacts) > 0 {
var why string
if parts, why = c.resolve(module, e, w); why != "" {
return nil, why
}
}
return parts, partsMissing(e, parts)
}
// partsMissing names the artifacts of e whose digest parts does not give; "" when each has one.
func partsMissing(e meshcatalog.CatalogEntry, parts map[string]string) string {
var out []string
for _, a := range e.Artifacts {
if parts[a.Ref] == "" {
out = append(out, a.Ref)
}
}
sort.Strings(out)
return strings.Join(out, ", ")
}
// resolve is the digests of module's build at w's version, from what is resolved already or asked of the seats now. why names
// what failed; a failure stands resolveAgain, then is asked again.
func (c *Composer) resolve(module string, e meshcatalog.CatalogEntry, w meshwants.WantsByWantNodeModuleValue) (map[string]string, string) {
key := resolvedKey{module, w.Fp, w.Version}
now := c.now()
c.mu.Lock()
r, ok := c.resolved[key]
c.mu.Unlock()
if ok && (r.failed == "" || now.Sub(r.at) < resolveAgain) {
return r.parts, r.failed
}
parts, failed := c.lookUp(module, e, w)
c.mu.Lock()
if c.resolved == nil {
c.resolved = map[resolvedKey]versionParts{}
}
// Only the resolutions of the builds a want names now are kept.
for k := range c.resolved {
if cur, wanted := c.wants[k.module]; k.module != module && (!wanted || cur.Fp != k.fp || cur.Version != k.version) {
delete(c.resolved, k)
}
}
c.resolved[key] = versionParts{parts: parts, failed: failed, at: now}
c.mu.Unlock()
if failed != "" {
time.AfterFunc(resolveAgain, c.askRehand)
return nil, failed
}
return parts, ""
}
// lookUp asks each artifact's digest at w's version of the seat of its kind. Every failure is said, naming the module, the version
// and the seat; any failure resolves nothing of the build.
func (c *Composer) lookUp(module string, e meshcatalog.CatalogEntry, w meshwants.WantsByWantNodeModuleValue) (map[string]string, string) {
parts := map[string]string{}
var failed []string
fail := func(seat, ref, why string) {
line := fmt.Sprintf("%s at version %s: the seat %s's verb versions resolves no digest of %s: %s, so the node holds %s and composes nothing of it",
module, w.Version, seat, ref, why, module)
c.sayOnceEach(line)
failed = append(failed, fmt.Sprintf("%s (seat %s: %s)", ref, seat, why))
}
var manifest *catalogue.Manifest
for _, a := range e.Artifacts {
_, artifact, _ := strings.Cut(a.Ref, ":")
if a.Kind == catalogue.ArtifactUpstream {
if manifest == nil {
_, raw, err := entryManifest(e)
m, derr := catalogue.Decode(raw)
if err != nil || derr != nil {
fail("(none)", a.Ref, "the entry's manifest cannot be read for the digest its upstream copy pins")
continue
}
manifest = &m
}
if d := pinnedDigest(*manifest, artifact); d != "" {
parts[a.Ref] = d
} else {
fail("(none)", a.Ref, "the manifest pins no digest of the upstream copy")
}
continue
}
seat, numbered := seatOfKind(a.Kind)
if !numbered {
fail("(none)", a.Ref, "no registry seat the node-engine reads numbers an artifact of kind "+a.Kind)
continue
}
digest, why := c.versionDigest(seat, registryName(module, a), w)
if why != "" {
fail(seat, a.Ref, why)
continue
}
parts[a.Ref] = digest
}
if len(failed) > 0 {
sort.Strings(failed)
return nil, "no digest is resolved at version " + w.Version + " for " + strings.Join(failed, ", ")
}
return parts, ""
}
// versionDigest is the digest the seat's verb versions answers for name at w's version (its version or its tag), the newest
// first: a version names one build (A-430a), so the version alone is matched.
func (c *Composer) versionDigest(seat, name string, w meshwants.WantsByWantNodeModuleValue) (string, string) {
if c.env.Registry == nil {
return "", "this node-engine reads no registry seat"
}
raw, err := c.env.Registry("seat:"+seat+"."+versionsVerb, map[string]string{"name": name})
if err != nil {
return "", fmt.Sprintf("the call for %s failed: %v", name, err)
}
answer, said, ok := buscalls.Unwrap(raw)
if !ok {
return "", fmt.Sprintf("the seat refused %s: %s", name, said)
}
var v registryVersions
if err := json.Unmarshal(answer, &v); err != nil {
return "", fmt.Sprintf("its answer for %s cannot be read: %v", name, err)
}
for _, a := range v.Versions {
if (a.Version == w.Version || a.Tag == w.Version) && a.Digest != "" {
return a.Digest, ""
}
}
return "", fmt.Sprintf("it holds no version %s of %s", w.Version, name)
}
// pinnedDigest is the digest the manifest's upstream artifact pins in its from (`<image>@sha256:<hex>`); "" when none.
func pinnedDigest(m catalogue.Manifest, artifact string) string {
if m.Build == nil {
return ""
}
for _, a := range m.Build.Artifacts {
if a.Name == artifact {
if _, d, ok := strings.Cut(a.From, "@"); ok && strings.HasPrefix(d, "sha256:") {
return d
}
}
}
return ""
}