Skip to content

Commit

Permalink
Update the mgh status for the deployment (#1049)
Browse files Browse the repository at this point in the history
Signed-off-by: myan <[email protected]>
  • Loading branch information
yanmxa authored Aug 12, 2024
1 parent cd57751 commit 86ec59c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
20 changes: 16 additions & 4 deletions operator/pkg/controllers/hubofhubs/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -179,15 +180,26 @@ func addGlobalHubControllerWatches(mgr ctrl.Manager, globalHubController control
return err
}

// Custom predicate to handle status changes
if err := globalHubController.Watch(
source.Kind(mgr.GetCache(), &appsv1.Deployment{},
handler.TypedEnqueueRequestForOwner[*appsv1.Deployment](
schema, restMapper, &v1alpha4.MulticlusterGlobalHub{}, handler.OnlyControllerOwner()),
[]predicate.TypedPredicate[*appsv1.Deployment]{
predicate.TypedGenerationChangedPredicate[*appsv1.Deployment]{},
// predicate.NewTypedPredicateFuncs[*appsv1.Deployment](func(node *appsv1.Deployment) bool {
// return handleDeployment(deployment)
// },
predicate.TypedFuncs[*appsv1.Deployment]{
CreateFunc: func(e event.TypedCreateEvent[*appsv1.Deployment]) bool {
return true
},
// status changes
UpdateFunc: func(e event.TypedUpdateEvent[*appsv1.Deployment]) bool {
oldDeployment := e.ObjectOld
newDeployment := e.ObjectNew
return !equality.Semantic.DeepEqual(oldDeployment.Status, newDeployment.Status)
},
DeleteFunc: func(e event.TypedDeleteEvent[*appsv1.Deployment]) bool {
return true
},
},
}...)); err != nil {
return err
}
Expand Down
28 changes: 12 additions & 16 deletions operator/pkg/controllers/hubofhubs/status/status_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog"
Expand Down Expand Up @@ -107,23 +106,20 @@ func (r *StatusReconciler) updateDeploymentStatus(ctx context.Context,
}

deployment := &appsv1.Deployment{}
if err := r.Client.Get(ctx, types.NamespacedName{
err := r.Client.Get(ctx, types.NamespacedName{
Name: deployName,
Namespace: mgh.Namespace,
}, deployment); err != nil && errors.IsNotFound(err) {
// deployment not found, ignore
r.log.Info("deployment not found", "name", deployName, "namespace", mgh.Namespace)
return nil
} else if err != nil {
return err
}

for _, cond := range deployment.Status.Conditions {
if cond.Type == appsv1.DeploymentAvailable {
desiredCondition.Status = metav1.ConditionStatus(cond.Status)
desiredCondition.Reason = cond.Reason
desiredCondition.Message = cond.Message
desiredCondition.LastTransitionTime = cond.LastTransitionTime
}, deployment)
if err != nil {
desiredCondition.Message = err.Error()
} else {
for _, cond := range deployment.Status.Conditions {
if cond.Type == appsv1.DeploymentAvailable {
desiredCondition.Status = metav1.ConditionStatus(cond.Status)
desiredCondition.Reason = cond.Reason
desiredCondition.Message = cond.Message
desiredCondition.LastTransitionTime = cond.LastTransitionTime
}
}
}

Expand Down

0 comments on commit 86ec59c

Please sign in to comment.