From eb2a4bd0516a8d5c3231a81ef78db00282398af9 Mon Sep 17 00:00:00 2001 From: Monokaix Date: Wed, 25 Dec 2024 12:16:36 +0800 Subject: [PATCH] decouple suspension of propagation and resourcebinding Signed-off-by: Monokaix --- api/openapi-spec/swagger.json | 16 ++++++++- pkg/apis/work/v1alpha2/binding_types.go | 7 +++- .../work/v1alpha2/zz_generated.deepcopy.go | 19 ++++++++++- pkg/controllers/binding/common.go | 2 +- pkg/controllers/binding/common_test.go | 12 +++---- pkg/detector/detector.go | 11 +++++-- pkg/generated/openapi/zz_generated.openapi.go | 33 +++++++++++++++++-- 7 files changed, 86 insertions(+), 14 deletions(-) diff --git a/api/openapi-spec/swagger.json b/api/openapi-spec/swagger.json index 2460fc7237c8..ea95dcf82dc5 100644 --- a/api/openapi-spec/swagger.json +++ b/api/openapi-spec/swagger.json @@ -20395,7 +20395,7 @@ }, "suspension": { "description": "Suspension declares the policy for suspending different aspects of propagation. nil means no suspension. no default values.", - "$ref": "#/definitions/com.github.karmada-io.karmada.pkg.apis.policy.v1alpha1.Suspension" + "$ref": "#/definitions/com.github.karmada-io.karmada.pkg.apis.work.v1alpha2.Suspension" } } }, @@ -20434,6 +20434,20 @@ } } }, + "com.github.karmada-io.karmada.pkg.apis.work.v1alpha2.Suspension": { + "description": "Suspension defines the policy for suspending of propagation.", + "type": "object", + "properties": { + "dispatching": { + "description": "Dispatching controls whether dispatching should be suspended. nil means not suspend, no default value, only accepts 'true'. Note: true means stop propagating to all clusters. Can not co-exist with DispatchingOnClusters which is used to suspend particular clusters.", + "type": "boolean" + }, + "dispatchingOnClusters": { + "description": "DispatchingOnClusters declares a list of clusters to which the dispatching should be suspended. Note: Can not co-exist with Dispatching which is used to suspend all.", + "$ref": "#/definitions/com.github.karmada-io.karmada.pkg.apis.policy.v1alpha1.SuspendClusters" + } + } + }, "com.github.karmada-io.karmada.pkg.apis.work.v1alpha2.TargetCluster": { "description": "TargetCluster represents the identifier of a member cluster.", "type": "object", diff --git a/pkg/apis/work/v1alpha2/binding_types.go b/pkg/apis/work/v1alpha2/binding_types.go index 92295e6b7738..b8cc06191f91 100644 --- a/pkg/apis/work/v1alpha2/binding_types.go +++ b/pkg/apis/work/v1alpha2/binding_types.go @@ -150,7 +150,7 @@ type ResourceBindingSpec struct { // Suspension declares the policy for suspending different aspects of propagation. // nil means no suspension. no default values. // +optional - Suspension *policyv1alpha1.Suspension `json:"suspension,omitempty"` + Suspension *Suspension `json:"suspension,omitempty"` // PreserveResourcesOnDeletion controls whether resources should be preserved on the // member clusters when the binding object is deleted. @@ -322,6 +322,11 @@ type BindingSnapshot struct { Clusters []TargetCluster `json:"clusters,omitempty"` } +// Suspension defines the policy for suspending of propagation. +type Suspension struct { + policyv1alpha1.Suspension `json:",inline"` +} + // ResourceBindingStatus represents the overall status of the strategy as well as the referenced resources. type ResourceBindingStatus struct { // SchedulerObservedGeneration is the generation(.metadata.generation) observed by the scheduler. diff --git a/pkg/apis/work/v1alpha2/zz_generated.deepcopy.go b/pkg/apis/work/v1alpha2/zz_generated.deepcopy.go index 89bedd3d2dfe..cd42228e6f7b 100644 --- a/pkg/apis/work/v1alpha2/zz_generated.deepcopy.go +++ b/pkg/apis/work/v1alpha2/zz_generated.deepcopy.go @@ -362,7 +362,7 @@ func (in *ResourceBindingSpec) DeepCopyInto(out *ResourceBindingSpec) { } if in.Suspension != nil { in, out := &in.Suspension, &out.Suspension - *out = new(v1alpha1.Suspension) + *out = new(Suspension) (*in).DeepCopyInto(*out) } if in.PreserveResourcesOnDeletion != nil { @@ -417,6 +417,23 @@ func (in *ResourceBindingStatus) DeepCopy() *ResourceBindingStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *Suspension) DeepCopyInto(out *Suspension) { + *out = *in + in.Suspension.DeepCopyInto(&out.Suspension) + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Suspension. +func (in *Suspension) DeepCopy() *Suspension { + if in == nil { + return nil + } + out := new(Suspension) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *TargetCluster) DeepCopyInto(out *TargetCluster) { *out = *in diff --git a/pkg/controllers/binding/common.go b/pkg/controllers/binding/common.go index a6c609f0ae2a..2280c840c4dd 100644 --- a/pkg/controllers/binding/common.go +++ b/pkg/controllers/binding/common.go @@ -302,7 +302,7 @@ func needReviseReplicas(replicas int32, placement *policyv1alpha1.Placement) boo return replicas > 0 && placement != nil && placement.ReplicaSchedulingType() == policyv1alpha1.ReplicaSchedulingTypeDivided } -func shouldSuspendDispatching(suspension *policyv1alpha1.Suspension, targetCluster workv1alpha2.TargetCluster) bool { +func shouldSuspendDispatching(suspension *workv1alpha2.Suspension, targetCluster workv1alpha2.TargetCluster) bool { if suspension == nil { return false } diff --git a/pkg/controllers/binding/common_test.go b/pkg/controllers/binding/common_test.go index e1c5832525f4..cb3073f39360 100644 --- a/pkg/controllers/binding/common_test.go +++ b/pkg/controllers/binding/common_test.go @@ -321,7 +321,7 @@ func Test_mergeConflictResolution(t *testing.T) { func Test_shouldSuspendDispatching(t *testing.T) { type args struct { - suspension *policyv1alpha1.Suspension + suspension *workv1alpha2.Suspension targetCluster workv1alpha2.TargetCluster } tests := []struct { @@ -337,28 +337,28 @@ func Test_shouldSuspendDispatching(t *testing.T) { { name: "false for nil dispatching", args: args{ - suspension: &policyv1alpha1.Suspension{Dispatching: nil}, + suspension: &workv1alpha2.Suspension{Suspension: policyv1alpha1.Suspension{Dispatching: nil}}, }, want: false, }, { name: "false for not suspension", args: args{ - suspension: &policyv1alpha1.Suspension{Dispatching: ptr.To(false)}, + suspension: &workv1alpha2.Suspension{Suspension: policyv1alpha1.Suspension{Dispatching: ptr.To(false)}}, }, want: false, }, { name: "true for suspension", args: args{ - suspension: &policyv1alpha1.Suspension{Dispatching: ptr.To(true)}, + suspension: &workv1alpha2.Suspension{Suspension: policyv1alpha1.Suspension{Dispatching: ptr.To(true)}}, }, want: true, }, { name: "true for matching cluster", args: args{ - suspension: &policyv1alpha1.Suspension{DispatchingOnClusters: &policyv1alpha1.SuspendClusters{ClusterNames: []string{"clusterA"}}}, + suspension: &workv1alpha2.Suspension{Suspension: policyv1alpha1.Suspension{DispatchingOnClusters: &policyv1alpha1.SuspendClusters{ClusterNames: []string{"clusterA"}}}}, targetCluster: workv1alpha2.TargetCluster{Name: "clusterA"}, }, want: true, @@ -366,7 +366,7 @@ func Test_shouldSuspendDispatching(t *testing.T) { { name: "false for mismatched cluster", args: args{ - suspension: &policyv1alpha1.Suspension{DispatchingOnClusters: &policyv1alpha1.SuspendClusters{ClusterNames: []string{"clusterB"}}}, + suspension: &workv1alpha2.Suspension{Suspension: policyv1alpha1.Suspension{DispatchingOnClusters: &policyv1alpha1.SuspendClusters{ClusterNames: []string{"clusterB"}}}}, targetCluster: workv1alpha2.TargetCluster{Name: "clusterA"}, }, want: false, diff --git a/pkg/detector/detector.go b/pkg/detector/detector.go index aacac44ae78e..734b20ddfbbd 100644 --- a/pkg/detector/detector.go +++ b/pkg/detector/detector.go @@ -724,7 +724,6 @@ func (d *ResourceDetector) BuildResourceBinding(object *unstructured.Unstructure Placement: &policySpec.Placement, Failover: policySpec.Failover, ConflictResolution: policySpec.ConflictResolution, - Suspension: policySpec.Suspension, PreserveResourcesOnDeletion: policySpec.PreserveResourcesOnDeletion, Resource: workv1alpha2.ObjectReference{ APIVersion: object.GetAPIVersion(), @@ -736,6 +735,11 @@ func (d *ResourceDetector) BuildResourceBinding(object *unstructured.Unstructure }, }, } + + if policySpec.Suspension != nil { + propagationBinding.Spec.Suspension = &workv1alpha2.Suspension{Suspension: *policySpec.Suspension} + } + claimFunc(propagationBinding, policyID, policyMeta) if d.ResourceInterpreter.HookEnabled(object.GroupVersionKind(), configv1alpha1.InterpreterOperationInterpretReplica) { @@ -769,7 +773,6 @@ func (d *ResourceDetector) BuildClusterResourceBinding(object *unstructured.Unst Placement: &policySpec.Placement, Failover: policySpec.Failover, ConflictResolution: policySpec.ConflictResolution, - Suspension: policySpec.Suspension, PreserveResourcesOnDeletion: policySpec.PreserveResourcesOnDeletion, Resource: workv1alpha2.ObjectReference{ APIVersion: object.GetAPIVersion(), @@ -781,6 +784,10 @@ func (d *ResourceDetector) BuildClusterResourceBinding(object *unstructured.Unst }, } + if policySpec.Suspension != nil { + binding.Spec.Suspension = &workv1alpha2.Suspension{Suspension: *policySpec.Suspension} + } + AddCPPClaimMetadata(binding, policyID, policyMeta) if d.ResourceInterpreter.HookEnabled(object.GroupVersionKind(), configv1alpha1.InterpreterOperationInterpretReplica) { diff --git a/pkg/generated/openapi/zz_generated.openapi.go b/pkg/generated/openapi/zz_generated.openapi.go index 5d76f3b2facc..ce9fadceac0e 100644 --- a/pkg/generated/openapi/zz_generated.openapi.go +++ b/pkg/generated/openapi/zz_generated.openapi.go @@ -180,6 +180,7 @@ func GetOpenAPIDefinitions(ref common.ReferenceCallback) map[string]common.OpenA "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.ResourceBindingList": schema_pkg_apis_work_v1alpha2_ResourceBindingList(ref), "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.ResourceBindingSpec": schema_pkg_apis_work_v1alpha2_ResourceBindingSpec(ref), "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.ResourceBindingStatus": schema_pkg_apis_work_v1alpha2_ResourceBindingStatus(ref), + "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.Suspension": schema_pkg_apis_work_v1alpha2_Suspension(ref), "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.TargetCluster": schema_pkg_apis_work_v1alpha2_TargetCluster(ref), "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.TaskOptions": schema_pkg_apis_work_v1alpha2_TaskOptions(ref), "k8s.io/api/admissionregistration/v1.AuditAnnotation": schema_k8sio_api_admissionregistration_v1_AuditAnnotation(ref), @@ -7374,7 +7375,7 @@ func schema_pkg_apis_work_v1alpha2_ResourceBindingSpec(ref common.ReferenceCallb "suspension": { SchemaProps: spec.SchemaProps{ Description: "Suspension declares the policy for suspending different aspects of propagation. nil means no suspension. no default values.", - Ref: ref("github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1.Suspension"), + Ref: ref("github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.Suspension"), }, }, "preserveResourcesOnDeletion": { @@ -7389,7 +7390,7 @@ func schema_pkg_apis_work_v1alpha2_ResourceBindingSpec(ref common.ReferenceCallb }, }, Dependencies: []string{ - "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1.FailoverBehavior", "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1.Placement", "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1.Suspension", "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.BindingSnapshot", "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.GracefulEvictionTask", "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.ObjectReference", "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.ReplicaRequirements", "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.TargetCluster", "k8s.io/apimachinery/pkg/apis/meta/v1.Time"}, + "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1.FailoverBehavior", "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1.Placement", "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.BindingSnapshot", "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.GracefulEvictionTask", "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.ObjectReference", "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.ReplicaRequirements", "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.Suspension", "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2.TargetCluster", "k8s.io/apimachinery/pkg/apis/meta/v1.Time"}, } } @@ -7456,6 +7457,34 @@ func schema_pkg_apis_work_v1alpha2_ResourceBindingStatus(ref common.ReferenceCal } } +func schema_pkg_apis_work_v1alpha2_Suspension(ref common.ReferenceCallback) common.OpenAPIDefinition { + return common.OpenAPIDefinition{ + Schema: spec.Schema{ + SchemaProps: spec.SchemaProps{ + Description: "Suspension defines the policy for suspending of propagation.", + Type: []string{"object"}, + Properties: map[string]spec.Schema{ + "dispatching": { + SchemaProps: spec.SchemaProps{ + Description: "Dispatching controls whether dispatching should be suspended. nil means not suspend, no default value, only accepts 'true'. Note: true means stop propagating to all clusters. Can not co-exist with DispatchingOnClusters which is used to suspend particular clusters.", + Type: []string{"boolean"}, + Format: "", + }, + }, + "dispatchingOnClusters": { + SchemaProps: spec.SchemaProps{ + Description: "DispatchingOnClusters declares a list of clusters to which the dispatching should be suspended. Note: Can not co-exist with Dispatching which is used to suspend all.", + Ref: ref("github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1.SuspendClusters"), + }, + }, + }, + }, + }, + Dependencies: []string{ + "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1.SuspendClusters"}, + } +} + func schema_pkg_apis_work_v1alpha2_TargetCluster(ref common.ReferenceCallback) common.OpenAPIDefinition { return common.OpenAPIDefinition{ Schema: spec.Schema{