work: fix workload name conflicts (#358) (#359)

Signed-off-by: Garrybest <garrybest@foxmail.com>
This commit is contained in:
Rui Fang 2021-05-20 10:34:27 +08:00 committed by GitHub
parent 7a0d6c9b8c
commit bbcf69f368
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 5 deletions

View File

@ -87,7 +87,7 @@ func (c *HorizontalPodAutoscalerController) buildWorks(hpa *autoscalingv1.Horizo
klog.Errorf("Failed to ensure Work for cluster: %s. Error: %v.", clusterName, err)
return err
}
workName := names.GenerateBindingName(hpaObj.GetKind(), hpaObj.GetName())
workName := names.GenerateWorkName(hpaObj.GetKind(), hpaObj.GetName(), hpa.GetNamespace())
objectMeta := metav1.ObjectMeta{
Name: workName,
Namespace: workNamespace,

View File

@ -105,7 +105,7 @@ func (c *Controller) buildWorks(namespace *v1.Namespace, clusters []v1alpha1.Clu
return err
}
workName := names.GenerateBindingName(namespaceObj.GetKind(), namespaceObj.GetName())
workName := names.GenerateWorkName(namespaceObj.GetKind(), namespaceObj.GetName(), namespaceObj.GetNamespace())
objectMeta := metav1.ObjectMeta{
Name: workName,
Namespace: workNamespace,

View File

@ -197,8 +197,8 @@ func (c *WorkStatusController) handleDeleteEvent(key keys.FederatedKey) error {
}
// Given the workload might has been deleted from informer cache, so that we can't get work object by it's label,
// we have to get work by naming rule as work's name also is binding's name and binding's name consist of workload's name and kind.
workName := names.GenerateBindingName(key.Kind, key.Name)
// we have to get work by naming rule as the work's name is generated by the workload's kind, name and namespace.
workName := names.GenerateWorkName(key.Kind, key.Name, key.Namespace)
work := &workv1alpha1.Work{}
if err := c.Client.Get(context.TODO(), client.ObjectKey{Namespace: executionSpace, Name: workName}, work); err != nil {
// stop processing as the work object has been removed, assume it's a normal delete operation.

View File

@ -174,7 +174,7 @@ func EnsureWork(c client.Client, workload *unstructured.Unstructured, clusterNam
return err
}
workName := binding.GetName()
workName := names.GenerateWorkName(workload.GetKind(), workload.GetName(), workload.GetNamespace())
workNamespace, err := names.GenerateExecutionSpaceName(clusterName)
if err != nil {
klog.Errorf("Failed to ensure Work for cluster: %s. Error: %v.", clusterName, err)

View File

@ -44,6 +44,14 @@ func GenerateBindingName(kind, name string) string {
return strings.ToLower(name + "-" + kind)
}
// GenerateWorkName will generate work name by namespace, kind and name
func GenerateWorkName(kind, name, namespace string) string {
if len(namespace) == 0 {
return strings.ToLower(name + "-" + kind)
}
return strings.ToLower(namespace + "-" + name + "-" + kind)
}
// GenerateServiceAccountName generates the name of a ServiceAccount.
func GenerateServiceAccountName(clusterName string) string {
return fmt.Sprintf("%s-%s", "karmada", clusterName)