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

fix: Filter patches to drop "remove" operation to prevent dropping unknown fields #1333

Closed
wants to merge 5 commits into from

Conversation

aramase
Copy link
Member

@aramase aramase commented Apr 30, 2024

Reason for Change:

Filter patches to only allow "add" operation to prevent dropping unknown fields

Issue Fixed:

fixes #1312

@codecov-commenter
Copy link

codecov-commenter commented Apr 30, 2024

Codecov Report

Attention: Patch coverage is 40.00000% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 52.85%. Comparing base (3874533) to head (cc3c97a).
Report is 6 commits behind head on main.

Files Patch % Lines
pkg/webhook/webhook.go 40.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1333      +/-   ##
==========================================
+ Coverage   51.66%   52.85%   +1.18%     
==========================================
  Files          53       53              
  Lines        2938     2433     -505     
==========================================
- Hits         1518     1286     -232     
+ Misses       1378     1104     -274     
- Partials       42       43       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@aramase aramase force-pushed the aramase/c/fix_fields_dropped branch 2 times, most recently from a221f09 to 3aa02ec Compare April 30, 2024 22:31
@aramase aramase marked this pull request as ready for review April 30, 2024 22:37
@aramase aramase requested a review from enj as a code owner April 30, 2024 22:37
@aramase aramase marked this pull request as draft April 30, 2024 22:48
@aramase aramase force-pushed the aramase/c/fix_fields_dropped branch from 3aa02ec to 046bc26 Compare May 1, 2024 00:23
@aramase aramase changed the title fix: Filter patches to only allow "add" operation to prevent dropping unknown fields fix: Filter patches to only drop "remove" operation to prevent dropping unknown fields May 1, 2024
@aramase aramase changed the title fix: Filter patches to only drop "remove" operation to prevent dropping unknown fields fix: Filter patches to drop "remove" operation to prevent dropping unknown fields May 1, 2024
Signed-off-by: Anish Ramasekar <[email protected]>
// // drop patches that are removing fields
// // this prevents patches that remove fields that the webhook doesn't know about
// // ref: https://github.com/Azure/azure-workload-identity/issues/1312
// if patch.Operation == "remove" {
Copy link
Member

Choose a reason for hiding this comment

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

This commented out approach seems safer? And maybe invert the check so we only keep the type of operations we understand?

// return patchResponse

// Merge the original raw object with the modified marshaled pod
patchedObject, err := strategicpatch.StrategicMergePatch(req.Object.Raw, marshaledPod, corev1.Pod{})
Copy link
Member

Choose a reason for hiding this comment

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

I haven't seen any other webhook use this approach, so it makes me nervous that it won't work correctly in all cases.

readerObjects: nil,
},
{
name: "pod has the required label, restart policy in init container",
Copy link
Member

Choose a reason for hiding this comment

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

Also add tests where we fabricate fields that will never exist at different nesting levels that don't exist anywhere in corev1.Pod. Like pod.Panda or pod.Spec.Panda or pod.Spec.Jungle.Panda.

@aramase aramase closed this May 2, 2024
@aramase aramase deleted the aramase/c/fix_fields_dropped branch May 2, 2024 16:58
@aramase
Copy link
Member Author

aramase commented May 2, 2024

Closing this PR after further testing.

In e2e tests we create a pod with init container and test injecting sidecar proxy. The sidecar is added as the first container in the list so it's started first and the way patches get generated instead of doing a complete replace of
/spec/containers/0, it does replace and remove on individual fields (which is expected)

{replace /spec/containers/0/name azwi-proxy}
{replace /spec/containers/0/image aramase/proxy:add_op_00}
{add /spec/containers/0/ports [map[containerPort:8080]]}
{add /spec/containers/0/securityContext/privileged false}
{replace /spec/containers/0/securityContext/runAsNonRoot true}
{add /spec/containers/0/securityContext/readOnlyRootFilesystem true}
{remove /spec/containers/0/securityContext/seccompProfile <nil>}
{remove /spec/containers/0/securityContext/runAsUser <nil>}
{remove /spec/containers/0/args/2 <nil>}
{replace /spec/containers/0/args/1 --log-level=info}
{replace /spec/containers/0/args/0 --proxy-port=8080}

if we skip "remove" operation, it breaks because the args and other fields incorrectly remain.

  1. The k8s version bump will resolve the current issue with Kubernetes native sidecar fields.
  2. I've opened Generating generic patches for only the required changes #1337 for us to investigate further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Kubernetes native sidecar not supported
3 participants