Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set promotion shard in webhook #1633

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions internal/kargo/kargo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const (
maxStageNamePrefixLength = 218
)

// NewPromotion returns a new Promotion from a given stage and freight with our naming convention.
// Ensures the owner reference is set to be the stage, and carries over any shard labels
// NewPromotion returns a new Promotion from a given stage and freight with our
// naming convention.
func NewPromotion(stage kargoapi.Stage, freight string) kargoapi.Promotion {
shortHash := freight
if len(shortHash) > 7 {
Expand All @@ -36,24 +36,16 @@ func NewPromotion(stage kargoapi.Stage, freight string) kargoapi.Promotion {
// We just want a unique ID that can be sorted lexicographically
promoName := strings.ToLower(fmt.Sprintf("%s.%s.%s", shortStageName, ulid.Make(), shortHash))

ownerRef := metav1.NewControllerRef(&stage, kargoapi.GroupVersion.WithKind("Stage"))

promotion := kargoapi.Promotion{
ObjectMeta: metav1.ObjectMeta{
Name: promoName,
Namespace: stage.Namespace,
OwnerReferences: []metav1.OwnerReference{*ownerRef},
Name: promoName,
Namespace: stage.Namespace,
},
Spec: &kargoapi.PromotionSpec{
Stage: stage.Name,
Freight: freight,
},
}
if stage.Labels != nil && stage.Labels[kargoapi.ShardLabelKey] != "" {
promotion.ObjectMeta.Labels = map[string]string{
kargoapi.ShardLabelKey: stage.Labels[kargoapi.ShardLabelKey],
}
}
return promotion
}

Expand Down
21 changes: 0 additions & 21 deletions internal/kargo/kargo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,6 @@ func TestNewPromotion(t *testing.T) {
require.Equal(t, testFreight[0:7], parts[2])
},
},
{
name: "Promote stage with shard",
stage: kargoapi.Stage{
ObjectMeta: metav1.ObjectMeta{
UID: "80b44831-ac8d-4900-9df9-ee95f80c0fae",
Name: "test",
Namespace: "kargo-demo",
Labels: map[string]string{
kargoapi.ShardLabelKey: "another-shard",
},
},
},
freight: testFreight,
assertions: func(t *testing.T, _ kargoapi.Stage, promo kargoapi.Promotion) {
parts := strings.Split(promo.Name, ".")
require.Equal(t, "test", parts[0])
require.Equal(t, testFreight[0:7], parts[2])
require.Equal(t, "another-shard", promo.Labels[kargoapi.ShardLabelKey])
},
},
{
name: "Promote stage with very long name",
stage: kargoapi.Stage{
Expand All @@ -86,7 +66,6 @@ func TestNewPromotion(t *testing.T) {
tc := tc
t.Run(tc.name, func(t *testing.T) {
promo := NewPromotion(tc.stage, tc.freight)
require.True(t, metav1.IsControlledBy(&promo, &tc.stage))
require.Equal(t, tc.freight, promo.Spec.Freight)
require.Equal(t, tc.stage.Name, promo.Spec.Stage)
require.Equal(t, tc.freight, promo.Spec.Freight)
Expand Down
11 changes: 11 additions & 0 deletions internal/webhook/promotion/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@
promo.Namespace,
)
}

// Make sure the Promotion has the same shard as the Stage
if stage.Spec.Shard != "" {
if promo.Labels == nil {
promo.Labels = make(map[string]string, 1)
}
promo.Labels[kargoapi.ShardLabelKey] = stage.Spec.Shard
} else {
delete(promo.Labels, kargoapi.ShardLabelKey)

Check warning on line 121 in internal/webhook/promotion/webhook.go

View check run for this annotation

Codecov / codecov/patch

internal/webhook/promotion/webhook.go#L120-L121

Added lines #L120 - L121 were not covered by tests
}

ownerRef :=
metav1.NewControllerRef(stage, kargoapi.GroupVersion.WithKind("Stage"))
promo.ObjectMeta.OwnerReferences = []metav1.OwnerReference{*ownerRef}
Expand Down
7 changes: 6 additions & 1 deletion internal/webhook/promotion/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ func TestDefault(t *testing.T) {
client.Client,
types.NamespacedName,
) (*kargoapi.Stage, error) {
return &kargoapi.Stage{}, nil
return &kargoapi.Stage{
Spec: &kargoapi.StageSpec{
Shard: "fake-shard",
},
}, nil
},
},
assertions: func(t *testing.T, promo *kargoapi.Promotion, err error) {
require.NoError(t, err)
require.Equal(t, "fake-shard", promo.Labels[kargoapi.ShardLabelKey])
require.NotEmpty(t, promo.OwnerReferences)
},
},
Expand Down
Loading