Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions components/buildless-serverless/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,18 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
rm Dockerfile.cross

build-image: ## Build buildless serverless backend image
docker build -t $(APP_NAME) -f Dockerfile .
docker build -t $(APP_NAME) -f Dockerfile ../..

######## disable operator to prevent undo of local image update to k3d
disable-operator:
$(KUBECTL) scale deployment serverless-operator -n kyma-system --replicas=0
$(KUBECTL) scale deployment serverless-operator -n kyma-system --replicas=0 || true

.PHONY: install-buildless
install-buildless: build-image disable-operator## Build buildless serverless image and install it on local k3d cluster
install-buildless: build-image disable-operator ## Build buildless serverless image and install it on local k3d cluster
$(eval HASH_TAG=$(shell docker images $(APP_NAME):latest --quiet))
docker tag $(APP_NAME) $(APP_NAME):$(HASH_TAG)

k3d image import $(APP_NAME):$(HASH_TAG) -c kyma
k3d image import $(APP_NAME):$(HASH_TAG)
$(KUBECTL) set image deployment -n kyma-system serverless-ctrl-mngr manager=$(APP_NAME):$(HASH_TAG)
##@ Deployment

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type Deployment struct {
podCmd []string
}

func NewDeployment(f *serverlessv1alpha2.Function, c *config.FunctionConfig, clusterDeployment *appsv1.Deployment, commit string, gitAuth *git.GitAuth, opts ...deployOptions) *Deployment {
func NewDeployment(f *serverlessv1alpha2.Function, c *config.FunctionConfig, clusterDeployment *appsv1.Deployment, commit string, gitAuth *git.GitAuth, appName string, opts ...deployOptions) *Deployment {
d := &Deployment{
functionConfig: c,
function: f,
Expand All @@ -114,6 +114,13 @@ func NewDeployment(f *serverlessv1alpha2.Function, c *config.FunctionConfig, clu
},
}

if appName != "" {
if d.podLabels == nil {
d.podLabels = make(map[string]string)
}
d.podLabels["app.kubernetes.io/name"] = appName
}

for _, o := range opts {
o(d)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestNewDeployment(t *testing.T) {
f := minimalFunction()
c := minimalFunctionConfig()

r := NewDeployment(f, c, nil, "test-commit", nil)
r := NewDeployment(f, c, nil, "test-commit", nil, "")

require.NotNil(t, r)
d := r.Deployment
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestDeployment_construct(t *testing.T) {
"shtern": "stoic",
"boyd": "vigilant",
}
d := NewDeployment(f, minimalFunctionConfig(), nil, "", nil)
d := NewDeployment(f, minimalFunctionConfig(), nil, "", nil, "")

r := d.construct()

Expand Down Expand Up @@ -167,7 +167,7 @@ func TestDeployment_construct(t *testing.T) {
t.Run("use container image based on function and function configuration", func(t *testing.T) {
d := NewDeployment(minimalFunction(), &config.FunctionConfig{
Images: config.ImagesConfig{Python312: "special-test-image"},
}, nil, "", nil)
}, nil, "", nil, "")

r := d.construct()

Expand Down Expand Up @@ -1218,7 +1218,7 @@ func TestDeployment_envs(t *testing.T) {
d := NewDeployment(tt.function, &config.FunctionConfig{
FunctionPublisherProxyAddress: "test-proxy-address",
FunctionTraceCollectorEndpoint: "test-trace-collector-endpoint",
}, nil, "", nil)
}, nil, "", nil, "")

assert.ElementsMatch(t, tt.want, d.podEnvs)
})
Expand Down Expand Up @@ -1445,7 +1445,7 @@ func minimalFunctionConfig() *config.FunctionConfig {
}

func minimalDeploymentForFunction(f *serverlessv1alpha2.Function) *Deployment {
return NewDeployment(f, minimalFunctionConfig(), nil, "", nil)
return NewDeployment(f, minimalFunctionConfig(), nil, "", nil, "")
}

func minimalDeployment() *Deployment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package state

import (
"context"
"testing"

serverlessv1alpha2 "github.com/kyma-project/serverless/components/buildless-serverless/api/v1alpha2"
"github.com/kyma-project/serverless/components/buildless-serverless/internal/config"
"github.com/kyma-project/serverless/components/buildless-serverless/internal/controller/fsm"
Expand All @@ -12,7 +14,6 @@ import (
k8sresource "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"testing"
)

func Test_sFnAdjustStatus(t *testing.T) {
Expand Down Expand Up @@ -40,7 +41,7 @@ func Test_sFnAdjustStatus(t *testing.T) {
m := fsm.StateMachine{
State: fsm.SystemState{
Function: f,
BuiltDeployment: resources.NewDeployment(&f, &fc, nil, "test-commit", nil),
BuiltDeployment: resources.NewDeployment(&f, &fc, nil, "test-commit", nil, ""),
ClusterDeployment: &appsv1.Deployment{
Status: appsv1.DeploymentStatus{
Replicas: int32(686)}}},
Expand Down Expand Up @@ -105,7 +106,7 @@ func Test_sFnAdjustStatus(t *testing.T) {
State: fsm.SystemState{
Function: f,
Commit: "test-commit",
BuiltDeployment: resources.NewDeployment(&f, &fc, nil, "test-commit", nil),
BuiltDeployment: resources.NewDeployment(&f, &fc, nil, "test-commit", nil, ""),
ClusterDeployment: &appsv1.Deployment{
Status: appsv1.DeploymentStatus{
Replicas: int32(686)}}},
Expand Down Expand Up @@ -180,7 +181,7 @@ func Test_sFnAdjustStatus(t *testing.T) {
m := fsm.StateMachine{
State: fsm.SystemState{
Function: f,
BuiltDeployment: resources.NewDeployment(&f, &fc, nil, "test-commit", nil),
BuiltDeployment: resources.NewDeployment(&f, &fc, nil, "test-commit", nil, ""),
ClusterDeployment: &appsv1.Deployment{
Status: appsv1.DeploymentStatus{
Replicas: int32(686)}}},
Expand Down Expand Up @@ -229,7 +230,7 @@ func Test_sFnAdjustStatus(t *testing.T) {
m := fsm.StateMachine{
State: fsm.SystemState{
Function: f,
BuiltDeployment: resources.NewDeployment(&f, &fc, nil, "test-commit", nil),
BuiltDeployment: resources.NewDeployment(&f, &fc, nil, "test-commit", nil, ""),
ClusterDeployment: &appsv1.Deployment{
Status: appsv1.DeploymentStatus{
Replicas: int32(686)}}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package state
import (
"context"
"fmt"
"reflect"
"time"

serverlessv1alpha2 "github.com/kyma-project/serverless/components/buildless-serverless/api/v1alpha2"
"github.com/kyma-project/serverless/components/buildless-serverless/internal/controller/fsm"
"github.com/kyma-project/serverless/components/buildless-serverless/internal/controller/resources"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"reflect"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"time"
)

func sFnHandleDeployment(ctx context.Context, m *fsm.StateMachine) (fsm.StateFn, *ctrl.Result, error) {
Expand All @@ -33,7 +34,7 @@ func sFnHandleDeployment(ctx context.Context, m *fsm.StateMachine) (fsm.StateFn,
}
m.State.ClusterDeployment = clusterDeployment

m.State.BuiltDeployment = resources.NewDeployment(&m.State.Function, &m.FunctionConfig, clusterDeployment, m.State.Commit, m.State.GitAuth)
m.State.BuiltDeployment = resources.NewDeployment(&m.State.Function, &m.FunctionConfig, clusterDeployment, m.State.Commit, m.State.GitAuth, "")
builtDeployment := m.State.BuiltDeployment.Deployment

if m.State.ClusterDeployment == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package state
import (
"context"
"errors"
"testing"
"time"

serverlessv1alpha2 "github.com/kyma-project/serverless/components/buildless-serverless/api/v1alpha2"
"github.com/kyma-project/serverless/components/buildless-serverless/internal/config"
"github.com/kyma-project/serverless/components/buildless-serverless/internal/controller/fsm"
Expand All @@ -19,8 +22,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
"testing"
"time"
)

func Test_sFnHandleDeployment(t *testing.T) {
Expand Down Expand Up @@ -207,7 +208,7 @@ func Test_sFnHandleDeployment(t *testing.T) {
}
cd := appsv1.Deployment{}
// identical deployment will be generated inside sFnHandleDeployment
deployment := resources.NewDeployment(&f, &fc, &cd, "test-commit", nil).Deployment
deployment := resources.NewDeployment(&f, &fc, &cd, "test-commit", nil, "").Deployment
// scheme and fake client
scheme := runtime.NewScheme()
require.NoError(t, serverlessv1alpha2.AddToScheme(scheme))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func (s *Server) handleFunctionRequest(w http.ResponseWriter, r *http.Request) {
ns := r.URL.Query().Get("namespace")
name := r.URL.Query().Get("name")
appName := r.URL.Query().Get("targetAppName")

s.log.Infof("handling function request for function '%s/%s'", ns, name)

Expand All @@ -27,7 +28,7 @@ func (s *Server) handleFunctionRequest(w http.ResponseWriter, r *http.Request) {
return
}

resourceFiles, err := runtime.BuildResources(&s.functionConfig, &function)
resourceFiles, err := runtime.BuildResources(&s.functionConfig, &function, appName)
if err != nil {
s.writeErrorResponse(w, http.StatusInternalServerError, errors.Wrapf(err, "failed to get resource files for function '%s/%s'", ns, name))
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,29 @@ import (
"go.yaml.in/yaml/v2"
)

func BuildResources(functionConfig *config.FunctionConfig, f *v1alpha2.Function) ([]types.FileResponse, error) {
svc, err := buildServiceFileData(f)
func BuildResources(functionConfig *config.FunctionConfig, f *v1alpha2.Function, appName string) ([]types.FileResponse, error) {
svc, err := buildServiceFileData(f, appName)
if err != nil {
return nil, errors.Wrapf(err, "failed to build service")
}

deployment, err := buildDeploymentFileData(functionConfig, f)
deployment, err := buildDeploymentFileData(functionConfig, f, appName)
if err != nil {
return nil, errors.Wrapf(err, "failed to build deployment")
}

return []types.FileResponse{
{Name: "resources/service.yaml", Data: base64.StdEncoding.EncodeToString(svc)},
{Name: "resources/deployment.yaml", Data: base64.StdEncoding.EncodeToString(deployment)},
{Name: "k8s/service.yaml", Data: base64.StdEncoding.EncodeToString(svc)},
{Name: "k8s/deployment.yaml", Data: base64.StdEncoding.EncodeToString(deployment)},
}, nil
}

func buildServiceFileData(function *v1alpha2.Function) ([]byte, error) {
svcName := fmt.Sprintf("%s-ejected", function.Name)
func buildServiceFileData(function *v1alpha2.Function, appName string) ([]byte, error) {
svcName := appName
if svcName == "" {
svcName = fmt.Sprintf("%s-ejected", function.Name)
}

svc := resources.NewService(
function,
resources.ServiceName(svcName),
Expand All @@ -49,19 +53,24 @@ func buildServiceFileData(function *v1alpha2.Function) ([]byte, error) {
return data, nil
}

func buildDeploymentFileData(functionConfig *config.FunctionConfig, function *v1alpha2.Function) ([]byte, error) {
func buildDeploymentFileData(functionConfig *config.FunctionConfig, function *v1alpha2.Function, appName string) ([]byte, error) {
if function.HasGitSources() {
// TODO: support git source
return nil, errors.New("ejecting functions with git source is not supported")
}

deployName := fmt.Sprintf("%s-ejected", function.Name)
deployName := appName
if deployName == "" {
deployName = fmt.Sprintf("%s-ejected", function.Name)
}

deploy := resources.NewDeployment(
function,
functionConfig,
nil,
"",
nil,
appName,
resources.DeploySetName(deployName),
resources.DeployTrimClusterInfoLabels(),
resources.DeployAppendSelectorLabels(map[string]string{
Expand Down
Loading
Loading