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

refactor: effective tls policies reconciler #927

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

KevFan
Copy link
Contributor

@KevFan KevFan commented Oct 9, 2024

Description

Part of #819 #824

  • Refactor the logic for the management of TLSPolicy Certificates into a workflow task
  • Re-add validation for TLS Policy to check for if Issuer kind is correct and if the issuer is present on cluster, with the addition of integrations tests to test for this

Verification

  • Passing integration tests should be enough to test that nothing is broken from this change

@KevFan KevFan self-assigned this Oct 11, 2024
@KevFan KevFan added the kind/enhancement New feature or request label Oct 11, 2024
Comment on lines +73 to +96
if policy.DeletionTimestamp != nil {
logger.V(1).Info("policy is marked for deletion, nothing to do", "name", policy.Name, "namespace", policy.Namespace, "uid", policy.GetUID())
continue
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No need for finalizer since associated Certificate's will also be deleted by owner ref

logger := controller.LoggerFromContext(ctx).WithName("EffectiveTLSPoliciesReconciler").WithName("Reconcile")

// Get all TLS Policies
policies := lo.Filter(topology.Policies().Items(), func(item machinery.Policy, index int) bool {
Copy link
Collaborator

@maleck13 maleck13 Oct 15, 2024

Choose a reason for hiding this comment

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

So are we reconciling every policy on the cluster with every change to the subscribed kinds? Not quite understanding how this is working

Copy link
Collaborator

@maleck13 maleck13 Oct 15, 2024

Choose a reason for hiding this comment

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

If so do we intend to have a way to not need to do this? Like if a certificate changes, there is a link between that certificate and the TLSPolicy that ownes it so we shouldn't need to reconcile all TLSPolicies because a certificate changed right. Or am I misunderstanding?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's fair. There's is a way to do reconcile only affected tls policies instead by using the events received. I've pushed a commit for this so this should be addressed now cd35907

@KevFan KevFan force-pushed the effective-tls-reconciler branch 5 times, most recently from af8c44c to cd35907 Compare October 16, 2024 09:21
@KevFan KevFan marked this pull request as ready for review October 16, 2024 09:24
Signed-off-by: KevFan <chfan@redhat.com>
Signed-off-by: KevFan <chfan@redhat.com>
Signed-off-by: KevFan <chfan@redhat.com>
Signed-off-by: KevFan <chfan@redhat.com>
…get ref

Signed-off-by: KevFan <chfan@redhat.com>
…rts"

This reverts commit 552d9f3.

Signed-off-by: KevFan <chfan@redhat.com>
Signed-off-by: KevFan <chfan@redhat.com>
Signed-off-by: KevFan <chfan@redhat.com>
Comment on lines +185 to +189
g := machinery.Gateway{Gateway: ob.(*gwapiv1.Gateway)}

affectedPolicies = append(affectedPolicies, lo.Filter(policies, func(item machinery.Policy, index int) bool {
for _, tg := range item.GetTargetRefs() {
if g.GetLocator() == tg.GetLocator() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you considered using g.Policies()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, dont think i'm able to since g is a new machinery.Gateway instance, so the policies will be nil unless you're saying to get the machinery.Gateway instance elsewhere? 🤔

Comment on lines +203 to +205
affectedPolicies = append(affectedPolicies, lo.Filter(policies, func(item machinery.Policy, index int) bool {
return item.GetName() == ob.GetName() && item.GetNamespace() == ob.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 understand this will filter out all TLSPolicies that have been deleted. Unless that's what indeed you want, could we simply cast ob maybe?

Suggested change
affectedPolicies = append(affectedPolicies, lo.Filter(policies, func(item machinery.Policy, index int) bool {
return item.GetName() == ob.GetName() && item.GetNamespace() == ob.GetNamespace()
})...)
p, _ := ob.(*TLSPolicy)
affectedPolicies = append(affectedPolicies, p)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I didn't intend to do this, but that is simpler indeed. Why it's working as is due to there's no work to be done on tlspolicies marked for deletion 🤔

Comment on lines +214 to +217
affectedPolicies = append(affectedPolicies, lo.Filter(policies, func(item machinery.Policy, index int) bool {
p := item.(*kuadrantv1alpha1.TLSPolicy)
return utils.IsOwnedBy(ob, p)
})...)
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe you could leverage the topology links here too:

Suggested change
affectedPolicies = append(affectedPolicies, lo.Filter(policies, func(item machinery.Policy, index int) bool {
p := item.(*kuadrantv1alpha1.TLSPolicy)
return utils.IsOwnedBy(ob, p)
})...)
c, _ := ob.(*controller.RuntimeObject).Object.(*certmanagerv1.Certificate)
listeners := topology.Targetables().Parents(machinery.LocatorFromObject(c))
affectedPolicies = append(affectedPolicies, lo.FlatMap(listeners, func(l machinery.Targetable) ([]machinery.Policy, bool) {
return l.(*machinery.Listener).Gateway.Policies()
})...)

In this particular case, not sure if it's better or worse. I'm just thinking about avoiding filtering the same list of policies so many times.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, that's true. If you feel strongly on this I can update, but using the owner refernence looks a lot more readable unless filtering the list of policies should be avoided in general? 🤔

}

// Return only unique policies as there can be duplicates from multiple events
return lo.Uniq(affectedPolicies)
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm. Maybe specify a uniqueness key and thus avoid possible problems with pointer comparison:

Suggested change
return lo.Uniq(affectedPolicies)
return lo.UniqBy(affectedPolicies, func(p machinery.Policy) string { return p.GetLocator() } )

// TODO: Update when targeting by section name is allowed, the listener will contain the policy rather than the gateway
listeners := lo.FilterMap(topology.Targetables().Items(), func(t machinery.Targetable, index int) (*machinery.Listener, bool) {
l, ok := t.(*machinery.Listener)
return l, ok && lo.Contains(l.Gateway.Policies(), p)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm actually surprised equality (==) between two policy works.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I think it compares the pointers between the two policies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants