Skip to content

Commit

Permalink
add FullyApplied to binding
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelyCZ committed Oct 16, 2021
1 parent e55ed1e commit 5d88838
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/apis/work/v1alpha2/binding_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ type AggregatedStatusItem struct {
AppliedMessage string `json:"appliedMessage,omitempty"`
}

// Conditions definition
const (
// 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

// ResourceBindingList contains a list of ResourceBinding.
Expand Down
31 changes: 31 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 @@ -139,6 +140,21 @@ func (c *ResourceBindingController) syncBinding(binding *workv1alpha2.ResourceBi
klog.V(4).Infof(msg)
c.EventRecorder.Event(binding, corev1.EventTypeNormal, eventReasonAggregateStatusSucceed, msg)
}

if len(binding.Spec.Clusters) == len(binding.Status.AggregatedStatus) && areWorksFullyApplied(binding.Status.AggregatedStatus) {
err := c.updateFullyAppliedCondition(binding, metav1.ConditionTrue, "FullyAppliedSuccess", "All works have been successfully applied.")
if err != nil {
klog.Errorf("Failed to update FullyApplied status for given resourceBinding(%s/%s). Error: %v.", binding.Namespace, binding.Name, err)
errs = append(errs, err)
}
} else {
err := c.updateFullyAppliedCondition(binding, metav1.ConditionFalse, "FullyAppliedFalse", "Failed to apply all works.")
if err != nil {
klog.Errorf("Failed to update FullyApplied status for given resourceBinding(%s/%s). Error: %v.", binding.Namespace, binding.Name, err)
errs = append(errs, err)
}
}

if len(errs) > 0 {
return controllerruntime.Result{Requeue: true}, errors.NewAggregate(errs)
}
Expand Down Expand Up @@ -253,3 +269,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, status metav1.ConditionStatus, reason, message string) error {
newBindingFullyAppliedCondition := metav1.Condition{
Type: workv1alpha2.FullyApplied,
Status: status,
Reason: reason,
Message: message,
LastTransitionTime: metav1.Now(),
}

meta.SetStatusCondition(&binding.Status.Conditions, newBindingFullyAppliedCondition)
err := c.Client.Status().Update(context.TODO(), binding)
return err
}
31 changes: 31 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 @@ -134,6 +135,21 @@ func (c *ClusterResourceBindingController) syncBinding(binding *workv1alpha2.Clu
klog.V(4).Infof(msg)
c.EventRecorder.Event(binding, corev1.EventTypeNormal, eventReasonAggregateStatusSucceed, msg)
}

if len(binding.Spec.Clusters) == len(binding.Status.AggregatedStatus) && areWorksFullyApplied(binding.Status.AggregatedStatus) {
err := c.updateFullyAppliedCondition(binding, metav1.ConditionTrue, "FullyAppliedSuccess", "All works have been successfully applied.")
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
}
} else {
err := c.updateFullyAppliedCondition(binding, metav1.ConditionFalse, "FullyAppliedFalse", "Failed to apply all works.")
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
}
}

if len(errs) > 0 {
return controllerruntime.Result{Requeue: true}, errors.NewAggregate(errs)
}
Expand Down Expand Up @@ -245,3 +261,18 @@ func (c *ClusterResourceBindingController) newReplicaSchedulingPolicyFunc() hand
return requests
}
}

// updateFullyAppliedCondition update the FullyApplied condition for the given ClusterResourceBinding
func (c *ClusterResourceBindingController) updateFullyAppliedCondition(binding *workv1alpha2.ClusterResourceBinding, status metav1.ConditionStatus, reason, message string) error {
newBindingFullyAppliedCondition := metav1.Condition{
Type: workv1alpha2.FullyApplied,
Status: status,
Reason: reason,
Message: message,
LastTransitionTime: metav1.Now(),
}

meta.SetStatusCondition(&binding.Status.Conditions, newBindingFullyAppliedCondition)
err := c.Client.Status().Update(context.TODO(), binding)
return err
}
10 changes: 10 additions & 0 deletions pkg/controllers/binding/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,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 5d88838

Please sign in to comment.