Skip to content

Commit

Permalink
move binding's namespace/name from work's label to annotation
Browse files Browse the repository at this point in the history
Signed-off-by: changzhen <[email protected]>
  • Loading branch information
XiShanYongYe-Chang committed Sep 22, 2021
1 parent e20aada commit 3f63536
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 54 deletions.
35 changes: 22 additions & 13 deletions pkg/controllers/binding/binding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -46,10 +45,7 @@ func (c *ResourceBindingController) Reconcile(ctx context.Context, req controlle
if err := c.Client.Get(context.TODO(), req.NamespacedName, binding); err != nil {
// The resource no longer exist, clean up derived Work objects.
if apierrors.IsNotFound(err) {
return helper.DeleteWorks(c.Client, labels.Set{
workv1alpha1.ResourceBindingNamespaceLabel: req.Namespace,
workv1alpha1.ResourceBindingNameLabel: req.Name,
})
return helper.DeleteWorkByRBNamespaceAndName(c.Client, req.Namespace, req.Name)
}

return controllerruntime.Result{Requeue: true}, err
Expand Down Expand Up @@ -116,18 +112,31 @@ func (c *ResourceBindingController) SetupWithManager(mgr controllerruntime.Manag
func(a client.Object) []reconcile.Request {
var requests []reconcile.Request

// TODO: Delete this logic in the next release to prevent incompatibility when upgrading the current release (v0.9.0).
labels := a.GetLabels()
resourcebindingNamespace, namespaceExist := labels[workv1alpha1.ResourceBindingNamespaceLabel]
resourcebindingName, nameExist := labels[workv1alpha1.ResourceBindingNameLabel]
if !namespaceExist || !nameExist {
return nil
crNamespace, namespaceExist := labels[workv1alpha1.ResourceBindingNamespaceLabel]
crName, nameExist := labels[workv1alpha1.ResourceBindingNameLabel]
if namespaceExist && nameExist {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: crNamespace,
Name: crName,
},
})
}
namespacesName := types.NamespacedName{
Namespace: resourcebindingNamespace,
Name: resourcebindingName,

annotations := a.GetAnnotations()
crNamespace, namespaceExist = annotations[workv1alpha1.ResourceBindingNamespaceLabel]
crName, nameExist = annotations[workv1alpha1.ResourceBindingNameLabel]
if namespaceExist && nameExist {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: crNamespace,
Name: crName,
},
})
}

requests = append(requests, reconcile.Request{NamespacedName: namespacesName})
return requests
})

Expand Down
28 changes: 18 additions & 10 deletions pkg/controllers/binding/cluster_resource_binding_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -46,9 +45,7 @@ func (c *ClusterResourceBindingController) Reconcile(ctx context.Context, req co
if err := c.Client.Get(context.TODO(), req.NamespacedName, clusterResourceBinding); err != nil {
// The resource no longer exist, clean up derived Work objects.
if apierrors.IsNotFound(err) {
return helper.DeleteWorks(c.Client, labels.Set{
workv1alpha1.ClusterResourceBindingLabel: req.Name,
})
return helper.DeleteWorkByCRBName(c.Client, req.Name)
}

return controllerruntime.Result{Requeue: true}, err
Expand Down Expand Up @@ -110,16 +107,27 @@ func (c *ClusterResourceBindingController) SetupWithManager(mgr controllerruntim
func(a client.Object) []reconcile.Request {
var requests []reconcile.Request

// TODO: Delete this logic in the next release to prevent incompatibility when upgrading the current release (v0.9.0).
labels := a.GetLabels()
clusterResourcebindingName, nameExist := labels[workv1alpha1.ClusterResourceBindingLabel]
if !nameExist {
return nil
crbName, nameExist := labels[workv1alpha1.ClusterResourceBindingLabel]
if nameExist {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: crbName,
},
})
}
namespacesName := types.NamespacedName{
Name: clusterResourcebindingName,

annotations := a.GetAnnotations()
crbName, nameExist = annotations[workv1alpha1.ClusterResourceBindingLabel]
if nameExist {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: crbName,
},
})
}

requests = append(requests, reconcile.Request{NamespacedName: namespacesName})
return requests
})

Expand Down
32 changes: 20 additions & 12 deletions pkg/controllers/binding/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func ensureWork(c client.Client, workload *unstructured.Unstructured, overrideMa
return err
}

workLabel := mergeLabel(clonedWorkload, workNamespace, binding, scope)
workLabel := mergeLabel(clonedWorkload, workNamespace)

if clonedWorkload.GetKind() == util.DeploymentKind && (referenceRSP != nil || hasScheduledReplica) {
err = applyReplicaSchedulingPolicy(clonedWorkload, desireReplicaInfos[targetCluster.Name])
Expand All @@ -96,7 +96,8 @@ func ensureWork(c client.Client, workload *unstructured.Unstructured, overrideMa
}
}

annotations, err := recordAppliedOverrides(cops, ops)
annotations := mergeAnnotations(clonedWorkload, binding, scope)
annotations, err = recordAppliedOverrides(cops, ops, annotations)
if err != nil {
klog.Errorf("failed to record appliedOverrides, Error: %v", err)
return err
Expand Down Expand Up @@ -145,26 +146,33 @@ func applyReplicaSchedulingPolicy(workload *unstructured.Unstructured, desireRep
return nil
}

func mergeLabel(workload *unstructured.Unstructured, workNamespace string, binding metav1.Object, scope apiextensionsv1.ResourceScope) map[string]string {
func mergeLabel(workload *unstructured.Unstructured, workNamespace string) map[string]string {
var workLabel = make(map[string]string)
util.MergeLabel(workload, workv1alpha1.WorkNamespaceLabel, workNamespace)
util.MergeLabel(workload, workv1alpha1.WorkNameLabel, names.GenerateWorkName(workload.GetKind(), workload.GetName(), workload.GetNamespace()))
return workLabel
}

func mergeAnnotations(workload *unstructured.Unstructured, binding metav1.Object, scope apiextensionsv1.ResourceScope) map[string]string {
annotations := make(map[string]string)
if scope == apiextensionsv1.NamespaceScoped {
util.MergeLabel(workload, workv1alpha1.ResourceBindingNamespaceLabel, binding.GetNamespace())
util.MergeLabel(workload, workv1alpha1.ResourceBindingNameLabel, binding.GetName())
workLabel[workv1alpha1.ResourceBindingNamespaceLabel] = binding.GetNamespace()
workLabel[workv1alpha1.ResourceBindingNameLabel] = binding.GetName()
util.MergeAnnotation(workload, workv1alpha1.ResourceBindingNamespaceLabel, binding.GetNamespace())
util.MergeAnnotation(workload, workv1alpha1.ResourceBindingNameLabel, binding.GetName())
annotations[workv1alpha1.ResourceBindingNamespaceLabel] = binding.GetNamespace()
annotations[workv1alpha1.ResourceBindingNameLabel] = binding.GetName()
} else {
util.MergeLabel(workload, workv1alpha1.ClusterResourceBindingLabel, binding.GetName())
workLabel[workv1alpha1.ClusterResourceBindingLabel] = binding.GetName()
util.MergeAnnotation(workload, workv1alpha1.ClusterResourceBindingLabel, binding.GetName())
annotations[workv1alpha1.ClusterResourceBindingLabel] = binding.GetName()
}

return workLabel
return annotations
}

func recordAppliedOverrides(cops *overridemanager.AppliedOverrides, ops *overridemanager.AppliedOverrides) (map[string]string, error) {
annotations := make(map[string]string)
func recordAppliedOverrides(cops *overridemanager.AppliedOverrides, ops *overridemanager.AppliedOverrides,
annotations map[string]string) (map[string]string, error) {
if annotations == nil {
annotations = make(map[string]string)
}

if cops != nil {
appliedBytes, err := cops.MarshalJSON()
Expand Down
48 changes: 47 additions & 1 deletion pkg/util/annotation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package util

import "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
import (
"context"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"

workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
)

// MergeAnnotation adds annotation for the given object.
func MergeAnnotation(obj *unstructured.Unstructured, annotationKey string, annotationValue string) {
Expand All @@ -18,3 +25,42 @@ func MergeAnnotations(dst *unstructured.Unstructured, src *unstructured.Unstruct
MergeAnnotation(dst, key, value)
}
}

// GetWorkListByCRBName get works by ClusterResourceBinding name.
func GetWorkListByCRBName(c client.Client, name string) ([]workv1alpha1.Work, error) {
workList := &workv1alpha1.WorkList{}
err := c.List(context.TODO(), workList)
if err != nil {
return nil, err
}

matches := make([]workv1alpha1.Work, 0)
for _, work := range workList.Items {
n, ok := work.Annotations[workv1alpha1.ClusterResourceBindingLabel]
if ok && n == name {
matches = append(matches, work)
}
}

return matches, nil
}

// GetWorkListByRBNamespaceAndName get works by ResourceBinding namespace and name.
func GetWorkListByRBNamespaceAndName(c client.Client, namespace, name string) ([]workv1alpha1.Work, error) {
workList := &workv1alpha1.WorkList{}
err := c.List(context.TODO(), workList)
if err != nil {
return nil, err
}

matches := make([]workv1alpha1.Work, 0)
for _, work := range workList.Items {
ns, nsOk := work.Annotations[workv1alpha1.ResourceBindingNamespaceLabel]
n, nOk := work.Annotations[workv1alpha1.ResourceBindingNameLabel]
if nsOk && nOk && ns == namespace && n == name {
matches = append(matches, work)
}
}

return matches, nil
}
80 changes: 76 additions & 4 deletions pkg/util/helper/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
"github.com/karmada-io/karmada/pkg/util"
"github.com/karmada-io/karmada/pkg/util/names"
"github.com/karmada-io/karmada/pkg/util/restmapper"
)
Expand Down Expand Up @@ -80,29 +81,47 @@ func GetBindingClusterNames(targetClusters []workv1alpha1.TargetCluster) []strin
// FindOrphanWorks retrieves all works that labeled with current binding(ResourceBinding or ClusterResourceBinding) objects,
// then pick the works that not meet current binding declaration.
func FindOrphanWorks(c client.Client, bindingNamespace, bindingName string, clusterNames []string, scope apiextensionsv1.ResourceScope) ([]workv1alpha1.Work, error) {
workList := &workv1alpha1.WorkList{}
var needJudgeWorks []workv1alpha1.Work
if scope == apiextensionsv1.NamespaceScoped {
// TODO: Delete this logic in the next release to prevent incompatibility when upgrading the current release (v0.9.0).
workList := &workv1alpha1.WorkList{}
selector := labels.SelectorFromSet(labels.Set{
workv1alpha1.ResourceBindingNamespaceLabel: bindingNamespace,
workv1alpha1.ResourceBindingNameLabel: bindingName,
})

if err := c.List(context.TODO(), workList, &client.ListOptions{LabelSelector: selector}); err != nil {
return nil, err
}
needJudgeWorks = append(needJudgeWorks, workList.Items...)

works, err := util.GetWorkListByRBNamespaceAndName(c, bindingNamespace, bindingName)
if err != nil {
klog.Errorf("Failed to get works by ResourceBinding(%s/%s): %v", bindingNamespace, bindingName, err)
return nil, err
}
needJudgeWorks = append(needJudgeWorks, works...)
} else {
// TODO: Delete this logic in the next release to prevent incompatibility when upgrading the current release (v0.9.0).
workList := &workv1alpha1.WorkList{}
selector := labels.SelectorFromSet(labels.Set{
workv1alpha1.ClusterResourceBindingLabel: bindingName,
})

if err := c.List(context.TODO(), workList, &client.ListOptions{LabelSelector: selector}); err != nil {
return nil, err
}
needJudgeWorks = append(needJudgeWorks, workList.Items...)

works, err := util.GetWorkListByCRBName(c, bindingName)
if err != nil {
klog.Errorf("Failed to get works by ClusterResourceBinding(%s): %v", bindingName, err)
return nil, err
}
needJudgeWorks = append(needJudgeWorks, works...)
}

var orphanWorks []workv1alpha1.Work
expectClusters := sets.NewString(clusterNames...)
for _, work := range workList.Items {
for _, work := range needJudgeWorks {
workTargetCluster, err := names.GetClusterName(work.GetNamespace())
if err != nil {
klog.Errorf("Failed to get cluster name which Work %s/%s belongs to. Error: %v.",
Expand Down Expand Up @@ -173,6 +192,59 @@ func GetWorks(c client.Client, ls labels.Set) (*workv1alpha1.WorkList, error) {
return works, c.List(context.TODO(), works, listOpt)
}

// DeleteWorkByRBNamespaceAndName will delete all Work objects by ResourceBinding namespace and name.
func DeleteWorkByRBNamespaceAndName(c client.Client, namespace, name string) (controllerruntime.Result, error) {
works, err := util.GetWorkListByRBNamespaceAndName(c, namespace, name)
if err != nil {
klog.Errorf("Failed to get works by ResourceBinding(%s/%s): %v", namespace, name, err)
return controllerruntime.Result{Requeue: true}, err
}

var errs []error
for index, work := range works {
if err := c.Delete(context.TODO(), &works[index]); err != nil {
klog.Errorf("Failed to delete work(%s/%s): %v", work.Namespace, work.Name, err)
errs = append(errs, err)
}
}

if len(errs) > 0 {
return controllerruntime.Result{Requeue: true}, errors.NewAggregate(errs)
}

// TODO: Delete this logic in the next release to prevent incompatibility when upgrading the current release (v0.9.0).
return DeleteWorks(c, labels.Set{
workv1alpha1.ResourceBindingNamespaceLabel: namespace,
workv1alpha1.ResourceBindingNameLabel: name,
})
}

// DeleteWorkByCRBName will delete all Work objects by ClusterResourceBinding name.
func DeleteWorkByCRBName(c client.Client, name string) (controllerruntime.Result, error) {
works, err := util.GetWorkListByCRBName(c, name)
if err != nil {
klog.Errorf("Failed to get works by ClusterResourceBinding(%s): %v", name, err)
return controllerruntime.Result{Requeue: true}, err
}

var errs []error
for index, work := range works {
if err := c.Delete(context.TODO(), &works[index]); err != nil {
klog.Errorf("Failed to delete work(%s/%s): %v", work.Namespace, work.Name, err)
errs = append(errs, err)
}
}

if len(errs) > 0 {
return controllerruntime.Result{Requeue: true}, errors.NewAggregate(errs)
}

// TODO: Delete this logic in the next release to prevent incompatibility when upgrading the current release (v0.9.0).
return DeleteWorks(c, labels.Set{
workv1alpha1.ClusterResourceBindingLabel: name,
})
}

// DeleteWorks will delete all Work objects by labels.
func DeleteWorks(c client.Client, selector labels.Set) (controllerruntime.Result, error) {
workList, err := GetWorks(c, selector)
Expand Down
Loading

0 comments on commit 3f63536

Please sign in to comment.