trigger resync for override policy changes (#250)
Signed-off-by: lihanbo <lihanbo2@huawei.com>
This commit is contained in:
parent
dcad956744
commit
a3ebc74cde
|
@ -0,0 +1,18 @@
|
||||||
|
apiVersion: policy.karmada.io/v1alpha1
|
||||||
|
kind: ClusterOverridePolicy
|
||||||
|
metadata:
|
||||||
|
name: example-cluster-override
|
||||||
|
spec:
|
||||||
|
resourceSelectors:
|
||||||
|
- apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
name: nginx
|
||||||
|
namespace: default
|
||||||
|
targetCluster:
|
||||||
|
clusterNames:
|
||||||
|
- member1
|
||||||
|
overriders:
|
||||||
|
plaintext:
|
||||||
|
- operator: replace
|
||||||
|
path: /spec/replicas
|
||||||
|
value: 1
|
|
@ -16,6 +16,7 @@ import (
|
||||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/source"
|
"sigs.k8s.io/controller-runtime/pkg/source"
|
||||||
|
|
||||||
|
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
|
||||||
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
|
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
|
||||||
"github.com/karmada-io/karmada/pkg/util"
|
"github.com/karmada-io/karmada/pkg/util"
|
||||||
"github.com/karmada-io/karmada/pkg/util/helper"
|
"github.com/karmada-io/karmada/pkg/util/helper"
|
||||||
|
@ -113,14 +114,14 @@ func (c *ResourceBindingController) SetupWithManager(mgr controllerruntime.Manag
|
||||||
var requests []reconcile.Request
|
var requests []reconcile.Request
|
||||||
|
|
||||||
labels := a.Meta.GetLabels()
|
labels := a.Meta.GetLabels()
|
||||||
_, namespaceExist := labels[util.ResourceBindingNamespaceLabel]
|
resourcebindingNamespace, namespaceExist := labels[util.ResourceBindingNamespaceLabel]
|
||||||
_, nameExist := labels[util.ResourceBindingNameLabel]
|
resourcebindingName, nameExist := labels[util.ResourceBindingNameLabel]
|
||||||
if !namespaceExist || !nameExist {
|
if !namespaceExist || !nameExist {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
namespacesName := types.NamespacedName{
|
namespacesName := types.NamespacedName{
|
||||||
Namespace: labels[util.ResourceBindingNamespaceLabel],
|
Namespace: resourcebindingNamespace,
|
||||||
Name: labels[util.ResourceBindingNameLabel],
|
Name: resourcebindingName,
|
||||||
}
|
}
|
||||||
|
|
||||||
requests = append(requests, reconcile.Request{NamespacedName: namespacesName})
|
requests = append(requests, reconcile.Request{NamespacedName: namespacesName})
|
||||||
|
@ -129,5 +130,45 @@ func (c *ResourceBindingController) SetupWithManager(mgr controllerruntime.Manag
|
||||||
|
|
||||||
return controllerruntime.NewControllerManagedBy(mgr).For(&workv1alpha1.ResourceBinding{}).
|
return controllerruntime.NewControllerManagedBy(mgr).For(&workv1alpha1.ResourceBinding{}).
|
||||||
Watches(&source.Kind{Type: &workv1alpha1.Work{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: workFn}, workPredicateFn).
|
Watches(&source.Kind{Type: &workv1alpha1.Work{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: workFn}, workPredicateFn).
|
||||||
|
Watches(&source.Kind{Type: &policyv1alpha1.OverridePolicy{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: c.newOverridePolicyFunc()}).
|
||||||
|
Watches(&source.Kind{Type: &policyv1alpha1.ClusterOverridePolicy{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: c.newOverridePolicyFunc()}).
|
||||||
Complete(c)
|
Complete(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *ResourceBindingController) newOverridePolicyFunc() handler.ToRequestsFunc {
|
||||||
|
return func(a handler.MapObject) []reconcile.Request {
|
||||||
|
var overrideRS []policyv1alpha1.ResourceSelector
|
||||||
|
switch t := a.Object.(type) {
|
||||||
|
case *policyv1alpha1.ClusterOverridePolicy:
|
||||||
|
overrideRS = t.Spec.ResourceSelectors
|
||||||
|
case *policyv1alpha1.OverridePolicy:
|
||||||
|
overrideRS = t.Spec.ResourceSelectors
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
bindingList := &workv1alpha1.ResourceBindingList{}
|
||||||
|
if err := c.Client.List(context.TODO(), bindingList); err != nil {
|
||||||
|
klog.Errorf("Failed to list resourceBindings, error: %v", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var requests []reconcile.Request
|
||||||
|
for _, binding := range bindingList.Items {
|
||||||
|
workload, err := helper.FetchWorkload(c.DynamicClient, c.RESTMapper, binding.Spec.Resource)
|
||||||
|
if err != nil {
|
||||||
|
klog.Errorf("Failed to fetch workload for resourceBinding(%s/%s). Error: %v.", binding.Namespace, binding.Name, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, rs := range overrideRS {
|
||||||
|
if util.ResourceMatches(workload, rs) {
|
||||||
|
klog.V(2).Infof("Enqueue ResourceBinding(%s/%s) as override policy(%s/%s) changes.", binding.Namespace, binding.Name, a.Meta.GetNamespace(), a.Meta.GetName())
|
||||||
|
requests = append(requests, reconcile.Request{NamespacedName: types.NamespacedName{Namespace: binding.Namespace, Name: binding.Name}})
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return requests
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@ import (
|
||||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/source"
|
"sigs.k8s.io/controller-runtime/pkg/source"
|
||||||
|
|
||||||
|
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
|
||||||
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
|
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
|
||||||
"github.com/karmada-io/karmada/pkg/util"
|
"github.com/karmada-io/karmada/pkg/util"
|
||||||
"github.com/karmada-io/karmada/pkg/util/helper"
|
"github.com/karmada-io/karmada/pkg/util/helper"
|
||||||
|
@ -108,12 +109,12 @@ func (c *ClusterResourceBindingController) SetupWithManager(mgr controllerruntim
|
||||||
var requests []reconcile.Request
|
var requests []reconcile.Request
|
||||||
|
|
||||||
labels := a.Meta.GetLabels()
|
labels := a.Meta.GetLabels()
|
||||||
_, nameExist := labels[util.ClusterResourceBindingLabel]
|
clusterResourcebindingName, nameExist := labels[util.ClusterResourceBindingLabel]
|
||||||
if !nameExist {
|
if !nameExist {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
namespacesName := types.NamespacedName{
|
namespacesName := types.NamespacedName{
|
||||||
Name: labels[util.ClusterResourceBindingLabel],
|
Name: clusterResourcebindingName,
|
||||||
}
|
}
|
||||||
|
|
||||||
requests = append(requests, reconcile.Request{NamespacedName: namespacesName})
|
requests = append(requests, reconcile.Request{NamespacedName: namespacesName})
|
||||||
|
@ -122,5 +123,46 @@ func (c *ClusterResourceBindingController) SetupWithManager(mgr controllerruntim
|
||||||
|
|
||||||
return controllerruntime.NewControllerManagedBy(mgr).For(&workv1alpha1.ClusterResourceBinding{}).
|
return controllerruntime.NewControllerManagedBy(mgr).For(&workv1alpha1.ClusterResourceBinding{}).
|
||||||
Watches(&source.Kind{Type: &workv1alpha1.Work{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: workFn}, workPredicateFn).
|
Watches(&source.Kind{Type: &workv1alpha1.Work{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: workFn}, workPredicateFn).
|
||||||
|
Watches(&source.Kind{Type: &policyv1alpha1.OverridePolicy{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: c.newOverridePolicyFunc()}).
|
||||||
|
Watches(&source.Kind{Type: &policyv1alpha1.ClusterOverridePolicy{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: c.newOverridePolicyFunc()}).
|
||||||
Complete(c)
|
Complete(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *ClusterResourceBindingController) newOverridePolicyFunc() handler.ToRequestsFunc {
|
||||||
|
return func(a handler.MapObject) []reconcile.Request {
|
||||||
|
var overrideRS []policyv1alpha1.ResourceSelector
|
||||||
|
switch t := a.Object.(type) {
|
||||||
|
case *policyv1alpha1.ClusterOverridePolicy:
|
||||||
|
overrideRS = t.Spec.ResourceSelectors
|
||||||
|
case *policyv1alpha1.OverridePolicy:
|
||||||
|
overrideRS = t.Spec.ResourceSelectors
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
bindingList := &workv1alpha1.ClusterResourceBindingList{}
|
||||||
|
if err := c.Client.List(context.TODO(), bindingList); err != nil {
|
||||||
|
klog.Errorf("Failed to list clusterResourceBindings, error: %v", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var requests []reconcile.Request
|
||||||
|
for _, binding := range bindingList.Items {
|
||||||
|
workload, err := helper.FetchWorkload(c.DynamicClient, c.RESTMapper, binding.Spec.Resource)
|
||||||
|
if err != nil {
|
||||||
|
klog.Errorf("Failed to fetch workload for clusterResourceBinding(%s). Error: %v.", binding.Name, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, rs := range overrideRS {
|
||||||
|
if util.ResourceMatches(workload, rs) {
|
||||||
|
klog.V(2).Infof("Enqueue ClusterResourceBinding(%s) as override policy(%s/%s) changes.", binding.Name, a.Meta.GetNamespace(), a.Meta.GetName())
|
||||||
|
requests = append(requests, reconcile.Request{NamespacedName: types.NamespacedName{Name: binding.Name}})
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return requests
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -89,7 +89,7 @@ func (o *overrideManagerImpl) applyClusterOverrides(rawObj *unstructured.Unstruc
|
||||||
|
|
||||||
matchedPolicies := o.getMatchedClusterOverridePolicy(policyList.Items, rawObj, cluster)
|
matchedPolicies := o.getMatchedClusterOverridePolicy(policyList.Items, rawObj, cluster)
|
||||||
if len(matchedPolicies) == 0 {
|
if len(matchedPolicies) == 0 {
|
||||||
klog.V(2).Infof("No cluster override policy for resource: %s", rawObj.GetName())
|
klog.V(2).Infof("No cluster override policy for resource: %s/%s", rawObj.GetNamespace(), rawObj.GetName())
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue