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: Only process owner references for known kinds of owners. #245

Merged
merged 8 commits into from
Mar 8, 2023

Conversation

hessjcg
Copy link
Collaborator

@hessjcg hessjcg commented Mar 2, 2023

Pods can be owned by multiple owners. We only want the operator to traverse the owners where it knows
the kind of workload: ReplicaSet, Deployment, etc. We don't want the operator to try to travers other
kinds of owner resources that it does not understand, because the operator was not granted privileges
to access those resources.

Fixes #244

@hessjcg hessjcg requested a review from a team as a code owner March 2, 2023 22:45
@hessjcg hessjcg marked this pull request as draft March 3, 2023 17:27
if err != nil {
l.Info("/mutate-pod request can't be processed",
"kind", req.Kind.Kind, "ns", req.Namespace, "name", req.Name)
return admission.Errored(http.StatusInternalServerError, err)
}

updatedPod, err := a.handleCreatePodRequest(ctx, p)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I split the Handle function into 2 separate responsibilities:

  • func Handle(ctx, req) does all the transformation between admission.Request and admission.Response into useful domain objects.
  • func handleCreatePodRequest(ctx, pod) has the business logic to decide when and how to add Proxy containers to a pod as it is being created.

This made it a lot easier to write unit tests for the business logic without having to implement a lot of extraneous json handling code.

"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

func TestPodWebhookWithDeploymentOwners(t *testing.T) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the new unit test for the pod create webhook business logic.

wantUpdate: false,
},
{
name: "Deployment Pod with unknown owner",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the test case for the bug fix.

@hessjcg hessjcg marked this pull request as ready for review March 6, 2023 21:51
@hessjcg hessjcg requested a review from enocom March 6, 2023 21:51
}, "webapp")
dWithOwner.ObjectMeta.Labels = map[string]string{"app": "webapp"}
deploymentOwner := &v1.PartialObjectMetadata{
TypeMeta: v1.TypeMeta{Kind: "BuildToolOperatror", APIVersion: "v1"},
Copy link
Member

Choose a reason for hiding this comment

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

sp: BuildToolOperatror

nit: Maybe just "DontTouchThisThing" or something obviously not of a known kind. BuildToolOperator looks legit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed. I added more comments to make this easier to understand.

name: "Deployment Pod with unknown owner",
p: p,
d: dWithOwner,
wantUpdate: true,
Copy link
Member

Choose a reason for hiding this comment

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

Should this be false?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, this should be true. The AuthProxyWorkload should act on the Deployment's pods, even if the deployment is owned by a different operator. We have a user who wants the Cloud SQL Proxy Operator to add proxy containers to pods that are related to a deployment managed by another operator.

Copy link
Member

Choose a reason for hiding this comment

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

Is there a test covering the change you made to ignoring owners of an unknown kind in that case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, this is that test.

@hessjcg hessjcg force-pushed the gh-244-unknown-owner-ref branch from d7f9503 to 5764fbc Compare March 6, 2023 22:24
@hessjcg hessjcg force-pushed the gh-244-unknown-owner-ref branch from 05de7a8 to b6bad6d Compare March 7, 2023 02:41
@hessjcg hessjcg requested a review from enocom March 7, 2023 04:17
@hessjcg
Copy link
Collaborator Author

hessjcg commented Mar 7, 2023

/gcbrun

@hessjcg
Copy link
Collaborator Author

hessjcg commented Mar 7, 2023

/gcbrun

@hessjcg hessjcg merged commit 12be1dc into main Mar 8, 2023
@hessjcg hessjcg deleted the gh-244-unknown-owner-ref branch March 8, 2023 14:24
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.

Unable to get owner of resource object does not deploy sidecar to deployable Kind specified
2 participants