@@ -30,7 +30,6 @@ import (
3030)
3131
3232const (
33- RevisionHashAnnotation = "olm.operatorframework.io/hash"
3433 ClusterExtensionRevisionPreviousLimit = 5
3534)
3635
@@ -216,19 +215,15 @@ func (bc *Boxcutter) getObjects(rev *ocv1.ClusterExtensionRevision) []client.Obj
216215 return objs
217216}
218217
219- func (bc * Boxcutter ) ensureGVKIsSet (obj client.Object ) error {
220- if ! obj .GetObjectKind ().GroupVersionKind ().Empty () {
221- return nil
222- }
223-
224- gvk , err := apiutil .GVKForObject (obj , bc .Scheme )
225- if err != nil {
226- return err
218+ func (bc * Boxcutter ) createOrUpdate (ctx context.Context , obj client.Object ) error {
219+ if obj .GetObjectKind ().GroupVersionKind ().Empty () {
220+ gvk , err := apiutil .GVKForObject (obj , bc .Scheme )
221+ if err != nil {
222+ return err
223+ }
224+ obj .GetObjectKind ().SetGroupVersionKind (gvk )
227225 }
228-
229- obj .GetObjectKind ().SetGroupVersionKind (gvk )
230-
231- return nil
226+ return bc .Client .Patch (ctx , obj , client .Apply , client .FieldOwner (bc .FieldOwner ), client .ForceOwnership )
232227}
233228
234229func (bc * Boxcutter ) apply (ctx context.Context , contentFS fs.FS , ext * ocv1.ClusterExtension , objectLabels , revisionAnnotations map [string ]string ) (bool , string , error ) {
@@ -251,54 +246,52 @@ func (bc *Boxcutter) apply(ctx context.Context, contentFS fs.FS, ext *ocv1.Clust
251246 currentRevision := & ocv1.ClusterExtensionRevision {}
252247 state := StateNeedsInstall
253248 // check if we can update the current revision.
254- if len (existingRevisions ) > 0 { // nolint:nestif
249+ if len (existingRevisions ) > 0 {
255250 // try first to update the current revision.
256251 currentRevision = & existingRevisions [len (existingRevisions )- 1 ]
257252 desiredRevision .Spec .Previous = currentRevision .Spec .Previous
258253 desiredRevision .Spec .Revision = currentRevision .Spec .Revision
259254 desiredRevision .Name = currentRevision .Name
260255
261- if err := bc .ensureGVKIsSet (desiredRevision ); err != nil {
262- return false , "" , fmt .Errorf ("setting gvk failed: %w" , err )
263- }
264- if err = bc .Client .Patch (ctx , desiredRevision , client .Apply , client .FieldOwner (bc .FieldOwner ), client .ForceOwnership ); err != nil {
265- if ! apierrors .IsInvalid (err ) {
266- return false , "" , fmt .Errorf ("patching %s Revision: %w" , desiredRevision .Name , err )
267- }
256+ err := bc .createOrUpdate (ctx , desiredRevision )
257+ switch {
258+ case apierrors .IsInvalid (err ):
268259 // We could not update the current revision due to trying to update an immutable field.
269260 // Therefore, we need to create a new revision.
270261 state = StateNeedsUpgrade
271- } else {
262+ case err == nil :
272263 // inplace patch was successful, no changes in phases
273264 state = StateUnchanged
265+ default :
266+ return false , "" , fmt .Errorf ("patching %s Revision: %w" , desiredRevision .Name , err )
274267 }
275268 }
276269
277- if state != StateUnchanged { // nolint:nestif
278- // Preflights
279- plainObjs := bc .getObjects (desiredRevision )
280- for _ , preflight := range bc .Preflights {
281- if shouldSkipPreflight (ctx , preflight , ext , state ) {
282- continue
270+ // Preflights
271+ plainObjs := bc .getObjects (desiredRevision )
272+ for _ , preflight := range bc .Preflights {
273+ if shouldSkipPreflight (ctx , preflight , ext , state ) {
274+ continue
275+ }
276+ switch state {
277+ case StateNeedsInstall :
278+ err := preflight .Install (ctx , plainObjs )
279+ if err != nil {
280+ return false , "" , err
283281 }
284- switch state {
285- case StateNeedsInstall :
286- err := preflight .Install (ctx , plainObjs )
287- if err != nil {
288- return false , "" , err
289- }
290- // TODO: jlanford's IDE says that "StateNeedsUpgrade" condition is always true, but
291- // it isn't immediately obvious why that is. Perhaps len(existingRevisions) is
292- // always greater than 0 (seems unlikely), or shouldSkipPreflight always returns
293- // true (and we continue) when state == StateNeedsInstall?
294- case StateNeedsUpgrade :
295- err := preflight .Upgrade (ctx , plainObjs )
296- if err != nil {
297- return false , "" , err
298- }
282+ // TODO: jlanford's IDE says that "StateNeedsUpgrade" condition is always true, but
283+ // it isn't immediately obvious why that is. Perhaps len(existingRevisions) is
284+ // always greater than 0 (seems unlikely), or shouldSkipPreflight always returns
285+ // true (and we continue) when state == StateNeedsInstall?
286+ case StateNeedsUpgrade :
287+ err := preflight .Upgrade (ctx , plainObjs )
288+ if err != nil {
289+ return false , "" , err
299290 }
300291 }
292+ }
301293
294+ if state != StateUnchanged {
302295 // need to create new revision
303296 prevRevisions := existingRevisions
304297 revisionNumber := latestRevisionNumber (prevRevisions ) + 1
@@ -310,10 +303,7 @@ func (bc *Boxcutter) apply(ctx context.Context, contentFS fs.FS, ext *ocv1.Clust
310303 return false , "" , fmt .Errorf ("garbage collecting old Revisions: %w" , err )
311304 }
312305
313- if err := bc .ensureGVKIsSet (desiredRevision ); err != nil {
314- return false , "" , fmt .Errorf ("setting gvk failed: %w" , err )
315- }
316- if err := bc .Client .Patch (ctx , desiredRevision , client .Apply , client .FieldOwner (bc .FieldOwner ), client .ForceOwnership ); err != nil {
306+ if err := bc .createOrUpdate (ctx , desiredRevision ); err != nil {
317307 return false , "" , fmt .Errorf ("creating new Revision: %w" , err )
318308 }
319309 currentRevision = desiredRevision
0 commit comments