-
Notifications
You must be signed in to change notification settings - Fork 0
added ready status update when all components are ready #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,15 +18,18 @@ package controllers | |
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| v1alpha1 "github.com/entgigi/upgrade-operator.git/api/v1alpha1" | ||
| "github.com/entgigi/upgrade-operator.git/api/v1alpha1" | ||
| "github.com/entgigi/upgrade-operator.git/controllers/reconciliation" | ||
| "github.com/entgigi/upgrade-operator.git/utils" | ||
| "github.com/go-logr/logr" | ||
| appsv1 "k8s.io/api/apps/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
| "sigs.k8s.io/controller-runtime/pkg/predicate" | ||
| "sigs.k8s.io/controller-runtime/pkg/handler" | ||
| "sigs.k8s.io/controller-runtime/pkg/source" | ||
| ) | ||
|
|
||
| const ( | ||
|
|
@@ -51,14 +54,57 @@ type EntandoAppV2Reconciler struct { | |
|
|
||
| func (r *EntandoAppV2Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
| log := r.Log.WithName(controllerLogName) | ||
| log.Info("Start reconciling EntandoAppV2 custom resources") | ||
| log.Info("Start reconciling EntandoAppV2 custom resources", "req", req) | ||
|
|
||
| entandoAppV2 := v1alpha1.EntandoAppV2{} | ||
| deployment := appsv1.Deployment{} | ||
| err := r.Client.Get(ctx, req.NamespacedName, &entandoAppV2) | ||
| if err != nil { | ||
| return ctrl.Result{}, client.IgnoreNotFound(err) | ||
| err2 := r.Client.Get(ctx, req.NamespacedName, &deployment) | ||
|
|
||
| if err2 == nil { | ||
| log.Info("is a deployment") | ||
| // are all deployment ready? | ||
| list := []string{"default-sso-in-namespace-deployment", "quickstart-ab-deployment", "quickstart-cm-deployment", "quickstart-deployment"} | ||
| counter := 0 | ||
| for _, componentname := range list { | ||
|
|
||
| r.Client.Get(ctx, types.NamespacedName{ | ||
| Namespace: req.Namespace, | ||
| Name: componentname, | ||
| }, &deployment) | ||
|
|
||
| //log.Info("deployment", "deployment", deployment) | ||
| // idx := slices.IndexFunc(deployment.Status.Conditions, func(c appsv1.DeploymentCondition) bool { return c.Type == "Available" && c.Status == "True" }) | ||
| if deployment.Status.ReadyReplicas > 0 { | ||
| counter++ | ||
| } | ||
| log.Info("ReadyReplicas has value", "deployment.Status.ReadyReplicas", deployment.Status.ReadyReplicas) | ||
| } | ||
| log.Info("counter has value", "counter", counter) | ||
| if counter == 4 { | ||
| statusUpdater := reconciliation.NewStatusUpdater(r.Client, r.Log) | ||
| statusUpdater.SetReady(ctx, types.NamespacedName{ | ||
| Namespace: req.Namespace, | ||
| Name: "entando-app-v2-sample", | ||
| }) | ||
| } else { | ||
| statusUpdater := reconciliation.NewStatusUpdater(r.Client, r.Log) | ||
| statusUpdater.SetNotReady(ctx, types.NamespacedName{ | ||
| Namespace: req.Namespace, | ||
| Name: "entando-app-v2-sample", | ||
| }) | ||
| } | ||
|
|
||
| return ctrl.Result{}, nil | ||
|
|
||
| } else { | ||
| log.Info("is a CR") | ||
| } | ||
|
|
||
| /*if err != nil { | ||
| return ctrl.Result{}, client.IgnoreNotFound(err) | ||
| }*/ | ||
|
|
||
| // Check if the EntandoApp instance is marked to be deleted, which is | ||
| // indicated by the deletion timestamp being set. | ||
| isEntandoAppV2MarkedToBeDeleted := entandoAppV2.GetDeletionTimestamp() != nil | ||
|
|
@@ -92,8 +138,10 @@ func (r *EntandoAppV2Reconciler) SetupWithManager(mgr ctrl.Manager) error { | |
| //log := r.Log.WithName("Upgrade Controller") | ||
| return ctrl.NewControllerManagedBy(mgr). | ||
| // FIXME! add filter on create for EntandoAppV2 cr | ||
| For(&v1alpha1.EntandoAppV2{}). | ||
| WithEventFilter(predicate.GenerationChangedPredicate{}). //solo modifiche a spec | ||
| For(&v1alpha1.EntandoAppV2{}, utils.ForOptionCustom()). | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. custom predicates can be used in For and in Watches methods, in this way it is possible to filter the events based on the type of resource, for example different behaviors from CR and deployments events can be used |
||
| Watches(&source.Kind{Type: &appsv1.Deployment{}}, &handler.EnqueueRequestForObject{}). | ||
| //WithEventFilter(predicate.GenerationChangedPredicate{}). //solo modifiche a spec | ||
| //Watches(&source.Kind{Type: &appsv1.Deployment{}}, &handler.EnqueueRequestForObject{}). | ||
| Complete(r) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,8 +16,9 @@ const ( | |
| statusUpdaterLogName = "StatusUpdater" | ||
|
|
||
| // Condition Types | ||
| Ready = "Ready" | ||
| Succeeded = "Succeeded" | ||
| Ready = "Ready" | ||
| Succeeded = "Succeeded" | ||
| Initialized = "Initialized" | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I only used Ready and Initialized |
||
|
|
||
| // Condition Reasons | ||
| CustomResourceChanged = "CustomResourceChanged" | ||
|
|
@@ -147,6 +148,63 @@ func (su *StatusUpdater) IncrementProgress(ctx context.Context, key types.Namesp | |
| return cr, err | ||
| } | ||
|
|
||
| func (su *StatusUpdater) SetInitialized(ctx context.Context, key types.NamespacedName) (*v1alpha1.EntandoAppV2, error) { | ||
| cr, err := su.updateStatus(ctx, key, func(cr *v1alpha1.EntandoAppV2) { | ||
| meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{ | ||
| Type: Initialized, | ||
| Status: metav1.ConditionTrue, | ||
| Reason: "ImageWritten", | ||
| Message: "Image written in components", | ||
| }) | ||
| }) | ||
|
|
||
| if err != nil { | ||
| su.log.Error(err, "Unable to update EntandoAppV2's status for reconciliation initialized") | ||
| } | ||
|
|
||
| return cr, err | ||
| } | ||
|
|
||
| func (su *StatusUpdater) SetReady(ctx context.Context, key types.NamespacedName) (*v1alpha1.EntandoAppV2, error) { | ||
| cr, err := su.updateStatus(ctx, key, func(cr *v1alpha1.EntandoAppV2) { | ||
| meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{ | ||
| Type: Ready, | ||
| Status: metav1.ConditionTrue, | ||
| Reason: "Ready", | ||
| Message: "All components are Ready", | ||
| }) | ||
| meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{ | ||
| Type: Initialized, | ||
| Status: metav1.ConditionFalse, | ||
| Reason: "ReconciliationDone", | ||
| Message: "Reconciliation process success", | ||
| }) | ||
| }) | ||
|
|
||
| if err != nil { | ||
| su.log.Error(err, "Unable to update EntandoAppV2's status for reconciliation initialized") | ||
| } | ||
|
|
||
| return cr, err | ||
| } | ||
|
|
||
| func (su *StatusUpdater) SetNotReady(ctx context.Context, key types.NamespacedName) (*v1alpha1.EntandoAppV2, error) { | ||
| cr, err := su.updateStatus(ctx, key, func(cr *v1alpha1.EntandoAppV2) { | ||
| meta.SetStatusCondition(&cr.Status.Conditions, metav1.Condition{ | ||
| Type: Ready, | ||
| Status: metav1.ConditionFalse, | ||
| Reason: "Ready", | ||
| Message: "All components are not Ready", | ||
| }) | ||
| }) | ||
|
|
||
| if err != nil { | ||
| su.log.Error(err, "Unable to update EntandoAppV2's status for reconciliation initialized") | ||
| } | ||
|
|
||
| return cr, err | ||
| } | ||
|
|
||
| func (su *StatusUpdater) updateStatus(ctx context.Context, key types.NamespacedName, updateFields func(cr *v1alpha1.EntandoAppV2)) (*v1alpha1.EntandoAppV2, error) { | ||
| cr := &v1alpha1.EntandoAppV2{} | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adds
additionalPrinterColumnsto CRD, in this way it's easier to see its evolution with kubectl