Skip to content

Commit 34ce988

Browse files
chore(backport release-1.3): fix: Long Stage names result in AnalysisRun creation failures during promotion (#3553)
Co-authored-by: Aidan Rowe <[email protected]>
1 parent 22fa462 commit 34ce988

File tree

9 files changed

+73
-19
lines changed

9 files changed

+73
-19
lines changed

api/v1alpha1/annotations.go

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ const (
3737
// of the annotation should be in the format of "<project>:<stage>".
3838
AnnotationKeyAuthorizedStage = "kargo.akuity.io/authorized-stage"
3939

40+
// AnnotationKeyPromotion is an annotation key that can be set on a
41+
// resource to indicate that it is related to a specific promotion.
42+
AnnotationKeyPromotion = "kargo.akuity.io/promotion"
43+
4044
// AnnotationValueTrue is a value that can be set on an annotation to
4145
// indicate that it applies.
4246
AnnotationValueTrue = "true"

api/v1alpha1/labels.go

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const (
2020
// Kargo core API
2121
FreightCollectionLabelKey = "kargo.akuity.io/freight-collection"
2222
ProjectLabelKey = "kargo.akuity.io/project"
23-
PromotionLabelKey = "kargo.akuity.io/promotion"
2423
ShardLabelKey = "kargo.akuity.io/shard"
2524
StageLabelKey = "kargo.akuity.io/stage"
2625

docs/docs/50-user-guide/20-how-to-guides/60-verification.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ metadata:
145145
generation: 3
146146
labels:
147147
kargo.akuity.io/freight-collection: 55d452301040a73e9fd05289b1f8ddbec1791222
148-
kargo.akuity.io/promotion: dev.01jjpfgfwkzk18k7cyq61jehf4.319ddec
149148
kargo.akuity.io/stage: dev
149+
annotations:
150+
kargo.akuity.io/promotion: dev.01jjpfgfwkzk18k7cyq61jehf4.319ddec
150151
name: dev.01jjqaq7qacfn766tcp1nqz2zv.55d4523
151152
namespace: guestbook
152153
ownerReferences:

internal/controller/stages/regular_stages.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ func (r *RegularStageReconciler) recordFreightVerificationEvent(
11941194
)
11951195
}
11961196
// AnalysisRun that triggered by a Promotion contains the Promotion name
1197-
if promoName, ok := ar.Labels[kargoapi.PromotionLabelKey]; ok {
1197+
if promoName, ok := ar.Annotations[kargoapi.AnnotationKeyPromotion]; ok {
11981198
annotations[kargoapi.AnnotationKeyEventPromotionName] = promoName
11991199
}
12001200
}
@@ -1333,8 +1333,8 @@ func (r *RegularStageReconciler) startVerification(
13331333
}
13341334
if curVI == nil || (req.ForID(curVI.ID) && req.ControlPlane && req.Actor != "") {
13351335
if stage.Status.LastPromotion != nil {
1336-
builderOpts = append(builderOpts, rollouts.WithExtraLabels{
1337-
kargoapi.PromotionLabelKey: stage.Status.LastPromotion.Name,
1336+
builderOpts = append(builderOpts, rollouts.WithExtraAnnotations{
1337+
kargoapi.AnnotationKeyPromotion: stage.Status.LastPromotion.Name,
13381338
})
13391339
}
13401340
}

internal/controller/stages/regular_stages_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3033,8 +3033,8 @@ func TestRegularStageReconciler_recordFreightVerificationEvent(t *testing.T) {
30333033
ObjectMeta: metav1.ObjectMeta{
30343034
Name: "test-analysis",
30353035
Namespace: "test-project",
3036-
Labels: map[string]string{
3037-
kargoapi.PromotionLabelKey: "test-promotion",
3036+
Annotations: map[string]string{
3037+
kargoapi.AnnotationKeyPromotion: "test-promotion",
30383038
},
30393039
},
30403040
},
@@ -3370,7 +3370,7 @@ func TestRegularStageReconciler_startVerification(t *testing.T) {
33703370
Namespace: vi.AnalysisRun.Namespace,
33713371
Name: vi.AnalysisRun.Name,
33723372
}, ar))
3373-
assert.Equal(t, "test-promotion", ar.Labels[kargoapi.PromotionLabelKey])
3373+
assert.Equal(t, "test-promotion", ar.Annotations[kargoapi.AnnotationKeyPromotion])
33743374
},
33753375
},
33763376
{

internal/rollouts/analysis_run_builder.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func (b *AnalysisRunBuilder) Build(
6666
b.generateName(opts.NamePrefix, opts.NameSuffix),
6767
cfg.AnalysisRunMetadata,
6868
opts.ExtraLabels,
69+
opts.ExtraAnnotations,
6970
)
7071

7172
analysisTemplates, clusterAnalysisTemplates, err := b.getAnalysisTemplates(
@@ -121,18 +122,24 @@ func (b *AnalysisRunBuilder) generateName(prefix, suffix string) string {
121122
func (b *AnalysisRunBuilder) buildMetadata(
122123
namespace, name string,
123124
metadata *kargoapi.AnalysisRunMetadata,
124-
extraLabels map[string]string,
125+
extraLabels, extraAnnotations map[string]string,
125126
) metav1.ObjectMeta {
126-
var annotations map[string]string
127+
annotations := make(map[string]string)
127128
labels := make(map[string]string)
128129

129130
if metadata != nil {
130-
annotations = metadata.Annotations
131+
if metadata.Annotations != nil {
132+
maps.Copy(annotations, metadata.Annotations)
133+
}
131134
if metadata.Labels != nil {
132135
maps.Copy(labels, metadata.Labels)
133136
}
134137
}
135138

139+
if extraAnnotations != nil {
140+
maps.Copy(annotations, extraAnnotations)
141+
}
142+
136143
if extraLabels != nil {
137144
maps.Copy(labels, extraLabels)
138145
}

internal/rollouts/analysis_run_builder_test.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,13 @@ func TestAnalysisRunBuilder_buildMetadata(t *testing.T) {
359359
}
360360

361361
tests := []struct {
362-
name string
363-
namespace string
364-
objName string
365-
metadata *kargoapi.AnalysisRunMetadata
366-
extraLabels map[string]string
367-
assertions func(*testing.T, metav1.ObjectMeta)
362+
name string
363+
namespace string
364+
objName string
365+
metadata *kargoapi.AnalysisRunMetadata
366+
extraLabels map[string]string
367+
extraAnnotations map[string]string
368+
assertions func(*testing.T, metav1.ObjectMeta)
368369
}{
369370
{
370371
name: "basic metadata",
@@ -377,7 +378,7 @@ func TestAnalysisRunBuilder_buildMetadata(t *testing.T) {
377378
},
378379
},
379380
{
380-
name: "with metadata and extra labels",
381+
name: "with metadata, extra labels and extra annotations",
381382
namespace: "test-ns",
382383
objName: "test-name",
383384
metadata: &kargoapi.AnalysisRunMetadata{
@@ -391,9 +392,13 @@ func TestAnalysisRunBuilder_buildMetadata(t *testing.T) {
391392
extraLabels: map[string]string{
392393
"extra": "value",
393394
},
395+
extraAnnotations: map[string]string{
396+
"extra2": "value2",
397+
},
394398
assertions: func(t *testing.T, meta metav1.ObjectMeta) {
395399
assert.Equal(t, "value1", meta.Labels["label1"])
396400
assert.Equal(t, "value", meta.Labels["extra"])
401+
assert.Equal(t, "value2", meta.Annotations["extra2"])
397402
assert.Equal(t, "value1", meta.Annotations["anno1"])
398403
assert.Equal(t, "test-controller", meta.Labels[controllerInstanceIDLabelKey])
399404
},
@@ -402,7 +407,7 @@ func TestAnalysisRunBuilder_buildMetadata(t *testing.T) {
402407

403408
for _, tt := range tests {
404409
t.Run(tt.name, func(t *testing.T) {
405-
result := builder.buildMetadata(tt.namespace, tt.objName, tt.metadata, tt.extraLabels)
410+
result := builder.buildMetadata(tt.namespace, tt.objName, tt.metadata, tt.extraLabels, tt.extraAnnotations)
406411
tt.assertions(t, result)
407412
})
408413
}

internal/rollouts/options.go

+13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type AnalysisRunOptions struct {
3131
NamePrefix string
3232
NameSuffix string
3333
ExtraLabels map[string]string
34+
ExtraAnnotations map[string]string
3435
Owners []Owner
3536
ExpressionConfig *ArgumentEvaluationConfig
3637
}
@@ -100,6 +101,18 @@ func (o WithExtraLabels) ApplyToAnalysisRun(opts *AnalysisRunOptions) {
100101
opts.ExtraLabels = o
101102
}
102103

104+
// WithExtraAnnotations sets the extra labels for the AnalysisRun. It can be passed
105+
// multiple times to add more annotations.
106+
type WithExtraAnnotations map[string]string
107+
108+
func (o WithExtraAnnotations) ApplyToAnalysisRun(opts *AnalysisRunOptions) {
109+
if opts.ExtraAnnotations != nil {
110+
maps.Copy(opts.ExtraAnnotations, o)
111+
return
112+
}
113+
opts.ExtraAnnotations = o
114+
}
115+
103116
// WithOwner sets the owner for the AnalysisRun. It can be passed multiple times
104117
// to add more owners.
105118
type WithOwner Owner

internal/rollouts/options_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,31 @@ func TestAnalysisRunOptions(t *testing.T) {
7474
}, opts.ExtraLabels)
7575
},
7676
},
77+
{
78+
name: "extra annotations: single set",
79+
options: []AnalysisRunOption{
80+
WithExtraAnnotations{"key1": "value1", "key2": "value2"},
81+
},
82+
assertions: func(t *testing.T, opts *AnalysisRunOptions) {
83+
assert.Equal(t, map[string]string{
84+
"key1": "value1",
85+
"key2": "value2",
86+
}, opts.ExtraAnnotations)
87+
},
88+
},
89+
{
90+
name: "extra annotations: multiple sets are merged",
91+
options: []AnalysisRunOption{
92+
WithExtraAnnotations{"key1": "value1"},
93+
WithExtraAnnotations{"key2": "value2"},
94+
},
95+
assertions: func(t *testing.T, opts *AnalysisRunOptions) {
96+
assert.Equal(t, map[string]string{
97+
"key1": "value1",
98+
"key2": "value2",
99+
}, opts.ExtraAnnotations)
100+
},
101+
},
77102
{
78103
name: "single owner",
79104
options: []AnalysisRunOption{

0 commit comments

Comments
 (0)