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

Make gwctl work with Policies having multiple targetRefs #3217

Closed
wants to merge 4 commits into from

Conversation

deszhou
Copy link
Member

@deszhou deszhou commented Jul 25, 2024

What type of PR is this?

What this PR does / why we need it:

Which issue(s) this PR fixes:

Fixes #3196

Does this PR introduce a user-facing change?:

Make gwctl work with Policies having multiple targetRefs

gwctl get policies -A

NAME                          KIND                       TARGET REFS                                                      POLICY TYPE  AGE
health-check-gatewayclass     HealthCheckPolicy.foo.com  foo-gatewayclass (GatewayClass), foo-gateway (Gateway), +2 more  Inherited    6d

gwctl describe policies health-check-gateway

Name: health-check-gatewayclass
Group: foo.com
Kind: HealthCheckPolicy
Inherited: "true"
Spec:
  ...
  targetRefs:
  - group: gateway.networking.k8s.io
    kind: GatewayClass
    name: foo-gatewayclass
    namespace: default
  - group: gateway.networking.k8s.io
    kind: Gateway
    name: foo-gateway
    namespace: default

@k8s-ci-robot k8s-ci-robot added the release-note Denotes a PR that will be considered when it comes time to generate release notes. label Jul 25, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: deszhou
Once this PR has been reviewed and has the lgtm label, please assign gauravkghildiyal for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jul 25, 2024
@gauravkghildiyal
Copy link
Member

Thanks @deszhou.

I'll give a more detailed look later, but just wanted to call out that the tricky thing here would be that we would like to support both targetRef and targetRefs i.e. allow both multiple targets and a single target.

@deszhou
Copy link
Member Author

deszhou commented Jul 26, 2024

Thanks @deszhou.

I'll give a more detailed look later, but just wanted to call out that the tricky thing here would be that we would like to support both targetRef and targetRefs i.e. allow both multiple targets and a single target.

SGTM, and for consistency, we should use the targetRefs field when describing. I’ll update the PR with this shortly

@gauravkghildiyal
Copy link
Member

Sorry for the delay here @deszhou. I'm attempting to get #3244 in and that may have some serious merge conflicts with this. I'd really appreciate if we can wait out a bit here, or else I'll try making an effort of rebasing on top of this change.

Comment on lines +252 to +256
Namespace: u.GetNamespace(),
}
if objRef.Namespace == "" {
objRef.Namespace = u.GetNamespace()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I may be missing something, but I understand objRef.Namespace has been initialized to u.GetNamespace() already, no? Then, if it's an empty string, it's set again to the same u.GetNamespace()? Maybe you meant:

Suggested change
Namespace: u.GetNamespace(),
}
if objRef.Namespace == "" {
objRef.Namespace = u.GetNamespace()
}
Namespace: structuredPolicy.GetNamespace(),
}
if objRef.Namespace == "" {
objRef.Namespace = u.GetNamespace()
}

Comment on lines +257 to +259
if targetRef.Namespace != nil {
objRef.Namespace = string(*targetRef.Namespace)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're willing to import k8s.io/utils/ptr, this kind of pattern can be written:

objRef.Namespace = ptr.Deref(targetRef.Namespace, structuredPolicy.GetNamespace())

Comment on lines +317 to +319
if objRef.Kind == "Namespace" && objRef.Name == "" {
objRef.Name = "default"
}
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this could be done outside the loop.

Comment on lines +323 to +325
if objRef.Kind != "Namespace" && objRef.Namespace == "" {
objRef.Namespace = "default"
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Same. Could be done outside the loop.

result, ok := spec.(map[string]interface{})
if !ok {
return nil
}

Copy link
Contributor

Choose a reason for hiding this comment

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

A bit out of scope, but we could probably use unstructured.NestedMap instead of unstructured.NestedFieldCopy at line 347 and thus save the extra type casting.

@@ -149,10 +156,26 @@ func mergePolicy(parent, child Policy) (Policy, error) {
result.u.SetUnstructuredContent(resultUnstructured)
// Merging two policies means the targetRef no longer makes any sense since
// since they can be conflicting. So we unset the targetRef.
result.targetRef = common.ObjRef{}
result.targetRefs = mergeTargetRefs(parent.targetRefs, child.targetRefs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Say parent has targetRefs: [a, b] and child has targetRefs: [a, c]. So the merged (effective) policy applies to [a, b, c]?

Sorry, I may need to educate myself on how policies to be merged are selected in the first place – or, more precisely, how the policies are assigned to a particular node in the resource model. It just triggered me the idea that, if you ask for the effective policy for a, it will return a merge of parent and child, along with the info that such effective policy also applies to c. However, that's not entire really true, because if you ask for the effective policy for c, it may return something identical to child only (not a necessarily the parent+child merge.)

Concrete example – Assume a == gateway-1, b == route-1, and c == route-2, though with route-1.parentRefs == [gateway-1] and route-2.parentRefs == [gateway-2]. (Notice route-2 is NOT parented by gateway-1.)

IOW:

kind: Gateway
metadata:
  name: gateway-1
---
kind: Gateway
metadata:
  name: gateway-2
---
kind: HTTPRoute
metadata:
  name: route-1
spec:
  parentRefs:
  - kind: Gateway
    name: gateway-1
---
kind: HTTPRoute
metadata:
  name: route-2
spec:
  parentRefs:
  - kind: Gateway
    name: gateway-2
---
kind: Policy
metadata:
  name: parent
spec:
  targetRefs:
  - kind: Gateway
    name: gateway-1
  - kind: HTTPRoute
    name: route-1
---
kind: Policy
metadata:
  name: child
spec:
  targetRefs:
  - kind: Gateway
    name: gateway-1
  - kind: HTTPRoute
    name: route-2

Effective policies:

gateway-1 → parent + child (targetRefs: [gateway-1, route-1, route-2])
gateway 2 → ∅
route-1 → parent (targetRefs: [gateway-1, route-1])
route-2 → child (targetRefs: [gateway-1, route-2])

By reading the first line, if you trust parent applies to route-2 (or that child applies to route-1 analogously), you would be wrong.

@deszhou
Copy link
Member Author

deszhou commented Sep 9, 2024

Thanks, @guicassolato! I’ll update this PR and fix the issue after #3244.

@k8s-ci-robot
Copy link
Contributor

@deszhou: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-gateway-api-crds-validation-4 7408571 link true /test pull-gateway-api-crds-validation-4

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 17, 2024
@k8s-ci-robot
Copy link
Contributor

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@shaneutt shaneutt added the priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. label Sep 18, 2024
@shaneutt shaneutt added this to the v1.3.0 milestone Sep 18, 2024
@deszhou deszhou closed this Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/gwctl cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
Development

Successfully merging this pull request may close these issues.

Make gwctl work with Policies having multiple targetRefs
5 participants