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
2 changes: 2 additions & 0 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"knative.dev/eventing/pkg/apis/sinks"
"knative.dev/eventing/pkg/auth"
"knative.dev/eventing/pkg/eventingtls"
"knative.dev/eventing/pkg/reconciler/eventpolicy"
"knative.dev/eventing/pkg/reconciler/jobsink"

"knative.dev/eventing/pkg/reconciler/apiserversource"
Expand Down Expand Up @@ -93,6 +94,7 @@ func main() {

// Eventing
eventtype.NewController,
eventpolicy.NewController,

// Flows
parallel.NewController,
Expand Down
49 changes: 49 additions & 0 deletions pkg/reconciler/eventpolicy/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
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 eventpolicy

import (
"context"

eventpolicyinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1alpha1/eventpolicy"
eventpolicyreconciler "knative.dev/eventing/pkg/client/injection/reconciler/eventing/v1alpha1/eventpolicy"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
"knative.dev/pkg/resolver"
)

// NewController initializes the controller and is called by the generated code
// Registers event handlers to enqueue events
func NewController(
ctx context.Context,
cmw configmap.Watcher,
) *controller.Impl {
// Access informers
eventPolicyInformer := eventpolicyinformer.Get(ctx)

r := &Reconciler{
eventPolicyLister: eventPolicyInformer.Lister(),
}
impl := eventpolicyreconciler.NewImpl(ctx, r)

r.fromRefResolver = resolver.NewAuthenticatableResolverFromTracker(ctx, impl.Tracker)

// Set up event handlers
eventPolicyInformer.Informer().AddEventHandler(controller.HandleAll(impl.Enqueue))

return impl
}
39 changes: 39 additions & 0 deletions pkg/reconciler/eventpolicy/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
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

Check failure on line 8 in pkg/reconciler/eventpolicy/controller_test.go

View workflow job for this annotation

GitHub Actions / style / Golang / Boilerplate Check (go)

[Go headers] reported by reviewdog 🐶 found mismatched boilerplate lines: Raw Output: pkg/reconciler/eventpolicy/controller_test.go:8: found mismatched boilerplate lines: {[]string}[0]: -: "\thttp://www.apache.org/licenses/LICENSE-2.0" +: " 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 eventpolicy

import (
"testing"

"knative.dev/pkg/configmap"

. "knative.dev/pkg/reconciler/testing"

// Fake injection informers
_ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1alpha1/eventpolicy/fake"
_ "knative.dev/pkg/client/injection/ducks/duck/v1/authstatus/fake"
)

func TestNew(t *testing.T) {
ctx, _ := SetupFakeContext(t)

c := NewController(ctx, configmap.NewStaticWatcher())

if c == nil {
t.Fatal("Expected NewController to return a non-nil value")
}
}
54 changes: 54 additions & 0 deletions pkg/reconciler/eventpolicy/eventpolicy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
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 eventpolicy

import (
"context"

"go.uber.org/zap"
"knative.dev/eventing/pkg/apis/eventing/v1alpha1"
"knative.dev/eventing/pkg/auth"
eventinglisters "knative.dev/eventing/pkg/client/listers/eventing/v1alpha1"
"knative.dev/pkg/logging"
pkgreconciler "knative.dev/pkg/reconciler"
"knative.dev/pkg/resolver"
)

type Reconciler struct {
eventPolicyLister eventinglisters.EventPolicyLister
dharmjit marked this conversation as resolved.
Show resolved Hide resolved
fromRefResolver *resolver.AuthenticatableResolver
}

// ReconcileKind implements Interface.ReconcileKind.
// 1. Verify the Reference exists.
func (r *Reconciler) ReconcileKind(ctx context.Context, ep *v1alpha1.EventPolicy) pkgreconciler.Event {
logger := logging.FromContext(ctx)
logger.Infow("Reconciling", zap.Any("EventPolicy", ep))
dharmjit marked this conversation as resolved.
Show resolved Hide resolved
// We reconcile the status of the EventPolicy by looking at:
// 1. All from[].refs have subjects
serverAccts, err := auth.ResolveSubjects(r.fromRefResolver, ep)
dharmjit marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
logger.Errorw("Error resolving from[].refs", zap.Error(err))
ep.GetConditionSet().Manage(ep.GetStatus()).MarkFalse(v1alpha1.EventPolicyConditionReady, "Error resolving from[].refs", "")
dharmjit marked this conversation as resolved.
Show resolved Hide resolved
} else {
dharmjit marked this conversation as resolved.
Show resolved Hide resolved
logger.Debug("All from[].refs resolved", zap.Error(err))
ep.GetConditionSet().Manage(ep.GetStatus()).MarkTrue(v1alpha1.EventPolicyConditionReady)
}
dharmjit marked this conversation as resolved.
Show resolved Hide resolved
ep.Status.From = serverAccts
logger.Debugw("Reconciled EventPolicy", zap.Any("EventPolicy", ep))
dharmjit marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
155 changes: 155 additions & 0 deletions pkg/reconciler/eventpolicy/eventpolicy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
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

Check failure on line 8 in pkg/reconciler/eventpolicy/eventpolicy_test.go

View workflow job for this annotation

GitHub Actions / style / Golang / Boilerplate Check (go)

[Go headers] reported by reviewdog 🐶 found mismatched boilerplate lines: Raw Output: pkg/reconciler/eventpolicy/eventpolicy_test.go:8: found mismatched boilerplate lines: {[]string}[0]: -: "\thttp://www.apache.org/licenses/LICENSE-2.0" +: " http://www.apache.org/licenses/LICENSE-2.0"
dharmjit marked this conversation as resolved.
Show resolved Hide resolved

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 eventpolicy

import (
"context"
"fmt"
"testing"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clientgotesting "k8s.io/client-go/testing"
sourcesv1 "knative.dev/eventing/pkg/apis/sources/v1"
fakeeventingclient "knative.dev/eventing/pkg/client/injection/client/fake"
"knative.dev/eventing/pkg/client/injection/reconciler/eventing/v1alpha1/eventpolicy"
. "knative.dev/eventing/pkg/reconciler/testing/v1"
duckv1authstatus "knative.dev/pkg/client/injection/ducks/duck/v1/authstatus"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
logtesting "knative.dev/pkg/logging/testing"
. "knative.dev/pkg/reconciler/testing"
"knative.dev/pkg/resolver"
"knative.dev/pkg/tracker"
)

const (
testNS = "test-namespace"
eventPolicyName = "test-eventpolicy"
pingSourceName = "test-pingsource"
apiServerSourceName = "test-apiserversource"
serviceAccountname = "test-sa"
)

var (
pingSourceWithServiceAccount = NewPingSource(pingSourceName, testNS, WithPingSourceOIDCServiceAccountName(serviceAccountname))
apiServerSourceWithServiceAccount = NewApiServerSource(apiServerSourceName, testNS, WithApiServerSourceOIDCServiceAccountName((serviceAccountname)))
)

func TestReconcile(t *testing.T) {
table := TableTest{
{
Name: "bad workqueue key",
// Make sure Reconcile handles bad keys.
Key: "too/many/parts",
},
{
Name: "subject not found, status set to NotReady",
Key: testNS + "/" + eventPolicyName,
Objects: []runtime.Object{
NewEventPolicy(eventPolicyName, testNS,
WithInitEventPolicyConditions,
WithEventPolicyFrom(v1.GroupVersionKind(sourcesv1.SchemeGroupVersion.WithKind("PingSource")), pingSourceName, testNS),
),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{
{
Object: NewEventPolicy(eventPolicyName, testNS,
creydr marked this conversation as resolved.
Show resolved Hide resolved
WithEventPolicyFrom(v1.GroupVersionKind(sourcesv1.SchemeGroupVersion.WithKind("PingSource")), pingSourceName, testNS),
WithUnreadyEventPolicyCondition),
},
},
WantErr: false,
},
{
Name: "subject found for pingsource, status set to Ready",
Key: testNS + "/" + eventPolicyName,
Objects: []runtime.Object{
pingSourceWithServiceAccount,
creydr marked this conversation as resolved.
Show resolved Hide resolved
NewEventPolicy(eventPolicyName, testNS,
WithInitEventPolicyConditions,
WithEventPolicyFrom(v1.GroupVersionKind(sourcesv1.SchemeGroupVersion.WithKind("PingSource")), pingSourceName, testNS)),
dharmjit marked this conversation as resolved.
Show resolved Hide resolved
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{
{
Object: NewEventPolicy(eventPolicyName, testNS,
creydr marked this conversation as resolved.
Show resolved Hide resolved
WithInitEventPolicyConditions,
WithEventPolicyFrom(v1.GroupVersionKind(sourcesv1.SchemeGroupVersion.WithKind("PingSource")), pingSourceName, testNS),
WithEventPolicyStatusFromSub([]string{fmt.Sprintf("system:serviceaccount:%s:%s", testNS, serviceAccountname)}),
WithReadyEventPolicyCondition),
},
},
WantErr: false,
},
{
Name: "subject found for apiserversource, status set to Ready",
Key: testNS + "/" + eventPolicyName,
Objects: []runtime.Object{
apiServerSourceWithServiceAccount,
NewEventPolicy(eventPolicyName, testNS,
WithInitEventPolicyConditions, WithEventPolicyFrom(v1.GroupVersionKind(sourcesv1.SchemeGroupVersion.WithKind("APIServerSource")), apiServerSourceName, testNS)),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{
{
Object: NewEventPolicy(eventPolicyName, testNS,
creydr marked this conversation as resolved.
Show resolved Hide resolved
WithEventPolicyFrom(v1.GroupVersionKind(sourcesv1.SchemeGroupVersion.WithKind("APIServerSource")), apiServerSourceName, testNS),
WithEventPolicyStatusFromSub([]string{fmt.Sprintf("system:serviceaccount:%s:%s", testNS, serviceAccountname)}),
WithReadyEventPolicyCondition),
},
},
WantErr: false,
},
{
Name: "Multiple subjects found, status set to Ready",
Key: testNS + "/" + eventPolicyName,
Objects: []runtime.Object{
apiServerSourceWithServiceAccount,
pingSourceWithServiceAccount,
NewEventPolicy(eventPolicyName, testNS,
WithInitEventPolicyConditions,
WithEventPolicyFrom(v1.GroupVersionKind(sourcesv1.SchemeGroupVersion.WithKind("PingSource")), pingSourceName, testNS),
WithEventPolicyFrom(v1.GroupVersionKind(sourcesv1.SchemeGroupVersion.WithKind("APIServerSource")), apiServerSourceName, testNS)),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{
{
Object: NewEventPolicy(eventPolicyName, testNS,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Object: NewEventPolicy(eventPolicyName, testNS,
NewEventPolicy(eventPolicyName, testNS,

WithEventPolicyFrom(v1.GroupVersionKind(sourcesv1.SchemeGroupVersion.WithKind("PingSource")), pingSourceName, testNS),
WithEventPolicyFrom(v1.GroupVersionKind(sourcesv1.SchemeGroupVersion.WithKind("APIServerSource")), apiServerSourceName, testNS),
WithEventPolicyStatusFromSub([]string{
fmt.Sprintf("system:serviceaccount:%s:%s", testNS, serviceAccountname),
fmt.Sprintf("system:serviceaccount:%s:%s", testNS, serviceAccountname),
}),
WithReadyEventPolicyCondition),
},
},
WantErr: false,
},
}
logger := logtesting.TestLogger(t)
table.Test(t, MakeFactory(func(ctx context.Context, listers *Listers, cmw configmap.Watcher) controller.Reconciler {
ctx = duckv1authstatus.WithDuck(ctx)
r := &Reconciler{
fromRefResolver: resolver.NewAuthenticatableResolverFromTracker(ctx, tracker.New(func(types.NamespacedName) {}, 0))}
return eventpolicy.NewReconciler(ctx, logger,
fakeeventingclient.Get(ctx), listers.GetEventPolicyLister(),
controller.GetEventRecorder(ctx), r)
},
false,
logger,
))
}
7 changes: 7 additions & 0 deletions pkg/reconciler/testing/v1/eventpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func WithUnreadyEventPolicyCondition(ep *v1alpha1.EventPolicy) {
{
Type: v1alpha1.EventPolicyConditionReady,
Status: corev1.ConditionFalse,
Reason: "Error resolving from[].refs",
},
}
}
Expand Down Expand Up @@ -102,3 +103,9 @@ func WithEventPolicyOwnerReferences(ownerRefs ...metav1.OwnerReference) EventPol
ep.ObjectMeta.OwnerReferences = append(ep.ObjectMeta.OwnerReferences, ownerRefs...)
}
}

func WithEventPolicyStatusFromSub(subs []string) EventPolicyOption {
return func(ep *v1alpha1.EventPolicy) {
ep.Status.From = append(ep.Status.From, subs...)
}
}
8 changes: 4 additions & 4 deletions pkg/reconciler/testing/v1/listers.go
creydr marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ func (l *Listers) GetAllObjects() []runtime.Object {
return all
}

func (l *Listers) GetEventTypeLister() eventingv1beta2listers.EventTypeLister {
return eventingv1beta2listers.NewEventTypeLister(l.indexerFor(&eventingv1beta2.EventType{}))
}

func (l *Listers) GetEventPolicyLister() eventingv1alpha1listers.EventPolicyLister {
return eventingv1alpha1listers.NewEventPolicyLister(l.indexerFor(&eventingv1alpha1.EventPolicy{}))
}

func (l *Listers) GetEventTypeLister() eventingv1beta2listers.EventTypeLister {
return eventingv1beta2listers.NewEventTypeLister(l.indexerFor(&eventingv1beta2.EventType{}))
}

func (l *Listers) GetPingSourceLister() sourcelisters.PingSourceLister {
return sourcelisters.NewPingSourceLister(l.indexerFor(&sourcesv1.PingSource{}))
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,7 @@ knative.dev/pkg/client/injection/apiextensions/reconciler/apiextensions/v1/custo
knative.dev/pkg/client/injection/ducks/duck/v1/addressable
knative.dev/pkg/client/injection/ducks/duck/v1/addressable/fake
knative.dev/pkg/client/injection/ducks/duck/v1/authstatus
knative.dev/pkg/client/injection/ducks/duck/v1/authstatus/fake
knative.dev/pkg/client/injection/ducks/duck/v1/conditions
knative.dev/pkg/client/injection/ducks/duck/v1/conditions/fake
knative.dev/pkg/client/injection/ducks/duck/v1/kresource
Expand Down
Loading