// Package build is the builder's logic (novox/hq ADR 0453 clause 4, ADR 0478), apart from the bus: what a manifest asks // to be built and where, the claim of a build key, the choice of build nodes, the build modules' long steps and the run of // one build. The bus is reached through the small interfaces here, so each part is tested with fakes. package build import ( "fmt" "sort" "strings" "git.novox.be/novox/mesh-sdk/go/catalogue" "git.novox.be/novox/mesh-sdk/go/delivery" "git.novox.be/novox/mesh-sdk/go/delivery/buildrun" ) // ImageBuildModule is the build module that builds an image artifact (novox/hq ADR 0487): buildah, rootless, on the nodes that // run it. It has no toolchain language; the builder finds its nodes from the node-engine's have. const ImageBuildModule = "mesh-build-image" // NoImageBuilderPrefix opens the error of a build key failed because no node runs ImageBuildModule (ADR 0487 clause 5): such a // key is not built again as a new revision, only when a node reports the module (ClaimOptions.ImageBuilder) or the fingerprint // changes. const NoImageBuilderPrefix = "no image builder:" // Task is one artifact to build: its name and kind in the manifest, the build module that builds it and the language // of the toolchain it is built with. type Task struct { Artifact string Kind string Module string Language string // From is, for an upstream artifact, the reference its manifest pins, which the image build module copies (ADR 0498 // clause 4). From string } // Plan is what a manifest asks the builder to build, by artifact name. type Plan struct{ Tasks []Task } // Modules are the build modules the plan calls, sorted. func (p Plan) Modules() []string { seen := map[string]bool{} var out []string for _, t := range p.Tasks { if !seen[t.Module] { seen[t.Module] = true out = append(out, t.Module) } } sort.Strings(out) return out } // NeedsToolchain is whether any task the build module builds is built with a toolchain: a bundle is, an archive is not // (the indexer gives an archive no language, so a fingerprint names no toolchain for it; ADR 0478 clause 1). func (p Plan) NeedsToolchain(module string) bool { for _, t := range p.Tasks { if t.Module == module && t.Kind != catalogue.ArtifactArchive && t.Kind != catalogue.ArtifactImage && t.Kind != catalogue.ArtifactUpstream { return true } } return false } // Refused is the error of a plan the builder does not build: an artifact of a kind (or of a language) no build module // builds. It opens delivery.RefusedKind, so its key is not built again until the fingerprint changes. type Refused struct{ Artifact, What string } func (r *Refused) Error() string { return fmt.Sprintf("%s%s (artifact %s): no build module builds it; the builder builds Go and TypeScript bundles, archives, images, upstream copies and TypeScript packages", delivery.RefusedKind, r.What, r.Artifact) } // NotPinned is the error of a plan with an upstream artifact whose reference names no digest (ADR 0498 clause 4): it opens // delivery.NotPinned, so its key is not built again until the fingerprint changes. type NotPinned struct{ Artifact, From string } func (n *NotPinned) Error() string { return fmt.Sprintf("%s%s: %q names no digest (@sha256:), so what would be copied is whatever its tag means today", delivery.NotPinned, n.Artifact, n.From) } // PlanOf is the plan of a manifest (ADR 0478 clauses 1 and 2, ADR 0498 clauses 4 and 5): a Go bundle and an archive are // built by the Go build module, an image by the image build module (ADR 0487), which also copies an upstream image pinned // by its digest, and a TypeScript bundle or package by the Node one, each in the order of the artifacts' names. A manifest // with an upstream artifact pinned by no digest, or a bundle or a package of another language, or a kind no build module // builds, is refused as a whole, naming the first such artifact by name. A manifest with no artifact has an empty plan. func PlanOf(m catalogue.Manifest) (Plan, error) { if m.Build == nil { return Plan{}, nil } artifacts := append([]catalogue.Artifact(nil), m.Build.Artifacts...) sort.Slice(artifacts, func(i, j int) bool { return artifacts[i].Name < artifacts[j].Name }) var p Plan for _, a := range artifacts { switch { case a.Kind == catalogue.ArtifactArchive: p.Tasks = append(p.Tasks, Task{Artifact: a.Name, Kind: a.Kind, Module: buildrun.GoBuildModule, Language: "go"}) case a.Kind == catalogue.ArtifactBundle && a.Language == "go": p.Tasks = append(p.Tasks, Task{Artifact: a.Name, Kind: a.Kind, Module: buildrun.GoBuildModule, Language: "go"}) case a.Kind == catalogue.ArtifactBundle && a.Language == "typescript": p.Tasks = append(p.Tasks, Task{Artifact: a.Name, Kind: a.Kind, Module: buildrun.NodeBuildModule, Language: "typescript"}) case a.Kind == catalogue.ArtifactImage: p.Tasks = append(p.Tasks, Task{Artifact: a.Name, Kind: a.Kind, Module: ImageBuildModule}) case a.Kind == catalogue.ArtifactUpstream && !strings.Contains(a.From, "@sha256:"): return Plan{}, &NotPinned{Artifact: a.Name, From: a.From} case a.Kind == catalogue.ArtifactUpstream: p.Tasks = append(p.Tasks, Task{Artifact: a.Name, Kind: a.Kind, Module: ImageBuildModule, From: a.From}) case a.Kind == catalogue.ArtifactPackage && a.Language == "typescript": p.Tasks = append(p.Tasks, Task{Artifact: a.Name, Kind: a.Kind, Module: buildrun.NodeBuildModule, Language: "typescript"}) case a.Kind == catalogue.ArtifactBundle: return Plan{}, &Refused{Artifact: a.Name, What: fmt.Sprintf("bundle of language %q", a.Language)} case a.Kind == catalogue.ArtifactPackage: return Plan{}, &Refused{Artifact: a.Name, What: fmt.Sprintf("package of language %q", a.Language)} default: return Plan{}, &Refused{Artifact: a.Name, What: a.Kind} } } return p, nil }