-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reconcile event policies for mt-broker (#8090)
* reconcile event policies for broker Signed-off-by: rahulii <r.sawra@gmail.com> * delete the orphaned event policies by owner references and changes as per review comments Signed-off-by: rahulii <r.sawra@gmail.com> * fix tests and move event policy creation to new file Signed-off-by: rahulii <r.sawra@gmail.com> * add test cases Signed-off-by: rahulii <r.sawra@gmail.com> * add test cases Signed-off-by: rahulii <r.sawra@gmail.com> * changes to review comments Signed-off-by: rahulii <r.sawra@gmail.com> * changes to review comments Signed-off-by: rahulii <r.sawra@gmail.com> * refactor the code to reduce redundant if else statements Signed-off-by: rahulii <r.sawra@gmail.com> * change logs from info to debug Signed-off-by: rahulii <r.sawra@gmail.com> * use a single logger object instead of creating it everytime from context and some minor fixes Signed-off-by: rahulii <r.sawra@gmail.com> --------- Signed-off-by: rahulii <r.sawra@gmail.com>
- Loading branch information
Showing
5 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
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" | ||
"k8s.io/utils/ptr" | ||
eventingduckv1 "knative.dev/eventing/pkg/apis/duck/v1" | ||
eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1" | ||
eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1" | ||
"knative.dev/pkg/kmeta" | ||
) | ||
|
||
const ( | ||
BackingChannelEventPolicyLabelPrefix = "eventing.knative.dev/" | ||
OIDCBrokerSub = "system:serviceaccount:knative-eventing:mt-broker-ingress-oidc" | ||
brokerKind = "Broker" | ||
) | ||
|
||
func MakeEventPolicyForBackingChannel(b *eventingv1.Broker, backingChannel *eventingduckv1.Channelable) *eventingv1alpha1.EventPolicy { | ||
return &eventingv1alpha1.EventPolicy{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: backingChannel.Namespace, | ||
Name: BrokerEventPolicyName(b.Name, backingChannel.Name), | ||
OwnerReferences: []metav1.OwnerReference{ | ||
{ | ||
APIVersion: eventingv1.SchemeGroupVersion.String(), | ||
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: ptr.To(OIDCBrokerSub), | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func LabelsForBackingChannelsEventPolicy(broker *eventingv1.Broker) map[string]string { | ||
return map[string]string{ | ||
BackingChannelEventPolicyLabelPrefix + "broker-group": eventingv1.SchemeGroupVersion.Group, | ||
BackingChannelEventPolicyLabelPrefix + "broker-version": eventingv1.SchemeGroupVersion.Version, | ||
BackingChannelEventPolicyLabelPrefix + "broker-kind": brokerKind, | ||
BackingChannelEventPolicyLabelPrefix + "broker-name": broker.Name, | ||
} | ||
} | ||
|
||
func BrokerEventPolicyName(brokerName, channelName string) string { | ||
return kmeta.ChildName(brokerName, "-ep-"+channelName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
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" | ||
"k8s.io/utils/ptr" | ||
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: BrokerEventPolicyName(broker.Name, backingChannel.Name), | ||
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: ptr.To(OIDCBrokerSub), | ||
}, | ||
}, | ||
}, | ||
} | ||
got := MakeEventPolicyForBackingChannel(broker, backingChannel) | ||
if diff := cmp.Diff(want, got); diff != "" { | ||
t.Errorf("MakeEventPolicyForBackingChannel() (-want, +got) = %v", diff) | ||
} | ||
} |
Oops, something went wrong.