Merge pull request #770 from dddddai/owner-ref

Remove invalid ownerReference in HPA controller
This commit is contained in:
karmada-bot 2021-09-30 15:37:37 +08:00 committed by GitHub
commit 3f275c53e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 5 deletions

View File

@ -43,9 +43,12 @@ func (c *HorizontalPodAutoscalerController) Reconcile(ctx context.Context, req c
hpa := &autoscalingv1.HorizontalPodAutoscaler{}
if err := c.Client.Get(context.TODO(), req.NamespacedName, hpa); err != nil {
// The resource may no longer exist, in which case we stop processing.
// The resource may no longer exist, in which case we delete related works.
if apierrors.IsNotFound(err) {
return controllerruntime.Result{}, nil
if err := c.deleteWorks(names.GenerateWorkName(util.HorizontalPodAutoscalerKind, req.Name, req.Namespace)); err != nil {
return controllerruntime.Result{Requeue: true}, err
}
return controllerruntime.Result{}, err
}
return controllerruntime.Result{Requeue: true}, err
@ -93,9 +96,6 @@ func (c *HorizontalPodAutoscalerController) buildWorks(hpa *autoscalingv1.Horizo
Name: workName,
Namespace: workNamespace,
Finalizers: []string{util.ExecutionControllerFinalizer},
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(hpa, hpa.GroupVersionKind()),
},
}
util.MergeLabel(hpaObj, workv1alpha2.WorkNamespaceLabel, workNamespace)
@ -140,3 +140,22 @@ func (c *HorizontalPodAutoscalerController) getTargetPlacement(objRef autoscalin
func (c *HorizontalPodAutoscalerController) SetupWithManager(mgr controllerruntime.Manager) error {
return controllerruntime.NewControllerManagedBy(mgr).For(&autoscalingv1.HorizontalPodAutoscaler{}).Complete(c)
}
func (c *HorizontalPodAutoscalerController) deleteWorks(workName string) error {
workList := &workv1alpha1.WorkList{}
if err := c.List(context.TODO(), workList); err != nil {
klog.Errorf("Failed to list works: %v.", err)
return err
}
for i := range workList.Items {
work := &workList.Items[i]
if workName == work.Name {
if err := c.Client.Delete(context.TODO(), work); err != nil {
klog.Errorf("Failed to delete work %s/%s: %v.", work.Namespace, work.Name, err)
return err
}
}
}
return nil
}

View File

@ -86,6 +86,8 @@ const (
EndpointSliceKind = "EndpointSlice"
// PersistentVolumeClaimKind indicated the target resource is a persistentvolumeclaim
PersistentVolumeClaimKind = "PersistentVolumeClaim"
// HorizontalPodAutoscalerKind indicated the target resource is a horizontalpodautoscaler
HorizontalPodAutoscalerKind = "HorizontalPodAutoscaler"
// ServiceExportKind indicates the target resource is a serviceexport crd
ServiceExportKind = "ServiceExport"