Skip to content

Commit

Permalink
Validate spec.runtime (kyma-project#1327)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwiatekus committed Dec 19, 2024
1 parent 122d80a commit f6ee7e1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Runtime string
const (
Python312 Runtime = "python312"
NodeJs20 Runtime = "nodejs20"
NodeJs22 Runtime = "nodejs22"
)

// FunctionSpec defines the desired state of Function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import (
"context"
"errors"
"fmt"
"slices"
"strings"

serverlessv1alpha2 "github.com/kyma-project/serverless/api/v1alpha2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilvalidation "k8s.io/apimachinery/pkg/util/validation"
ctrl "sigs.k8s.io/controller-runtime"
"strings"
)

func sFnValidateFunction(_ context.Context, m *stateMachine) (stateFn, *ctrl.Result, error) {
v := NewFunctionValidator(&m.state.instance)
validationFns := []func() []string{
v.validateEnvs,
v.validateInlineDeps,
v.validateRuntime,
//TODO: add more validation functions
}

Expand Down Expand Up @@ -72,6 +75,17 @@ func (v *functionValidator) validateInlineDeps() []string {
return []string{}
}

func (v *functionValidator) validateRuntime() []string {
runtime := v.instance.Spec.Runtime

if err := validateRuntime(runtime); err != nil {
return []string{
fmt.Sprintf("invalid runtime value: %s", err.Error()),
}
}
return []string{}
}

func validateDependencies(runtime serverlessv1alpha2.Runtime, dependencies string) error {
switch runtime {
case serverlessv1alpha2.NodeJs20:
Expand All @@ -89,6 +103,17 @@ func validateNodeJSDependencies(dependencies string) error {
return nil
}

func validateRuntime(runtime serverlessv1alpha2.Runtime) error {
if len(runtime) == 0 {
return nil
}
supportedruntimes := []serverlessv1alpha2.Runtime{serverlessv1alpha2.NodeJs20, serverlessv1alpha2.NodeJs22, serverlessv1alpha2.Python312}
if slices.Contains(supportedruntimes, runtime) {
return nil
}
return fmt.Errorf("cannot find runtime: %s", runtime)
}

func enrichErrors(errs []string, path string, value string) []string {
enrichedErrs := []string{}
for _, err := range errs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package serverless
import (
"context"
"fmt"
"strings"

serverlessv1alpha2 "github.com/kyma-project/serverless/components/serverless/pkg/apis/serverless/v1alpha2"
corev1 "k8s.io/api/core/v1"
resource "k8s.io/apimachinery/pkg/api/resource"
Expand All @@ -12,7 +14,6 @@ import (
utilvalidation "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"strings"
)

var _ stateFn = stateFnValidateFunction
Expand All @@ -28,6 +29,7 @@ func stateFnValidateFunction(_ context.Context, r *reconciler, s *systemState) (

spec := s.instance.Spec
validationFns := []validationFn{
validateRuntime(spec.Runtime),
validateFunctionResources,
validateBuildResources,
validateEnvs(spec.Env, "spec.env"),
Expand Down Expand Up @@ -142,6 +144,17 @@ func validateInlineDeps(runtime serverlessv1alpha2.Runtime, inlineSource *server
}
}

func validateRuntime(runtime serverlessv1alpha2.Runtime) validationFn {
return func() []string {
if err := serverlessv1alpha2.ValidateRuntime(runtime); err != nil {
return []string{
fmt.Sprintf("invalid runtime value: %s", err.Error()),
}
}
return []string{}
}
}

func validateFunctionLabels(labels map[string]string, path string) validationFn {
return func() []string {
errs := field.ErrorList{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,15 @@ func TestValidation_Invalid(t *testing.T) {
},
expectedCondMsg: "source.gitRepository.URL: parse \"[email protected]:kyma-project/kyma.git\": invalid URI for request",
},
"Invalid runtime": {
fn: serverlessv1alpha2.Function{
ObjectMeta: metav1.ObjectMeta{GenerateName: "test-fn"},
Spec: serverlessv1alpha2.FunctionSpec{
Runtime: "foo",
},
},
expectedCondMsg: "invalid runtime value: cannot find runtime: foo",
},
}

//WHEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1alpha2
import (
"errors"
"fmt"
"slices"
"strings"
)

Expand All @@ -13,7 +14,7 @@ func ValidateDependencies(runtime Runtime, dependencies string) error {
case Python312:
return nil
}
return fmt.Errorf("cannot find runtime: %s", runtime)
return nil
}

func validateNodeJSDependencies(dependencies string) error {
Expand All @@ -22,3 +23,14 @@ func validateNodeJSDependencies(dependencies string) error {
}
return nil
}

func ValidateRuntime(runtime Runtime) error {
if len(runtime) == 0 {
return nil
}
supportedruntimes := []Runtime{NodeJs20, NodeJs22, Python312}
if slices.Contains(supportedruntimes, runtime) {
return nil
}
return fmt.Errorf("cannot find runtime: %s", runtime)
}

0 comments on commit f6ee7e1

Please sign in to comment.