Skip to content

Commit

Permalink
chore(directives): wire inputs into engine
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Dec 13, 2024
1 parent e6610db commit 0102ace
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/controller/promotions/promotions.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ func (r *reconciler) promote(
Kind: step.Uses,
Alias: step.As,
Retry: step.Retry,
Inputs: step.Inputs,
Config: step.Config.Raw,
}
}
Expand Down
70 changes: 70 additions & 0 deletions internal/directives/outputs_composer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package directives

import (
"context"
"fmt"

"github.com/xeipuuv/gojsonschema"

kargoapi "github.com/akuity/kargo/api/v1alpha1"
)

func init() {
builtins.RegisterPromotionStepRunner(newOutputsComposer(), nil)
}

// outputsComposer is an implementation of the PromotionStepRunner interface that
// allows composing outputs from previous steps into new outputs.
type outputsComposer struct {
schemaLoader gojsonschema.JSONLoader
}

// newOutputsComposer returns an implementation of the PromotionStepRunner
// interface that composes outputs from previous steps into new outputs.
func newOutputsComposer() PromotionStepRunner {
r := &outputsComposer{}
r.schemaLoader = getConfigSchemaLoader(r.Name())
return r
}

// Name implements the PromotionStepRunner interface.
func (c *outputsComposer) Name() string {
return "compose-outputs"
}

// RunPromotionStep implements the PromotionStepRunner interface.
func (c *outputsComposer) RunPromotionStep(
ctx context.Context,
stepCtx *PromotionStepContext,
) (PromotionStepResult, error) {
// Validate the configuration against the JSON Schema.
if err := validate(c.schemaLoader, gojsonschema.NewGoLoader(stepCtx.Config), c.Name()); err != nil {
return PromotionStepResult{Status: kargoapi.PromotionPhaseErrored}, err
}

// Convert the configuration into a typed object.
cfg, err := ConfigToStruct[ComposeOutputs](stepCtx.Config)
if err != nil {
return PromotionStepResult{Status: kargoapi.PromotionPhaseErrored},
fmt.Errorf("could not convert config into %s config: %w", c.Name(), err)
}

return c.runPromotionStep(ctx, stepCtx, cfg)
}

func (c *outputsComposer) runPromotionStep(
ctx context.Context,
stepCtx *PromotionStepContext,
cfg ComposeOutputs,
) (PromotionStepResult, error) {
for newKey, f := range cfg.Fields {
stepState, ok := stepCtx.SharedState[f.FromStep]
if !ok {
return PromotionStepResult{Status: kargoapi.PromotionPhaseFailed},
fmt.Errorf("unable to compose output %q from step %q: step not found", newKey, f.FromStep)
}


}
return PromotionStepResult{Status: kargoapi.PromotionPhaseSucceeded}, nil
}
14 changes: 14 additions & 0 deletions internal/directives/promotions.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ type PromotionStep struct {
Alias string
// Retry is the retry configuration for the PromotionStep.
Retry *kargoapi.PromotionStepRetry
// Inputs is a list of inputs to be made available to the Config of this step.
Inputs []kargoapi.PromotionStepInput
// Config is an opaque JSON to be passed to the PromotionStepRunner executing
// this step.
Config []byte
Expand Down Expand Up @@ -144,6 +146,8 @@ func (s *PromotionStep) GetConfig(
return nil, err
}

inputs := s.GetInputs()

evaledCfgJSON, err := expressions.EvaluateJSONTemplate(
s.Config,
map[string]any{
Expand All @@ -153,6 +157,7 @@ func (s *PromotionStep) GetConfig(
"stage": promoCtx.Stage,
},
"vars": vars,
"inputs": inputs,
"secrets": promoCtx.Secrets,
"outputs": state,
},
Expand Down Expand Up @@ -212,6 +217,15 @@ func (s *PromotionStep) GetVars(promoCtx PromotionContext) (map[string]any, erro
return vars, nil
}

// GetInputs returns the inputs of the PromotionStep as a map.
func (s *PromotionStep) GetInputs() map[string]any {
inputs := make(map[string]any, len(s.Inputs))
for _, i := range s.Inputs {
inputs[i.Name] = i.Value
}
return inputs
}

// PromotionResult is the result of a user-defined promotion process executed by
// the Engine. It aggregates the status and output of the individual
// PromotionStepResults returned by the PromotionStepRunner executing each
Expand Down

0 comments on commit 0102ace

Please sign in to comment.