Skip to content

Commit

Permalink
add first implementaion
Browse files Browse the repository at this point in the history
  • Loading branch information
dbadura committed Oct 12, 2023
1 parent 8d9f2d8 commit e131a14
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 22 deletions.
3 changes: 3 additions & 0 deletions components/serverless/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ vendor/
cover.out
filered.cov
log_config.yaml

# Directory with temporary files used to do development
hack/test_files
10 changes: 5 additions & 5 deletions components/serverless/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ override VERIFY_IGNORE := /vendor\|/automock\|/pkg/apis/serverless/v1alpha2/zz_g
# Image URL to use all building/pushing image targets
IMG ?= $(APP_NAME):latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true,crdVersions=v1"
#CRD_OPTIONS ?= "crd:trivialVersions=true,crdVersions=v1"

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand All @@ -34,20 +34,20 @@ test-local:
@go tool cover -func=/tmp/artifacts/cover.out | grep total | awk '{print $$3}'

# Generate manifests e.g. CRD, RBAC etc.
CONTROLLER_GEN_VERSION=v0.6.2
CONTROLLER_GEN_VERSION=v0.9.2

.PHONY: manifests
manifests: controller-gen-local generate-local
@make -C ${PROJECT_ROOT} kustomize
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=serverless webhook paths="./..." \
$(CONTROLLER_GEN) rbac:roleName=serverless crd webhook paths="./..." \
object:headerFile=hack/boilerplate.go.txt \
output:crd:artifacts:config=config/crd/bases \
output:rbac:artifacts:config=config/rbac \
output:webhook:artifacts:config=config/webhook
$(KUSTOMIZE) build config/crd > config/crd/crd-serverless.yaml
cp config/crd/crd-serverless.yaml $(PROJECT_ROOT)/config/serverless/templates/crds.yaml
# TODO: Fix it. Now this docu is in https://kyma-project.io/#/serverless-manager/user/resources/06-10-function-cr?id=custom-resource-parameters. Remove table-gen from kyma.
# (cd ../../hack/table-gen && make serverless-docs )
# TODO: Fix it. Now this docu is in https://kyma-project.io/#/serverless-manager/user/resources/06-10-function-cr?id=custom-resource-parameters. Remove table-gen from kyma.
# (cd ../../hack/table-gen && make serverless-docs )

# Generate code
.PHONY: generate-local
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.6.2
controller-gen.kubebuilder.io/version: v0.9.2
creationTimestamp: null
name: functions.serverless.kyma-project.io
spec:
Expand Down Expand Up @@ -112,6 +111,7 @@ spec:
required:
- key
type: object
x-kubernetes-map-type: atomic
fieldRef:
description: 'Selects a field of the pod: supports metadata.name,
metadata.namespace, `metadata.labels[''<KEY>'']`, `metadata.annotations[''<KEY>'']`,
Expand All @@ -129,6 +129,7 @@ spec:
required:
- fieldPath
type: object
x-kubernetes-map-type: atomic
resourceFieldRef:
description: 'Selects a resource of the container: only
resources limits and requests (limits.cpu, limits.memory,
Expand All @@ -153,6 +154,7 @@ spec:
required:
- resource
type: object
x-kubernetes-map-type: atomic
secretKeyRef:
description: Selects a key of a secret in the pod's namespace
properties:
Expand All @@ -171,6 +173,7 @@ spec:
required:
- key
type: object
x-kubernetes-map-type: atomic
type: object
required:
- name
Expand Down Expand Up @@ -234,6 +237,10 @@ spec:
type: object
type: object
type: object
x-kubernetes-validations:
- message: Use profile or resources
rule: has(self.profile) && !has(self.resources) || !has(self.profile)
&& has(self.resources)
function:
description: Specifies resources requested by the Function's Pod.
properties:
Expand Down Expand Up @@ -271,6 +278,10 @@ spec:
type: object
type: object
type: object
x-kubernetes-validations:
- message: Use profile or resources
rule: has(self.profile) && !has(self.resources) || !has(self.profile)
&& has(self.resources)
type: object
runtime:
description: Specifies the runtime of the Function. The available
Expand Down Expand Up @@ -480,9 +491,3 @@ spec:
specReplicasPath: .spec.replicas
statusReplicasPath: .status.replicas
status: {}
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
1 change: 0 additions & 1 deletion components/serverless/config/rbac/role.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
Expand Down
1 change: 0 additions & 1 deletion components/serverless/config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

---
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@ type ScaleConfig struct {
type ResourceConfiguration struct {
// Specifies resources requested by the build Job's Pod.
// +optional
// +kubebuilder:validation:XValidation:message="Use profile or resources",rule="has(self.profile) && !has(self.resources) || !has(self.profile) && has(self.resources)"
Build *ResourceRequirements `json:"build,omitempty"`

// Specifies resources requested by the Function's Pod.
// +optional
// +kubebuilder:validation:XValidation:message="Use profile or resources",rule="has(self.profile) && !has(self.resources) || !has(self.profile) && has(self.resources)"
Function *ResourceRequirements `json:"function,omitempty"`
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package v1alpha2_test

import (
"context"
"github.com/kyma-project/kyma/components/function-controller/internal/testenv"
serverlessv1alpha2 "github.com/kyma-project/kyma/components/function-controller/pkg/apis/serverless/v1alpha2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
)

func Test_XKubernetesValidations(t *testing.T) {
ctx := context.TODO()
k8sClient, testEnv := testenv.SetUpTestEnv(t)
defer testenv.TearDownTestEnv(t, testEnv)

testNs := corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: "test"},
}
err := k8sClient.Create(ctx, &testNs)
require.NoError(t, err)

testCases := map[string]struct {
fn *serverlessv1alpha2.Function
expectedErr bool
expectedErrMsg string
fieldPath string
}{
"Resource and Profiles used together in function": {
fn: &serverlessv1alpha2.Function{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
Spec: serverlessv1alpha2.FunctionSpec{
ResourceConfiguration: &serverlessv1alpha2.ResourceConfiguration{Function: &serverlessv1alpha2.ResourceRequirements{
Profile: "Test",
Resources: &corev1.ResourceRequirements{},
}},
},
},
expectedErr: true,
expectedErrMsg: "Use profile or resources",
fieldPath: "spec.resourceConfiguration.function",
},
"Profile set only for function": {
fn: &serverlessv1alpha2.Function{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
Spec: serverlessv1alpha2.FunctionSpec{
ResourceConfiguration: &serverlessv1alpha2.ResourceConfiguration{Function: &serverlessv1alpha2.ResourceRequirements{
Profile: "Test",
}},
},
},
},
"Resource set only for function": {
fn: &serverlessv1alpha2.Function{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
Spec: serverlessv1alpha2.FunctionSpec{
ResourceConfiguration: &serverlessv1alpha2.ResourceConfiguration{Function: &serverlessv1alpha2.ResourceRequirements{
Profile: "Test",
}},
},
},
},
"Resource and Profiles used together in buildJob": {
fn: &serverlessv1alpha2.Function{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
Spec: serverlessv1alpha2.FunctionSpec{
ResourceConfiguration: &serverlessv1alpha2.ResourceConfiguration{Build: &serverlessv1alpha2.ResourceRequirements{
Profile: "Test",
Resources: &corev1.ResourceRequirements{},
}},
},
},
expectedErr: true,
expectedErrMsg: "Use profile or resources",
fieldPath: "spec.resourceConfiguration.build",
},
"Profile set only for buildJob": {
fn: &serverlessv1alpha2.Function{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
Spec: serverlessv1alpha2.FunctionSpec{
ResourceConfiguration: &serverlessv1alpha2.ResourceConfiguration{Build: &serverlessv1alpha2.ResourceRequirements{
Profile: "Test",
}},
},
},
},
"Resource set only for buildJob": {
fn: &serverlessv1alpha2.Function{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
Spec: serverlessv1alpha2.FunctionSpec{
ResourceConfiguration: &serverlessv1alpha2.ResourceConfiguration{Build: &serverlessv1alpha2.ResourceRequirements{
Profile: "Test",
}},
},
},
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
//GIVEN

//WHEN
err := k8sClient.Create(ctx, tc.fn)
//THEN
if tc.expectedErr {
require.Error(t, err)
errStatus, ok := err.(*k8serrors.StatusError)
require.True(t, ok)
causes := errStatus.Status().Details.Causes
require.Len(t, causes, 1)
cause := causes[0]
assert.Equal(t, metav1.CauseTypeFieldValueInvalid, cause.Type)
assert.Equal(t, tc.fieldPath, cause.Field)
assert.Contains(t, cause.Message, tc.expectedErrMsg)
} else {
require.NoError(t, err)
require.NoError(t, k8sClient.Delete(ctx, tc.fn))
}
})
}
}
20 changes: 13 additions & 7 deletions config/serverless/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.6.2
controller-gen.kubebuilder.io/version: v0.9.2
creationTimestamp: null
name: functions.serverless.kyma-project.io
spec:
Expand Down Expand Up @@ -110,6 +110,7 @@ spec:
required:
- key
type: object
x-kubernetes-map-type: atomic
fieldRef:
description: 'Selects a field of the pod: supports metadata.name,
metadata.namespace, `metadata.labels[''<KEY>'']`, `metadata.annotations[''<KEY>'']`,
Expand All @@ -127,6 +128,7 @@ spec:
required:
- fieldPath
type: object
x-kubernetes-map-type: atomic
resourceFieldRef:
description: 'Selects a resource of the container: only
resources limits and requests (limits.cpu, limits.memory,
Expand All @@ -151,6 +153,7 @@ spec:
required:
- resource
type: object
x-kubernetes-map-type: atomic
secretKeyRef:
description: Selects a key of a secret in the pod's namespace
properties:
Expand All @@ -169,6 +172,7 @@ spec:
required:
- key
type: object
x-kubernetes-map-type: atomic
type: object
required:
- name
Expand Down Expand Up @@ -232,6 +236,10 @@ spec:
type: object
type: object
type: object
x-kubernetes-validations:
- message: Use profile or resources
rule: has(self.profile) && !has(self.resources) || !has(self.profile)
&& has(self.resources)
function:
description: Specifies resources requested by the Function's Pod.
properties:
Expand Down Expand Up @@ -269,6 +277,10 @@ spec:
type: object
type: object
type: object
x-kubernetes-validations:
- message: Use profile or resources
rule: has(self.profile) && !has(self.resources) || !has(self.profile)
&& has(self.resources)
type: object
runtime:
description: Specifies the runtime of the Function. The available
Expand Down Expand Up @@ -478,9 +490,3 @@ spec:
specReplicasPath: .spec.replicas
statusReplicasPath: .status.replicas
status: {}
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []

0 comments on commit e131a14

Please sign in to comment.