Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preserve order of list in strategicMergePatch #467

Closed
wants to merge 1 commit into from

Conversation

mengqiy
Copy link
Member

@mengqiy mengqiy commented Mar 16, 2017

This is kind of a small proposal of how to preserve the order of list in strategicMergePatch.

Old behavior: we never preserve the order for list with merge patchStrategy. Instead we sort them by the mergeKey.

Related issue: #40373

cc: @pwittrock @lavalamp

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Mar 16, 2017
@mengqiy
Copy link
Member Author

mengqiy commented Mar 23, 2017

@lavalamp Ping

and list with replace `patchStrategy`, list with merge `patchStrategy`),
we always preserve the order of the list.

It is obvious that list with replace `patchStrategy` will preserve the order.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this sentence. No need to state that the obvious is obvious.

@@ -595,6 +595,16 @@ Strategic Merge Patch also supports special operations as listed below.

### List Operations

For all lists (including list of maps, list of primitives
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't strictly true or possible. What does preserve the order even mean when you merge lists:

  • AB
  • AC

@@ -595,6 +595,16 @@ Strategic Merge Patch also supports special operations as listed below.

### List Operations

For all lists (including list of maps, list of primitives
and list with replace `patchStrategy`, list with merge `patchStrategy`),
we always preserve the order of the list.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only true if we send the parallel list, which isn't required.


It is obvious that list with replace `patchStrategy` will preserve the order.

For list with merge `patchStrategy`, we send a parallel reorder list when there are changes in the list.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifying element ordering for a strategic merge patch on a list [Alpha]

By default, patching a list using the Strategic Merge strategy will provide no guarantees to the final ordering of elements in the patched list.

As of Kubernetes 1.7, Alpha support exists for explicitly specifying the ordering of elements in a during a Strategic Merge Patch. This can be done by providing a parallel list containing all of the patchMergeKeys of elements that need to be ordered. Any elements missing from the parallel list are guaranteed to appear after elements appearing in the parallel list.

When patching Strategic Merge lists, kubectl apply sends a parallel list containing keys for each of the elements in the configuration file.

@mengqiy
Copy link
Member Author

mengqiy commented Mar 29, 2017

Updated.

@pwittrock pwittrock assigned lavalamp and apelisse and unassigned lavalamp and pwittrock Apr 3, 2017
@pwittrock
Copy link
Member

@apelisse Could you give this a review?


By default, patching a list using the Strategic Merge strategy will provide no guarantees to the final ordering of elements in the patched list.

As of Kubernetes 1.7, Alpha support exists for explicitly specifying the ordering of elements in a during a Strategic Merge Patch.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing word "list" ?

Alpha support exists for explicitly specifying the ordering of elements in a LIST during a Strategic Merge Patch.


As of Kubernetes 1.7, Alpha support exists for explicitly specifying the ordering of elements in a during a Strategic Merge Patch.
This can be done by providing a parallel list containing all of the patchMergeKeys of elements that need to be ordered.
Any elements missing from the parallel list are guaranteed to appear after elements appearing in the parallel list.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is a relevant use-case, but what happens if the server adds many?

Original:

- A

Server adds B to F:

- A
- B
- C
- D
- E
- F

User applies:

- A
- B

We only guarantee that A and B will keep order?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or if the server reorders things in the meantime. Intent gets really muddled here.

Original:
A B C D E

how does a client indicate it want to move A to the end, tolerating any server-side reordering of the other elements?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We guaranteed the order from the user's config will always be preserved in the server.

We only guarantee that A and B will keep order?

Yes

how does a client indicate it want to move A to the end, tolerating any server-side reordering of the other elements?

If the server add some items in the list, a client cannot move something to the end without specify the whole list using this approach.

@@ -595,6 +595,16 @@ Strategic Merge Patch also supports special operations as listed below.

### List Operations

#### Specifying element ordering for a strategic merge patch on a list [Alpha]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is super confusing to put this here. This section of the doc is about when the verb is "list" (i.e., GET on a collection). Please make this part of the strategic merge patch section. Also should we really be editing the doc before it's implemented? I guess I would have expected a proposal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, sorry, I am wrong.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should specify strategic merge patch elsewhere. This is not the right doc to define it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Pulled strategic merge patch doc out.
#535

By default, patching a list using the Strategic Merge strategy will provide no guarantees to the final ordering of elements in the patched list.

As of Kubernetes 1.7, Alpha support exists for explicitly specifying the ordering of elements in a during a Strategic Merge Patch.
This can be done by providing a parallel list containing all of the patchMergeKeys of elements that need to be ordered.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what this means. An example would be helpful. Something that explicitly states the syntax of the patch would be best.

value: foo
- name: ENV2
value: bar
- name: ENV5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that this is a useful feature. It is very complex and I'm not sure why it is necessary, or even why it is an improvement? I can see adding an "append" command.

#### List of Primitives

We have two patch strategies for lists of primitives: replace and merge.
Replace is the default patch strategy, which will replace the whole list on update and it will preserve the order;
while merge strategy works as an unordered set. In this section, we call a primitive list with merge strategy an unordered set.
while merge strategy works as an ordered set. In this section, we call a primitive list with merge strategy an ordered set.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it still be possible to treat these items as an unordered set?

@lavalamp
Copy link
Member

lavalamp commented Apr 3, 2017

Apologies for delay in looking at this. I am lost in a sea of github notifications, next time chat me up on slack (or internally).

Is there a reason why kubernetes/kubernetes#40373 can't just use the replace strategy? We don't have random things mutating the environment variables, I don't think.

The patch strategy is defined in the go struct tag of the API objects,
e.g. `finalizers` uses `merge` as patch strategy.
```go
Finalizers []string `json:"finalizers,omitempty" patchStrategy:"merge" protobuf:"bytes,14,rep,name=finalizers"`
```

##### Unordered Set
##### Ordered Set

There are 3 operations: add, delete, replace.
Copy link
Member

@liggitt liggitt Apr 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's truly an ordered set, then there should be more than three operations (or add should be insert)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we shouldn't get rid of the current unordered set, we should add a way to deal with lists instead.

@liggitt
Copy link
Member

liggitt commented Apr 4, 2017

Is there a reason why kubernetes/kubernetes#40373 can't just use the replace strategy? We don't have random things mutating the environment variables, I don't think.

kubectl edit submits a patch computed by CreateTwoWayMergePatch, which means clients have baked-in structs with merge strategies, so they will only send the added/deleted items, which means we can't change the strategy to replace server-side or we'll drop all the envvars they didn't change.

@lavalamp
Copy link
Member

lavalamp commented Apr 4, 2017 via email

@mengqiy
Copy link
Member Author

mengqiy commented Apr 14, 2017

Move to a proposal PR in #537. PTAL

@mengqiy mengqiy closed this Apr 14, 2017
@mengqiy mengqiy deleted the preserve_order_SMP branch September 6, 2017 21:30
danehans pushed a commit to danehans/community that referenced this pull request Jul 18, 2023
* init

* cant be in both lists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants