Skip to content

Commit

Permalink
interface: update apply strategy to call out allowing co-ownership ex…
Browse files Browse the repository at this point in the history
…plicitly (Azure#737)
  • Loading branch information
zhiying-lin authored Mar 27, 2024
1 parent 72b325a commit 71e6caf
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 20 deletions.
23 changes: 16 additions & 7 deletions apis/placement/v1beta1/clusterresourceplacement_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,16 +427,24 @@ type RolloutStrategy struct {
}

// ApplyStrategy describes how to resolve the conflict if the resource to be placed already exists in the target cluster
// and is owned by other appliers.
// and whether it's allowed to be co-owned by other non-fleet appliers.
// Note: If multiple CRPs try to place the same resource with different apply strategy, the later ones will fail with the
// reason ApplyConflictBetweenPlacements.
type ApplyStrategy struct {
// Type defines the type of strategy to use. Default to FailIfExists.
// +kubebuilder:default=FailIfExists
// +kubebuilder:validation:Enum=FailIfExists;ServerSideApply
// Type defines the type of strategy to use. Default to ClientSideApply.
// Server-side apply is a safer choice. Read more about the differences between server-side apply and client-side
// apply: https://kubernetes.io/docs/reference/using-api/server-side-apply/#comparison-with-client-side-apply.
// +kubebuilder:default=ClientSideApply
// +kubebuilder:validation:Enum=ClientSideApply;ServerSideApply
// +optional
Type ApplyStrategyType `json:"type,omitempty"`

// AllowCoOwnership defines whether to apply the resource if it already exists in the target cluster and is not
// solely owned by fleet (i.e., metadata.ownerReferences contains only fleet custom resources).
// If true, apply the resource and add fleet as a co-owner.
// If false, leave the resource unchanged and fail the apply.
AllowCoOwnership bool `json:"allowCoOwnership,omitempty"`

// ServerSideApplyConfig defines the configuration for server side apply. It is honored only when type is ServerSideApply.
// +optional
ServerSideApplyConfig *ServerSideApplyConfig `json:"serverSideApplyConfig,omitempty"`
Expand All @@ -448,9 +456,10 @@ type ApplyStrategy struct {
type ApplyStrategyType string

const (
// ApplyStrategyTypeFailIfExists will fail to apply a resource if it already exists in the target cluster and is owned
// by other appliers.
ApplyStrategyTypeFailIfExists ApplyStrategyType = "FailIfExists"
// ApplyStrategyTypeClientSideApply will use three-way merge patch similar to how `kubectl apply` does by storing
// last applied state in the `last-applied-configuration` annotation.
// When the `last-applied-configuration` annotation size is greater than 256kB, it falls back to the server-side apply.
ApplyStrategyTypeClientSideApply ApplyStrategyType = "ClientSideApply"

// ApplyStrategyTypeServerSideApply will use server-side apply to resolve conflicts between the resource to be placed
// and the existing resource in the target cluster.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ spec:
the resource to be placed already exists in the target cluster and
is owned by other appliers.
properties:
allowCoOwnership:
description: AllowCoOwnership defines whether to apply the resource
if it already exists in the target cluster and is not solely
owned by fleet (i.e., metadata.ownerReferences contains only
fleet custom resources). If true, apply the resource and add
fleet as a co-owner. If false, leave the resource unchanged
and fail the apply.
type: boolean
serverSideApplyConfig:
description: ServerSideApplyConfig defines the configuration for
server side apply. It is honored only when type is ServerSideApply.
Expand All @@ -72,11 +80,13 @@ spec:
type: boolean
type: object
type:
default: FailIfExists
description: Type defines the type of strategy to use. Default
to FailIfExists.
default: ClientSideApply
description: 'Type defines the type of strategy to use. Default
to ClientSideApply. Server-side apply is a safer choice. Read
more about the differences between server-side apply and client-side
apply: https://kubernetes.io/docs/reference/using-api/server-side-apply/#comparison-with-client-side-apply.'
enum:
- FailIfExists
- ClientSideApply
- ServerSideApply
type: string
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,14 @@ spec:
if the resource to be placed already exists in the target cluster
and is owned by other appliers.
properties:
allowCoOwnership:
description: AllowCoOwnership defines whether to apply the
resource if it already exists in the target cluster and
is not solely owned by fleet (i.e., metadata.ownerReferences
contains only fleet custom resources). If true, apply the
resource and add fleet as a co-owner. If false, leave the
resource unchanged and fail the apply.
type: boolean
serverSideApplyConfig:
description: ServerSideApplyConfig defines the configuration
for server side apply. It is honored only when type is ServerSideApply.
Expand All @@ -641,11 +649,13 @@ spec:
type: boolean
type: object
type:
default: FailIfExists
description: Type defines the type of strategy to use. Default
to FailIfExists.
default: ClientSideApply
description: 'Type defines the type of strategy to use. Default
to ClientSideApply. Server-side apply is a safer choice.
Read more about the differences between server-side apply
and client-side apply: https://kubernetes.io/docs/reference/using-api/server-side-apply/#comparison-with-client-side-apply.'
enum:
- FailIfExists
- ClientSideApply
- ServerSideApply
type: string
type: object
Expand Down
18 changes: 14 additions & 4 deletions config/crd/bases/placement.kubernetes-fleet.io_works.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ spec:
the resource to be placed already exists in the target cluster and
is owned by other appliers.
properties:
allowCoOwnership:
description: AllowCoOwnership defines whether to apply the resource
if it already exists in the target cluster and is not solely
owned by fleet (i.e., metadata.ownerReferences contains only
fleet custom resources). If true, apply the resource and add
fleet as a co-owner. If false, leave the resource unchanged
and fail the apply.
type: boolean
serverSideApplyConfig:
description: ServerSideApplyConfig defines the configuration for
server side apply. It is honored only when type is ServerSideApply.
Expand All @@ -58,11 +66,13 @@ spec:
type: boolean
type: object
type:
default: FailIfExists
description: Type defines the type of strategy to use. Default
to FailIfExists.
default: ClientSideApply
description: 'Type defines the type of strategy to use. Default
to ClientSideApply. Server-side apply is a safer choice. Read
more about the differences between server-side apply and client-side
apply: https://kubernetes.io/docs/reference/using-api/server-side-apply/#comparison-with-client-side-apply.'
enum:
- FailIfExists
- ClientSideApply
- ServerSideApply
type: string
type: object
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/validator/clusterresourceplacement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func TestValidateClusterResourcePlacement_RolloutStrategy(t *testing.T) {
strategy: placementv1beta1.RolloutStrategy{
Type: placementv1beta1.RollingUpdateRolloutStrategyType,
ApplyStrategy: &placementv1beta1.ApplyStrategy{
Type: placementv1beta1.ApplyStrategyTypeFailIfExists,
Type: placementv1beta1.ApplyStrategyTypeClientSideApply,
ServerSideApplyConfig: &placementv1beta1.ServerSideApplyConfig{
ForceConflicts: false,
},
Expand Down

0 comments on commit 71e6caf

Please sign in to comment.