Skip to content

Commit

Permalink
add eventpolicy reconciler
Browse files Browse the repository at this point in the history
Signed-off-by: Dharmjit Singh <sdharmjit@vmware.com>
  • Loading branch information
Dharmjit Singh committed Jun 24, 2024
1 parent d4e647d commit 5297b9f
Show file tree
Hide file tree
Showing 9 changed files with 341 additions and 4 deletions.
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
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))
// We reconcile the status of the EventPolicy by looking at:
// 1. All from[].refs have subjects
serverAccts, err := auth.ResolveSubjects(r.fromRefResolver, ep)
if err != nil {
logger.Errorw("Error resolving from[].refs", zap.Error(err))
ep.GetConditionSet().Manage(ep.GetStatus()).MarkFalse(v1alpha1.EventPolicyConditionReady, "Error resolving from[].refs", "")
} else {
logger.Debug("All from[].refs resolved", zap.Error(err))
ep.GetConditionSet().Manage(ep.GetStatus()).MarkTrue(v1alpha1.EventPolicyConditionReady)
}
ep.Status.From = serverAccts
logger.Debugw("Reconciled EventPolicy", zap.Any("EventPolicy", ep))
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"
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,
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,
NewEventPolicy(eventPolicyName, testNS,
WithInitEventPolicyConditions,
WithEventPolicyFrom(v1.GroupVersionKind(sourcesv1.SchemeGroupVersion.WithKind("PingSource")), pingSourceName, testNS)),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{
{
Object: NewEventPolicy(eventPolicyName, testNS,
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,
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,
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
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

0 comments on commit 5297b9f

Please sign in to comment.