Skip to content
This repository was archived by the owner on Sep 3, 2025. It is now read-only.

Commit 0597427

Browse files
Fix HLF stop when hosted cluster unresponsive
1 parent ed3d07b commit 0597427

File tree

4 files changed

+57
-27
lines changed

4 files changed

+57
-27
lines changed

controllers/hostedcluster/hostedcluster_controller.go

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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
4041
type 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.
173175
func (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
}

controllers/hypershiftlogforwarder/hypershiftlogforwarder_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (r *HyperShiftLogForwarderReconciler) Reconcile(ctx context.Context, req ct
9090
return ctrl.Result{}, nil
9191
}
9292

93-
err := r.Get(ctx, req.NamespacedName, instance)
93+
err := r.Get(context.TODO(), req.NamespacedName, instance)
9494
if errors.IsNotFound(err) {
9595
return ctrl.Result{}, nil
9696
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import (
3131
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3232

3333
"github.com/openshift/hypershift-logging-operator/api/v1alpha1"
34-
"github.com/openshift/hypershift-logging-operator/controllers/clusterlogforwardertemplate"
3534

35+
"github.com/openshift/hypershift-logging-operator/controllers/clusterlogforwardertemplate"
3636
hostedclustercontroller "github.com/openshift/hypershift-logging-operator/controllers/hostedcluster"
3737

3838
loggingv1 "github.com/openshift/cluster-logging-operator/apis/logging/v1"

pkg/hostedcluster/hosted_cluster.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,30 @@ func BuildGuestKubeConfig(
160160

161161
return restConfig, nil
162162
}
163+
164+
// Validate kube config
165+
func ValidateKubeConfig(c client.Client, hcpNamespace string) (bool, error) {
166+
167+
//check the secrets
168+
secret := &corev1.Secret{
169+
ObjectMeta: metav1.ObjectMeta{
170+
Name: KubeConfigSecret,
171+
Namespace: hcpNamespace,
172+
},
173+
}
174+
175+
if err := c.Get(context.Background(), client.ObjectKeyFromObject(secret), secret); err != nil {
176+
return false, fmt.Errorf("failed to get hostedcluster admin kubeconfig: %w", err)
177+
}
178+
179+
//check the kubeconfig
180+
kubeConfig, err := clientcmd.Load(secret.Data["kubeconfig"])
181+
if err != nil {
182+
return false, fmt.Errorf("failed to parse kubeconfig from the secret: %w", err)
183+
}
184+
if len(kubeConfig.Clusters) == 0 {
185+
return false, fmt.Errorf("no clusters found in admin kubeconfig")
186+
}
187+
188+
return true, nil
189+
}

0 commit comments

Comments
 (0)