Skip to content

Commit

Permalink
fix: ensure (step) vars can access outputs
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Dec 17, 2024
1 parent ccfe327 commit f766300
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 34 deletions.
90 changes: 57 additions & 33 deletions internal/directives/promotions.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,37 +143,34 @@ func (s *PromotionStep) GetConfig(
return nil, nil
}

vars, err := s.GetVars(promoCtx)
vars, err := s.GetVars(promoCtx, state)
if err != nil {
return nil, err
}

// If the alias has a namespace, filter the state to only include keys that
// belong to that namespace.
outputs := state
if namespace := getAliasNamespace(s.Alias); namespace != "" {
outputs = make(State)
for k, v := range state.DeepCopy() {
if getAliasNamespace(k) == namespace {
// Strip the namespace from the key before adding it to the
// filtered outputs.
outputs[k[len(namespace)+2:]] = v
}
env := map[string]any{
"ctx": map[string]any{
"project": promoCtx.Project,
"promotion": promoCtx.Promotion,
"stage": promoCtx.Stage,
},
"vars": vars,
"secrets": promoCtx.Secrets,
"outputs": state,
}

// If the alias has a namespace prefix, we are dealing with a step which is
// inflated from a task. In this case, we need to add "task" specific
// outputs to the environment.
if taskOutput := s.getTaskOutputs(state); taskOutput != nil {
env["task"] = map[string]any{
"outputs": taskOutput,
}
}

evaledCfgJSON, err := expressions.EvaluateJSONTemplate(
s.Config,
map[string]any{
"ctx": map[string]any{
"project": promoCtx.Project,
"promotion": promoCtx.Promotion,
"stage": promoCtx.Stage,
},
"vars": vars,
"secrets": promoCtx.Secrets,
"outputs": outputs,
},
env,
expr.Function("warehouse", warehouseFunc, new(func(string) kargoapi.FreightOrigin)),
expr.Function(
"commitFrom",
Expand Down Expand Up @@ -208,7 +205,10 @@ func (s *PromotionStep) GetConfig(

// GetVars returns the variables defined in the PromotionStep. The variables are
// evaluated in the context of the provided PromotionContext.
func (s *PromotionStep) GetVars(promoCtx PromotionContext) (map[string]any, error) {
func (s *PromotionStep) GetVars(
promoCtx PromotionContext,
state State,
) (map[string]any, error) {
var rawVars = make(map[string]string, len(promoCtx.Vars))
for _, v := range promoCtx.Vars {
rawVars[v.Name] = v.Value
Expand All @@ -217,19 +217,27 @@ func (s *PromotionStep) GetVars(promoCtx PromotionContext) (map[string]any, erro
rawVars[v.Name] = v.Value
}

taskOutput := s.getTaskOutputs(state)

vars := make(map[string]any, len(rawVars))
for k, v := range rawVars {
newVar, err := expressions.EvaluateTemplate(
v,
map[string]any{
"ctx": map[string]any{
"project": promoCtx.Project,
"promotion": promoCtx.Promotion,
"stage": promoCtx.Stage,
},
"vars": vars,
env := map[string]any{
"ctx": map[string]any{
"project": promoCtx.Project,
"promotion": promoCtx.Promotion,
"stage": promoCtx.Stage,
},
)
"vars": vars,
"outputs": state,
}

if taskOutput != nil {
env["task"] = map[string]any{
"outputs": taskOutput,
}
}

newVar, err := expressions.EvaluateTemplate(v, env)
if err != nil {
return nil, fmt.Errorf("error pre-processing promotion variable %q: %w", k, err)
}
Expand All @@ -238,6 +246,22 @@ func (s *PromotionStep) GetVars(promoCtx PromotionContext) (map[string]any, erro
return vars, nil
}

// getTaskOutputs returns the outputs of a task that are relevant to the current
// step. This is useful when a step is inflated from a task and needs to access
// the outputs of that task.
func (s *PromotionStep) getTaskOutputs(state State) State {
if namespace := getAliasNamespace(s.Alias); namespace != "" {
taskOutputs := make(State)
for k, v := range state.DeepCopy() {
if getAliasNamespace(k) == namespace {
taskOutputs[k[len(namespace)+2:]] = v
}
}
return taskOutputs
}
return nil
}

// 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
2 changes: 1 addition & 1 deletion internal/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func RunningPromotionsByArgoCDApplications(
Promotion: promo.Name,
Vars: promo.Spec.Vars,
}
vars, err := dirStep.GetVars(promoCtx)
vars, err := dirStep.GetVars(promoCtx, promoCtx.State)
if err != nil {
logger.Error(
err,
Expand Down

0 comments on commit f766300

Please sign in to comment.