Skip to content

Commit

Permalink
apply @hiddeco feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Kent Rancourt <kent.rancourt@gmail.com>
  • Loading branch information
krancour committed Oct 30, 2024
1 parent bcd50a1 commit 185a14f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
6 changes: 6 additions & 0 deletions api/v1alpha1/promotion_types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package v1alpha1

import (
"regexp"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -33,6 +35,10 @@ const (
PromotionPhaseAborted PromotionPhase = "Aborted"
)

// ReservedStepAliasRegex is a regular expression that matches step aliases that
// are reserved for internal use.
var ReservedStepAliasRegex = regexp.MustCompile(`^step-\d+$`)

// IsTerminal returns true if the PromotionPhase is a terminal one.
func (p *PromotionPhase) IsTerminal() bool {
switch *p {
Expand Down
5 changes: 1 addition & 4 deletions internal/directives/simple_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"os"
"regexp"
"strings"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand All @@ -15,8 +14,6 @@ import (
"github.com/akuity/kargo/internal/credentials"
)

var forbiddenStepAliasRegex = regexp.MustCompile(`^step-\d+$`)

// SimpleEngine is a simple engine that executes a list of PromotionSteps in
// sequence.
type SimpleEngine struct {
Expand Down Expand Up @@ -94,7 +91,7 @@ func (e *SimpleEngine) Promote(
step.Alias = strings.TrimSpace(step.Alias)
if step.Alias == "" {
step.Alias = fmt.Sprintf("step-%d", i)
} else if forbiddenStepAliasRegex.MatchString(step.Alias) {
} else if kargoapi.ReservedStepAliasRegex.MatchString(step.Alias) {
// A webhook enforces this regex as well, but we're checking here to
// account for the possibility of EXISTING Stages with a promotionTemplate
// containing a step with a now-reserved alias.
Expand Down
5 changes: 1 addition & 4 deletions internal/webhook/stage/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package stage
import (
"context"
"fmt"
"regexp"
"strings"

admissionv1 "k8s.io/api/admission/v1"
Expand All @@ -26,8 +25,6 @@ var (
}
)

var forbiddenStepAliasRegex = regexp.MustCompile(`^step-\d+$`)

type webhook struct {
client client.Client
decoder admission.Decoder
Expand Down Expand Up @@ -240,7 +237,7 @@ func (w *webhook) ValidatePromotionTemplate(
errs := field.ErrorList{}
for i, step := range promoTemplate.Spec.Steps {
stepAlias := strings.TrimSpace(step.As)
if forbiddenStepAliasRegex.MatchString(stepAlias) {
if kargoapi.ReservedStepAliasRegex.MatchString(stepAlias) {
errs = append(errs, field.Invalid(
f.Child("spec", "steps").Index(i).Child("as"),
stepAlias,
Expand Down

0 comments on commit 185a14f

Please sign in to comment.