Skip to content

Commit

Permalink
add membercluster status controller
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlihanbo authored and RainbowMango committed Dec 23, 2020
1 parent 226ca3b commit fb91d91
Show file tree
Hide file tree
Showing 7 changed files with 398 additions and 171 deletions.
9 changes: 9 additions & 0 deletions cmd/controller-manager/app/controllermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ func setupControllers(mgr controllerruntime.Manager) {
klog.Fatalf("Failed to setup membercluster controller: %v", err)
}

MemberClusterStatusController := &status.MemberClusterStatusController{
Client: mgr.GetClient(),
KubeClientSet: kubeClientSet,
EventRecorder: mgr.GetEventRecorderFor(status.ControllerName),
}
if err := MemberClusterStatusController.SetupWithManager(mgr); err != nil {
klog.Fatalf("Failed to setup memberclusterstatus controller: %v", err)
}

policyController := &policy.PropagationPolicyController{
Client: mgr.GetClient(),
DynamicClient: dynamicClientSet,
Expand Down
16 changes: 2 additions & 14 deletions pkg/controllers/execution/execution_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,6 @@ func (c *Controller) syncWork(propagationWork *propagationstrategy.PropagationWo
return controllerruntime.Result{}, nil
}

// isMemberClusterReady checking readiness for the given member cluster
func (c *Controller) isMemberClusterReady(clusterStatus *v1alpha1.MemberClusterStatus) bool {
for _, condition := range clusterStatus.Conditions {
if condition.Type == "ClusterReady" {
if condition.Status == v1.ConditionTrue {
return true
}
}
}
return false
}

// isResourceApplied checking weather resource has been dispatched to member cluster or not
func (c *Controller) isResourceApplied(propagationWorkStatus *propagationstrategy.PropagationWorkStatus) bool {
for _, condition := range propagationWorkStatus.Conditions {
Expand Down Expand Up @@ -135,7 +123,7 @@ func (c *Controller) tryDeleteWorkload(propagationWork *propagationstrategy.Prop
}

// Do not clean up resource in the given member cluster if the status of the given member cluster is unready
if !c.isMemberClusterReady(&memberCluster.Status) {
if !util.IsMemberClusterReady(&memberCluster.Status) {
klog.Infof("Do not clean up resource in the given member cluster if the status of the given member cluster %s is unready", memberCluster.Name)
return nil
}
Expand Down Expand Up @@ -177,7 +165,7 @@ func (c *Controller) dispatchPropagationWork(propagationWork *propagationstrateg
return err
}

if !c.isMemberClusterReady(&memberCluster.Status) {
if !util.IsMemberClusterReady(&memberCluster.Status) {
klog.Errorf("The status of the given member cluster %s is unready", memberCluster.Name)
return fmt.Errorf("cluster %s is not ready, requeuing operation until cluster state is ready", memberCluster.Name)
}
Expand Down
42 changes: 17 additions & 25 deletions pkg/controllers/membercluster/membercluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package membercluster
import (
"context"
"fmt"
"time"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -23,10 +22,11 @@ import (

const (
// ControllerName is the controller name that will be used when reporting events.
ControllerName = "membercluster-controller"
finalizer = "karmada.io/membercluster-controller"
executionSpaceLabelKey = "karmada.io/executionspace"
executionSpaceLabelValue = ""
ControllerName = "membercluster-controller"
// FinalizerMemberClusterController is the finalizer added by membercluster controller
FinalizerMemberClusterController = "karmada.io/membercluster-controller"
executionSpaceLabelKey = "karmada.io/executionspace"
executionSpaceLabelValue = ""
)

// Controller is to sync MemberCluster.
Expand Down Expand Up @@ -72,18 +72,7 @@ func (c *Controller) syncMemberCluster(memberCluster *v1alpha1.MemberCluster) (c
}

// ensure finalizer
updated, err := c.ensureFinalizer(memberCluster)
if err != nil {
return controllerruntime.Result{Requeue: true}, err
} else if updated {
return controllerruntime.Result{}, nil
}

// update status of the given member cluster
// TODO: update status of member cluster in status controller.
updateIndividualClusterStatus(memberCluster, c.Client, c.KubeClientSet)

return controllerruntime.Result{RequeueAfter: 10 * time.Second}, nil
return c.ensureFinalizer(memberCluster)
}

func (c *Controller) removeMemberCluster(memberCluster *v1alpha1.MemberCluster) (controllerruntime.Result, error) {
Expand Down Expand Up @@ -148,10 +137,10 @@ func (c *Controller) removeFinalizer(memberCluster *v1alpha1.MemberCluster) (con
return controllerruntime.Result{Requeue: true}, err
}
finalizers := sets.NewString(accessor.GetFinalizers()...)
if !finalizers.Has(finalizer) {
if !finalizers.Has(FinalizerMemberClusterController) {
return controllerruntime.Result{}, nil
}
finalizers.Delete(finalizer)
finalizers.Delete(FinalizerMemberClusterController)
accessor.SetFinalizers(finalizers.List())
err = c.Client.Update(context.TODO(), memberCluster)
if err != nil {
Expand All @@ -160,19 +149,22 @@ func (c *Controller) removeFinalizer(memberCluster *v1alpha1.MemberCluster) (con
return controllerruntime.Result{}, nil
}

func (c *Controller) ensureFinalizer(memberCluster *v1alpha1.MemberCluster) (bool, error) {
func (c *Controller) ensureFinalizer(memberCluster *v1alpha1.MemberCluster) (controllerruntime.Result, error) {
accessor, err := meta.Accessor(memberCluster)
if err != nil {
return false, err
return controllerruntime.Result{Requeue: true}, err
}
finalizers := sets.NewString(accessor.GetFinalizers()...)
if finalizers.Has(finalizer) {
return false, nil
if finalizers.Has(FinalizerMemberClusterController) {
return controllerruntime.Result{}, nil
}
finalizers.Insert(finalizer)
finalizers.Insert(FinalizerMemberClusterController)
accessor.SetFinalizers(finalizers.List())
err = c.Client.Update(context.TODO(), memberCluster)
return true, err
if err != nil {
return controllerruntime.Result{Requeue: true}, err
}
return controllerruntime.Result{}, nil
}

// createExecutionSpace create member cluster execution space when member cluster joined
Expand Down
127 changes: 0 additions & 127 deletions pkg/controllers/membercluster/membercluster_status.go

This file was deleted.

Loading

0 comments on commit fb91d91

Please sign in to comment.