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

Update #2

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio

Here is an overview of all new **experimental** features:

- **General**: TODO ([#XXX](https://github.com/kedacore/keda/issues/XXX))
- **General**: Introduce Azure Event Grid as a new CloudEvent destination ([#3587](https://github.com/kedacore/keda/issues/3587))

### Improvements

Expand Down
10 changes: 10 additions & 0 deletions apis/eventing/v1alpha1/cloudeventsource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type CloudEventSourceSpec struct {

Destination Destination `json:"destination"`

// +optional
AuthenticationRef *v1alpha1.AuthenticationRef `json:"authenticationRef,omitempty"`

// +optional
EventSubscription EventSubscription `json:"eventSubscription,omitempty"`
}
Expand All @@ -67,12 +70,19 @@ type CloudEventSourceStatus struct {
type Destination struct {
// +optional
HTTP *CloudEventHTTP `json:"http"`

// +optional
AzureEventGridTopic *AzureEventGridTopicSpec `json:"azureEventGridTopic"`
}

type CloudEventHTTP struct {
URI string `json:"uri"`
}

type AzureEventGridTopicSpec struct {
Endpoint string `json:"endpoint"`
}

// EventSubscription defines filters for events
type EventSubscription struct {
// +optional
Expand Down
20 changes: 20 additions & 0 deletions apis/eventing/v1alpha1/zz_generated.deepcopy.go

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

2 changes: 1 addition & 1 deletion cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ func main() {

globalHTTPTimeout := time.Duration(globalHTTPTimeoutMS) * time.Millisecond
eventRecorder := mgr.GetEventRecorderFor("keda-operator")
eventEmitter := eventemitter.NewEventEmitter(mgr.GetClient(), eventRecorder, k8sClusterName)

kubeClientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
Expand All @@ -217,6 +216,7 @@ func main() {
}

scaledHandler := scaling.NewScaleHandler(mgr.GetClient(), scaleClient, mgr.GetScheme(), globalHTTPTimeout, eventRecorder, secretInformer.Lister())
eventEmitter := eventemitter.NewEventEmitter(mgr.GetClient(), eventRecorder, k8sClusterName, secretInformer.Lister())

if err = (&kedacontrollers.ScaledObjectReconciler{
Client: mgr.GetClient(),
Expand Down
21 changes: 21 additions & 0 deletions config/crd/bases/eventing.keda.sh_cloudeventsources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,32 @@ spec:
spec:
description: CloudEventSourceSpec defines the spec of CloudEventSource
properties:
authenticationRef:
description: |-
AuthenticationRef points to the TriggerAuthentication or ClusterTriggerAuthentication object that
is used to authenticate the scaler with the environment
properties:
kind:
description: Kind of the resource being referred to. Defaults
to TriggerAuthentication.
type: string
name:
type: string
required:
- name
type: object
clusterName:
type: string
destination:
description: Destination defines the various ways to emit events
properties:
azureEventGridTopic:
properties:
endpoint:
type: string
required:
- endpoint
type: object
http:
properties:
uri:
Expand Down
2 changes: 1 addition & 1 deletion controllers/keda/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var _ = BeforeSuite(func() {
Recorder: k8sManager.GetEventRecorderFor("keda-operator"),
ScaleHandler: scaling.NewScaleHandler(k8sManager.GetClient(), scaleClient, k8sManager.GetScheme(), time.Duration(10), k8sManager.GetEventRecorderFor("keda-operator"), nil),
ScaleClient: scaleClient,
EventEmitter: eventemitter.NewEventEmitter(k8sManager.GetClient(), k8sManager.GetEventRecorderFor("keda-operator"), "kubernetes-default"),
EventEmitter: eventemitter.NewEventEmitter(k8sManager.GetClient(), k8sManager.GetEventRecorderFor("keda-operator"), "kubernetes-default", nil),
}).SetupWithManager(k8sManager, controller.Options{})
Expect(err).ToNot(HaveOccurred())

Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/Azure/azure-event-hubs-go/v3 v3.6.2
github.com/Azure/azure-kusto-go v0.15.0
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.1
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.10.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.1
github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus v1.6.0
github.com/Azure/azure-storage-blob-go v0.15.0
Expand Down Expand Up @@ -159,7 +159,8 @@ require (
cloud.google.com/go/iam v1.1.5 // indirect
code.cloudfoundry.org/clock v1.1.0 // indirect
github.com/Azure/azure-pipeline-go v0.2.3 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2 // indirect
github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventgrid v0.4.0
github.com/Azure/go-amqp v1.0.4 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.23 // indirect
Expand Down Expand Up @@ -335,7 +336,7 @@ require (
golang.org/x/crypto v0.19.0
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -819,14 +819,16 @@ github.com/Azure/azure-pipeline-go v0.2.3/go.mod h1:x841ezTBIMG6O3lAcl8ATHnsOPVl
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible h1:fcYLmCpyNYRnvJbPerq7U0hS+6+I79yEDJBqVNcqUzU=
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.19.0/go.mod h1:h6H6c8enJmmocHUbLiiGY6sx7f9i+X3m1CHdd5c6Rdw=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.1 h1:lGlwhPtrX6EVml1hO0ivjkUxsSyl4dsiw9qcA1k/3IQ=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.1/go.mod h1:RKUqNu35KJYcVG/fqTRqmuXJZYNhYkBrnC/hX7yGbTA=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.10.0 h1:n1DH8TPV4qqPTje2RcUBYwtrTWlabVp4n46+74X2pn4=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.10.0/go.mod h1:HDcZnuGbiyppErN6lB+idp4CKhjbc8gwjto6OPpyggM=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.11.0/go.mod h1:HcM1YX14R7CJcghJGOYCgdezslRSVzqwLf/q+4Y2r/0=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.1 h1:sO0/P7g68FrryJzljemN+6GTssUXdANk6aJ7T1ZxnsQ=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.1/go.mod h1:h8hyGFDsU5HMivxiS2iYFZsgDbU9OnnJ163x5UGVKYo=
github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.0/go.mod h1:yqy467j36fJxcRV2TzfVZ1pCb5vxm4BtZPUdYWe/Xo8=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.1 h1:6oNBlSdi1QqM1PNW7FPA6xOGA5UNsXnkaYZz9vdPGhA=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.1/go.mod h1:s4kgfzA0covAXNicZHDMN58jExvcng2mC/DepXiF1EI=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2 h1:LqbJ/WzJUwBf8UiaSzgX7aMclParm9/5Vgp+TY51uBQ=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.2/go.mod h1:yInRyqWXAuaPrgI7p70+lDDgh3mlBohis29jGMISnmc=
github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventgrid v0.4.0 h1:d7S13DPk63SvBJfSUiMJJ26tRsvrBumkLPEfQEAarGk=
github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventgrid v0.4.0/go.mod h1:7e/gsXp4INB4k/vg0h3UOkYpDK6oZqctxr+L05FGybg=
github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus v1.6.0 h1:Fhg/LkAagiLv9Xpw6r2knr19tn9t1TiQoJu5bOMzflc=
github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus v1.6.0/go.mod h1:7xwz/6tTwO9zMKni8/EozIMi0DTexFSm7YNE9HdD3cQ=
github.com/Azure/azure-storage-blob-go v0.15.0 h1:rXtgp8tN1p29GvpGgfJetavIG0V7OgcSXPpwp3tx6qk=
Expand Down
128 changes: 128 additions & 0 deletions pkg/eventemitter/azure_event_grid_topic_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
Copyright 2024 The KEDA 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.
*/

// ******************************* DESCRIPTION ****************************** \\
// AzureEventGridHandler focuses on emitting the CloudEventSource to Azure Event Grid
// ************************************************************************** \\

package eventemitter

import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/messaging"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azeventgrid/publisher"
"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

eventingv1alpha1 "github.com/kedacore/keda/v2/apis/eventing/v1alpha1"
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
"github.com/kedacore/keda/v2/pkg/eventemitter/eventdata"
"github.com/kedacore/keda/v2/pkg/scalers/azure"
)

type AzureEventGridTopicHandler struct {
Context context.Context
Endpoint string
ClusterName string
Client *publisher.Client
logger logr.Logger
activeStatus metav1.ConditionStatus
}

func NewAzureEventGridTopicHandler(context context.Context, clusterName string, spec *eventingv1alpha1.AzureEventGridTopicSpec, authParams map[string]string, podIdentity kedav1alpha1.AuthPodIdentity, logger logr.Logger) (*AzureEventGridTopicHandler, error) {
var err error
var client *publisher.Client

switch podIdentity.Provider {
case "", kedav1alpha1.PodIdentityProviderNone:
if authParams["accessKey"] == "" {
err = fmt.Errorf("no azure event grid access key provided")
break
}
client, err = publisher.NewClientWithSharedKeyCredential(spec.Endpoint, azcore.NewKeyCredential(authParams["accessKey"]), nil)
case kedav1alpha1.PodIdentityProviderAzure, kedav1alpha1.PodIdentityProviderAzureWorkload:
creds, chainedErr := azure.NewChainedCredential(logger, podIdentity.GetIdentityID(), podIdentity.GetIdentityTenantID(), podIdentity.Provider)
if chainedErr != nil {
err = chainedErr
break
}
client, err = publisher.NewClient(spec.Endpoint, creds, nil)
default:
err = fmt.Errorf("incorrect auth provided")
}

if err != nil {
return nil, err
}

logger.Info("Create new azure event grid handler")
return &AzureEventGridTopicHandler{
Context: context,
Client: client,
Endpoint: spec.Endpoint,
ClusterName: clusterName,
logger: logger,
activeStatus: metav1.ConditionTrue,
}, nil
}

func (a *AzureEventGridTopicHandler) CloseHandler() {

}

func (a *AzureEventGridTopicHandler) SetActiveStatus(status metav1.ConditionStatus) {
a.activeStatus = status
}

func (a *AzureEventGridTopicHandler) GetActiveStatus() metav1.ConditionStatus {
return a.activeStatus
}

func (a *AzureEventGridTopicHandler) EmitEvent(eventData eventdata.EventData, failureFunc func(eventData eventdata.EventData, err error)) {
source := generateCloudEventSource(a.ClusterName)
subject := generateCloudEventSubjectFromEventData(a.ClusterName, eventData)

opt := &messaging.CloudEventOptions{
Subject: &subject,
DataContentType: to.Ptr("application/json"),
Time: &eventData.Time,
}

event, err := messaging.NewCloudEvent(source, string(eventData.CloudEventType), EmitData{Reason: eventData.Reason, Message: eventData.Message}, opt)

if err != nil {
a.logger.Error(err, "EmitEvent error %s")
return
}

eventsToSend := []messaging.CloudEvent{
event,
}

_, err = a.Client.PublishCloudEvents(a.Context, eventsToSend, &publisher.PublishCloudEventsOptions{})

if err != nil {
a.logger.Error(err, "Failed to Publish Event to Azure Event Grid ")
failureFunc(eventData, err)
return
}

a.logger.Info("Publish Event to Azure Event Grid Successfully")
}
9 changes: 2 additions & 7 deletions pkg/eventemitter/cloudevent_http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kedacore/keda/v2/pkg/eventemitter/eventdata"
"github.com/kedacore/keda/v2/pkg/util"
)

var (
kedaNamespace, _ = util.GetClusterObjectNamespace()
)

type CloudEventHTTPHandler struct {
Expand Down Expand Up @@ -87,8 +82,8 @@ func (c *CloudEventHTTPHandler) CloseHandler() {
}

func (c *CloudEventHTTPHandler) EmitEvent(eventData eventdata.EventData, failureFunc func(eventData eventdata.EventData, err error)) {
source := fmt.Sprintf("/%s/%s/keda", c.clusterName, kedaNamespace)
subject := fmt.Sprintf("/%s/%s/%s/%s", c.clusterName, eventData.Namespace, eventData.ObjectType, eventData.ObjectName)
source := generateCloudEventSource(c.clusterName)
subject := generateCloudEventSubjectFromEventData(c.clusterName, eventData)

event := cloudevents.NewEvent()
event.SetSource(source)
Expand Down
Loading
Loading