-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KUBESAW-12: Convert the health-check goroutine into ToolchainCluster …
…controller Signed-off-by: Feny Mehta <[email protected]>
- Loading branch information
Showing
3 changed files
with
154 additions
and
79 deletions.
There are no files selected for viewing
62 changes: 1 addition & 61 deletions
62
...rs/toolchainclustercache/healthchecker.go → ...s/toolchainclusterhealth/healthchecker.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
controllers/toolchainclusterhealth/toolchaincluster_healthcheck_controller.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package toolchainclusterhealth | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1" | ||
"github.com/codeready-toolchain/toolchain-common/pkg/cluster" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
kubeclientset "k8s.io/client-go/kubernetes" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
// NewReconciler returns a new Reconciler | ||
func NewReconciler(mgr manager.Manager, namespace string, timeout time.Duration, requeAfter time.Duration) *Reconciler { | ||
cacheLog := log.Log.WithName("toolchaincluster_health") | ||
clusterCacheService := cluster.NewToolchainClusterService(mgr.GetClient(), cacheLog, namespace, timeout) | ||
return &Reconciler{ | ||
client: mgr.GetClient(), | ||
scheme: mgr.GetScheme(), | ||
clusterCacheService: clusterCacheService, | ||
requeAfter: requeAfter, | ||
} | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&toolchainv1alpha1.ToolchainCluster{}). | ||
Complete(r) | ||
} | ||
|
||
// Reconciler reconciles a ToolchainCluster object | ||
type Reconciler struct { | ||
client client.Client | ||
scheme *runtime.Scheme | ||
clusterCacheService cluster.ToolchainClusterService | ||
requeAfter time.Duration | ||
} | ||
|
||
// Reconcile reads that state of the cluster for a ToolchainCluster object and makes changes based on the state read | ||
// and what is in the ToolchainCluster.Spec. It updates the status of the individual cluster | ||
// Note: | ||
// The Controller will requeue the Request to be processed again if the returned error is non-nil or | ||
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue. | ||
func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) { | ||
reqLogger := log.FromContext(ctx).WithName("health") | ||
reqLogger.Info("Reconciling ToolchainCluster") | ||
|
||
// Fetch the ToolchainCluster instance | ||
toolchainCluster := &toolchainv1alpha1.ToolchainCluster{} | ||
err := r.client.Get(ctx, request.NamespacedName, toolchainCluster) | ||
if err != nil { | ||
if errors.IsNotFound(err) { | ||
// Stop monitoring the toolchain cluster as it is deleted | ||
return reconcile.Result{}, nil | ||
} | ||
// Error reading the object - requeue the request. | ||
return reconcile.Result{}, err | ||
} | ||
|
||
cachedCluster, ok := cluster.GetCachedToolchainCluster(toolchainCluster.Name) | ||
if !ok { | ||
err := fmt.Errorf("cluster %s not found in cache", toolchainCluster.Name) | ||
reqLogger.Error(err, "failed to retrieve stored data for cluster") | ||
return reconcile.Result{}, err | ||
} | ||
|
||
clientSet, err := kubeclientset.NewForConfig(cachedCluster.RestConfig) | ||
if err != nil { | ||
reqLogger.Error(err, "cannot create ClientSet for a ToolchainCluster") | ||
return reconcile.Result{}, err | ||
} | ||
|
||
healthChecker := &HealthChecker{ | ||
localClusterClient: r.client, | ||
remoteClusterClient: cachedCluster.Client, | ||
remoteClusterClientset: clientSet, | ||
logger: reqLogger, | ||
} | ||
|
||
//update the status of the individual cluster. | ||
if err := healthChecker.updateIndividualClusterStatus(ctx, toolchainCluster); err != nil { | ||
reqLogger.Error(err, "unable to update cluster status of ToolchainCluster") | ||
return reconcile.Result{}, err | ||
} | ||
|
||
return reconcile.Result{RequeueAfter: r.requeAfter}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters