11package operator
22
33import (
4+ "context"
45 "fmt"
56 "os"
67 "strconv"
@@ -15,8 +16,10 @@ import (
1516 "github.com/openshift/library-go/pkg/controller/factory"
1617 "github.com/openshift/library-go/pkg/operator/csi/csidrivercontrollerservicecontroller"
1718 dc "github.com/openshift/library-go/pkg/operator/deploymentcontroller"
19+ "github.com/openshift/library-go/pkg/operator/status"
1820 appsv1 "k8s.io/api/apps/v1"
1921 corev1 "k8s.io/api/core/v1"
22+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2023 "k8s.io/apimachinery/pkg/labels"
2124 "k8s.io/apimachinery/pkg/runtime/schema"
2225 "k8s.io/klog/v2"
@@ -252,3 +255,46 @@ func withHyperShiftRunAsUser(c *clients.Clients) (dc.DeploymentHookFunc, []facto
252255 }
253256 return hook , nil
254257}
258+
259+ func hasFinishedProgressing (deployment * appsv1.Deployment ) bool {
260+ if deployment .Status .ObservedGeneration != deployment .Generation {
261+ // The Deployment controller did not act on the Deployment spec change yet.
262+ // Any condition in the status may be stale.
263+ return false
264+ }
265+ // Deployment whose rollout is complete gets Progressing condition with Reason NewReplicaSetAvailable condition.
266+ // https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#complete-deployment
267+ for _ , cond := range deployment .Status .Conditions {
268+ if cond .Type == appsv1 .DeploymentProgressing {
269+ return cond .Status == corev1 .ConditionTrue && cond .Reason == "NewReplicaSetAvailable"
270+ }
271+ }
272+ return false
273+ }
274+
275+ func GetDeploymentVersionBuilder () config.DeploymentHookBuilder {
276+ return func (c * clients.Clients ) (dc.DeploymentHookFunc , []factory.Informer ) {
277+ hook := func (_ * opv1.OperatorSpec , deployment * appsv1.Deployment ) error {
278+ var desiredVersion string
279+ for k , v := range deployment .Annotations {
280+ if k == "release.openshift.io/desired-version" {
281+ desiredVersion = v
282+ }
283+ }
284+ if desiredVersion != status .VersionForOperatorFromEnv () {
285+ return nil
286+ }
287+
288+ // deployment.Status is empty: {0 0 0 0 0 0 <nil> [] <nil>}
289+ deployment2 , err := c .ControlPlaneKubeClient .AppsV1 ().Deployments (deployment .Namespace ).Get (context .TODO (), deployment .Name , metav1.GetOptions {})
290+ if err != nil {
291+ return fmt .Errorf ("could not get Deployment %s in Namespace %s: %w" , deployment .Name , deployment .Namespace , err )
292+ }
293+ if hasFinishedProgressing (deployment2 ) {
294+ deployment .Annotations ["release.openshift.io/version" ] = desiredVersion
295+ }
296+ return nil
297+ }
298+ return hook , nil
299+ }
300+ }
0 commit comments