@@ -23,8 +23,6 @@ import (
2323 ctrl "sigs.k8s.io/controller-runtime"
2424 "sigs.k8s.io/controller-runtime/pkg/client"
2525 "sigs.k8s.io/controller-runtime/pkg/cluster"
26- "sigs.k8s.io/controller-runtime/pkg/event"
27- "sigs.k8s.io/controller-runtime/pkg/predicate"
2826
2927 "github.com/openshift/hypershift-logging-operator/api/v1alpha1"
3028 "github.com/openshift/hypershift-logging-operator/controllers/hypershiftlogforwarder"
@@ -34,7 +32,10 @@ import (
3432 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3533)
3634
37- var hostedClusters = map [string ]hypershiftlogforwarder.HostedCluster {}
35+ var (
36+ clusterScheme = runtime .NewScheme ()
37+ hostedClusters = map [string ]hypershiftlogforwarder.HostedCluster {}
38+ )
3839
3940// HostedClusterReconciler reconciles a HostedCluster object
4041type HostedClusterReconciler struct {
@@ -62,12 +63,6 @@ func (r *HostedClusterReconciler) Reconcile(
6263 log := logr.Logger {}.WithName ("hostedcluster-controller" )
6364
6465 hostedCluster := & hyperv1beta1.HostedCluster {}
65- if err := r .Get (ctx , req .NamespacedName , hostedCluster ); err != nil {
66- // Ignore not-found errors, since they can't be fixed by an immediate
67- // requeue (we'll need to wait for a new notification).
68- return ctrl.Result {}, client .IgnoreNotFound (err )
69- }
70-
7166 found := false
7267 err := r .Get (ctx , req .NamespacedName , hostedCluster )
7368 if err != nil && errors .IsNotFound (err ) {
@@ -81,19 +76,22 @@ func (r *HostedClusterReconciler) Reconcile(
8176 _ , exist := hostedClusters [req .NamespacedName .Name ]
8277
8378 hcpNamespace := fmt .Sprintf ("%s-%s" , hostedCluster .Namespace , hostedCluster .Name )
79+ isReadyCluster := hostedcluster .IsReadyHostedCluster (* hostedCluster )
8480
8581 if ! exist {
8682 // check hosted cluster status, if it's new created and ready, start the reconcile
87- newReadyCluster := hostedcluster . IsReadyHostedCluster ( * hostedCluster )
88- if newReadyCluster {
83+
84+ if isReadyCluster {
8985 restConfig , err := hostedcluster .BuildGuestKubeConfig (r .Client , hcpNamespace , r .log )
9086 if err != nil {
9187 log .Error (err , "getting guest cluster kubeconfig" )
88+ return ctrl.Result {}, err
9289 }
9390
9491 hsCluster , err := cluster .New (restConfig )
9592 if err != nil {
9693 log .Error (err , "creating guest cluster kubeconfig" )
94+ return ctrl.Result {}, err
9795 }
9896 clusterScheme := hsCluster .GetScheme ()
9997 utilruntime .Must (hyperv1beta1 .AddToScheme (clusterScheme ))
@@ -128,8 +126,17 @@ func (r *HostedClusterReconciler) Reconcile(
128126 Namespace : constants .HLFWatchedNamespace ,
129127 })
130128
129+ if err != nil {
130+ log .Error (err , "creating new sub manager" )
131+ return ctrl.Result {}, err
132+ }
133+
131134 //Adding hosted cluster to sub manger
132- mgrHostedCluster .Add (hsCluster )
135+ err = mgrHostedCluster .Add (hsCluster )
136+ if err != nil {
137+ log .Error (err , "Adding hosted cluster runnable to sub manager" )
138+ return ctrl.Result {}, err
139+ }
133140
134141 go func () {
135142 err = ctrl .NewControllerManagedBy (mgrHostedCluster ).
@@ -148,31 +155,27 @@ func (r *HostedClusterReconciler) Reconcile(
148155 }
149156
150157 } else {
151- if ! found {
152- //if it's deleted, stop the reconcile
153- r .log .V (1 ).Info ("testing" , "found" , found )
158+ //Stop the controller when cluster is not ready or deleted
154159
160+ r .log .V (1 ).Info ("Stop existing managers" , "ready cluster" , isReadyCluster , "found" , found )
161+ validKubeConfig , _ := hostedcluster .ValidateKubeConfig (r .Client , hcpNamespace )
162+
163+ if ! isReadyCluster || ! found || ! validKubeConfig {
155164 cancelFunc := hostedClusters [req .NamespacedName .Name ].CancelFunc
156165 cancelFunc ()
157- r .log .V (1 ).Info ("finished context" )
166+ r .log .V (1 ).Info ("stop the manager" , "controller name" , hostedClusters [req .NamespacedName .Name ])
167+
158168 }
159169 }
160170
161171 return ctrl.Result {}, nil
162172}
163173
164- func eventPredicates () predicate.Predicate {
165- return predicate.Funcs {
166- DeleteFunc : func (e event.DeleteEvent ) bool {
167- return true
168- },
169- }
170- }
171-
172174// SetupWithManager sets up the controller with the Manager.
173175func (r * HostedClusterReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
174176 return ctrl .NewControllerManagedBy (mgr ).
175177 For (& hyperv1beta1.HostedCluster {}).
176- WithEventFilter (eventPredicates ()).
178+ //WithEventFilter(eventPredicates()).
179+ //WithEventFilter(predicate.ResourceVersionChangedPredicate{}).
177180 Complete (r )
178181}
0 commit comments