Separate pipeline reference and binding resolution
This commit is contained in:
103
internal/framework/pipeline/generated_bindings.go
Normal file
103
internal/framework/pipeline/generated_bindings.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
func validateGeneratedBindings(pipelineID string, resolved ResolvedPipeline, catalog ModuleCatalog) error {
|
||||
stepIndex := make(map[string]int, len(resolved.Steps))
|
||||
laneIndex := make(map[generatedOutputKey]ResolvedArtifactLane)
|
||||
for index, step := range resolved.Steps {
|
||||
if _, exists := stepIndex[step.ID]; exists {
|
||||
return fmt.Errorf("pipeline %q step id %q is ambiguous", pipelineID, step.ID)
|
||||
}
|
||||
stepIndex[step.ID] = index
|
||||
for _, lane := range step.ArtifactLanes {
|
||||
laneIndex[generatedOutputKeyFor(step.ID, lane.ID)] = lane
|
||||
}
|
||||
}
|
||||
for consumerIndex, step := range resolved.Steps {
|
||||
for _, lane := range step.ArtifactLanes {
|
||||
for _, target := range []ResolvedReferenceTarget{lane.ExtractReferences, lane.MergeReferences, lane.NormalizeReferences} {
|
||||
slots, err := resolvedReferenceSlots(target, lane.ArtifactKind, catalog)
|
||||
if err != nil {
|
||||
return fmt.Errorf("pipeline %q step %q lane %q: %w", pipelineID, step.ID, lane.ID, err)
|
||||
}
|
||||
slotByName := make(map[string]contracts.ReferenceSlot, len(slots))
|
||||
for _, slot := range slots {
|
||||
slotByName[slot.Name] = slot
|
||||
}
|
||||
for _, binding := range target.Bindings {
|
||||
if binding.Artifact == nil {
|
||||
continue
|
||||
}
|
||||
context := generatedBindingContext{pipelineID: pipelineID, stepID: step.ID, laneID: lane.ID, consumerIndex: consumerIndex, stage: target.Stage, steps: stepIndex, lanes: laneIndex, slots: slotByName, catalog: catalog}
|
||||
if err := validateGeneratedBinding(context, binding); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type generatedBindingContext struct {
|
||||
pipelineID, stepID, laneID string
|
||||
consumerIndex int
|
||||
stage ModuleStage
|
||||
steps map[string]int
|
||||
lanes map[generatedOutputKey]ResolvedArtifactLane
|
||||
slots map[string]contracts.ReferenceSlot
|
||||
catalog ModuleCatalog
|
||||
}
|
||||
|
||||
func validateGeneratedBinding(context generatedBindingContext, binding ReferenceBinding) error {
|
||||
producerStep, ok := context.steps[strings.TrimSpace(binding.Artifact.Step)]
|
||||
if !ok {
|
||||
return fmt.Errorf("pipeline %q step %q lane %q %s reference slot %q producer step %q is not declared", context.pipelineID, context.stepID, context.laneID, context.stage, binding.SlotName, binding.Artifact.Step)
|
||||
}
|
||||
if producerStep >= context.consumerIndex {
|
||||
return fmt.Errorf("pipeline %q step %q lane %q reference slot %q producer %q must be an earlier step", context.pipelineID, context.stepID, context.laneID, binding.SlotName, binding.Artifact.Step)
|
||||
}
|
||||
producer, ok := context.lanes[generatedOutputKeyFor(strings.TrimSpace(binding.Artifact.Step), strings.TrimSpace(binding.Artifact.Lane))]
|
||||
if !ok {
|
||||
return fmt.Errorf("pipeline %q step %q lane %q reference slot %q producer lane %q is not selected", context.pipelineID, context.stepID, context.laneID, binding.SlotName, binding.Artifact.Lane)
|
||||
}
|
||||
slot, ok := context.slots[strings.TrimSpace(binding.SlotName)]
|
||||
if !ok {
|
||||
return fmt.Errorf("pipeline %q step %q lane %q %s reference slot %q is not declared", context.pipelineID, context.stepID, context.laneID, context.stage, binding.SlotName)
|
||||
}
|
||||
if len(slot.AcceptedArtifactKinds) == 0 {
|
||||
return fmt.Errorf("pipeline %q step %q lane %q reference slot %q does not accept generated artifacts", context.pipelineID, context.stepID, context.laneID, binding.SlotName)
|
||||
}
|
||||
accepted := false
|
||||
for _, kind := range slot.AcceptedArtifactKinds {
|
||||
if kind == producer.ArtifactKind {
|
||||
accepted = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !accepted {
|
||||
return fmt.Errorf("pipeline %q step %q lane %q reference slot %q does not accept artifact kind %q", context.pipelineID, context.stepID, context.laneID, binding.SlotName, producer.ArtifactKind)
|
||||
}
|
||||
codecSpec, ok := context.catalog.ArtifactCodecs.Spec(producer.ArtifactKind)
|
||||
if !ok {
|
||||
return fmt.Errorf("pipeline %q step %q lane %q producer %q artifact codec %q is not registered", context.pipelineID, context.stepID, context.laneID, producer.ID, producer.ArtifactKind)
|
||||
}
|
||||
if !referenceMediaTypeAccepted(codecSpec.MediaType, slot.AcceptedMediaTypes) {
|
||||
return fmt.Errorf("pipeline %q step %q lane %q reference slot %q does not accept producer media type %q", context.pipelineID, context.stepID, context.laneID, binding.SlotName, codecSpec.MediaType)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func resolvedReferenceSlots(target ResolvedReferenceTarget, artifactKind contracts.ArtifactKind, catalog ModuleCatalog) ([]contracts.ReferenceSlot, error) {
|
||||
spec, err := referenceTargetSpec(target, artifactKind, catalog)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return contracts.CloneReferenceSlots(spec.ReferenceSlots), nil
|
||||
}
|
||||
Reference in New Issue
Block a user