Skip to content

Commit

Permalink
Add FullyApplied to binding
Browse files Browse the repository at this point in the history
Signed-off-by: lonelyCZ <[email protected]>
  • Loading branch information
lonelyCZ committed Oct 20, 2021
1 parent e9f4bde commit f3eae22
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/apis/work/v1alpha2/binding_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ type AggregatedStatusItem struct {
const (
// Scheduled represents the condition that the ResourceBinding or ClusterResourceBinding has been scheduled.
Scheduled string = "Scheduled"

// FullyApplied represents the condition that the resource referencing by ResourceBinding or ClusterResourceBinding
// has been applied to all scheduled clusters.
FullyApplied string = "FullyApplied"
)

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
28 changes: 28 additions & 0 deletions pkg/controllers/binding/binding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/dynamic"
Expand Down Expand Up @@ -141,6 +142,18 @@ func (c *ResourceBindingController) syncBinding(binding *workv1alpha2.ResourceBi
if len(errs) > 0 {
return controllerruntime.Result{Requeue: true}, errors.NewAggregate(errs)
}

fullyAppliedCondition := meta.FindStatusCondition(binding.Status.Conditions, workv1alpha2.FullyApplied)
if fullyAppliedCondition == nil {
if len(binding.Spec.Clusters) == len(binding.Status.AggregatedStatus) && areWorksFullyApplied(binding.Status.AggregatedStatus) {
err := c.updateFullyAppliedCondition(binding)
if err != nil {
klog.Errorf("Failed to update FullyApplied status for given resourceBinding(%s/%s). Error: %v.", binding.Namespace, binding.Name, err)
return controllerruntime.Result{Requeue: true}, err
}
}
}

return controllerruntime.Result{}, nil
}

Expand Down Expand Up @@ -252,3 +265,18 @@ func (c *ResourceBindingController) newReplicaSchedulingPolicyFunc() handler.Map
return requests
}
}

// updateFullyAppliedCondition update the FullyApplied condition for the given ResourceBinding
func (c *ResourceBindingController) updateFullyAppliedCondition(binding *workv1alpha2.ResourceBinding) error {
newBindingFullyAppliedCondition := metav1.Condition{
Type: workv1alpha2.FullyApplied,
Status: metav1.ConditionTrue,
Reason: FullyAppliedSuccessReason,
Message: FullyAppliedSuccessMessage,
}

meta.SetStatusCondition(&binding.Status.Conditions, newBindingFullyAppliedCondition)
err := c.Client.Status().Update(context.TODO(), binding)

return err
}
27 changes: 27 additions & 0 deletions pkg/controllers/binding/cluster_resource_binding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/dynamic"
Expand Down Expand Up @@ -137,6 +138,17 @@ func (c *ClusterResourceBindingController) syncBinding(binding *workv1alpha2.Clu
return controllerruntime.Result{Requeue: true}, errors.NewAggregate(errs)
}

fullyAppliedCondition := meta.FindStatusCondition(binding.Status.Conditions, workv1alpha2.FullyApplied)
if fullyAppliedCondition == nil {
if len(binding.Spec.Clusters) == len(binding.Status.AggregatedStatus) && areWorksFullyApplied(binding.Status.AggregatedStatus) {
err := c.updateFullyAppliedCondition(binding)
if err != nil {
klog.Errorf("Failed to update FullyApplied status for given clusterResourceBinding(%s), Error: %v", binding.Name, err)
return controllerruntime.Result{Requeue: true}, err
}
}
}

return controllerruntime.Result{}, nil
}

Expand Down Expand Up @@ -244,3 +256,18 @@ func (c *ClusterResourceBindingController) newReplicaSchedulingPolicyFunc() hand
return requests
}
}

// updateFullyAppliedCondition update the FullyApplied condition for the given ClusterResourceBinding
func (c *ClusterResourceBindingController) updateFullyAppliedCondition(binding *workv1alpha2.ClusterResourceBinding) error {
newBindingFullyAppliedCondition := metav1.Condition{
Type: workv1alpha2.FullyApplied,
Status: metav1.ConditionTrue,
Reason: FullyAppliedSuccessReason,
Message: FullyAppliedSuccessMessage,
}

meta.SetStatusCondition(&binding.Status.Conditions, newBindingFullyAppliedCondition)
err := c.Client.Status().Update(context.TODO(), binding)

return err
}
16 changes: 16 additions & 0 deletions pkg/controllers/binding/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import (
"github.com/karmada-io/karmada/pkg/util/overridemanager"
)

const (
FullyAppliedSuccessReason = "FullyAppliedSuccess"

FullyAppliedSuccessMessage = "All works have been successfully applied"
)

var workPredicateFn = builder.WithPredicates(predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return false
Expand Down Expand Up @@ -323,3 +329,13 @@ func calculateReplicas(c client.Client, policy *policyv1alpha1.ReplicaScheduling

return desireReplicaInfos, nil
}

// areWorksFullyApplied checks if all works are applied for a Binding
func areWorksFullyApplied(aggregatedStatuses []workv1alpha2.AggregatedStatusItem) bool {
for _, aggregatedSatusItem := range aggregatedStatuses {
if !aggregatedSatusItem.Applied {
return false
}
}
return true
}

0 comments on commit f3eae22

Please sign in to comment.