@@ -32,16 +32,46 @@ func (optr *Operator) syncOSImageStream(_ *renderConfig, _ *configv1.ClusterOper
3232 // This sync runs once per version. Before performing the streams fetching
3333 // process, that takes time as it requires inspecting images, ensure this function
3434 // needs to build the stream.
35- existingOSImageStream , updateRequired , err := optr .isOSImageStreamBuildRequired ()
36- if ! updateRequired || err != nil {
35+ existingOSImageStream , rebuildRequired , err := optr .isOSImageStreamBuildRequired ()
36+ if err != nil {
3737 return err
3838 }
3939
40- // If the code reaches this point the OSImageStream CR is not
41- // present (new cluster) or it's out-dated (cluster update).
42- // Build the new OSImageStream and push it.
43- return optr .buildOSImageStream (existingOSImageStream )
40+ if rebuildRequired {
41+ // If the code reaches this point the OSImageStream status containing
42+ // the available OS Image Streams is outdated or doesn't exist.
43+ return optr .buildOSImageStream (existingOSImageStream )
44+ } else if existingOSImageStream != nil {
45+ // We didn't require a rebuild, but check if we need to update the default
46+ return optr .updateOSImageStream (existingOSImageStream )
47+ }
4448
49+ return err
50+ }
51+
52+ func (optr * Operator ) updateOSImageStream (existingOSImageStream * v1alpha1.OSImageStream ) error {
53+ requestedDefault := osimagestream .GetOSImageStreamSpecDefault (existingOSImageStream )
54+ if requestedDefault == "" {
55+ requestedDefault = osimagestream .GetBuiltinDefault (existingOSImageStream )
56+ }
57+
58+ currentDefault := existingOSImageStream .Status .DefaultStream
59+ if currentDefault != requestedDefault {
60+ if _ , err := osimagestream .GetOSImageStreamSetByName (existingOSImageStream , requestedDefault ); err != nil {
61+ return fmt .Errorf ("error syncing default OSImageStream with OSImageStream %s: %v" , requestedDefault , err )
62+ }
63+
64+ existingOSImageStream .Status .DefaultStream = requestedDefault
65+ if _ , err := optr .client .
66+ MachineconfigurationV1alpha1 ().
67+ OSImageStreams ().
68+ UpdateStatus (context .TODO (), existingOSImageStream , metav1.UpdateOptions {}); err != nil {
69+ return fmt .Errorf ("error updating the default OSImageStream status: %w" , err )
70+ }
71+
72+ klog .Infof ("OSImageStream default has changed from %s to %s" , currentDefault , requestedDefault )
73+ }
74+ return nil
4575}
4676
4777func (optr * Operator ) buildOSImageStream (existingOSImageStream * v1alpha1.OSImageStream ) error {
@@ -76,8 +106,25 @@ func (optr *Operator) buildOSImageStream(existingOSImageStream *v1alpha1.OSImage
76106 return fmt .Errorf ("could not build SysContext for OSImageStream build: %w" , err )
77107 }
78108
79- imageStreamFactory := osimagestream .NewDefaultStreamSourceFactory (optr .mcoCmLister , & osimagestream.DefaultImagesInspectorFactory {})
80- osImageStream , err := osimagestream .BuildOsImageStreamRuntime (buildCtx , sysCtxBuilder , image , imageStreamFactory )
109+ sysCtx , err := sysCtxBuilder .Build ()
110+ if err != nil {
111+ return fmt .Errorf ("could not prepare for OSImageStream inspection: %w" , err )
112+ }
113+ defer func () {
114+ if err := sysCtx .Cleanup (); err != nil {
115+ klog .Warningf ("Unable to clean resources after OSImageStream inspection: %s" , err )
116+ }
117+ }()
118+
119+ factory := osimagestream .NewDefaultStreamSourceFactory (& osimagestream.DefaultImagesInspectorFactory {})
120+ osImageStream , err := factory .Create (
121+ buildCtx ,
122+ sysCtx .SysContext ,
123+ osimagestream.CreateOptions {
124+ ReleaseImage : image ,
125+ ConfigMapLister : optr .mcoCmLister ,
126+ ExistingOSImageStream : existingOSImageStream ,
127+ })
81128 if err != nil {
82129 return fmt .Errorf ("error building the OSImageStream: %w" , err )
83130 }
@@ -179,9 +226,9 @@ func (optr *Operator) isOSImageStreamBuildRequired() (*v1alpha1.OSImageStream, b
179226 }
180227
181228 // Check if an update is needed
182- if ! osImageStreamRequiresUpdate (existingOSImageStream ) {
229+ if ! osImageStreamRequiresRebuild (existingOSImageStream ) {
183230 klog .V (4 ).Info ("OSImageStream is already up-to-date, skipping sync" )
184- return nil , false , nil
231+ return existingOSImageStream , false , nil
185232 }
186233 return existingOSImageStream , true , nil
187234}
@@ -235,12 +282,13 @@ func (optr *Operator) getExistingOSImageStream() (*v1alpha1.OSImageStream, error
235282 return osImageStream , nil
236283}
237284
238- // osImageStreamRequiresUpdate checks if the OSImageStream needs to be created or updated.
239- // Returns true if osImageStream is nil or if its version annotation doesn't match the current version.
240- func osImageStreamRequiresUpdate (osImageStream * v1alpha1.OSImageStream ) bool {
285+ // osImageStreamRequiresRebuild checks if the OSImageStream needs to be created or updated.
286+ // Returns true if osImageStream is nil, if its version annotation doesn't match the current version,
287+ // or if the builtin default stream annotation is missing.
288+ func osImageStreamRequiresRebuild (osImageStream * v1alpha1.OSImageStream ) bool {
241289 if osImageStream == nil {
242290 return true
243291 }
244292 releaseVersion , ok := osImageStream .Annotations [ctrlcommon .ReleaseImageVersionAnnotationKey ]
245- return ! ok || releaseVersion != version .Hash
293+ return ! ok || releaseVersion != version .Hash || osimagestream . GetBuiltinDefault ( osImageStream ) == ""
246294}
0 commit comments