-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(directives): add
compose-output
directive
Signed-off-by: Hidde Beydals <[email protected]>
- Loading branch information
Showing
4 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package directives | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"maps" | ||
|
||
"github.com/xeipuuv/gojsonschema" | ||
|
||
kargoapi "github.com/akuity/kargo/api/v1alpha1" | ||
) | ||
|
||
func init() { | ||
builtins.RegisterPromotionStepRunner(newOutputComposer(), nil) | ||
} | ||
|
||
// outputComposer is an implementation of the PromotionStepRunner interface | ||
// that allows composing outputs from previous steps into new outputs. | ||
// | ||
// It works based on the PromotionStepContext.Config field allowing to an | ||
// arbitrary number of key-value pairs to be exported as outputs. | ||
// Because the values are allowed to be expressions and can contain | ||
// references to outputs from previous steps, this allows for remapping | ||
// the outputs of previous steps to new keys, or even combining them | ||
// into new structures. | ||
type outputComposer struct { | ||
schemaLoader gojsonschema.JSONLoader | ||
} | ||
|
||
// newOutputComposer returns an implementation of the PromotionStepRunner | ||
// interface that composes output from previous steps into new output. | ||
func newOutputComposer() PromotionStepRunner { | ||
r := &outputComposer{} | ||
r.schemaLoader = getConfigSchemaLoader(r.Name()) | ||
return r | ||
} | ||
|
||
// Name implements the PromotionStepRunner interface. | ||
func (c *outputComposer) Name() string { | ||
return "compose-output" | ||
} | ||
|
||
// RunPromotionStep implements the PromotionStepRunner interface. | ||
func (c *outputComposer) RunPromotionStep( | ||
_ 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[ComposeOutput](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(cfg) | ||
} | ||
|
||
func (c *outputComposer) runPromotionStep( | ||
cfg ComposeOutput, | ||
) (PromotionStepResult, error) { | ||
return PromotionStepResult{ | ||
Status: kargoapi.PromotionPhaseSucceeded, | ||
Output: maps.Clone(cfg), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"title": "ComposeOutput", | ||
"type": "object", | ||
"minProperties": 1 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"title": "ComposeOutput", | ||
"type": "object", | ||
"minProperties": 1 | ||
} |