Skip to content

Commit

Permalink
feat: add resoruce id label
Browse files Browse the repository at this point in the history
Signed-off-by: jwcesign <jwcesign@gmail.com>
  • Loading branch information
jwcesign committed Nov 7, 2023
1 parent 60af4e8 commit 6a2dfa0
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 53 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/gogo/protobuf v1.3.2
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.5.9
github.com/google/uuid v1.3.0
github.com/kr/pretty v0.3.1
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f
github.com/olekukonko/tablewriter v0.0.5
Expand Down Expand Up @@ -100,7 +101,6 @@ require (
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/safetext v0.0.0-20220905092116-b49f7bc46da2 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/policy/v1alpha1/well_known_constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package v1alpha1

const (
// PropagationPolicyIDLabel is the id of PropagationPolicy object.
PropagationPolicyIDLabel = "propagationpolicy.karmada.io/id"

// ClusterPropagationPolicyIDLabel is the id of ClusterPropagationPolicy object.
ClusterPropagationPolicyIDLabel = "clusterpropagationpolicy.karmada.io/id"

// PropagationPolicyUIDLabel is the uid of PropagationPolicy object.
PropagationPolicyUIDLabel = "propagationpolicy.karmada.io/uid"

Expand Down
9 changes: 9 additions & 0 deletions pkg/apis/work/v1alpha2/well_known_constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package v1alpha2

const (
// ResourceBindingIDLabel is the ID of ResourceBinding object.
ResourceBindingIDLabel = "resourcebinding.karmada.io/id"

// ClusterResourceBindingIDLabel is the ID of ClusterResourceBinding object.
ClusterResourceBindingIDLabel = "clusterresourcebinding.karmada.io/id"

// WorkIDLabel is the ID of Work object.
WorkIDLabel = "work.karmada.io/id"

// ResourceBindingUIDLabel is the UID of ResourceBinding object.
ResourceBindingUIDLabel = "resourcebinding.karmada.io/uid"

Expand Down
16 changes: 12 additions & 4 deletions pkg/controllers/binding/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,23 @@ func mergeLabel(workload *unstructured.Unstructured, workNamespace string, bindi
util.MergeLabel(workload, workv1alpha1.WorkNameLabel, names.GenerateWorkName(workload.GetKind(), workload.GetName(), workload.GetNamespace()))
util.MergeLabel(workload, util.ManagedByKarmadaLabel, util.ManagedByKarmadaLabelValue)
if scope == apiextensionsv1.NamespaceScoped {
util.RemoveLabels(workload, workv1alpha2.ResourceBindingUIDLabel)

bindingID := util.GetLabelValue(binding.GetLabels(), workv1alpha2.ResourceBindingIDLabel)
util.MergeLabel(workload, workv1alpha2.ResourceBindingReferenceKey, names.GenerateBindingReferenceKey(binding.GetNamespace(), binding.GetName()))
util.MergeLabel(workload, workv1alpha2.ResourceBindingUIDLabel, string(binding.GetUID()))
util.MergeLabel(workload, workv1alpha2.ResourceBindingIDLabel, bindingID)

workLabel[workv1alpha2.ResourceBindingReferenceKey] = names.GenerateBindingReferenceKey(binding.GetNamespace(), binding.GetName())
workLabel[workv1alpha2.ResourceBindingUIDLabel] = string(binding.GetUID())
workLabel[workv1alpha2.ResourceBindingIDLabel] = bindingID
} else {
util.RemoveLabels(workload, workv1alpha2.ClusterResourceBindingUIDLabel)

bindingID := util.GetLabelValue(binding.GetLabels(), workv1alpha2.ClusterResourceBindingIDLabel)
util.MergeLabel(workload, workv1alpha2.ClusterResourceBindingReferenceKey, names.GenerateBindingReferenceKey("", binding.GetName()))
util.MergeLabel(workload, workv1alpha2.ClusterResourceBindingUIDLabel, string(binding.GetUID()))
util.MergeLabel(workload, workv1alpha2.ClusterResourceBindingIDLabel, bindingID)

workLabel[workv1alpha2.ClusterResourceBindingReferenceKey] = names.GenerateBindingReferenceKey("", binding.GetName())
workLabel[workv1alpha2.ClusterResourceBindingUIDLabel] = string(binding.GetUID())
workLabel[workv1alpha2.ClusterResourceBindingIDLabel] = bindingID
}
return workLabel
}
Expand Down
15 changes: 9 additions & 6 deletions pkg/controllers/binding/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"

policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
Expand Down Expand Up @@ -88,7 +87,7 @@ func Test_mergeTargetClusters(t *testing.T) {
func Test_mergeLabel(t *testing.T) {
namespace := "fake-ns"
bindingName := "fake-bindingName"
rbUID := "93162d3c-ee8e-4995-9034-05f4d5d2c2b9"
rbID := "93162d3c-ee8e-4995-9034-05f4d5d2c2b9"

tests := []struct {
name string
Expand All @@ -115,12 +114,14 @@ func Test_mergeLabel(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: bindingName,
Namespace: namespace,
UID: types.UID(rbUID),
Labels: map[string]string{
workv1alpha2.ResourceBindingIDLabel: rbID,
},
},
},
scope: v1.NamespaceScoped,
want: map[string]string{
workv1alpha2.ResourceBindingUIDLabel: rbUID,
workv1alpha2.ResourceBindingIDLabel: rbID,
workv1alpha2.ResourceBindingReferenceKey: names.GenerateBindingReferenceKey(namespace, bindingName),
},
},
Expand All @@ -138,12 +139,14 @@ func Test_mergeLabel(t *testing.T) {
binding: &workv1alpha2.ClusterResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: bindingName,
UID: types.UID(rbUID),
Labels: map[string]string{
workv1alpha2.ClusterResourceBindingIDLabel: rbID,
},
},
},
scope: v1.ClusterScoped,
want: map[string]string{
workv1alpha2.ClusterResourceBindingUIDLabel: rbUID,
workv1alpha2.ClusterResourceBindingIDLabel: rbID,
workv1alpha2.ClusterResourceBindingReferenceKey: names.GenerateBindingReferenceKey("", bindingName),
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/execution/execution_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ func (c *Controller) syncToClusters(clusterName string, work *workv1alpha1.Work)
for _, manifest := range work.Spec.Workload.Manifests {
workload := &unstructured.Unstructured{}
err := workload.UnmarshalJSON(manifest.Raw)
util.MergeLabel(workload, workv1alpha2.WorkUIDLabel, string(work.UID))
if err != nil {
klog.Errorf("Failed to unmarshal workload, error is: %v", err)
errs = append(errs, err)
continue
}
util.MergeLabel(workload, workv1alpha2.WorkIDLabel, util.GetLabelValue(work.Labels, workv1alpha2.WorkIDLabel))

if err = c.tryCreateOrUpdateWorkload(clusterName, workload); err != nil {
klog.Errorf("Failed to create or update resource(%v/%v) in the given member cluster %s, err is %v", workload.GetNamespace(), workload.GetName(), clusterName, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (r *HPAReplicasSyncer) Generic(e event.GenericEvent) bool {
}

func hasBeenPropagated(hpa *autoscalingv2.HorizontalPodAutoscaler) bool {
_, ppExist := hpa.GetLabels()[policyv1alpha1.PropagationPolicyUIDLabel]
_, cppExist := hpa.GetLabels()[policyv1alpha1.ClusterPropagationPolicyUIDLabel]
_, ppExist := hpa.GetLabels()[policyv1alpha1.PropagationPolicyNameLabel]
_, cppExist := hpa.GetLabels()[policyv1alpha1.ClusterPropagationPolicyLabel]
return ppExist || cppExist
}
36 changes: 24 additions & 12 deletions pkg/dependenciesdistributor/dependencies_distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"

"github.com/google/uuid"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -45,6 +46,9 @@ import (
)

const (
// bindingDependedIdLabelKey is the resoruce id of the independent binding which the attached binding depends on.
bindingDependedIdLabelKey = "resourcebinding.karmada.io/depended-id"

// bindingDependedByLabelKeyPrefix is the prefix to a label key specifying an attached binding referred by which independent binding.
// the key is in the label of an attached binding which should be unique, because resource like secret can be referred by multiple deployments.
bindingDependedByLabelKeyPrefix = "resourcebinding.karmada.io/depended-by-"
Expand Down Expand Up @@ -498,19 +502,14 @@ func (d *DependenciesDistributor) removeScheduleResultFromAttachedBindings(bindi
}

func (d *DependenciesDistributor) createOrUpdateAttachedBinding(attachedBinding *workv1alpha2.ResourceBinding) error {
if err := d.Client.Create(context.TODO(), attachedBinding); err != nil {
if !apierrors.IsAlreadyExists(err) {
klog.Infof("Failed to create resource binding(%s/%s): %v", attachedBinding.Namespace, attachedBinding.Name, err)
return err
existBinding := &workv1alpha2.ResourceBinding{}
key := client.ObjectKeyFromObject(attachedBinding)
err := d.Client.Get(context.TODO(), key, existBinding)
if err == nil {
if util.GetLabelValue(existBinding.Labels, workv1alpha2.ResourceBindingIDLabel) == "" {
existBinding.Labels = util.DedupeAndMergeLabels(existBinding.Labels,
map[string]string{workv1alpha2.ResourceBindingIDLabel: uuid.New().String()})
}

existBinding := &workv1alpha2.ResourceBinding{}
key := client.ObjectKeyFromObject(attachedBinding)
if err := d.Client.Get(context.TODO(), key, existBinding); err != nil {
klog.Infof("Failed to get resource binding(%s/%s): %v", attachedBinding.Namespace, attachedBinding.Name, err)
return err
}

existBinding.Spec.RequiredBy = mergeBindingSnapshot(existBinding.Spec.RequiredBy, attachedBinding.Spec.RequiredBy)
existBinding.Labels = util.DedupeAndMergeLabels(existBinding.Labels, attachedBinding.Labels)
existBinding.Spec.Resource = attachedBinding.Spec.Resource
Expand All @@ -520,6 +519,17 @@ func (d *DependenciesDistributor) createOrUpdateAttachedBinding(attachedBinding
return err
}
}

if !apierrors.IsNotFound(err) {
klog.Infof("Failed to get resource binding(%s/%s): %v", attachedBinding.Namespace, attachedBinding.Name, err)
return err
}

attachedBinding.Labels = util.DedupeAndMergeLabels(attachedBinding.Labels,
map[string]string{workv1alpha2.ResourceBindingIDLabel: uuid.New().String()})
if err := d.Client.Create(context.TODO(), attachedBinding); err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -630,6 +640,8 @@ func buildAttachedBinding(binding *workv1alpha2.ResourceBinding, object *unstruc
Clusters: binding.Spec.Clusters,
})

policyID := util.GetLabelValue(binding.Labels, workv1alpha2.ResourceBindingIDLabel)
dependedLabels = util.DedupeAndMergeLabels(dependedLabels, map[string]string{bindingDependedIdLabelKey: policyID})
return &workv1alpha2.ResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: names.GenerateBindingName(object.GetKind(), object.GetName()),
Expand Down
Loading

0 comments on commit 6a2dfa0

Please sign in to comment.