Skip to content

Commit

Permalink
Add node22 support to buildless serverless (#1369)
Browse files Browse the repository at this point in the history
  • Loading branch information
halamix2 authored Jan 9, 2025
1 parent c64c427 commit 6cb0961
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const (

// FunctionSpec defines the desired state of Function.
type FunctionSpec struct {
// Specifies the runtime of the Function. The available values are `nodejs20`, and `python312`.
// +kubebuilder:validation:Enum=nodejs20;python312;
// Specifies the runtime of the Function. The available values are `nodejs20`, `nodejs22`, and `python312`.
// +kubebuilder:validation:Enum=nodejs20;nodejs22;python312;
Runtime Runtime `json:"runtime"`

// Specifies the runtime image used instead of the default one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,10 @@ spec:
type: object
runtime:
description: Specifies the runtime of the Function. The available
values are `nodejs20`, and `python312`.
values are `nodejs20`, `nodejs22`, and `python312`.
enum:
- nodejs20
- nodejs22
- python312
type: string
runtimeImageOverride:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "time"

type FunctionConfig struct {
ImageNodeJs20 string
ImageNodeJs22 string
ImagePython312 string
RequeueDuration time.Duration `envconfig:"default=1m"`
FunctionReadyRequeueDuration time.Duration `envconfig:"default=5m"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package resources

import (
"path"

serverlessv1alpha2 "github.com/kyma-project/serverless/api/v1alpha2"
"github.com/kyma-project/serverless/internal/config"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"path"
)

const DefaultDeploymentReplicas int32 = 1
Expand Down Expand Up @@ -178,7 +179,7 @@ func (d *Deployment) volumeMounts() []corev1.VolumeMount {
MountPath: d.workingSourcesDir(),
},
}
if runtime == serverlessv1alpha2.NodeJs20 {
if runtime == serverlessv1alpha2.NodeJs20 || runtime == serverlessv1alpha2.NodeJs22 {
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: "package-registry-config",
ReadOnly: true,
Expand Down Expand Up @@ -211,6 +212,8 @@ func (d *Deployment) runtimeImage() string {
switch d.function.Spec.Runtime {
case serverlessv1alpha2.NodeJs20:
return d.functionConfig.ImageNodeJs20
case serverlessv1alpha2.NodeJs22:
return d.functionConfig.ImageNodeJs22
case serverlessv1alpha2.Python312:
return d.functionConfig.ImagePython312
default:
Expand All @@ -220,7 +223,7 @@ func (d *Deployment) runtimeImage() string {

func (d *Deployment) workingSourcesDir() string {
switch d.function.Spec.Runtime {
case serverlessv1alpha2.NodeJs20:
case serverlessv1alpha2.NodeJs20, serverlessv1alpha2.NodeJs22:
return "/usr/src/app/function"
case serverlessv1alpha2.Python312:
return "/kubeless"
Expand All @@ -233,7 +236,7 @@ func (d *Deployment) runtimeCommand() string {
spec := &d.function.Spec
dependencies := spec.Source.Inline.Dependencies
switch spec.Runtime {
case serverlessv1alpha2.NodeJs20:
case serverlessv1alpha2.NodeJs20, serverlessv1alpha2.NodeJs22:
if dependencies != "" {
// if deps are not empty use pip
return `printf "${FUNC_HANDLER_SOURCE}" > handler.js;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package resources

import (
"testing"

serverlessv1alpha2 "github.com/kyma-project/serverless/api/v1alpha2"
"github.com/kyma-project/serverless/internal/config"
"github.com/stretchr/testify/assert"
Expand All @@ -10,7 +12,6 @@ import (
k8sresource "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"testing"
)

func TestNewDeployment(t *testing.T) {
Expand Down Expand Up @@ -300,6 +301,11 @@ func TestDeployment_workingSourcesDir(t *testing.T) {
runtime: serverlessv1alpha2.NodeJs20,
want: "/usr/src/app/function",
},
{
name: "get working dir for nodejs22",
runtime: serverlessv1alpha2.NodeJs22,
want: "/usr/src/app/function",
},
{
name: "get working dir for python312",
runtime: serverlessv1alpha2.Python312,
Expand All @@ -326,6 +332,7 @@ func TestDeployment_workingSourcesDir(t *testing.T) {
func TestDeployment_runtimeImage(t *testing.T) {
c := &config.FunctionConfig{
ImageNodeJs20: "image-for-nodejs20",
ImageNodeJs22: "image-for-nodejs22",
ImagePython312: "image-for-python312",
}
type fields struct {
Expand Down Expand Up @@ -353,6 +360,14 @@ func TestDeployment_runtimeImage(t *testing.T) {
},
want: "image-for-nodejs20",
},
{
name: "get nodejs22 image from function config",
fields: fields{
runtime: serverlessv1alpha2.NodeJs22,
runtimeImageOverride: "",
},
want: "image-for-nodejs22",
},
{
name: "get overridden image name from function",
fields: fields{
Expand All @@ -361,6 +376,14 @@ func TestDeployment_runtimeImage(t *testing.T) {
},
want: "overridden-image",
},
{
name: "get overridden image name from function",
fields: fields{
runtime: serverlessv1alpha2.NodeJs22,
runtimeImageOverride: "overridden-image",
},
want: "overridden-image",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -453,6 +476,22 @@ func TestDeployment_volumeMounts(t *testing.T) {
},
},
},
{
name: "build volume mounts for nodejs22 based on function",
runtime: serverlessv1alpha2.NodeJs22,
want: []corev1.VolumeMount{
{
Name: "sources",
MountPath: "/usr/src/app/function",
},
{
Name: "package-registry-config",
ReadOnly: true,
MountPath: "/usr/src/app/function/package-registry-config/.npmrc",
SubPath: ".npmrc",
},
},
},
{
name: "build volume mounts for python312 based on function",
runtime: serverlessv1alpha2.Python312,
Expand Down Expand Up @@ -521,6 +560,27 @@ func TestDeployment_volumes(t *testing.T) {
},
},
},
{
name: "build volumes for nodejs22 based on function",
runtime: serverlessv1alpha2.NodeJs22,
want: []corev1.Volume{
{
Name: "sources",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
{
Name: "package-registry-config",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "test-secret-name",
Optional: ptr.To[bool](true),
},
},
},
},
},
{
name: "build volumes for python312 based on function",
runtime: serverlessv1alpha2.Python312,
Expand Down Expand Up @@ -679,6 +739,34 @@ func TestDeployment_envs(t *testing.T) {
},
},
},
{
name: "build envs based on nodejs22 function",
function: &serverlessv1alpha2.Function{
Spec: serverlessv1alpha2.FunctionSpec{
Runtime: serverlessv1alpha2.NodeJs22,
Source: serverlessv1alpha2.Source{
Inline: &serverlessv1alpha2.InlineSource{
Source: "function-source",
Dependencies: "function-dependencies",
},
},
},
},
want: []corev1.EnvVar{
{
Name: "FUNC_HANDLER_SOURCE",
Value: "function-source",
},
{
Name: "FUNC_HANDLER_DEPENDENCIES",
Value: "function-dependencies",
},
{
Name: "PUBLISHER_PROXY_ADDRESS",
Value: "test-proxy-address",
},
},
},
{
name: "build envs based on python312 function",
function: &serverlessv1alpha2.Function{
Expand Down Expand Up @@ -806,6 +894,41 @@ npm start;`,
printf "${FUNC_HANDLER_DEPENDENCIES}" > package.json;
npm install --prefer-offline --no-audit --progress=false;
cd ..;
npm start;`,
},
{
name: "build runtime command for nodejs22 without dependencies",
function: &serverlessv1alpha2.Function{
Spec: serverlessv1alpha2.FunctionSpec{
Runtime: serverlessv1alpha2.NodeJs22,
Source: serverlessv1alpha2.Source{
Inline: &serverlessv1alpha2.InlineSource{
Source: "function-source",
},
},
},
},
want: `printf "${FUNC_HANDLER_SOURCE}" > handler.js;
cd ..;
npm start;`,
},
{
name: "build runtime command for nodejs22 with dependencies",
function: &serverlessv1alpha2.Function{
Spec: serverlessv1alpha2.FunctionSpec{
Runtime: serverlessv1alpha2.NodeJs22,
Source: serverlessv1alpha2.Source{
Inline: &serverlessv1alpha2.InlineSource{
Source: "function-source",
Dependencies: "function-dependencies",
},
},
},
},
want: `printf "${FUNC_HANDLER_SOURCE}" > handler.js;
printf "${FUNC_HANDLER_DEPENDENCIES}" > package.json;
npm install --prefer-offline --no-audit --progress=false;
cd ..;
npm start;`,
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
"errors"
"fmt"
"github.com/kyma-project/serverless/internal/controller/fsm"
"slices"
"strings"

"github.com/kyma-project/serverless/internal/controller/fsm"

serverlessv1alpha2 "github.com/kyma-project/serverless/api/v1alpha2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilvalidation "k8s.io/apimachinery/pkg/util/validation"
Expand Down Expand Up @@ -89,7 +90,7 @@ func (v *functionValidator) validateRuntime() []string {

func validateDependencies(runtime serverlessv1alpha2.Runtime, dependencies string) error {
switch runtime {
case serverlessv1alpha2.NodeJs20:
case serverlessv1alpha2.NodeJs20, serverlessv1alpha2.NodeJs22:
return validateNodeJSDependencies(dependencies)
case serverlessv1alpha2.Python312:
return nil
Expand Down
3 changes: 2 additions & 1 deletion config/buildless-serverless/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,10 @@ spec:
type: object
runtime:
description: Specifies the runtime of the Function. The available
values are `nodejs20`, and `python312`.
values are `nodejs20`, `nodejs22`, and `python312`.
enum:
- nodejs20
- nodejs22
- python312
type: string
runtimeImageOverride:
Expand Down

0 comments on commit 6cb0961

Please sign in to comment.