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

Add EventPolicy Reconciler #8024

Merged
merged 6 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pkg/apis/eventing/v1alpha1/eventpolicy_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ func (ep *EventPolicyStatus) MarkOIDCAuthenticationEnabled() {
eventPolicyCondSet.Manage(ep).MarkTrue(EventPolicyConditionAuthenticationEnabled)
}

// MarkOIDCAuthenticationNotEnabled sets EventPolicyConditionAuthenticationEnabled condition to false.
func (ep *EventPolicyStatus) MarkOIDCAuthenticationNotEnabled(reason, messageFormat string, messageA ...interface{}) {
// MarkOIDCAuthenticationDisabled sets EventPolicyConditionAuthenticationEnabled condition to false.
func (ep *EventPolicyStatus) MarkOIDCAuthenticationDisabled(reason, messageFormat string, messageA ...interface{}) {
eventPolicyCondSet.Manage(ep).MarkFalse(EventPolicyConditionAuthenticationEnabled, reason, messageFormat, messageA...)
}

// MarkSubjectsResolved sets EventPolicyConditionAuthenticationEnabled condition to true.
func (ep *EventPolicyStatus) MarkSubjectsResolved() {
// MarkSubjectsResolved sets EventPolicyConditionSubjectsResolved condition to true.
func (ep *EventPolicyStatus) MarkSubjectsResolvedSucceeded() {
eventPolicyCondSet.Manage(ep).MarkTrue(EventPolicyConditionSubjectsResolved)
}

// MarkSubjectsNotResolved sets EventPolicyConditionAuthenticationEnabled condition to false.
func (ep *EventPolicyStatus) MarkSubjectsNotResolved(reason, messageFormat string, messageA ...interface{}) {
// MarkSubjectsNotResolved sets EventPolicyConditionSubjectsResolved condition to false.
func (ep *EventPolicyStatus) MarkSubjectsResolvedFailed(reason, messageFormat string, messageA ...interface{}) {
eventPolicyCondSet.Manage(ep).MarkFalse(EventPolicyConditionSubjectsResolved, reason, messageFormat, messageA...)
}
119 changes: 112 additions & 7 deletions pkg/apis/eventing/v1alpha1/eventpolicy_lifecycle_test.go
creydr marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func TestEventPolicyGetConditionSet(t *testing.T) {
func TestEventPolicyGetCondition(t *testing.T) {
tests := []struct {
name string
ets *EventPolicyStatus
eps *EventPolicyStatus
condQuery apis.ConditionType
want *apis.Condition
}{{
name: "single condition",
ets: &EventPolicyStatus{
eps: &EventPolicyStatus{
Status: duckv1.Status{
Conditions: []apis.Condition{
eventPolicyConditionReady,
Expand All @@ -67,7 +67,7 @@ func TestEventPolicyGetCondition(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := test.ets.GetCondition(test.condQuery)
got := test.eps.GetCondition(test.condQuery)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Error("unexpected condition (-want, +got) =", diff)
}
Expand All @@ -78,12 +78,12 @@ func TestEventPolicyGetCondition(t *testing.T) {
func TestEventPolicyInitializeConditions(t *testing.T) {
tests := []struct {
name string
ets *EventPolicyStatus
eps *EventPolicyStatus
want *EventPolicyStatus
}{
{
name: "empty",
ets: &EventPolicyStatus{},
eps: &EventPolicyStatus{},
want: &EventPolicyStatus{
Status: duckv1.Status{
Conditions: []apis.Condition{
Expand All @@ -107,10 +107,115 @@ func TestEventPolicyInitializeConditions(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.ets.InitializeConditions()
if diff := cmp.Diff(test.want, test.ets, ignoreAllButTypeAndStatus); diff != "" {
test.eps.InitializeConditions()
if diff := cmp.Diff(test.want, test.eps, ignoreAllButTypeAndStatus); diff != "" {
t.Error("unexpected conditions (-want, +got) =", diff)
}
})
}
}

func TestEventPolicyReadyCondition(t *testing.T) {
tests := []struct {
name string
eps *EventPolicyStatus
markOIDCAuthenticationEnabled bool
markSubjectsResolvedSucceeded bool
wantReady bool
}{
{
name: "authenticationenabled and subjectresolved marked to true for a ready status ep",
eps: &EventPolicyStatus{
Status: duckv1.Status{
Conditions: []apis.Condition{
{Type: EventPolicyConditionReady, Status: corev1.ConditionTrue},
{Type: EventPolicyConditionAuthenticationEnabled, Status: corev1.ConditionTrue},
{Type: EventPolicyConditionSubjectsResolved, Status: corev1.ConditionTrue},
},
},
},
creydr marked this conversation as resolved.
Show resolved Hide resolved
markOIDCAuthenticationEnabled: true,
markSubjectsResolvedSucceeded: true,
wantReady: true,
},
{
name: "authenticationenabled condition marked to false for a ready status ep",
creydr marked this conversation as resolved.
Show resolved Hide resolved
eps: &EventPolicyStatus{
Status: duckv1.Status{
Conditions: []apis.Condition{
{Type: EventPolicyConditionReady, Status: corev1.ConditionTrue},
{Type: EventPolicyConditionAuthenticationEnabled, Status: corev1.ConditionTrue},
{Type: EventPolicyConditionSubjectsResolved, Status: corev1.ConditionTrue},
},
},
},
markOIDCAuthenticationEnabled: false,
markSubjectsResolvedSucceeded: true,
wantReady: false,
},
{
name: "authenticationenabled condition marked to false for a ready status ep",
creydr marked this conversation as resolved.
Show resolved Hide resolved
eps: &EventPolicyStatus{
Status: duckv1.Status{
Conditions: []apis.Condition{
{Type: EventPolicyConditionReady, Status: corev1.ConditionTrue},
{Type: EventPolicyConditionAuthenticationEnabled, Status: corev1.ConditionTrue},
{Type: EventPolicyConditionSubjectsResolved, Status: corev1.ConditionTrue},
},
},
},
markOIDCAuthenticationEnabled: true,
markSubjectsResolvedSucceeded: false,
wantReady: false,
},
{
name: "authenticationenabled condition marked to true for a not ready status ep",
creydr marked this conversation as resolved.
Show resolved Hide resolved
eps: &EventPolicyStatus{
Status: duckv1.Status{
Conditions: []apis.Condition{
{Type: EventPolicyConditionReady, Status: corev1.ConditionFalse},
{Type: EventPolicyConditionAuthenticationEnabled, Status: corev1.ConditionFalse},
{Type: EventPolicyConditionSubjectsResolved, Status: corev1.ConditionTrue},
},
},
},
markOIDCAuthenticationEnabled: true,
markSubjectsResolvedSucceeded: true,
wantReady: true,
},
{
name: "subjectresolved condition marked to true for a not ready status ep",
eps: &EventPolicyStatus{
Status: duckv1.Status{
Conditions: []apis.Condition{
{Type: EventPolicyConditionReady, Status: corev1.ConditionFalse},
{Type: EventPolicyConditionAuthenticationEnabled, Status: corev1.ConditionTrue},
{Type: EventPolicyConditionSubjectsResolved, Status: corev1.ConditionFalse},
},
},
},
markOIDCAuthenticationEnabled: true,
markSubjectsResolvedSucceeded: true,
wantReady: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.markOIDCAuthenticationEnabled {
test.eps.MarkOIDCAuthenticationEnabled()
} else {
test.eps.MarkOIDCAuthenticationDisabled("OIDCAuthenticationDisabled", "")
}
if test.markSubjectsResolvedSucceeded {
test.eps.MarkSubjectsResolvedSucceeded()
} else {
test.eps.MarkSubjectsResolvedFailed("SubjectsNotResolved", "")
}
ep := EventPolicy{Status: *test.eps}
got := ep.GetConditionSet().Manage(test.eps).IsHappy()
if test.wantReady != got {
t.Errorf("unexpected readiness: want %v, got %v", test.wantReady, got)
}
})
}
}
6 changes: 3 additions & 3 deletions pkg/reconciler/eventpolicy/eventpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, ep *v1alpha1.EventPolicy
if featureFlags.IsOIDCAuthentication() {
ep.Status.MarkOIDCAuthenticationEnabled()
} else {
ep.Status.MarkOIDCAuthenticationNotEnabled("AuthOIDCFeatureNotEnabled", "")
ep.Status.MarkOIDCAuthenticationDisabled("OIDCAuthenticationDisabled", "")
return nil
creydr marked this conversation as resolved.
Show resolved Hide resolved
}
// We reconcile the status of the EventPolicy
// by looking at all .spec.from[].refs have subjects
// and accordingly set the eventpolicy status
subjects, err := auth.ResolveSubjects(r.authResolver, ep)
if err != nil {
ep.Status.MarkSubjectsNotResolved("SubjectsNotResolved", err.Error())
ep.Status.MarkSubjectsResolvedFailed("SubjectsNotResolved", err.Error())
return fmt.Errorf("failed to resolve .spec.from[].ref: %w", err)
}
ep.Status.MarkSubjectsResolved()
ep.Status.MarkSubjectsResolvedSucceeded()
ep.Status.From = subjects
return nil
}
32 changes: 16 additions & 16 deletions pkg/reconciler/eventpolicy/eventpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ func TestReconcile(t *testing.T) {
{
Object: NewEventPolicy(eventPolicyName, testNS,
creydr marked this conversation as resolved.
Show resolved Hide resolved
WithEventPolicyFrom(pingSourceGVK, pingSourceName, testNS),
WithFalseAuthenticationEnabledCondition,
WithUnreadyEventPolicyCondition("AuthOIDCFeatureNotEnabled", ""),
WithUnknownSubjectsResolvedCondition,
WithEventPolicyAuthenticationDisabledCondition,
WithUnreadyEventPolicyCondition("OIDCAuthenticationDisabled", ""),
WithEventPolicySubjectsResolvedUnknown,
),
},
},
Expand All @@ -106,9 +106,9 @@ func TestReconcile(t *testing.T) {
{
Object: NewEventPolicy(eventPolicyName, testNS,
creydr marked this conversation as resolved.
Show resolved Hide resolved
WithEventPolicyFrom(pingSourceGVK, pingSourceName, testNS),
WithTrueAuthenticationEnabledCondition,
WithEventPolicyAuthenticationEnabledCondition,
WithUnreadyEventPolicyCondition("SubjectsNotResolved", SubjectsNotResolvedErrorMessage),
WithFalseSubjectsResolvedCondition("SubjectsNotResolved", SubjectsNotResolvedErrorMessage),
WithEventPolicySubjectsResolvedFailed("SubjectsNotResolved", SubjectsNotResolvedErrorMessage),
),
},
},
Expand All @@ -132,9 +132,9 @@ func TestReconcile(t *testing.T) {
Object: NewEventPolicy(eventPolicyName, testNS,
creydr marked this conversation as resolved.
Show resolved Hide resolved
WithEventPolicyFrom(pingSourceGVK, pingSourceName, testNS),
WithEventPolicyStatusFromSub([]string{fmt.Sprintf("system:serviceaccount:%s:%s", testNS, serviceAccountname)}),
WithTrueAuthenticationEnabledCondition,
WithEventPolicyAuthenticationEnabledCondition,
WithReadyEventPolicyCondition,
WithTrueSubjectsResolvedCondition,
WithEventPolicySubjectsResolvedSucceeded,
),
},
},
Expand All @@ -156,9 +156,9 @@ func TestReconcile(t *testing.T) {
Object: NewEventPolicy(eventPolicyName, testNS,
creydr marked this conversation as resolved.
Show resolved Hide resolved
WithEventPolicyFrom(apiServerSourceGVK, apiServerSourceName, testNS),
WithEventPolicyStatusFromSub([]string{fmt.Sprintf("system:serviceaccount:%s:%s", testNS, serviceAccountname)}),
WithTrueAuthenticationEnabledCondition,
WithEventPolicyAuthenticationEnabledCondition,
WithReadyEventPolicyCondition,
WithTrueSubjectsResolvedCondition,
WithEventPolicySubjectsResolvedSucceeded,
),
},
},
Expand Down Expand Up @@ -187,9 +187,9 @@ func TestReconcile(t *testing.T) {
fmt.Sprintf("system:serviceaccount:%s:%s", testNS, serviceAccountname),
fmt.Sprintf("system:serviceaccount:%s:%s", testNS, serviceAccountname),
}),
WithTrueAuthenticationEnabledCondition,
WithEventPolicyAuthenticationEnabledCondition,
WithReadyEventPolicyCondition,
WithTrueSubjectsResolvedCondition,
WithEventPolicySubjectsResolvedSucceeded,
),
},
},
Expand All @@ -207,17 +207,17 @@ func TestReconcile(t *testing.T) {
pingSourceWithServiceAccount,
NewEventPolicy(eventPolicyName, testNS,
WithReadyEventPolicyCondition,
WithTrueAuthenticationEnabledCondition,
WithTrueSubjectsResolvedCondition,
WithEventPolicyAuthenticationEnabledCondition,
WithEventPolicySubjectsResolvedSucceeded,
WithEventPolicyFrom(pingSourceGVK, pingSourceName, testNS)),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{
{
Object: NewEventPolicy(eventPolicyName, testNS,
creydr marked this conversation as resolved.
Show resolved Hide resolved
WithEventPolicyFrom(pingSourceGVK, pingSourceName, testNS),
WithFalseAuthenticationEnabledCondition,
WithUnreadyEventPolicyCondition("AuthOIDCFeatureNotEnabled", ""),
WithTrueSubjectsResolvedCondition,
WithEventPolicyAuthenticationDisabledCondition,
WithUnreadyEventPolicyCondition("OIDCAuthenticationDisabled", ""),
WithEventPolicySubjectsResolvedSucceeded,
),
},
},
Expand Down
12 changes: 6 additions & 6 deletions pkg/reconciler/testing/v1/eventpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,32 @@ func WithInitEventPolicyConditions(ep *v1alpha1.EventPolicy) {
ep.Status.InitializeConditions()
}

func WithTrueAuthenticationEnabledCondition(ep *v1alpha1.EventPolicy) {
func WithEventPolicyAuthenticationEnabledCondition(ep *v1alpha1.EventPolicy) {
ep.Status.Conditions = append(ep.Status.Conditions,
apis.Condition{
Type: v1alpha1.EventPolicyConditionAuthenticationEnabled,
Status: corev1.ConditionTrue,
})
}

func WithFalseAuthenticationEnabledCondition(ep *v1alpha1.EventPolicy) {
func WithEventPolicyAuthenticationDisabledCondition(ep *v1alpha1.EventPolicy) {
ep.Status.Conditions = append(ep.Status.Conditions,
apis.Condition{
Type: v1alpha1.EventPolicyConditionAuthenticationEnabled,
Status: corev1.ConditionFalse,
Reason: "AuthOIDCFeatureNotEnabled",
Reason: "OIDCAuthenticationDisabled",
})
}

func WithTrueSubjectsResolvedCondition(ep *v1alpha1.EventPolicy) {
func WithEventPolicySubjectsResolvedSucceeded(ep *v1alpha1.EventPolicy) {
ep.Status.Conditions = append(ep.Status.Conditions,
apis.Condition{
Type: v1alpha1.EventPolicyConditionSubjectsResolved,
Status: corev1.ConditionTrue,
})
}

func WithFalseSubjectsResolvedCondition(reason, message string) EventPolicyOption {
func WithEventPolicySubjectsResolvedFailed(reason, message string) EventPolicyOption {
return func(ep *v1alpha1.EventPolicy) {
ep.Status.Conditions = append(ep.Status.Conditions,
apis.Condition{
Expand All @@ -85,7 +85,7 @@ func WithFalseSubjectsResolvedCondition(reason, message string) EventPolicyOptio
}
}

func WithUnknownSubjectsResolvedCondition(ep *v1alpha1.EventPolicy) {
func WithEventPolicySubjectsResolvedUnknown(ep *v1alpha1.EventPolicy) {
ep.Status.Conditions = append(ep.Status.Conditions,
apis.Condition{
Type: v1alpha1.EventPolicyConditionSubjectsResolved,
Expand Down