mirror of
https://github.com/open-cluster-management-io/ocm.git
synced 2026-08-23 22:26:49 +00:00
* Use basecontroller in sdk-go instead for better logging Signed-off-by: Jian Qiu <jqiu@redhat.com> * Rename to fakeSyncContext Signed-off-by: Jian Qiu <jqiu@redhat.com> --------- Signed-off-by: Jian Qiu <jqiu@redhat.com>
96 lines
3.2 KiB
Go
96 lines
3.2 KiB
Go
package taint
|
|
|
|
import (
|
|
"context"
|
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/klog/v2"
|
|
|
|
clientset "open-cluster-management.io/api/client/cluster/clientset/versioned"
|
|
informerv1 "open-cluster-management.io/api/client/cluster/informers/externalversions/cluster/v1"
|
|
listerv1 "open-cluster-management.io/api/client/cluster/listers/cluster/v1"
|
|
v1 "open-cluster-management.io/api/cluster/v1"
|
|
"open-cluster-management.io/sdk-go/pkg/basecontroller/factory"
|
|
"open-cluster-management.io/sdk-go/pkg/patcher"
|
|
|
|
"open-cluster-management.io/ocm/pkg/common/queue"
|
|
"open-cluster-management.io/ocm/pkg/registration/helpers"
|
|
)
|
|
|
|
var (
|
|
UnavailableTaint = v1.Taint{
|
|
Key: v1.ManagedClusterTaintUnavailable,
|
|
Effect: v1.TaintEffectNoSelect,
|
|
}
|
|
|
|
UnreachableTaint = v1.Taint{
|
|
Key: v1.ManagedClusterTaintUnreachable,
|
|
Effect: v1.TaintEffectNoSelect,
|
|
}
|
|
)
|
|
|
|
// taintController
|
|
type taintController struct {
|
|
patcher patcher.Patcher[*v1.ManagedCluster, v1.ManagedClusterSpec, v1.ManagedClusterStatus]
|
|
clusterLister listerv1.ManagedClusterLister
|
|
}
|
|
|
|
// NewTaintController creates a new taint controller
|
|
func NewTaintController(
|
|
clusterClient clientset.Interface,
|
|
clusterInformer informerv1.ManagedClusterInformer) factory.Controller {
|
|
c := &taintController{
|
|
patcher: patcher.NewPatcher[
|
|
*v1.ManagedCluster, v1.ManagedClusterSpec, v1.ManagedClusterStatus](
|
|
clusterClient.ClusterV1().ManagedClusters()),
|
|
clusterLister: clusterInformer.Lister(),
|
|
}
|
|
return factory.New().
|
|
WithInformersQueueKeysFunc(queue.QueueKeyByMetaName, clusterInformer.Informer()).
|
|
WithSync(c.sync).
|
|
ToController("taintController")
|
|
}
|
|
|
|
func (c *taintController) sync(ctx context.Context, syncCtx factory.SyncContext, managedClusterName string) error {
|
|
logger := klog.FromContext(ctx).WithValues("managedClusterName", managedClusterName)
|
|
logger.V(4).Info("Reconciling ManagedCluster")
|
|
managedCluster, err := c.clusterLister.Get(managedClusterName)
|
|
if errors.IsNotFound(err) {
|
|
// Spoke cluster not found, could have been deleted, do nothing.
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !managedCluster.DeletionTimestamp.IsZero() {
|
|
return nil
|
|
}
|
|
|
|
newManagedCluster := managedCluster.DeepCopy()
|
|
newTaints := newManagedCluster.Spec.Taints
|
|
cond := meta.FindStatusCondition(newManagedCluster.Status.Conditions, v1.ManagedClusterConditionAvailable)
|
|
var updated bool
|
|
|
|
switch {
|
|
case cond == nil || cond.Status == metav1.ConditionUnknown:
|
|
updated = helpers.RemoveTaints(&newTaints, UnavailableTaint)
|
|
updated = helpers.AddTaints(&newTaints, UnreachableTaint) || updated
|
|
case cond.Status == metav1.ConditionFalse:
|
|
updated = helpers.RemoveTaints(&newTaints, UnreachableTaint)
|
|
updated = helpers.AddTaints(&newTaints, UnavailableTaint) || updated
|
|
case cond.Status == metav1.ConditionTrue:
|
|
updated = helpers.RemoveTaints(&newTaints, UnavailableTaint, UnreachableTaint)
|
|
}
|
|
|
|
if updated {
|
|
newManagedCluster.Spec.Taints = newTaints
|
|
if _, err = c.patcher.PatchSpec(ctx, newManagedCluster, newManagedCluster.Spec, managedCluster.Spec); err != nil {
|
|
return err
|
|
}
|
|
syncCtx.Recorder().Eventf(ctx, "ManagedClusterConditionAvailableUpdated", "Update the original taints to the %+v", newTaints)
|
|
}
|
|
return nil
|
|
}
|