Skip to content

Commit

Permalink
feat: wire directives into promotion reconciler
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <hidde@hhh.computer>
  • Loading branch information
hiddeco committed Sep 17, 2024
1 parent 05af50c commit e6d3458
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
14 changes: 14 additions & 0 deletions api/v1alpha1/promotion_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1alpha1
import (
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)

type PromotionPhase string
Expand Down Expand Up @@ -100,6 +101,19 @@ type PromotionStep struct {
Config *apiextensionsv1.JSON `json:"config,omitempty" protobuf:"bytes,3,opt,name=config"`
}

// GetConfig returns the Config field as unmarshalled YAML.
func (s *PromotionStep) GetConfig() map[string]any {
if s.Config == nil {
return nil
}

var config map[string]any
if err := yaml.Unmarshal(s.Config.Raw, &config); err != nil {
return nil
}
return config
}

// PromotionStatus describes the current state of the transition represented by
// a Promotion.
type PromotionStatus struct {
Expand Down
50 changes: 41 additions & 9 deletions internal/controller/promotions/promotions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/akuity/kargo/internal/controller/promotion"
"github.com/akuity/kargo/internal/controller/runtime"
"github.com/akuity/kargo/internal/credentials"
"github.com/akuity/kargo/internal/directives"
"github.com/akuity/kargo/internal/kargo"
"github.com/akuity/kargo/internal/kubeclient"
libEvent "github.com/akuity/kargo/internal/kubernetes/event"
Expand Down Expand Up @@ -53,8 +54,9 @@ func ReconcilerConfigFromEnv() ReconcilerConfig {

// reconciler reconciles Promotion resources.
type reconciler struct {
kargoClient client.Client
promoMechanisms promotion.Mechanism
kargoClient client.Client
directivesEngine *directives.Engine
promoMechanisms promotion.Mechanism

cfg ReconcilerConfig

Expand Down Expand Up @@ -186,9 +188,15 @@ func newReconciler(
}
r := &reconciler{
kargoClient: kargoClient,
recorder: recorder,
cfg: cfg,
pqs: &pqs,
directivesEngine: directives.NewEngine(
directives.BuiltinsRegistry(),
credentialsDB,
kargoClient,
argocdClient,
),
recorder: recorder,
cfg: cfg,
pqs: &pqs,
promoMechanisms: promotion.NewMechanisms(
kargoClient,
argocdClient,
Expand Down Expand Up @@ -497,11 +505,35 @@ func (r *reconciler) promote(
stage,
)

if err := r.promoMechanisms.Promote(ctx, stage, workingPromo); err != nil {
return nil, err
}
if workingPromo.Spec.Steps == nil {
// If the Promotion has no steps, assume we are dealing with "legacy"
// Promotion mechanisms.
if err := r.promoMechanisms.Promote(ctx, stage, workingPromo); err != nil {
return nil, err
}

logger.Debug("promotion", "phase", workingPromo.Status.Phase)
logger.Debug("promotion", "phase", workingPromo.Status.Phase)
} else {
// If the Promotion has steps, execute them in sequence.
var steps []directives.Step
for _, step := range workingPromo.Spec.Steps {
steps = append(steps, directives.Step{
Directive: step.Step,
Alias: step.As,
Config: step.GetConfig(),
})
}
status, err := r.directivesEngine.Execute(ctx, steps)
switch status {
case directives.StatusSuccess:
workingPromo.Status.Phase = kargoapi.PromotionPhaseSucceeded
case directives.StatusFailure:
workingPromo.Status.Phase = kargoapi.PromotionPhaseFailed
}
if err != nil {
return nil, err
}
}

if workingPromo.Status.Phase == kargoapi.PromotionPhaseSucceeded {
// Trigger re-verification of the Stage if the promotion succeeded and
Expand Down

0 comments on commit e6d3458

Please sign in to comment.