From 01acd8959e63100ed6f28ffc091d233144a03642 Mon Sep 17 00:00:00 2001 From: Ryan Zhang Date: Sun, 24 Nov 2024 13:35:19 -0800 Subject: [PATCH] add delete type override --- apis/placement/v1alpha1/override_types.go | 22 +++++++++++++++++-- .../v1alpha1/zz_generated.deepcopy.go | 5 +++-- pkg/controllers/updaterun/controller.go | 4 ++-- pkg/controllers/updaterun/controller_test.go | 3 ++- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/apis/placement/v1alpha1/override_types.go b/apis/placement/v1alpha1/override_types.go index 50f8526cc..4d3e211b1 100644 --- a/apis/placement/v1alpha1/override_types.go +++ b/apis/placement/v1alpha1/override_types.go @@ -56,6 +56,7 @@ type OverridePolicy struct { // OverrideRules defines an array of override rules to be applied on the selected resources. // The order of the rules determines the override order. // When there are two rules selecting the same fields on the target cluster, the last one will win. + // There can only be one override rule if it is a `Delete` type override rule. // You can have 1-20 rules. // +kubebuilder:validation:Required // +kubebuilder:validation:MinItems=1 @@ -66,6 +67,12 @@ type OverridePolicy struct { // OverrideRule defines how to override the selected resources on the target clusters. type OverrideRule struct { + // OverrideType defines the type of the override rules. + // +kubebuilder:validation:Enum=JSONPatch;Delete + // +kubebuilder:default:JSONPatch + // +optional + OverrideType OverrideType `json:"overrideType,omitempty"` + // ClusterSelectors selects the target clusters. // The resources will be overridden before applying to the matching clusters. // An empty clusterSelector selects ALL the member clusters. @@ -78,10 +85,21 @@ type OverrideRule struct { // +kubebuilder:validation:Required // +kubebuilder:validation:MinItems=1 // +kubebuilder:validation:MaxItems=20 - // +required - JSONPatchOverrides []JSONPatchOverride `json:"jsonPatchOverrides"` + // +optional + JSONPatchOverrides []JSONPatchOverride `json:"jsonPatchOverrides,omitempty"` } +// OverrideType defines the type of Override +type OverrideType string + +const ( + // JSONPatchOverrideType applies a JSON patch on the selected resources following [RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902). + JSONPatchOverrideType OverrideType = "JSONPatch" + + // DeleteOverrideType deletes the selected resources on the target clusters. + DeleteOverrideType OverrideType = "Delete" +) + // +genclient // +genclient:Namespaced // +kubebuilder:object:root=true diff --git a/apis/placement/v1alpha1/zz_generated.deepcopy.go b/apis/placement/v1alpha1/zz_generated.deepcopy.go index d26fe49a5..1a3f72658 100644 --- a/apis/placement/v1alpha1/zz_generated.deepcopy.go +++ b/apis/placement/v1alpha1/zz_generated.deepcopy.go @@ -10,10 +10,11 @@ Licensed under the MIT license. package v1alpha1 import ( - "go.goms.io/fleet/apis/placement/v1beta1" - "k8s.io/apimachinery/pkg/apis/meta/v1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/intstr" + + "go.goms.io/fleet/apis/placement/v1beta1" ) // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. diff --git a/pkg/controllers/updaterun/controller.go b/pkg/controllers/updaterun/controller.go index d570bf399..43fe30c4b 100644 --- a/pkg/controllers/updaterun/controller.go +++ b/pkg/controllers/updaterun/controller.go @@ -11,8 +11,6 @@ import ( "fmt" "time" - "go.goms.io/fleet/pkg/utils" - "go.goms.io/fleet/pkg/utils/controller" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/record" "k8s.io/client-go/util/workqueue" @@ -27,6 +25,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" placementv1alpha1 "go.goms.io/fleet/apis/placement/v1alpha1" + "go.goms.io/fleet/pkg/utils" + "go.goms.io/fleet/pkg/utils/controller" ) // Reconciler reconciles a ClusterStagedUpdateRun object. diff --git a/pkg/controllers/updaterun/controller_test.go b/pkg/controllers/updaterun/controller_test.go index bf7d07f53..2fd1b3032 100644 --- a/pkg/controllers/updaterun/controller_test.go +++ b/pkg/controllers/updaterun/controller_test.go @@ -8,11 +8,12 @@ package updaterun import ( "testing" - placementv1alpha1 "go.goms.io/fleet/apis/placement/v1alpha1" "k8s.io/client-go/util/workqueue" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllertest" "sigs.k8s.io/controller-runtime/pkg/reconcile" + + placementv1alpha1 "go.goms.io/fleet/apis/placement/v1alpha1" ) func TestHandleClusterApprovalRequest(t *testing.T) {