Merge pull request #1670 from XiShanYongYe-Chang/update-work-contains

Update judge work contains serviceExport with .spec.workload.manifests
This commit is contained in:
karmada-bot 2022-05-07 10:44:27 +08:00 committed by GitHub
commit 810048fe89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 15 deletions

View File

@ -81,7 +81,7 @@ func (c *ServiceExportController) Reconcile(ctx context.Context, req controllerr
return controllerruntime.Result{}, nil
}
if !helper.IsWorkContains(&work.Status, serviceExportGVK) {
if !isWorkContains(work.Spec.Workload.Manifests, serviceExportGVK) {
return controllerruntime.Result{}, nil
}
@ -105,6 +105,23 @@ func (c *ServiceExportController) Reconcile(ctx context.Context, req controllerr
return c.buildResourceInformers(cluster)
}
// isWorkContains checks if the target resource exists in a work.spec.workload.manifests.
func isWorkContains(manifests []workv1alpha1.Manifest, targetResource schema.GroupVersionKind) bool {
for index := range manifests {
workload := &unstructured.Unstructured{}
err := workload.UnmarshalJSON(manifests[index].Raw)
if err != nil {
klog.Errorf("failed to unmarshal work manifests index %d, error is: %v", index, err)
continue
}
if targetResource == workload.GroupVersionKind() {
return true
}
}
return false
}
// SetupWithManager creates a controller and register to controller manager.
func (c *ServiceExportController) SetupWithManager(mgr controllerruntime.Manager) error {
return controllerruntime.NewControllerManagedBy(mgr).For(&workv1alpha1.Work{}).WithEventFilter(c.PredicateFunc).Complete(c)

View File

@ -272,20 +272,6 @@ func IsResourceApplied(workStatus *workv1alpha1.WorkStatus) bool {
return meta.IsStatusConditionTrue(workStatus.Conditions, workv1alpha1.WorkApplied)
}
// IsWorkContains checks if the target resource exists in a work.
// Note: This function checks the Work object's status to detect the target resource, so the Work should be 'Applied',
// otherwise always returns false.
func IsWorkContains(workStatus *workv1alpha1.WorkStatus, targetResource schema.GroupVersionKind) bool {
for _, manifestStatuses := range workStatus.ManifestStatuses {
if targetResource.Group == manifestStatuses.Identifier.Group &&
targetResource.Version == manifestStatuses.Identifier.Version &&
targetResource.Kind == manifestStatuses.Identifier.Kind {
return true
}
}
return false
}
// BuildStatusRawExtension builds raw JSON by a status map.
func BuildStatusRawExtension(status interface{}) (*runtime.RawExtension, error) {
statusJSON, err := json.Marshal(status)