@@ -21,6 +21,7 @@ import (
2121 "fmt"
2222 "os"
2323 "reflect"
24+ "strings"
2425
2526 apierrors "k8s.io/apimachinery/pkg/api/errors"
2627 "k8s.io/apimachinery/pkg/runtime/schema"
@@ -74,6 +75,10 @@ func (r *OpenStackVersion) ValidateCreate(ctx context.Context, c goClient.Client
7475 )
7576 }
7677
78+ if err := validateMinorUpdateTargetStageAnnotation (r .Annotations , r .GetName ()); err != nil {
79+ return nil , err
80+ }
81+
7782 versionList , err := GetOpenStackVersions (r .Namespace , c )
7883
7984 if err != nil {
@@ -114,6 +119,10 @@ func (r *OpenStackVersion) ValidateCreate(ctx context.Context, c goClient.Client
114119func (r * OpenStackVersion ) ValidateUpdate (ctx context.Context , old runtime.Object , c goClient.Client ) (admission.Warnings , error ) {
115120 openstackversionlog .Info ("validate update" , "name" , r .Name )
116121
122+ if err := validateMinorUpdateTargetStageAnnotation (r .Annotations , r .GetName ()); err != nil {
123+ return nil , err
124+ }
125+
117126 _ , ok := r .Status .ContainerImageVersionDefaults [r .Spec .TargetVersion ]
118127 if r .Spec .TargetVersion != openstackVersionDefaults .AvailableVersion && ! ok {
119128 return nil , apierrors .NewForbidden (
@@ -135,6 +144,11 @@ func (r *OpenStackVersion) ValidateUpdate(ctx context.Context, old runtime.Objec
135144 return nil , apierrors .NewInternalError (fmt .Errorf ("failed to convert old object to OpenStackVersion" ))
136145 }
137146
147+ // Validate that the target stage annotation is not from earlier stage while a minor update is in progress
148+ if err := validateMinorUpdateTargetStageAnnotationProgress (oldVersion , r ); err != nil {
149+ return nil , err
150+ }
151+
138152 // Check if targetVersion is changing and this is a minor update
139153 if oldVersion .Spec .TargetVersion != r .Spec .TargetVersion && oldVersion .Status .DeployedVersion != nil {
140154 // Check if the skip annotation is present
@@ -174,6 +188,113 @@ func (r *OpenStackVersion) ValidateUpdate(ctx context.Context, old runtime.Objec
174188 return nil , nil
175189}
176190
191+ func validateMinorUpdateTargetStageAnnotation (annotations map [string ]string , resourceName string ) error {
192+ if annotations == nil {
193+ return nil
194+ }
195+ stage , ok := annotations [MinorUpdateTargetStageAnnotation ]
196+ if ! ok {
197+ return nil
198+ }
199+ annotationField := "metadata.annotations[" + MinorUpdateTargetStageAnnotation + "]"
200+ if stage == "" {
201+ return apierrors .NewForbidden (
202+ schema.GroupResource {
203+ Group : GroupVersion .WithKind ("OpenStackVersion" ).Group ,
204+ Resource : GroupVersion .WithKind ("OpenStackVersion" ).Kind ,
205+ }, resourceName , & field.Error {
206+ Type : field .ErrorTypeForbidden ,
207+ Field : annotationField ,
208+ BadValue : stage ,
209+ Detail : "Annotation value must not be empty. Remove the annotation or set a valid stage name" ,
210+ },
211+ )
212+ }
213+ if ! IsValidMinorUpdateTargetStage (stage ) {
214+ return apierrors .NewForbidden (
215+ schema.GroupResource {
216+ Group : GroupVersion .WithKind ("OpenStackVersion" ).Group ,
217+ Resource : GroupVersion .WithKind ("OpenStackVersion" ).Kind ,
218+ }, resourceName , & field.Error {
219+ Type : field .ErrorTypeForbidden ,
220+ Field : annotationField ,
221+ BadValue : stage ,
222+ Detail : fmt .Sprintf (
223+ "Invalid target stage %q. Must be one of: %s" ,
224+ stage ,
225+ strings .Join (ValidMinorUpdateTargetStages (), ", " ),
226+ ),
227+ },
228+ )
229+ }
230+ return nil
231+ }
232+
233+ func minorUpdateInProgress (v * OpenStackVersion ) bool {
234+ if v .Status .DeployedVersion == nil {
235+ return false
236+ }
237+ return v .Spec .TargetVersion != * v .Status .DeployedVersion
238+ }
239+
240+ // validateMinorUpdateTargetStageAnnotationProgress rejects moving the target-stage
241+ // annotation to an earlier rollout stage while a minor update is in progress, and rejects
242+ // adding the annotation behind stages already completed when it was absent at update start.
243+ func validateMinorUpdateTargetStageAnnotationProgress (old , updated * OpenStackVersion ) error {
244+ if ! minorUpdateInProgress (updated ) {
245+ return nil
246+ }
247+ oldStage , oldOK := MinorUpdateTargetStageFromAnnotations (old .Annotations )
248+ newStage , newOK := MinorUpdateTargetStageFromAnnotations (updated .Annotations )
249+ if ! newOK {
250+ return nil
251+ }
252+ newIdx , okNew := MinorUpdateTargetStageIndex (newStage )
253+ if ! okNew {
254+ return nil
255+ }
256+ annotationField := "metadata.annotations[" + MinorUpdateTargetStageAnnotation + "]"
257+ gr := schema.GroupResource {
258+ Group : GroupVersion .WithKind ("OpenStackVersion" ).Group ,
259+ Resource : GroupVersion .WithKind ("OpenStackVersion" ).Kind ,
260+ }
261+
262+ if ! oldOK {
263+ latest := LatestCompletedMinorUpdateTargetStageIndex (old .Status )
264+ if latest >= 0 && newIdx < latest {
265+ completedStage := validMinorUpdateTargetStagesOrdered [latest ]
266+ return apierrors .NewForbidden (
267+ gr , updated .GetName (), & field.Error {
268+ Type : field .ErrorTypeForbidden ,
269+ Field : annotationField ,
270+ BadValue : newStage ,
271+ Detail : fmt .Sprintf (
272+ "Cannot set update target stage to %q while minor update is in progress: update has already completed stage %q (targetVersion %q, deployedVersion %q); choose a further stage" ,
273+ newStage , completedStage , updated .Spec .TargetVersion , * updated .Status .DeployedVersion ,
274+ ),
275+ },
276+ )
277+ }
278+ return nil
279+ }
280+
281+ oldIdx , _ := MinorUpdateTargetStageIndex (oldStage )
282+ if newIdx >= oldIdx {
283+ return nil
284+ }
285+ return apierrors .NewForbidden (
286+ gr , updated .GetName (), & field.Error {
287+ Type : field .ErrorTypeForbidden ,
288+ Field : annotationField ,
289+ BadValue : newStage ,
290+ Detail : fmt .Sprintf (
291+ "Cannot move update target stage from %q to earlier stage %q while minor update is in progress (targetVersion %q, deployedVersion %q); remove the annotation or set a further stage" ,
292+ oldStage , newStage , updated .Spec .TargetVersion , * updated .Status .DeployedVersion ,
293+ ),
294+ },
295+ )
296+ }
297+
177298// hasAnyCustomImage checks if any image field in CustomContainerImages is set
178299func hasAnyCustomImage (images CustomContainerImages ) bool {
179300 // Check CinderVolumeImages map
0 commit comments