-
Notifications
You must be signed in to change notification settings - Fork 595
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
Reconcile event policies for mt-broker #8090
Changes from 5 commits
0d02e63
36ba38b
eac9d90
6d6c791
1babdd5
06cfe3a
692d303
a568dbd
60076af
cc53eba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -28,6 +28,7 @@ import ( | |||||
"k8s.io/apimachinery/pkg/api/meta" | ||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||||||
"k8s.io/apimachinery/pkg/labels" | ||||||
"k8s.io/apimachinery/pkg/types" | ||||||
"k8s.io/client-go/dynamic" | ||||||
corev1listers "k8s.io/client-go/listers/core/v1" | ||||||
|
@@ -46,6 +47,7 @@ import ( | |||||
duckv1 "knative.dev/eventing/pkg/apis/duck/v1" | ||||||
"knative.dev/eventing/pkg/apis/eventing" | ||||||
eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1" | ||||||
eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1" | ||||||
"knative.dev/eventing/pkg/apis/feature" | ||||||
messagingv1 "knative.dev/eventing/pkg/apis/messaging/v1" | ||||||
"knative.dev/eventing/pkg/auth" | ||||||
|
@@ -264,6 +266,11 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, b *eventingv1.Broker) pk | |||||
return fmt.Errorf("could not update broker status with EventPolicies: %v", err) | ||||||
} | ||||||
|
||||||
// Reconcile the EventPolicy for the Broker. | ||||||
if err := r.reconcileBrokerChannelEventPolicies(ctx, b, triggerChan, featureFlags); err != nil { | ||||||
return fmt.Errorf("failed to reconcile EventPolicy for Broker %s: %w", b.Name, err) | ||||||
} | ||||||
|
||||||
// So, at this point the Broker is ready and everything should be solid | ||||||
// for the triggers to act upon. | ||||||
return nil | ||||||
|
@@ -428,6 +435,55 @@ func (r *Reconciler) reconcileChannel(ctx context.Context, channelResourceInterf | |||||
return channelable, nil | ||||||
} | ||||||
|
||||||
func (r *Reconciler) reconcileBrokerChannelEventPolicies(ctx context.Context, b *eventingv1.Broker, triggerChan *duckv1.Channelable, featureFlags feature.Flags) error { | ||||||
expected := resources.MakeEventPolicyForBackingChannel(b, triggerChan) | ||||||
|
||||||
if featureFlags.IsOIDCAuthentication() { | ||||||
// get the eventpolicy , create if not exists. | ||||||
foundEP, err := r.eventPolicyLister.EventPolicies(expected.Namespace).Get(expected.Name) | ||||||
if apierrs.IsNotFound(err) { | ||||||
// create the EventPolicy if it doesn't exists. | ||||||
logging.FromContext(ctx).Info("Creating EventPolicy for Broker %s", expected.Name) | ||||||
|
||||||
_, err = r.eventingClientSet.EventingV1alpha1().EventPolicies(expected.Namespace).Create(ctx, expected, metav1.CreateOptions{}) | ||||||
if err != nil { | ||||||
return fmt.Errorf("failed to create EventPolicy for Broker %s: %w", expected.Name, err) | ||||||
} | ||||||
} else if err != nil { | ||||||
return fmt.Errorf("failed to get EventPolicy for Broker %s: %w", expected.Name, err) | ||||||
} else if r.policyNeedsUpdate(foundEP, expected) { | ||||||
// update the EventPolicy if it exists and needs update. | ||||||
logging.FromContext(ctx).Info("Updating EventPolicy for Broker %s", expected.Name) | ||||||
expected.SetResourceVersion(foundEP.GetResourceVersion()) | ||||||
_, err = r.eventingClientSet.EventingV1alpha1().EventPolicies(expected.Namespace).Update(ctx, expected, metav1.UpdateOptions{}) | ||||||
if err != nil { | ||||||
return fmt.Errorf("failed to update EventPolicy for Broker %s: %w", expected.Name, err) | ||||||
} | ||||||
} | ||||||
} else { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all of these if/else blocks can make it a little unclear what is going on, I might refactor this to return early in many of these cases. For example, if the EventPolicy is successfully created, we can return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point, thanks for the feedback, I will make the changes! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, PTAL! |
||||||
// list all the orphaned eventpolicies that have owner reference set to the broker and delete them. | ||||||
eventPolicies, err := r.eventPolicyLister.EventPolicies(expected.Namespace).List(labels.Everything()) | ||||||
if err != nil { | ||||||
return fmt.Errorf("failed to list EventPolicies for Broker %s: %w", expected.Name, err) | ||||||
} | ||||||
for _, ep := range eventPolicies { | ||||||
if metav1.IsControlledBy(ep, b) { | ||||||
logging.FromContext(ctx).Info("Deleting EventPolicy for Broker %s", expected.Name) | ||||||
err := r.eventingClientSet.EventingV1alpha1().EventPolicies(ep.Namespace).Delete(ctx, ep.Name, metav1.DeleteOptions{}) | ||||||
if err != nil { | ||||||
return fmt.Errorf("failed to delete EventPolicy for Broker %s: %w", expected.Name, err) | ||||||
} | ||||||
logging.FromContext(ctx).Info("Deleted EventPolicy for Broker %s", expected.Name) | ||||||
} | ||||||
} | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
func (r *Reconciler) policyNeedsUpdate(foundEP, expected *eventingv1alpha1.EventPolicy) bool { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this function doesn't really need to be a method on the Reconciler struct
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, PTAL! |
||||||
return !equality.Semantic.DeepDerivative(expected, foundEP) | ||||||
} | ||||||
|
||||||
// TriggerChannelLabels are all the labels placed on the Trigger Channel for the given brokerName. This | ||||||
// should only be used by Broker and Trigger code. | ||||||
func TriggerChannelLabels(brokerName, brokerNamespace string) map[string]string { | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,79 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Copyright 2024 The Knative Authors | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
You may obtain a copy of the License at | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||||||||||||||||||||||||||
distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||||||||||||||||||||||||||
limitations under the License. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
package resources | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
eventingduckv1 "knative.dev/eventing/pkg/apis/duck/v1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
const ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
BackingChannelEventPolicyLabelPrefix = "eventing.knative.dev/" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
OIDCBrokerSub = "system:serviceaccount:knative-eventing:mt-broker-ingress-oidc" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
brokerAPIVersion = "eventing.knative.dev/v1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
version = "v1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
brokerKind = "Broker" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
brokerGroup = "eventing.knative.dev" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
func MakeEventPolicyForBackingChannel(b *eventingv1.Broker, backingChannel *eventingduckv1.Channelable) *eventingv1alpha1.EventPolicy { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return &eventingv1alpha1.EventPolicy{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
ObjectMeta: metav1.ObjectMeta{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Namespace: backingChannel.Namespace, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Name: b.Name + "-event-policy", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a bit more complexity here than just adding the suffix to the name of the broker, since we might run into scenarios where the resulting name exceeds the allowed name length in k8s. Instead, we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, PTAL! |
||||||||||||||||||||||||||||||||||||||||||||||||||||
OwnerReferences: []metav1.OwnerReference{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
APIVersion: brokerAPIVersion, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Kind: brokerKind, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Name: b.Name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Labels: LabelsForBackingChannelsEventPolicy(b), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Spec: eventingv1alpha1.EventPolicySpec{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
To: []eventingv1alpha1.EventPolicySpecTo{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Ref: &eventingv1alpha1.EventPolicyToReference{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
APIVersion: backingChannel.APIVersion, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Kind: backingChannel.Kind, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Name: backingChannel.Name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
From: []eventingv1alpha1.EventPolicySpecFrom{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Sub: toStrPtr(OIDCBrokerSub), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using our own implementation here, we normally use the k8s There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, PTAL! |
||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
func LabelsForBackingChannelsEventPolicy(broker *eventingv1.Broker) map[string]string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return map[string]string{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
BackingChannelEventPolicyLabelPrefix + "broker-group": brokerGroup, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
BackingChannelEventPolicyLabelPrefix + "broker-version": version, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
BackingChannelEventPolicyLabelPrefix + "broker-kind": brokerKind, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
BackingChannelEventPolicyLabelPrefix + "broker-name": broker.Name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious if we actually need all of these labels for the EventPolicies, what exactly are you using them all for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Cali0707 I took reference from channel - https://github.com/knative/eventing/blob/main/pkg/reconciler/channel/resources/eventpolicy.go#L69 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah looking there it seems like those labels are used to correctly select the EventPolicies which were created by the channel. We should also use the labels to make a label selector when checking which EventPolicies are owned by the broker. See here for how it is used in the channel: eventing/pkg/reconciler/channel/channel.go Lines 142 to 166 in bb2e0a3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gotcha. So just to clarify: currently we are filtering EventPolicies based on the Owner References of broker, instead filter out based on the labels, right ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, filtering the eventpolicies by ownerreference is good, but when we list the eventpolicies, we should use a label selector with these labels (see line 152 in the code I linked above). So, to summarize what we would do is:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, PTAL! |
||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
func toStrPtr(s string) *string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return &s | ||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Copyright 2024 The Knative Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package resources | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
eventingduckv1 "knative.dev/eventing/pkg/apis/duck/v1" | ||
eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1" | ||
eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1" | ||
) | ||
|
||
func TestMakeEventPolicyForBackingChannel(t *testing.T) { | ||
broker := &eventingv1.Broker{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-broker", | ||
Namespace: "test-namespace", | ||
}, | ||
} | ||
backingChannel := &eventingduckv1.Channelable{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-channel", | ||
Namespace: "test-namespace", | ||
}, | ||
} | ||
want := &eventingv1alpha1.EventPolicy{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "test-namespace", | ||
Name: "test-broker-event-policy", | ||
OwnerReferences: []metav1.OwnerReference{ | ||
{ | ||
APIVersion: "eventing.knative.dev/v1", | ||
Kind: "Broker", | ||
Name: "test-broker", | ||
}, | ||
}, | ||
Labels: LabelsForBackingChannelsEventPolicy(broker), | ||
}, | ||
Spec: eventingv1alpha1.EventPolicySpec{ | ||
To: []eventingv1alpha1.EventPolicySpecTo{ | ||
{ | ||
Ref: &eventingv1alpha1.EventPolicyToReference{ | ||
APIVersion: backingChannel.APIVersion, | ||
Kind: backingChannel.Kind, | ||
Name: backingChannel.Name, | ||
}, | ||
}, | ||
}, | ||
From: []eventingv1alpha1.EventPolicySpecFrom{ | ||
{ | ||
Sub: toStrPtr(OIDCBrokerSub), | ||
}, | ||
}, | ||
}, | ||
} | ||
got := MakeEventPolicyForBackingChannel(broker, backingChannel) | ||
if diff := cmp.Diff(want, got); diff != "" { | ||
t.Errorf("MakeEventPolicyForBackingChannel() (-want, +got) = %v", diff) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm adding a comment here, but it applies to all of the logging in this function:
I don't think these need to be logged at the
Info
level, it generally seems to beDebug
logs. Additionally, if we are going to be using the logger everywhere, it seems to make sense to create the logger once at the start of the function and then re-use it throughout, instead of fetching it from the context every time we want to log somethingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, will change from Info -> Debug.
On the other point, I was trying to be consistent with the rest of the code base!
So, should I keep it as it is for change to create a single logger object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would create a single logger object for this function if you are going to have this many log statements. In other places, since there isn't a ton of logging it might be fine to just fetch the logger everytime. But, normally we fetch the logger once when we have this much logging to do in a function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, PTAL!