Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FullyApplied to binding #825

Merged
merged 1 commit into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder who set lastTransitionTime for us?

  - lastTransitionTime: "2021-10-21T02:16:18Z"
    message: All works have been successfully applied
    reason: FullyAppliedSuccess
    status: "True"
    type: FullyApplied

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/lonelyCZ/karmada/blob/master/vendor/k8s.io/apimachinery/pkg/apis/meta/v1/types.go#L1412-L1418

It seem automatically set lastTransitionTime.

Now the new status is shown below:

...
conditions:
  - lastTransitionTime: "2021-10-21T03:58:22Z"
    message: the binding has been scheduled
    reason: BindingScheduled
    status: "True"
    type: Scheduled
  - lastTransitionTime: "2021-10-21T03:58:23Z"
    message: All works have been successfully applied
    reason: FullyAppliedSuccess
    status: "True"
    type: FullyApplied

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if newCondition.LastTransitionTime.IsZero() {
newCondition.LastTransitionTime = metav1.NewTime(time.Now())
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing that out!

}

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
}