Share catalog-independent pipeline layout

This commit is contained in:
2026-11-26 02:33:25 +00:00
parent ad357db22d
commit 39e578dc60
8 changed files with 215 additions and 122 deletions

View File

@@ -0,0 +1,22 @@
package config
import (
"testing"
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
)
func TestValidateUsesMapIdentityWithoutMutatingProfile(t *testing.T) {
cfg := Default()
cfg.Pipelines[" demo "] = pipeline.PipelineProfile{Artifacts: map[string]pipeline.ArtifactLaneProfile{" lane ": {}}}
if err := cfg.Validate(); err != nil {
t.Fatal(err)
}
profile := cfg.Pipelines[" demo "]
if profile.ID != "" || len(profile.Artifacts) != 1 {
t.Fatalf("validation mutated profile: %#v", profile)
}
if _, ok := profile.Artifacts[" lane "]; !ok {
t.Fatalf("validation changed lane identity: %#v", profile.Artifacts)
}
}

View File

@@ -57,45 +57,20 @@ func validatePipelineProfiles(profiles map[string]pipeline.PipelineProfile) erro
if err := validateReferenceMap(id, "", profile.References); err != nil {
return err
}
explicitSteps := profile.Steps != nil
if len(profile.Artifacts) > 0 && explicitSteps {
return fmt.Errorf("pipeline %q must not declare both artifacts and steps", id)
layoutProfile := profile
layoutProfile.ID = id
layout, err := pipeline.LayoutPipeline(layoutProfile)
if err != nil {
return err
}
steps := profile.Steps
if !explicitSteps {
steps = []pipeline.PipelineStepProfile{{ID: "default", Artifacts: profile.Artifacts}}
}
if explicitSteps && len(steps) == 0 {
return fmt.Errorf("pipeline %q must declare at least one ordered step", id)
}
seenSteps := make(map[string]struct{}, len(steps))
seenLanes := make(map[string]struct{})
for index, step := range steps {
stepID := strings.TrimSpace(step.ID)
if stepID == "" {
return fmt.Errorf("pipeline %q step[%d] id must not be empty", id, index)
}
if _, ok := seenSteps[stepID]; ok {
return fmt.Errorf("pipeline %q step id %q is duplicated after trimming", id, stepID)
}
seenSteps[stepID] = struct{}{}
if explicitSteps {
if err := validateReferenceMapForContext(id, "", "step "+stepID, step.References, true); err != nil {
for _, step := range layout.Steps {
if layout.ExplicitSteps {
if err := validateReferenceMapForContext(id, "", "step "+step.ID, step.References, true); err != nil {
return err
}
}
for rawLaneID, lane := range step.Artifacts {
laneID := strings.TrimSpace(rawLaneID)
if laneID == "" {
return fmt.Errorf("pipeline %q artifact lane id must not be empty", id)
}
if _, ok := seenLanes[laneID]; ok {
if !explicitSteps {
return fmt.Errorf("pipeline %q artifact lane id %q is duplicated after trimming", id, laneID)
}
return fmt.Errorf("pipeline %q artifact lane id %q is duplicated across steps", id, laneID)
}
seenLanes[laneID] = struct{}{}
for _, laneID := range step.LaneIDs {
lane := step.Artifacts[laneID]
if err := validateReferenceMapForContext(id, laneID, "", lane.References, true); err != nil {
return err
}

View File

@@ -0,0 +1,78 @@
package pipeline
import (
"fmt"
"sort"
"strings"
)
// PipelineLayout is the catalog-independent ordered structure of a profile.
type PipelineLayout struct {
ID string
ExplicitSteps bool
Steps []PipelineLayoutStep
ConfiguredLaneIDs []string
}
type PipelineLayoutStep struct {
ID string
References map[string]ReferenceSource
Artifacts map[string]ArtifactLaneProfile
LaneIDs []string
}
// LayoutPipeline copies and normalizes identities without selecting modules or
// interpreting references. Empty steps remain representable for config
// composition; complete resolution requires each selected step to have a lane.
func LayoutPipeline(profile PipelineProfile) (PipelineLayout, error) {
profile = ClonePipelineProfile(profile)
id := strings.TrimSpace(profile.ID)
if id == "" {
return PipelineLayout{}, fmt.Errorf("pipeline id must not be empty")
}
explicit := profile.Steps != nil
if explicit && len(profile.Artifacts) > 0 {
return PipelineLayout{}, fmt.Errorf("pipeline %q must not declare both artifacts and steps", id)
}
steps := profile.Steps
if !explicit {
steps = []PipelineStepProfile{{ID: "default", Artifacts: profile.Artifacts}}
}
if explicit && len(steps) == 0 {
return PipelineLayout{}, fmt.Errorf("pipeline %q must declare at least one ordered step", id)
}
layout := PipelineLayout{ID: id, ExplicitSteps: explicit}
seenSteps := make(map[string]struct{}, len(steps))
seenLanes := make(map[string]string)
for index, step := range steps {
stepID := strings.TrimSpace(step.ID)
if stepID == "" {
return PipelineLayout{}, fmt.Errorf("pipeline %q step[%d] id must not be empty", id, index)
}
if _, exists := seenSteps[stepID]; exists {
return PipelineLayout{}, fmt.Errorf("pipeline %q step id %q is duplicated after trimming", id, stepID)
}
seenSteps[stepID] = struct{}{}
ordered := PipelineLayoutStep{ID: stepID, References: step.References, Artifacts: make(map[string]ArtifactLaneProfile, len(step.Artifacts))}
for rawLaneID, lane := range step.Artifacts {
laneID := strings.TrimSpace(rawLaneID)
if laneID == "" {
return PipelineLayout{}, fmt.Errorf("pipeline %q artifact lane id must not be empty", id)
}
if _, exists := ordered.Artifacts[laneID]; exists {
return PipelineLayout{}, fmt.Errorf("pipeline %q artifact lane id %q is duplicated after trimming", id, laneID)
}
if previous, exists := seenLanes[laneID]; exists {
return PipelineLayout{}, fmt.Errorf("pipeline %q artifact lane id %q is duplicated across steps %q and %q", id, laneID, previous, stepID)
}
seenLanes[laneID] = stepID
ordered.Artifacts[laneID] = lane
ordered.LaneIDs = append(ordered.LaneIDs, laneID)
layout.ConfiguredLaneIDs = append(layout.ConfiguredLaneIDs, laneID)
}
sort.Strings(ordered.LaneIDs)
layout.Steps = append(layout.Steps, ordered)
}
sort.Strings(layout.ConfiguredLaneIDs)
return layout, nil
}

View File

@@ -0,0 +1,74 @@
package pipeline
import (
"reflect"
"strings"
"testing"
)
func TestLayoutPipelineOrdersAndDetachesFlatProfile(t *testing.T) {
profile := PipelineProfile{ID: " demo ", References: map[string]ReferenceSource{"top": {Path: "top.txt"}}, Artifacts: map[string]ArtifactLaneProfile{
" z ": {Extract: ModuleBinding{Module: " producer ", Options: map[string]any{"items": []string{"original"}}}},
" a ": {References: map[string]ReferenceSource{"source": {Artifact: &ArtifactReference{Step: " prior ", Lane: " lane "}}}},
}}
layout, err := LayoutPipeline(profile)
if err != nil {
t.Fatal(err)
}
if layout.ID != "demo" || layout.ExplicitSteps || len(layout.Steps) != 1 || layout.Steps[0].ID != "default" || !reflect.DeepEqual(layout.Steps[0].LaneIDs, []string{"a", "z"}) || !reflect.DeepEqual(layout.ConfiguredLaneIDs, []string{"a", "z"}) {
t.Fatalf("layout = %#v", layout)
}
lane := layout.Steps[0].Artifacts["a"]
lane.References["source"].Artifact.Step = "changed"
layout.Steps[0].Artifacts["z"].Extract.Options["items"].([]string)[0] = "changed"
if profile.ID != " demo " || profile.Artifacts[" a "].References["source"].Artifact.Step != " prior " || profile.Artifacts[" z "].Extract.Options["items"].([]string)[0] != "original" {
t.Fatal("layout mutated profile")
}
}
func TestLayoutPipelinePreservesExplicitOrderAndReferences(t *testing.T) {
profile := PipelineProfile{ID: "demo", Steps: []PipelineStepProfile{
{ID: " first ", References: map[string]ReferenceSource{"rules": {Path: " rules.txt "}}, Artifacts: map[string]ArtifactLaneProfile{" z ": {}, " a ": {}}},
{ID: " second ", References: map[string]ReferenceSource{"generated": {Artifact: &ArtifactReference{Step: "first", Lane: "a"}}}, Artifacts: map[string]ArtifactLaneProfile{" b ": {}}},
}}
layout, err := LayoutPipeline(profile)
if err != nil {
t.Fatal(err)
}
if !layout.ExplicitSteps || len(layout.Steps) != 2 || layout.Steps[0].ID != "first" || layout.Steps[1].ID != "second" || !reflect.DeepEqual(layout.Steps[0].LaneIDs, []string{"a", "z"}) || !reflect.DeepEqual(layout.ConfiguredLaneIDs, []string{"a", "b", "z"}) || layout.Steps[0].References["rules"].Path != " rules.txt " || layout.Steps[1].References["generated"].Artifact.Lane != "a" {
t.Fatalf("layout = %#v", layout)
}
layout.Steps[1].References["generated"].Artifact.Lane = "changed"
if profile.Steps[1].References["generated"].Artifact.Lane != "a" {
t.Fatal("layout references alias profile")
}
}
func TestLayoutPipelineRejectsInvalidIdentitiesAndShapes(t *testing.T) {
for _, test := range []struct {
name string
profile PipelineProfile
want string
}{
{"missing identity", PipelineProfile{}, "pipeline id"},
{"mixed", PipelineProfile{ID: "demo", Artifacts: map[string]ArtifactLaneProfile{"a": {}}, Steps: []PipelineStepProfile{{ID: "one"}}}, "both artifacts and steps"},
{"empty explicit", PipelineProfile{ID: "demo", Steps: []PipelineStepProfile{}}, "at least one ordered step"},
{"blank step", PipelineProfile{ID: "demo", Steps: []PipelineStepProfile{{ID: " "}}}, "step[0] id"},
{"duplicate steps", PipelineProfile{ID: "demo", Steps: []PipelineStepProfile{{ID: "a"}, {ID: " a "}}}, "step id"},
{"blank lane", PipelineProfile{ID: "demo", Artifacts: map[string]ArtifactLaneProfile{" ": {}}}, "lane id"},
{"duplicate lane", PipelineProfile{ID: "demo", Artifacts: map[string]ArtifactLaneProfile{"a": {}, " a ": {}}}, "duplicated after trimming"},
{"duplicate across steps", PipelineProfile{ID: "demo", Steps: []PipelineStepProfile{{ID: "one", Artifacts: map[string]ArtifactLaneProfile{"a": {}}}, {ID: "two", Artifacts: map[string]ArtifactLaneProfile{" a ": {}}}}}, "duplicated across steps"},
} {
t.Run(test.name, func(t *testing.T) {
if _, err := LayoutPipeline(test.profile); err == nil || !strings.Contains(err.Error(), test.want) {
t.Fatalf("LayoutPipeline() error = %v, want %q", err, test.want)
}
})
}
for _, profile := range []PipelineProfile{{ID: "demo"}, {ID: "demo", Steps: []PipelineStepProfile{{ID: "empty"}}}} {
layout, err := LayoutPipeline(profile)
if err != nil || len(layout.Steps) != 1 || len(layout.Steps[0].LaneIDs) != 0 {
t.Fatalf("empty composable layout = %#v, %v", layout, err)
}
}
}

View File

@@ -344,32 +344,16 @@ func Binding(module string) ModuleBinding {
}
func ResolvePipeline(profile PipelineProfile, options ResolveOptions, catalog ModuleCatalog) (ResolvedPipeline, error) {
pipelineID := strings.TrimSpace(profile.ID)
if pipelineID == "" {
return ResolvedPipeline{}, fmt.Errorf("pipeline id must not be empty")
}
explicitSteps := profile.Steps != nil
if len(profile.Artifacts) > 0 && explicitSteps {
return ResolvedPipeline{}, fmt.Errorf("pipeline %q must not declare both artifacts and steps", pipelineID)
}
if explicitSteps && len(options.Only) > 0 {
return ResolvedPipeline{}, fmt.Errorf("pipeline %q lane selection is not supported for explicit ordered steps", pipelineID)
}
steps := profile.Steps
if !explicitSteps {
steps = []PipelineStepProfile{{ID: "default", Artifacts: profile.Artifacts}}
}
if explicitSteps && len(steps) == 0 {
return ResolvedPipeline{}, fmt.Errorf("pipeline %q must declare at least one ordered step", pipelineID)
}
if len(steps) == 0 {
return ResolvedPipeline{}, fmt.Errorf("pipeline %q must declare at least one artifact lane", pipelineID)
}
configuredLaneIDs, err := configuredProfileLaneIDs(pipelineID, steps)
layout, err := LayoutPipeline(profile)
if err != nil {
return ResolvedPipeline{}, err
}
pipelineID, explicitSteps := layout.ID, layout.ExplicitSteps
if explicitSteps && len(options.Only) > 0 {
return ResolvedPipeline{}, fmt.Errorf("pipeline %q lane selection is not supported for explicit ordered steps", pipelineID)
}
steps := layout.Steps
configuredLaneIDs := layout.ConfiguredLaneIDs
input, err := resolveBinding(profile.Input, "", fmt.Sprintf("pipeline %q input reference slot", pipelineID))
if err != nil {
@@ -408,9 +392,9 @@ func ResolvePipeline(profile PipelineProfile, options ResolveOptions, catalog Mo
}
} else {
allLanes := make(map[string]ArtifactLaneProfile)
for _, step := range profile.Steps {
for _, step := range layout.Steps {
for laneID, lane := range step.Artifacts {
allLanes[strings.TrimSpace(laneID)] = lane
allLanes[laneID] = lane
}
}
if err := validatePipelineReferenceDefaults(pipelineID, profile.References, chunkSpec, allLanes, catalog); err != nil {
@@ -455,23 +439,14 @@ func ResolvePipeline(profile PipelineProfile, options ResolveOptions, catalog Mo
}
resolved.ValidatorChains = append(resolved.ValidatorChains, chunkValidatorChain)
outputCapabilities := capabilities.clone()
seenResolvedLanes := make(map[string]string)
seenResolvedSteps := make(map[string]struct{}, len(steps))
for _, step := range steps {
stepID := strings.TrimSpace(step.ID)
if stepID == "" {
return ResolvedPipeline{}, fmt.Errorf("pipeline %q step id must not be empty", pipelineID)
}
if _, exists := seenResolvedSteps[stepID]; exists {
return ResolvedPipeline{}, fmt.Errorf("pipeline %q step id %q is duplicated after trimming", pipelineID, stepID)
}
seenResolvedSteps[stepID] = struct{}{}
stepID := step.ID
laneOptions := ResolveOptions{}
if !explicitSteps {
laneOptions = options
}
lanesByID, selectedLaneIDs, err := selectedArtifactLanes(pipelineID, step.Artifacts, laneOptions)
lanesByID := step.Artifacts
selectedLaneIDs, err := selectedLayoutLaneIDs(pipelineID, step, laneOptions)
if err != nil {
return ResolvedPipeline{}, err
}
@@ -487,10 +462,6 @@ func ResolvePipeline(profile PipelineProfile, options ResolveOptions, catalog Mo
stepReferences := mergeReferenceMaps(profile.References, step.References)
resolvedStep := ResolvedPipelineStep{ID: stepID}
for _, laneID := range selectedLaneIDs {
if previousStep, exists := seenResolvedLanes[laneID]; exists {
return ResolvedPipeline{}, fmt.Errorf("pipeline %q artifact lane id %q is duplicated across steps %q and %q", pipelineID, laneID, previousStep, stepID)
}
seenResolvedLanes[laneID] = stepID
laneProfile := lanesByID[laneID]
lane, validatorChains, laneCapabilities, err := resolveArtifactLane(pipelineID, stepID, laneID, laneProfile, stepReferences, options, capabilities, catalog)
if err != nil {
@@ -532,29 +503,6 @@ func ResolvePipeline(profile PipelineProfile, options ResolveOptions, catalog Mo
return resolved, nil
}
// configuredProfileLaneIDs validates the complete configured lane identity
// set before invocation-level selection removes legacy-profile lanes.
func configuredProfileLaneIDs(pipelineID string, steps []PipelineStepProfile) ([]string, error) {
seen := make(map[string]string)
var laneIDs []string
for _, step := range steps {
_, ids, err := selectedArtifactLanes(pipelineID, step.Artifacts, ResolveOptions{})
if err != nil {
return nil, err
}
stepID := strings.TrimSpace(step.ID)
for _, laneID := range ids {
if previous, ok := seen[laneID]; ok {
return nil, fmt.Errorf("pipeline %q artifact lane id %q is duplicated across steps %q and %q", pipelineID, laneID, previous, stepID)
}
seen[laneID] = stepID
laneIDs = append(laneIDs, laneID)
}
}
sort.Strings(laneIDs)
return laneIDs, nil
}
func resolveArtifactLane(
pipelineID string,
stepID string,
@@ -1455,36 +1403,19 @@ func resolveBindings(bindings []ModuleBinding, defaultModule string, referenceSl
return resolved, nil
}
func selectedArtifactLanes(pipelineID string, artifacts map[string]ArtifactLaneProfile, options ResolveOptions) (map[string]ArtifactLaneProfile, []string, error) {
lanesByID := make(map[string]ArtifactLaneProfile, len(artifacts))
for rawLaneID, lane := range artifacts {
laneID := strings.TrimSpace(rawLaneID)
if laneID == "" {
return nil, nil, fmt.Errorf("pipeline %q artifact lane id must not be empty", pipelineID)
}
if _, ok := lanesByID[laneID]; ok {
return nil, nil, fmt.Errorf("pipeline %q artifact lane %q is duplicated after trimming", pipelineID, laneID)
}
lanesByID[laneID] = lane
}
func selectedLayoutLaneIDs(pipelineID string, step PipelineLayoutStep, options ResolveOptions) ([]string, error) {
if len(options.Only) == 0 {
keys := make([]string, 0, len(lanesByID))
for laneID := range lanesByID {
keys = append(keys, laneID)
}
sort.Strings(keys)
return lanesByID, keys, nil
return step.LaneIDs, nil
}
selected := make(map[string]struct{}, len(options.Only))
for _, rawLaneID := range options.Only {
laneID := strings.TrimSpace(rawLaneID)
if laneID == "" {
return nil, nil, fmt.Errorf("pipeline %q selected artifact lane id must not be empty", pipelineID)
return nil, fmt.Errorf("pipeline %q selected artifact lane id must not be empty", pipelineID)
}
if _, ok := lanesByID[laneID]; !ok {
return nil, nil, fmt.Errorf("pipeline %q selected artifact lane %q is not declared", pipelineID, laneID)
if _, ok := step.Artifacts[laneID]; !ok {
return nil, fmt.Errorf("pipeline %q selected artifact lane %q is not declared", pipelineID, laneID)
}
selected[laneID] = struct{}{}
}
@@ -1494,7 +1425,7 @@ func selectedArtifactLanes(pipelineID string, artifacts map[string]ArtifactLaneP
keys = append(keys, laneID)
}
sort.Strings(keys)
return lanesByID, keys, nil
return keys, nil
}
func resolvedPipelineDigest(resolved ResolvedPipeline) (string, error) {