Skip to content

Commit

Permalink
Add timeout logic
Browse files Browse the repository at this point in the history
Signed-off-by: Matej Feder <matej.feder@dnation.cloud>
  • Loading branch information
matofeder committed Jan 4, 2024
1 parent ed6e517 commit 10fcbe1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
13 changes: 12 additions & 1 deletion api/v1alpha1/conditions_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ const (
)

const (
// OpenStackImageReadyCondition reports on whether the image of cluster stack release is imported and actice.
// OpenStackImageImportStartCondition reports the image import start.
OpenStackImageImportStartCondition = "OpenStackImageImportStart"

// OpenStackImageImportNotStartReason is used when image import does not start yet.
OpenStackImageImportNotStartReason = "OpenStackImageImportNotStartReason"
)

const (
// OpenStackImageReadyCondition reports on whether the image of cluster stack release is imported and active.
OpenStackImageReadyCondition clusterv1beta1.ConditionType = "OpenStackImageActive"

// OpenStackImageNotCreatedYetReason is used when image is not yet created.
Expand All @@ -73,6 +81,9 @@ const (
// OpenStackImageNotImportedYetReason is used when image is not yet imported.
OpenStackImageNotImportedYetReason = "OpenStackImageNotImportedYet"

// OpenStackImageImportTimeOutReason is used when image import timeout.
OpenStackImageImportTimeOutReason = "OpenStackImageImportTimeOutReason"

// IssueWithOpenStackImageReason is used when image has an issue.
IssueWithOpenStackImageReason = "IssueWithOpenStackImage"
)
11 changes: 8 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func init() {
//+kubebuilder:scaffold:scheme
}

var releaseDir string
var (
releaseDir string
waitForImageBecomeActiveMinutes int
)

func main() {
var metricsAddr string
Expand All @@ -60,6 +63,7 @@ func main() {
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&releaseDir, "release-dir", "/tmp/downloads/", "Specify release directory for cluster-stack releases")
flag.IntVar(&waitForImageBecomeActiveMinutes, "import-timeout", 0, "Maximum time in minutes that you allow cspo to import image. If import-timeout <= 0, cspo waits forever.")
opts := zap.Options{
Development: true,
}
Expand Down Expand Up @@ -103,8 +107,9 @@ func main() {
os.Exit(1)
}
if err = (&controller.OpenStackNodeImageReleaseReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
WaitForImageBecomeActiveMinutes: waitForImageBecomeActiveMinutes,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OpenStackNodeImageRelease")
os.Exit(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func (r *OpenStackClusterStackReleaseReconciler) Reconcile(ctx context.Context,
return ctrl.Result{RequeueAfter: waitForOpenStackNodeImageReleasesBecomeReady}, nil
}
for _, openStackNodeImageRelease := range ownedOpenStackNodeImageReleases {
// TODO: Handle case when `import-timeout > 0`. Then the oscsr_controller should stop the reconciliation
if openStackNodeImageRelease.Status.Ready {
continue
}
Expand Down
27 changes: 25 additions & 2 deletions internal/controller/openstacknodeimagerelease_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ import (
// OpenStackNodeImageReleaseReconciler reconciles a OpenStackNodeImageRelease object.
type OpenStackNodeImageReleaseReconciler struct {
client.Client
Scheme *runtime.Scheme
Scheme *runtime.Scheme
WaitForImageBecomeActiveMinutes int
}

const (
Expand Down Expand Up @@ -143,6 +144,7 @@ func (r *OpenStackNodeImageReleaseReconciler) Reconcile(ctx context.Context, req

if imageID == "" {
conditions.MarkFalse(openstacknodeimagerelease, apiv1alpha1.OpenStackImageReadyCondition, apiv1alpha1.OpenStackImageNotCreatedYetReason, clusterv1beta1.ConditionSeverityInfo, "image is not created yet")
conditions.MarkFalse(openstacknodeimagerelease, apiv1alpha1.OpenStackImageImportStartCondition, apiv1alpha1.OpenStackImageImportNotStartReason, clusterv1beta1.ConditionSeverityInfo, "image import not start yet")
openstacknodeimagerelease.Status.Ready = false

imageCreateOpts := (*images.CreateOpts)(openstacknodeimagerelease.Spec.Image.CreateOpts)
Expand Down Expand Up @@ -172,6 +174,7 @@ func (r *OpenStackNodeImageReleaseReconciler) Reconcile(ctx context.Context, req
return ctrl.Result{}, fmt.Errorf("failed to import an image: %w", err)
}

conditions.MarkTrue(openstacknodeimagerelease, apiv1alpha1.OpenStackImageImportStartCondition)
// requeue to make sure that image ID can be find by image name
return ctrl.Result{Requeue: true}, nil
}
Expand All @@ -188,7 +191,27 @@ func (r *OpenStackNodeImageReleaseReconciler) Reconcile(ctx context.Context, req
return ctrl.Result{}, fmt.Errorf("failed to get an image: %w", err)
}

// TODO: Add timeout logic - import start time could be taken from OpenStackImageNotImportedYetReason condition, or somehow better
// Check wait for image ACTIVE status duration
if r.WaitForImageBecomeActiveMinutes > 0 && conditions.IsTrue(openstacknodeimagerelease, apiv1alpha1.OpenStackImageImportStartCondition) {
// Calculate elapsed time since the OpenStackImageImportStartCondition is true
startTime := conditions.GetLastTransitionTime(openstacknodeimagerelease, apiv1alpha1.OpenStackImageImportStartCondition)
elapsedTime := time.Since(startTime.Time)

waitForImageBecomeActiveTimeout := time.Duration(r.WaitForImageBecomeActiveMinutes) * time.Minute

// Check if the image has been active after waitForImageBecomeActiveTimeout minutes
if image.Status != images.ImageStatusActive && elapsedTime > waitForImageBecomeActiveTimeout {
err = fmt.Errorf("timeout - wait for the image %s to transition to the ACTIVE status exceeds the timeout duration %d minutes", image.Name, r.WaitForImageBecomeActiveMinutes)
conditions.MarkFalse(openstacknodeimagerelease,
apiv1alpha1.OpenStackImageReadyCondition,
apiv1alpha1.OpenStackImageImportTimeOutReason,
clusterv1beta1.ConditionSeverityError,
err.Error(),
)
// Image import timeout - nothing to do
return ctrl.Result{}, nil
}
}

// Manage image statuses according to the guidelines outlined in https://docs.openstack.org/glance/stein/user/statuses.html.
switch image.Status {
Expand Down

0 comments on commit 10fcbe1

Please sign in to comment.