Skip to content

Commit 2c5cdc1

Browse files
committed
keda http-addon crd integrateed
Signed-off-by: B1F030 <[email protected]>
1 parent 00c94a3 commit 2c5cdc1

File tree

14 files changed

+803
-19
lines changed

14 files changed

+803
-19
lines changed

apis/core/v1beta2/function_webhook.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,28 @@ func (r *Function) ValidateServing() error {
306306
}
307307

308308
if scaleOptions.Keda != nil {
309-
if scaleOptions.Keda.ScaledJob != nil && scaleOptions.Keda.ScaledObject != nil {
309+
if (scaleOptions.Keda.ScaledJob != nil && scaleOptions.Keda.ScaledObject != nil && scaleOptions.Keda.HTTPScaledObject != nil) ||
310+
(scaleOptions.Keda.ScaledObject != nil && scaleOptions.Keda.HTTPScaledObject != nil) ||
311+
(scaleOptions.Keda.ScaledJob != nil && scaleOptions.Keda.HTTPScaledObject != nil) ||
312+
(scaleOptions.Keda.ScaledJob != nil && scaleOptions.Keda.ScaledObject != nil) {
310313
return field.Required(
311314
field.NewPath("spec", "serving", "scaleOptions", "keda", "scaledObject"),
312-
"scaledJob and scaledObject have at most one enabled")
315+
"scaledJob, scaledObject and httpScaledObject have at most one enabled")
316+
}
317+
if scaleOptions.Keda.HTTPScaledObject != nil {
318+
httpScaledObject := scaleOptions.Keda.HTTPScaledObject
319+
if httpScaledObject.TargetPendingRequests != nil && *httpScaledObject.TargetPendingRequests < 0 {
320+
return field.Invalid(
321+
field.NewPath("spec", "serving", "scaleOptions", "keda", "httpScaledObject", "targetPendingRequests"),
322+
httpScaledObject.TargetPendingRequests,
323+
"cannot be less than 0")
324+
}
325+
if httpScaledObject.CooldownPeriod != nil && *httpScaledObject.CooldownPeriod < 0 {
326+
return field.Invalid(
327+
field.NewPath("spec", "serving", "scaleOptions", "keda", "scaledObject", "cooldownPeriod"),
328+
httpScaledObject.CooldownPeriod,
329+
"cannot be less than 0")
330+
}
313331
}
314332
if scaleOptions.Keda.ScaledObject != nil {
315333
scaledObject := scaleOptions.Keda.ScaledObject
@@ -373,6 +391,15 @@ func (r *Function) ValidateServing() error {
373391
}
374392
}
375393

394+
if r.Spec.Serving.Triggers.Http.Engine != nil {
395+
if *r.Spec.Serving.Triggers.Http.Engine != HttpRuntimeKeda && *r.Spec.Serving.Triggers.Http.Engine != HttpRuntimeKnative {
396+
return field.Invalid(
397+
field.NewPath("spec", "serving", "triggers", "http", "engine"),
398+
r.Spec.Serving.Triggers.Http.Engine,
399+
"unknown engine type, neither knative nor keda")
400+
}
401+
}
402+
376403
return nil
377404
}
378405

apis/core/v1beta2/function_webhook_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func Test_Validate(t *testing.T) {
3737
maxReplicasNegative := int32(-1)
3838
minReplicas := int32(5)
3939
maxReplicas := int32(0)
40+
targetPendingRequests := int32(-1)
4041
pollingInterval := int32(-1)
4142
cooldownPeriod := int32(-1)
4243
stabilizationWindowSecondsNegative := int32(-1)
@@ -248,7 +249,7 @@ func Test_Validate(t *testing.T) {
248249
},
249250
{
250251
// TODO: add validation for this case
251-
name: "function.spec.serving.scaleOptions.keda.ScaledJob and ScaledObject",
252+
name: "function.spec.serving.scaleOptions.keda.ScaledJob and ScaledObject and HTTPScaledObject",
252253
r: Function{
253254
Spec: FunctionSpec{
254255
Image: "test",
@@ -263,6 +264,26 @@ func Test_Validate(t *testing.T) {
263264
},
264265
wantErr: false,
265266
},
267+
{
268+
name: "function.spec.serving.scaleOptions.keda.httpScaledObject.targetPendingRequests",
269+
r: Function{
270+
Spec: FunctionSpec{
271+
Image: "test",
272+
ImageCredentials: &v1.LocalObjectReference{Name: "secret"},
273+
Serving: &ServingImpl{
274+
Triggers: &Triggers{Http: &HttpTrigger{}},
275+
ScaleOptions: &ScaleOptions{
276+
Keda: &KedaScaleOptions{
277+
HTTPScaledObject: &HTTPScaledObject{
278+
TargetPendingRequests: &targetPendingRequests,
279+
},
280+
},
281+
},
282+
},
283+
},
284+
},
285+
wantErr: true,
286+
},
266287
{
267288
name: "function.spec.serving.scaleOptions.keda.scaledObject.pollingInterval",
268289
r: Function{

apis/core/v1beta2/serving_types.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ import (
2626
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
2727
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
2828

29+
type Engine string
30+
2931
const (
3032
HookPolicyAppend = "Append"
3133
HookPolicyOverride = "Override"
3234

33-
WorkloadTypeJob = "Job"
34-
WorkloadTypeStatefulSet = "StatefulSet"
35-
WorkloadTypeDeployment = "Deployment"
35+
WorkloadTypeJob = "Job"
36+
WorkloadTypeStatefulSet = "StatefulSet"
37+
WorkloadTypeDeployment = "Deployment"
38+
HttpRuntimeKnative Engine = "knative"
39+
HttpRuntimeKeda Engine = "keda"
3640
)
3741

3842
type Triggers struct {
@@ -49,6 +53,9 @@ type HttpTrigger struct {
4953
//
5054
// +optional
5155
Route *RouteImpl `json:"route,omitempty"`
56+
// Http function runtime engine, can be set to knative or keda, default to knative if not set
57+
// +optional
58+
Engine *Engine `json:"engine,omitempty"`
5259
}
5360

5461
type DaprTrigger struct {
@@ -94,6 +101,15 @@ type State struct {
94101
Spec *componentsv1alpha1.ComponentSpec `json:"spec,omitempty"`
95102
}
96103

104+
type HTTPScaledObject struct {
105+
// Target metric value
106+
// +optional
107+
TargetPendingRequests *int32 `json:"targetPendingRequests,omitempty"`
108+
// Cooldown period value
109+
// +optional
110+
CooldownPeriod *int32 `json:"cooldownPeriod,omitempty"`
111+
}
112+
97113
type KedaScaledObject struct {
98114
// +optional
99115
PollingInterval *int32 `json:"pollingInterval,omitempty"`
@@ -122,6 +138,8 @@ type KedaScaledJob struct {
122138
}
123139

124140
type KedaScaleOptions struct {
141+
// +optional
142+
HTTPScaledObject *HTTPScaledObject `json:"httpScaledObject,omitempty"`
125143
// +optional
126144
ScaledObject *KedaScaledObject `json:"scaledObject,omitempty"`
127145
// +optional

apis/core/v1beta2/zz_generated.deepcopy.go

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/bundle.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11451,6 +11451,17 @@ spec:
1145111451
properties:
1145211452
keda:
1145311453
properties:
11454+
httpScaledObject:
11455+
properties:
11456+
cooldownPeriod:
11457+
description: Cooldown period value
11458+
format: int32
11459+
type: integer
11460+
targetPendingRequests:
11461+
description: Target metric value
11462+
format: int32
11463+
type: integer
11464+
type: object
1145411465
scaledJob:
1145511466
properties:
1145611467
failedJobsHistoryLimit:
@@ -19223,6 +19234,10 @@ spec:
1922319234
type: array
1922419235
http:
1922519236
properties:
19237+
engine:
19238+
description: Http function runtime engine, can be set
19239+
to knative or keda, default to knative if not set
19240+
type: string
1922619241
port:
1922719242
description: The port on which the function will be invoked
1922819243
format: int32
@@ -29141,6 +29156,17 @@ spec:
2914129156
properties:
2914229157
keda:
2914329158
properties:
29159+
httpScaledObject:
29160+
properties:
29161+
cooldownPeriod:
29162+
description: Cooldown period value
29163+
format: int32
29164+
type: integer
29165+
targetPendingRequests:
29166+
description: Target metric value
29167+
format: int32
29168+
type: integer
29169+
type: object
2914429170
scaledJob:
2914529171
properties:
2914629172
failedJobsHistoryLimit:
@@ -36616,6 +36642,10 @@ spec:
3661636642
type: array
3661736643
http:
3661836644
properties:
36645+
engine:
36646+
description: Http function runtime engine, can be set to knative
36647+
or keda, default to knative if not set
36648+
type: string
3661936649
port:
3662036650
description: The port on which the function will be invoked
3662136651
format: int32

config/crd/bases/core.openfunction.io_functions.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9729,6 +9729,17 @@ spec:
97299729
properties:
97309730
keda:
97319731
properties:
9732+
httpScaledObject:
9733+
properties:
9734+
cooldownPeriod:
9735+
description: Cooldown period value
9736+
format: int32
9737+
type: integer
9738+
targetPendingRequests:
9739+
description: Target metric value
9740+
format: int32
9741+
type: integer
9742+
type: object
97329743
scaledJob:
97339744
properties:
97349745
failedJobsHistoryLimit:
@@ -17501,6 +17512,10 @@ spec:
1750117512
type: array
1750217513
http:
1750317514
properties:
17515+
engine:
17516+
description: Http function runtime engine, can be set
17517+
to knative or keda, default to knative if not set
17518+
type: string
1750417519
port:
1750517520
description: The port on which the function will be invoked
1750617521
format: int32

config/crd/bases/core.openfunction.io_servings.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7886,6 +7886,17 @@ spec:
78867886
properties:
78877887
keda:
78887888
properties:
7889+
httpScaledObject:
7890+
properties:
7891+
cooldownPeriod:
7892+
description: Cooldown period value
7893+
format: int32
7894+
type: integer
7895+
targetPendingRequests:
7896+
description: Target metric value
7897+
format: int32
7898+
type: integer
7899+
type: object
78897900
scaledJob:
78907901
properties:
78917902
failedJobsHistoryLimit:
@@ -15361,6 +15372,10 @@ spec:
1536115372
type: array
1536215373
http:
1536315374
properties:
15375+
engine:
15376+
description: Http function runtime engine, can be set to knative
15377+
or keda, default to knative if not set
15378+
type: string
1536415379
port:
1536515380
description: The port on which the function will be invoked
1536615381
format: int32

config/rbac/role.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,18 @@ rules:
337337
- patch
338338
- update
339339
- watch
340+
- apiGroups:
341+
- networking.k8s.io
342+
resources:
343+
- ingresses
344+
verbs:
345+
- create
346+
- delete
347+
- get
348+
- list
349+
- patch
350+
- update
351+
- watch
340352
- apiGroups:
341353
- keda.sh
342354
resources:
@@ -350,6 +362,18 @@ rules:
350362
- patch
351363
- update
352364
- watch
365+
- apiGroups:
366+
- http.keda.sh
367+
resources:
368+
- httpscaledobjects
369+
verbs:
370+
- create
371+
- delete
372+
- get
373+
- list
374+
- patch
375+
- update
376+
- watch
353377
- apiGroups:
354378
- networking.openfunction.io
355379
resources:

controllers/core/serving_controller.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
openfunction "github.com/openfunction/apis/core/v1beta2"
3535
"github.com/openfunction/pkg/constants"
3636
"github.com/openfunction/pkg/core"
37+
"github.com/openfunction/pkg/core/serving/kedahttp"
3738
"github.com/openfunction/pkg/core/serving/knative"
3839
"github.com/openfunction/pkg/core/serving/openfuncasync"
3940
"github.com/openfunction/pkg/util"
@@ -213,7 +214,13 @@ func (r *ServingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
213214

214215
func (r *ServingReconciler) getServingRun(s *openfunction.Serving) core.ServingRun {
215216
if s.Spec.Triggers.Http != nil {
216-
return knative.NewServingRun(r.ctx, r.Client, r.Scheme, r.Log)
217+
if s.Spec.Triggers.Http.Engine == nil || *s.Spec.Triggers.Http.Engine == openfunction.HttpRuntimeKnative {
218+
return knative.NewServingRun(r.ctx, r.Client, r.Scheme, r.Log)
219+
} else if *s.Spec.Triggers.Http.Engine == openfunction.HttpRuntimeKeda {
220+
return kedahttp.NewServingRun(r.ctx, r.Client, r.Scheme, r.Log)
221+
} else {
222+
return nil
223+
}
217224
} else {
218225
return openfuncasync.NewServingRun(r.ctx, r.Client, r.Scheme, r.Log)
219226
}

0 commit comments

Comments
 (0)