Skip to content

Commit

Permalink
Add BoundServiceAccountToken trigger authentication type
Browse files Browse the repository at this point in the history
Signed-off-by: Max Cao <[email protected]>
  • Loading branch information
maxcao13 committed Oct 24, 2024
1 parent b2ce95d commit 7ed0af9
Show file tree
Hide file tree
Showing 16 changed files with 327 additions and 32 deletions.
9 changes: 9 additions & 0 deletions apis/keda/v1alpha1/triggerauthentication_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ type TriggerAuthenticationSpec struct {

// +optional
AwsSecretManager *AwsSecretManager `json:"awsSecretManager,omitempty"`

// +optional
BoundServiceAccountToken []BoundServiceAccountToken `json:"boundServiceAccountToken,omitempty"`
}

// TriggerAuthenticationStatus defines the observed state of TriggerAuthentication
Expand Down Expand Up @@ -378,6 +381,12 @@ type AwsSecretManagerSecret struct {
VersionStage string `json:"versionStage,omitempty"`
}

type BoundServiceAccountToken struct {
Parameter string `json:"parameter"`
ServiceAccountName string `json:"serviceAccountName"`
Expiry string `json:"expiry"`
}

func init() {
SchemeBuilder.Register(&ClusterTriggerAuthentication{}, &ClusterTriggerAuthenticationList{})
SchemeBuilder.Register(&TriggerAuthentication{}, &TriggerAuthenticationList{})
Expand Down
20 changes: 20 additions & 0 deletions apis/keda/v1alpha1/zz_generated.deepcopy.go

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

14 changes: 10 additions & 4 deletions cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/kedacore/keda/v2/pkg/k8s"
"github.com/kedacore/keda/v2/pkg/metricscollector"
"github.com/kedacore/keda/v2/pkg/metricsservice"
"github.com/kedacore/keda/v2/pkg/scalers/authentication"
"github.com/kedacore/keda/v2/pkg/scaling"
kedautil "github.com/kedacore/keda/v2/pkg/util"
//+kubebuilder:scaffold:imports
Expand Down Expand Up @@ -217,8 +218,14 @@ func main() {
os.Exit(1)
}

scaledHandler := scaling.NewScaleHandler(mgr.GetClient(), scaleClient, mgr.GetScheme(), globalHTTPTimeout, eventRecorder, secretInformer.Lister())
eventEmitter := eventemitter.NewEventEmitter(mgr.GetClient(), eventRecorder, k8sClusterName, secretInformer.Lister())
authClientSet := &authentication.AuthClientSet{
TokenReviewInterface: kubeClientset.AuthenticationV1().TokenReviews(),
CoreV1Interface: kubeClientset.CoreV1(),
SecretLister: secretInformer.Lister(),
}

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

if err = (&kedacontrollers.ScaledObjectReconciler{
Client: mgr.GetClient(),
Expand All @@ -237,8 +244,7 @@ func main() {
Scheme: mgr.GetScheme(),
GlobalHTTPTimeout: globalHTTPTimeout,
EventEmitter: eventEmitter,
SecretsLister: secretInformer.Lister(),
SecretsSynced: secretInformer.Informer().HasSynced,
AuthClientSet: authClientSet,
}).SetupWithManager(mgr, controller.Options{
MaxConcurrentReconciles: scaledJobMaxReconciles,
}); err != nil {
Expand Down
15 changes: 15 additions & 0 deletions config/crd/bases/keda.sh_clustertriggerauthentications.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,21 @@ spec:
- secrets
- vaultUri
type: object
boundServiceAccountToken:
items:
properties:
expiry:
type: string
parameter:
type: string
serviceAccountName:
type: string
required:
- expiry
- parameter
- serviceAccountName
type: object
type: array
configMapTargetRef:
items:
description: AuthConfigMapTargetRef is used to authenticate using
Expand Down
15 changes: 15 additions & 0 deletions config/crd/bases/keda.sh_triggerauthentications.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,21 @@ spec:
- secrets
- vaultUri
type: object
boundServiceAccountToken:
items:
properties:
expiry:
type: string
parameter:
type: string
serviceAccountName:
type: string
required:
- expiry
- parameter
- serviceAccountName
type: object
type: array
configMapTargetRef:
items:
description: AuthConfigMapTargetRef is used to authenticate using
Expand Down
7 changes: 7 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ rules:
verbs:
- list
- watch
- apiGroups:
- ""
resources:
- serviceaccounts/token
verbs:
- create
- get
- apiGroups:
- '*'
resources:
Expand Down
7 changes: 3 additions & 4 deletions controllers/keda/scaledjob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
corev1listers "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
Expand All @@ -45,6 +44,7 @@ import (
"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/scalers/authentication"
"github.com/kedacore/keda/v2/pkg/scaling"
kedastatus "github.com/kedacore/keda/v2/pkg/status"
"github.com/kedacore/keda/v2/pkg/util"
Expand All @@ -59,11 +59,10 @@ type ScaledJobReconciler struct {
Scheme *runtime.Scheme
GlobalHTTPTimeout time.Duration
EventEmitter eventemitter.EventHandler
AuthClientSet *authentication.AuthClientSet

scaledJobGenerations *sync.Map
scaleHandler scaling.ScaleHandler
SecretsLister corev1listers.SecretLister
SecretsSynced cache.InformerSynced
}

type scaledJobMetricsData struct {
Expand All @@ -83,7 +82,7 @@ func init() {

// SetupWithManager initializes the ScaledJobReconciler instance and starts a new controller managed by the passed Manager instance.
func (r *ScaledJobReconciler) SetupWithManager(mgr ctrl.Manager, options controller.Options) error {
r.scaleHandler = scaling.NewScaleHandler(mgr.GetClient(), nil, mgr.GetScheme(), r.GlobalHTTPTimeout, mgr.GetEventRecorderFor("scale-handler"), r.SecretsLister)
r.scaleHandler = scaling.NewScaleHandler(mgr.GetClient(), nil, mgr.GetScheme(), r.GlobalHTTPTimeout, mgr.GetEventRecorderFor("scale-handler"), r.AuthClientSet)
r.scaledJobGenerations = &sync.Map{}
return ctrl.NewControllerManagedBy(mgr).
WithOptions(options).
Expand Down
1 change: 1 addition & 0 deletions controllers/keda/scaledobject_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
// +kubebuilder:rbac:groups=autoscaling,resources=horizontalpodautoscalers,verbs="*"
// +kubebuilder:rbac:groups="",resources=configmaps;configmaps/status,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=events,verbs="*"
// +kubebuilder:rbac:groups="",resources=serviceaccounts/token,verbs=create;get
// +kubebuilder:rbac:groups="",resources=pods;services;services;secrets;external,verbs=get;list;watch
// +kubebuilder:rbac:groups="*",resources="*/scale",verbs=get;list;watch;update;patch
// +kubebuilder:rbac:groups="",resources="serviceaccounts",verbs=list;watch
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.22.4 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect
github.com/aws/smithy-go v1.20.3 // indirect
github.com/aws/smithy-go v1.20.3
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
Expand Down Expand Up @@ -240,7 +240,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down
10 changes: 5 additions & 5 deletions pkg/eventemitter/eventemitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
corev1listers "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"

eventingv1alpha1 "github.com/kedacore/keda/v2/apis/eventing/v1alpha1"
"github.com/kedacore/keda/v2/pkg/eventemitter/eventdata"
"github.com/kedacore/keda/v2/pkg/metricscollector"
"github.com/kedacore/keda/v2/pkg/scalers/authentication"
"github.com/kedacore/keda/v2/pkg/scaling/resolver"
kedastatus "github.com/kedacore/keda/v2/pkg/status"
)
Expand All @@ -66,7 +66,7 @@ type EventEmitter struct {
eventFilterCacheLock *sync.RWMutex
eventLoopContexts *sync.Map
cloudEventProcessingChan chan eventdata.EventData
secretsLister corev1listers.SecretLister
authClientSet *authentication.AuthClientSet
}

// EventHandler defines the behavior for EventEmitter clients
Expand Down Expand Up @@ -96,7 +96,7 @@ const (
)

// NewEventEmitter creates a new EventEmitter
func NewEventEmitter(client client.Client, recorder record.EventRecorder, clusterName string, secretsLister corev1listers.SecretLister) EventHandler {
func NewEventEmitter(client client.Client, recorder record.EventRecorder, clusterName string, authClientSet *authentication.AuthClientSet) EventHandler {
return &EventEmitter{
log: logf.Log.WithName("event_emitter"),
client: client,
Expand All @@ -108,7 +108,7 @@ func NewEventEmitter(client client.Client, recorder record.EventRecorder, cluste
eventFilterCacheLock: &sync.RWMutex{},
eventLoopContexts: &sync.Map{},
cloudEventProcessingChan: make(chan eventdata.EventData, maxChannelBuffer),
secretsLister: secretsLister,
authClientSet: authClientSet,
}
}

Expand Down Expand Up @@ -188,7 +188,7 @@ func (e *EventEmitter) createEventHandlers(ctx context.Context, cloudEventSource
}

// Resolve auth related
authParams, podIdentity, err := resolver.ResolveAuthRefAndPodIdentity(ctx, e.client, e.log, spec.AuthenticationRef, nil, cloudEventSourceI.GetNamespace(), e.secretsLister)
authParams, podIdentity, err := resolver.ResolveAuthRefAndPodIdentity(ctx, e.client, e.log, spec.AuthenticationRef, nil, cloudEventSourceI.GetNamespace(), e.authClientSet)
if err != nil {
e.log.Error(err, "error resolving auth params", "cloudEventSource", cloudEventSourceI)
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/mock/mock_scaling/mock_executor/mock_interface.go

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

9 changes: 9 additions & 0 deletions pkg/scalers/authentication/authentication_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ import (
libs "github.com/dysnix/predictkube-libs/external/configs"
"github.com/dysnix/predictkube-libs/external/http_transport"
pConfig "github.com/prometheus/common/config"
authenticationv1client "k8s.io/client-go/kubernetes/typed/authentication/v1"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
corev1listers "k8s.io/client-go/listers/core/v1"

kedautil "github.com/kedacore/keda/v2/pkg/util"
)

type AuthClientSet struct {
authenticationv1client.TokenReviewInterface
corev1client.CoreV1Interface
corev1listers.SecretLister
}

const (
AuthModesKey = "authModes"
)
Expand Down
Loading

0 comments on commit 7ed0af9

Please sign in to comment.