Deprecate create by label from manifests (#191)
Signed-off-by: RainbowMango <renhongcai@huawei.com>
This commit is contained in:
parent
7a8eca1bfb
commit
288d24c7b9
|
@ -203,6 +203,18 @@ func (c *PropagationBindingController) ensureWork(workload *unstructured.Unstruc
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
workNamespace, err := names.GenerateExecutionSpaceName(clusterName)
|
||||||
|
if err != nil {
|
||||||
|
klog.Errorf("Failed to ensure Work for cluster: %s. Error: %v.", clusterName, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
workName := binding.Name
|
||||||
|
|
||||||
|
util.MergeLabel(clonedWorkload, util.ResourceBindingNamespaceLabel, binding.Namespace)
|
||||||
|
util.MergeLabel(clonedWorkload, util.ResourceBindingNameLabel, binding.Name)
|
||||||
|
util.MergeLabel(clonedWorkload, util.WorkNamespaceLabel, workNamespace)
|
||||||
|
util.MergeLabel(clonedWorkload, util.WorkNameLabel, workName)
|
||||||
|
|
||||||
workloadJSON, err := clonedWorkload.MarshalJSON()
|
workloadJSON, err := clonedWorkload.MarshalJSON()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("Failed to marshal workload, kind: %s, namespace: %s, name: %s. Error: %v",
|
klog.Errorf("Failed to marshal workload, kind: %s, namespace: %s, name: %s. Error: %v",
|
||||||
|
@ -210,16 +222,10 @@ func (c *PropagationBindingController) ensureWork(workload *unstructured.Unstruc
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
executionSpace, err := names.GenerateExecutionSpaceName(clusterName)
|
|
||||||
if err != nil {
|
|
||||||
klog.Errorf("Failed to ensure Work for cluster: %s. Error: %v.", clusterName, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
work := &v1alpha1.Work{
|
work := &v1alpha1.Work{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: binding.Name,
|
Name: workName,
|
||||||
Namespace: executionSpace,
|
Namespace: workNamespace,
|
||||||
Finalizers: []string{util.ExecutionControllerFinalizer},
|
Finalizers: []string{util.ExecutionControllerFinalizer},
|
||||||
OwnerReferences: []metav1.OwnerReference{
|
OwnerReferences: []metav1.OwnerReference{
|
||||||
*metav1.NewControllerRef(binding, controllerKind),
|
*metav1.NewControllerRef(binding, controllerKind),
|
||||||
|
|
|
@ -180,8 +180,6 @@ func (c *Controller) syncToClusters(cluster *v1alpha1.Cluster, work *policyv1alp
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
util.MergeLabel(workload, util.OwnerLabel, names.GenerateOwnerLabelValue(work.GetNamespace(), work.GetName()))
|
|
||||||
|
|
||||||
applied := c.isResourceApplied(&work.Status)
|
applied := c.isResourceApplied(&work.Status)
|
||||||
if applied {
|
if applied {
|
||||||
// todo: get clusterObj from cache
|
// todo: get clusterObj from cache
|
||||||
|
|
|
@ -116,28 +116,22 @@ func (c *WorkStatusController) syncWorkStatus(key util.QueueKey) error {
|
||||||
klog.V(2).Infof("Ignore the event key %s which not managed by karmada.", key)
|
klog.V(2).Infof("Ignore the event key %s which not managed by karmada.", key)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
owner := util.GetLabelValue(obj.GetLabels(), util.OwnerLabel)
|
|
||||||
if len(owner) == 0 {
|
workNamespace := util.GetLabelValue(obj.GetLabels(), util.WorkNamespaceLabel)
|
||||||
// Ignore the object which not managed by karmada.
|
workName := util.GetLabelValue(obj.GetLabels(), util.WorkNameLabel)
|
||||||
// TODO(RainbowMango): Consider to add event filter to informer event handler to skip event from enqueue.
|
if len(workNamespace) == 0 || len(workName) == 0 {
|
||||||
klog.V(2).Infof("Ignore the event of %s(%s/%s) which not managed by karmada.", obj.GetKind(), obj.GetNamespace(), obj.GetName())
|
klog.Infof("Ignore object(%s) which not managed by karmada.", keyStr)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ownerNamespace, ownerName, err := names.GetNamespaceAndName(owner)
|
|
||||||
if err != nil {
|
|
||||||
klog.Errorf("Failed to parse object(%s/%s) owner by label: %s", obj.GetNamespace(), obj.GetName(), owner)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
workObject := &v1alpha1.Work{}
|
workObject := &v1alpha1.Work{}
|
||||||
if err := c.Client.Get(context.TODO(), client.ObjectKey{Namespace: ownerNamespace, Name: ownerName}, workObject); err != nil {
|
if err := c.Client.Get(context.TODO(), client.ObjectKey{Namespace: workNamespace, Name: workName}, workObject); err != nil {
|
||||||
// Stop processing if resource no longer exist.
|
// Stop processing if resource no longer exist.
|
||||||
if errors.IsNotFound(err) {
|
if errors.IsNotFound(err) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
klog.Errorf("Failed to get Work(%s/%s) from cache: %v", ownerNamespace, ownerName, err)
|
klog.Errorf("Failed to get Work(%s/%s) from cache: %v", workNamespace, workName, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,9 +141,7 @@ func (c *WorkStatusController) syncWorkStatus(key util.QueueKey) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
util.MergeLabel(desireObj, util.OwnerLabel, names.GenerateOwnerLabelValue(workObject.GetNamespace(), workObject.GetName()))
|
clusterName, err := names.GetClusterName(workNamespace)
|
||||||
|
|
||||||
clusterName, err := names.GetClusterName(ownerNamespace)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("Failed to get member cluster name: %v", err)
|
klog.Errorf("Failed to get member cluster name: %v", err)
|
||||||
return err
|
return err
|
||||||
|
@ -165,7 +157,7 @@ func (c *WorkStatusController) syncWorkStatus(key util.QueueKey) error {
|
||||||
return c.ObjectWatcher.Update(clusterName, desireObj, obj)
|
return c.ObjectWatcher.Update(clusterName, desireObj, obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
klog.Infof("reflecting %s(%s/%s) status of to Work(%s/%s)", obj.GetKind(), obj.GetNamespace(), obj.GetName(), ownerNamespace, ownerName)
|
klog.Infof("reflecting %s(%s/%s) status to Work(%s/%s)", obj.GetKind(), obj.GetNamespace(), obj.GetName(), workNamespace, workName)
|
||||||
return c.reflectStatus(workObject, obj)
|
return c.reflectStatus(workObject, obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,9 +205,6 @@ func (c *WorkStatusController) recreateResourceIfNeeded(work *v1alpha1.Work, clu
|
||||||
if reflect.DeepEqual(desiredGVK, clusterWorkload.GVK) &&
|
if reflect.DeepEqual(desiredGVK, clusterWorkload.GVK) &&
|
||||||
manifest.GetNamespace() == clusterWorkload.Namespace &&
|
manifest.GetNamespace() == clusterWorkload.Namespace &&
|
||||||
manifest.GetName() == clusterWorkload.Name {
|
manifest.GetName() == clusterWorkload.Name {
|
||||||
|
|
||||||
util.MergeLabel(manifest, util.OwnerLabel, names.GenerateOwnerLabelValue(work.GetNamespace(), work.GetName()))
|
|
||||||
|
|
||||||
klog.Infof("recreating %s/%s/%s in member cluster %s", clusterWorkload.GVK.Kind, clusterWorkload.Namespace, clusterWorkload.Name, clusterWorkload.Cluster)
|
klog.Infof("recreating %s/%s/%s in member cluster %s", clusterWorkload.GVK.Kind, clusterWorkload.Namespace, clusterWorkload.Name, clusterWorkload.Cluster)
|
||||||
return c.ObjectWatcher.Create(clusterWorkload.Cluster, manifest)
|
return c.ObjectWatcher.Create(clusterWorkload.Cluster, manifest)
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,12 @@ const (
|
||||||
// ClusterResourceBindingLabel is added to objects to specify associated ClusterResourceBinding.
|
// ClusterResourceBindingLabel is added to objects to specify associated ClusterResourceBinding.
|
||||||
ClusterResourceBindingLabel = "clusterresourcebinding.karmada.io/name"
|
ClusterResourceBindingLabel = "clusterresourcebinding.karmada.io/name"
|
||||||
|
|
||||||
|
// WorkNamespaceLabel is added to objects to specify associated Work's namespace.
|
||||||
|
WorkNamespaceLabel = "work.karmada.io/namespace"
|
||||||
|
|
||||||
|
// WorkNameLabel is added to objects to specify associated Work's name.
|
||||||
|
WorkNameLabel = "work.karmada.io/name"
|
||||||
|
|
||||||
// OwnerLabel will set in karmada CRDs, indicates that who created it.
|
// OwnerLabel will set in karmada CRDs, indicates that who created it.
|
||||||
// We can use labelSelector to find who created it quickly.
|
// We can use labelSelector to find who created it quickly.
|
||||||
// example1: set it in propagationBinding, the label value is propagationPolicy.
|
// example1: set it in propagationBinding, the label value is propagationPolicy.
|
||||||
|
|
|
@ -106,26 +106,14 @@ func GenerateKey(obj interface{}) (QueueKey, error) {
|
||||||
|
|
||||||
// getClusterNameFromLabel gets cluster name from ownerLabel, if label not exist, means resource is not created by karmada.
|
// getClusterNameFromLabel gets cluster name from ownerLabel, if label not exist, means resource is not created by karmada.
|
||||||
func getClusterNameFromLabel(resource *unstructured.Unstructured) (string, error) {
|
func getClusterNameFromLabel(resource *unstructured.Unstructured) (string, error) {
|
||||||
workloadLabels := resource.GetLabels()
|
workNamespace := GetLabelValue(resource.GetLabels(), WorkNamespaceLabel)
|
||||||
if workloadLabels == nil {
|
if len(workNamespace) == 0 {
|
||||||
klog.V(2).Infof("Resource %s/%s/%s is not created by karmada.", resource.GetKind(),
|
klog.Infof("Resource(%s/%s/%s) not created by karmada", resource.GetKind(), resource.GetNamespace(), resource.GetName())
|
||||||
resource.GetNamespace(), resource.GetName())
|
|
||||||
return "", nil
|
|
||||||
}
|
}
|
||||||
value, exist := workloadLabels[OwnerLabel]
|
|
||||||
if !exist {
|
cluster, err := names.GetClusterName(workNamespace)
|
||||||
klog.V(2).Infof("Resource %s/%s/%s is not created by karmada.", resource.GetKind(),
|
|
||||||
resource.GetNamespace(), resource.GetName())
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
executionNamespace, _, err := names.GetNamespaceAndName(value)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("Failed to get executionNamespace from label %s", value)
|
klog.Errorf("Failed to get cluster name from work namespace: %s, error: %v.", workNamespace, err)
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
cluster, err := names.GetClusterName(executionNamespace)
|
|
||||||
if err != nil {
|
|
||||||
klog.Errorf("Failed to get member cluster name by %s. Error: %v.", value, err)
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return cluster, nil
|
return cluster, nil
|
||||||
|
|
Loading…
Reference in New Issue