Skip to content

Commit

Permalink
Provide ClusterCloudEventSource around the management of TriggerAuthe…
Browse files Browse the repository at this point in the history
…ntication/ClusterTriggerAuthentication resources (#6131)
  • Loading branch information
SpiritZhou authored Sep 5, 2024
1 parent 9e51a78 commit e843e2f
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 36 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio
### New

- **CloudEventSource**: Introduce ClusterCloudEventSource ([#3533](https://github.com/kedacore/keda/issues/3533))
- **CloudEventSource**: Provide CloudEvents around the management of ScaledJobs resources ([#3523](https://github.com/kedacore/keda/issues/3523))
- **CloudEventSource**: Provide ClusterCloudEventSource around the management of ScaledJobs resources ([#3523](https://github.com/kedacore/keda/issues/3523))
- **CloudEventSource**: Provide ClusterCloudEventSource around the management of TriggerAuthentication/ClusterTriggerAuthentication resources ([#3524](https://github.com/kedacore/keda/issues/3524))

#### Experimental

Expand Down
21 changes: 20 additions & 1 deletion apis/eventing/v1alpha1/cloudevent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ limitations under the License.
package v1alpha1

// CloudEventType contains the list of cloudevent types
// +kubebuilder:validation:Enum=keda.scaledobject.ready.v1;keda.scaledobject.failed.v1;keda.scaledobject.removed.v1;keda.scaledjob.ready.v1;keda.scaledjob.failed.v1;keda.scaledjob.removed.v1
// +kubebuilder:validation:Enum=keda.scaledobject.ready.v1;keda.scaledobject.failed.v1;keda.scaledobject.removed.v1;keda.scaledjob.ready.v1;keda.scaledjob.failed.v1;keda.scaledjob.removed.v1;keda.authentication.triggerauthentication.created.v1;keda.authentication.triggerauthentication.updated.v1;keda.authentication.triggerauthentication.removed.v1;keda.authentication.clustertriggerauthentication.created.v1;keda.authentication.clustertriggerauthentication.updated.v1;keda.authentication.clustertriggerauthentication.removed.v1

type CloudEventType string

const (
Expand All @@ -38,6 +39,24 @@ const (

// ScaledJobRemovedType is for event when removed ScaledJob
ScaledJobRemovedType CloudEventType = "keda.scaledjob.removed.v1"

// TriggerAuthenticationCreatedType is for event when a new TriggerAuthentication is created
TriggerAuthenticationCreatedType CloudEventType = "keda.authentication.triggerauthentication.created.v1"

// TriggerAuthenticationUpdatedType is for event when a TriggerAuthentication is updated
TriggerAuthenticationUpdatedType CloudEventType = "keda.authentication.triggerauthentication.updated.v1"

// TriggerAuthenticationRemovedType is for event when a TriggerAuthentication is deleted
TriggerAuthenticationRemovedType CloudEventType = "keda.authentication.triggerauthentication.removed.v1"

// ClusterTriggerAuthenticationCreatedType is for event when a new ClusterTriggerAuthentication is created
ClusterTriggerAuthenticationCreatedType CloudEventType = "keda.authentication.clustertriggerauthentication.created.v1"

// ClusterTriggerAuthenticationCreatedType is for event when a ClusterTriggerAuthentication is updated
ClusterTriggerAuthenticationUpdatedType CloudEventType = "keda.authentication.clustertriggerauthentication.updated.v1"

// ClusterTriggerAuthenticationRemovedType is for event when a ClusterTriggerAuthentication is deleted
ClusterTriggerAuthenticationRemovedType CloudEventType = "keda.authentication.clustertriggerauthentication.removed.v1"
)

var AllEventTypes = []CloudEventType{
Expand Down
8 changes: 4 additions & 4 deletions cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,15 @@ func main() {
os.Exit(1)
}
if err = (&kedacontrollers.TriggerAuthenticationReconciler{
Client: mgr.GetClient(),
EventRecorder: eventRecorder,
Client: mgr.GetClient(),
EventHandler: eventEmitter,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "TriggerAuthentication")
os.Exit(1)
}
if err = (&kedacontrollers.ClusterTriggerAuthenticationReconciler{
Client: mgr.GetClient(),
EventRecorder: eventRecorder,
Client: mgr.GetClient(),
EventHandler: eventEmitter,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ClusterTriggerAuthentication")
os.Exit(1)
Expand Down
16 changes: 12 additions & 4 deletions config/crd/bases/eventing.keda.sh_cloudeventsources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,36 @@ spec:
properties:
excludedEventTypes:
items:
description: CloudEventType contains the list of cloudevent
types
enum:
- keda.scaledobject.ready.v1
- keda.scaledobject.failed.v1
- keda.scaledobject.removed.v1
- keda.scaledjob.ready.v1
- keda.scaledjob.failed.v1
- keda.scaledjob.removed.v1
- keda.authentication.triggerauthentication.created.v1
- keda.authentication.triggerauthentication.updated.v1
- keda.authentication.triggerauthentication.removed.v1
- keda.authentication.clustertriggerauthentication.created.v1
- keda.authentication.clustertriggerauthentication.updated.v1
- keda.authentication.clustertriggerauthentication.removed.v1
type: string
type: array
includedEventTypes:
items:
description: CloudEventType contains the list of cloudevent
types
enum:
- keda.scaledobject.ready.v1
- keda.scaledobject.failed.v1
- keda.scaledobject.removed.v1
- keda.scaledjob.ready.v1
- keda.scaledjob.failed.v1
- keda.scaledjob.removed.v1
- keda.authentication.triggerauthentication.created.v1
- keda.authentication.triggerauthentication.updated.v1
- keda.authentication.triggerauthentication.removed.v1
- keda.authentication.clustertriggerauthentication.created.v1
- keda.authentication.clustertriggerauthentication.updated.v1
- keda.authentication.clustertriggerauthentication.removed.v1
type: string
type: array
type: object
Expand Down
16 changes: 12 additions & 4 deletions config/crd/bases/eventing.keda.sh_clustercloudeventsources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,36 @@ spec:
properties:
excludedEventTypes:
items:
description: CloudEventType contains the list of cloudevent
types
enum:
- keda.scaledobject.ready.v1
- keda.scaledobject.failed.v1
- keda.scaledobject.removed.v1
- keda.scaledjob.ready.v1
- keda.scaledjob.failed.v1
- keda.scaledjob.removed.v1
- keda.authentication.triggerauthentication.created.v1
- keda.authentication.triggerauthentication.updated.v1
- keda.authentication.triggerauthentication.removed.v1
- keda.authentication.clustertriggerauthentication.created.v1
- keda.authentication.clustertriggerauthentication.updated.v1
- keda.authentication.clustertriggerauthentication.removed.v1
type: string
type: array
includedEventTypes:
items:
description: CloudEventType contains the list of cloudevent
types
enum:
- keda.scaledobject.ready.v1
- keda.scaledobject.failed.v1
- keda.scaledobject.removed.v1
- keda.scaledjob.ready.v1
- keda.scaledjob.failed.v1
- keda.scaledjob.removed.v1
- keda.authentication.triggerauthentication.created.v1
- keda.authentication.triggerauthentication.updated.v1
- keda.authentication.triggerauthentication.removed.v1
- keda.authentication.clustertriggerauthentication.created.v1
- keda.authentication.clustertriggerauthentication.updated.v1
- keda.authentication.clustertriggerauthentication.removed.v1
type: string
type: array
type: object
Expand Down
13 changes: 10 additions & 3 deletions controllers/keda/clustertriggerauthentication_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,29 @@ package keda

import (
"context"
"fmt"
"sync"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"

eventingv1alpha1 "github.com/kedacore/keda/v2/apis/eventing/v1alpha1"
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
"github.com/kedacore/keda/v2/pkg/common/message"
"github.com/kedacore/keda/v2/pkg/eventemitter"
"github.com/kedacore/keda/v2/pkg/eventreason"
"github.com/kedacore/keda/v2/pkg/metricscollector"
)

// ClusterTriggerAuthenticationReconciler reconciles a ClusterTriggerAuthentication object
type ClusterTriggerAuthenticationReconciler struct {
client.Client
record.EventRecorder
eventemitter.EventHandler
}

type clusterTriggerAuthMetricsData struct {
Expand Down Expand Up @@ -80,8 +83,12 @@ func (r *ClusterTriggerAuthenticationReconciler) Reconcile(ctx context.Context,
r.updatePromMetrics(clusterTriggerAuthentication, req.NamespacedName.String())

if clusterTriggerAuthentication.ObjectMeta.Generation == 1 {
r.EventRecorder.Event(clusterTriggerAuthentication, corev1.EventTypeNormal, eventreason.ClusterTriggerAuthenticationAdded, "New ClusterTriggerAuthentication configured")
r.Emit(clusterTriggerAuthentication, req.NamespacedName.Namespace, corev1.EventTypeNormal, eventingv1alpha1.ClusterTriggerAuthenticationCreatedType, eventreason.ClusterTriggerAuthenticationAdded, message.ClusterTriggerAuthenticationCreatedMsg)
} else {
msg := fmt.Sprintf(message.ClusterTriggerAuthenticationUpdatedMsg, clusterTriggerAuthentication.Name)
r.Emit(clusterTriggerAuthentication, req.NamespacedName.Namespace, corev1.EventTypeNormal, eventingv1alpha1.ClusterTriggerAuthenticationUpdatedType, eventreason.ClusterTriggerAuthenticationUpdated, msg)
}

return ctrl.Result{}, nil
}

Expand Down
12 changes: 9 additions & 3 deletions controllers/keda/triggerauthentication_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ package keda

import (
"context"
"fmt"
"sync"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"

eventingv1alpha1 "github.com/kedacore/keda/v2/apis/eventing/v1alpha1"
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
"github.com/kedacore/keda/v2/pkg/common/message"
"github.com/kedacore/keda/v2/pkg/eventemitter"
"github.com/kedacore/keda/v2/pkg/eventreason"
"github.com/kedacore/keda/v2/pkg/metricscollector"
"github.com/kedacore/keda/v2/pkg/util"
Expand All @@ -38,7 +41,7 @@ import (
// TriggerAuthenticationReconciler reconciles a TriggerAuthentication object
type TriggerAuthenticationReconciler struct {
client.Client
record.EventRecorder
eventemitter.EventHandler
}

type triggerAuthMetricsData struct {
Expand Down Expand Up @@ -81,7 +84,10 @@ func (r *TriggerAuthenticationReconciler) Reconcile(ctx context.Context, req ctr
r.updatePromMetrics(triggerAuthentication, req.NamespacedName.String())

if triggerAuthentication.ObjectMeta.Generation == 1 {
r.EventRecorder.Event(triggerAuthentication, corev1.EventTypeNormal, eventreason.TriggerAuthenticationAdded, "New TriggerAuthentication configured")
r.Emit(triggerAuthentication, req.NamespacedName.Namespace, corev1.EventTypeNormal, eventingv1alpha1.TriggerAuthenticationCreatedType, eventreason.TriggerAuthenticationAdded, message.TriggerAuthenticationCreatedMsg)
} else {
msg := fmt.Sprintf(message.TriggerAuthenticationUpdatedMsg, triggerAuthentication.Name)
r.Emit(triggerAuthentication, req.NamespacedName.Namespace, corev1.EventTypeNormal, eventingv1alpha1.TriggerAuthenticationUpdatedType, eventreason.TriggerAuthenticationUpdated, msg)
}

return ctrl.Result{}, nil
Expand Down
10 changes: 7 additions & 3 deletions controllers/keda/util/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"

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"
"github.com/kedacore/keda/v2/pkg/eventreason"
)

Expand All @@ -19,7 +20,7 @@ const (

type authenticationReconciler interface {
client.Client
record.EventRecorder
eventemitter.EventHandler
UpdatePromMetricsOnDelete(string)
}

Expand Down Expand Up @@ -48,13 +49,16 @@ func EnsureAuthenticationResourceFinalizer(ctx context.Context, logger logr.Logg

func FinalizeAuthenticationResource(ctx context.Context, logger logr.Logger, reconciler authenticationReconciler, authResource client.Object, namespacedName string) error {
var authResourceType, reason string
var cloudEventType eventingv1alpha1.CloudEventType
switch authResource.(type) {
case *kedav1alpha1.TriggerAuthentication:
authResourceType = "TriggerAuthentication"
reason = eventreason.TriggerAuthenticationDeleted
cloudEventType = eventingv1alpha1.TriggerAuthenticationRemovedType
case *kedav1alpha1.ClusterTriggerAuthentication:
authResourceType = "ClusterTriggerAuthentication"
reason = eventreason.ClusterTriggerAuthenticationDeleted
cloudEventType = eventingv1alpha1.ClusterTriggerAuthenticationRemovedType
}

if Contains(authResource.GetFinalizers(), authenticationFinalizer) {
Expand All @@ -68,6 +72,6 @@ func FinalizeAuthenticationResource(ctx context.Context, logger logr.Logger, rec
}

logger.Info(fmt.Sprintf("Successfully finalized %s", authResourceType))
reconciler.Event(authResource, corev1.EventTypeNormal, reason, fmt.Sprintf("%s was deleted", authResourceType))
reconciler.Emit(authResource, namespacedName, corev1.EventTypeNormal, cloudEventType, reason, fmt.Sprintf("%s was deleted", authResourceType))
return nil
}
8 changes: 8 additions & 0 deletions pkg/common/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ const (
ScaledJobReadyMsg = "ScaledJob is ready for scaling"

ScaledJobRemoved = "ScaledJob was deleted"

TriggerAuthenticationCreatedMsg = "New TriggerAuthentication configured"

TriggerAuthenticationUpdatedMsg = "ClusterTriggerAuthentication %s is updated"

ClusterTriggerAuthenticationCreatedMsg = "New ClusterTriggerAuthentication configured"

ClusterTriggerAuthenticationUpdatedMsg = "ClusterTriggerAuthentication %s is updated"
)
6 changes: 6 additions & 0 deletions pkg/eventreason/eventreason.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ const (
// TriggerAuthenticationFailed is for event when a TriggerAuthentication occurs error
TriggerAuthenticationFailed = "TriggerAuthenticationFailed"

// TriggerAuthenticationUpdated is for event when a TriggerAuthentication is updated
TriggerAuthenticationUpdated = "ClusterTriggerAuthenticationUpdated"

// ClusterTriggerAuthenticationDeleted is for event when a ClusterTriggerAuthentication is deleted
ClusterTriggerAuthenticationDeleted = "ClusterTriggerAuthenticationDeleted"

Expand All @@ -85,4 +88,7 @@ const (

// ClusterTriggerAuthenticationFailed is for event when a ClusterTriggerAuthentication occurs error
ClusterTriggerAuthenticationFailed = "ClusterTriggerAuthenticationFailed"

// ClusterTriggerAuthenticationUpdated is for event when a ClusterTriggerAuthentication is updated
ClusterTriggerAuthenticationUpdated = "ClusterTriggerAuthenticationUpdated"
)
Loading

0 comments on commit e843e2f

Please sign in to comment.