Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support to scale function replicas to zero when hpa is disabled #590

Merged
merged 3 commits into from
Mar 6, 2023
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
12 changes: 12 additions & 0 deletions .ci/helm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,15 @@ function ci::verify_function_with_encryption() {
fi
return 1
}

function ci::verify_function_with_scale_down_to_zero() {
function_name=$1

kubectl patch functions ${function_name} -p '{"spec": {"replicas": 0}}'
num=1
while [[ ${num} -ne 0 ]]; do
sleep 5
num=$(kubectl get pods -l compute.functionmesh.io/name="${function_name}" --no-headers|wc -l)
done
}

55 changes: 55 additions & 0 deletions .ci/tests/integration/cases/scale-to-zero/manifests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
apiVersion: compute.functionmesh.io/v1alpha1
kind: Function
metadata:
name: py-function-replica-one
namespace: default
spec:
image: streamnative/pulsar-functions-python-sample:2.9.2.23
className: exclamation_function.ExclamationFunction
forwardSourceMessageProperty: true
maxPendingAsyncRequests: 1000
minReplicas: 1
logTopic: persistent://public/default/py-function-logs
input:
topics:
- persistent://public/default/input-python-topic
output:
topic: persistent://public/default/output-python-topic
resources:
requests:
cpu: 50m
memory: 1G
limits:
cpu: "0.2"
memory: 1.1G
# each secret will be loaded ad an env variable from the `path` secret with the `key` in that secret in the name of `name`
secretsMap:
"name":
path: "test-py-secret"
key: "username"
"pwd":
path: "test-py-secret"
key: "password"
pulsar:
pulsarConfig: "test-py-pulsar"
python:
py: /pulsar/examples/python-examples/exclamation_function.py
clusterName: test
autoAck: true
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test-py-pulsar
data:
webServiceURL: http://sn-platform-pulsar-broker.default.svc.cluster.local:8080
brokerServiceURL: pulsar://sn-platform-pulsar-broker.default.svc.cluster.local:6650
---
apiVersion: v1
data:
username: YWRtaW4=
password: MWYyZDFlMmU2N2Rm
kind: Secret
metadata:
name: test-py-secret
type: Opaque
52 changes: 52 additions & 0 deletions .ci/tests/integration/cases/scale-to-zero/verify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

set -e

E2E_DIR=$(dirname "$0")
BASE_DIR=$(cd "${E2E_DIR}"/../../../../..;pwd)
PULSAR_NAMESPACE=${PULSAR_NAMESPACE:-"default"}
PULSAR_RELEASE_NAME=${PULSAR_RELEASE_NAME:-"sn-platform"}
E2E_KUBECONFIG=${E2E_KUBECONFIG:-"/tmp/e2e-k8s.config"}

source "${BASE_DIR}"/.ci/helm.sh

if [ ! "$KUBECONFIG" ]; then
export KUBECONFIG=${E2E_KUBECONFIG}
fi

manifests_file="${BASE_DIR}"/.ci/tests/integration/cases/scale-to-zero/manifests.yaml

kubectl apply -f "${manifests_file}" > /dev/null 2>&1

verify_fm_result=$(ci::verify_function_mesh py-function-replica-one 2>&1)
if [ $? -ne 0 ]; then
echo "$verify_fm_result"
kubectl delete -f "${manifests_file}" > /dev/null 2>&1 || true
exit 1
fi

verify_python_result=$(NAMESPACE=${PULSAR_NAMESPACE} CLUSTER=${PULSAR_RELEASE_NAME} ci::verify_function_with_scale_down_to_zero py-function-replica-one 2>&1)
if [ $? -eq 0 ]; then
echo "e2e-test: ok" | yq eval -
else
echo "$verify_python_result"
fi
kubectl delete -f "${manifests_file}" > /dev/null 2>&1 || true
4 changes: 2 additions & 2 deletions api/compute/v1alpha1/function_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ type FunctionSpec struct {
Tenant string `json:"tenant,omitempty"`
Namespace string `json:"namespace,omitempty"`
ClusterName string `json:"clusterName,omitempty"`
// +kubebuilder:validation:Minimum=1
// +kubebuilder:validation:Minimum=0
// +kubebuilder:default=1
Replicas *int32 `json:"replicas,omitempty"`
// +kubebuilder:validation:Minimum=1
// +kubebuilder:validation:Minimum=0
// +kubebuilder:default=1
MinReplicas *int32 `json:"minReplicas,omitempty"`

Expand Down
4 changes: 2 additions & 2 deletions api/compute/v1alpha1/sink_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ type SinkSpec struct {
Tenant string `json:"tenant,omitempty"`
Namespace string `json:"namespace,omitempty"`
SinkType string `json:"sinkType,omitempty"` // refer to `--sink-type` as builtin connector
// +kubebuilder:validation:Minimum=1
// +kubebuilder:validation:Minimum=0
// +kubebuilder:default=1
Replicas *int32 `json:"replicas,omitempty"`
// +kubebuilder:validation:Minimum=1
// +kubebuilder:validation:Minimum=0
// +kubebuilder:default=1
MinReplicas *int32 `json:"minReplicas,omitempty"`

Expand Down
4 changes: 2 additions & 2 deletions api/compute/v1alpha1/source_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ type SourceSpec struct {
Namespace string `json:"namespace,omitempty"`
ClusterName string `json:"clusterName,omitempty"`
SourceType string `json:"sourceType,omitempty"` // refer to `--source-type` as builtin connector
// +kubebuilder:validation:Minimum=1
// +kubebuilder:validation:Minimum=0
// +kubebuilder:default=1
Replicas *int32 `json:"replicas,omitempty"`
// +kubebuilder:validation:Minimum=1
// +kubebuilder:validation:Minimum=0
// +kubebuilder:default=1
MinReplicas *int32 `json:"minReplicas,omitempty"`

Expand Down
26 changes: 14 additions & 12 deletions api/compute/v1alpha1/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,23 @@ func validateGolangRuntime(golang *GoRuntime) []*field.Error {

func validateReplicasAndMinReplicasAndMaxReplicas(replicas, minReplicas, maxReplicas *int32) []*field.Error {
var allErrs field.ErrorList
// TODO: allow 0 replicas, currently hpa's min value has to be 1
//if replicas == nil {
// e := field.Invalid(field.NewPath("spec").Child("replicas"), nil, "replicas cannot be nil")
// allErrs = append(allErrs, e)
//}

if replicas != nil && *replicas <= 0 {
e := field.Invalid(field.NewPath("spec").Child("replicas"), *replicas, "replicas cannot be zero or negative")
if replicas != nil && *replicas < 0 {
e := field.Invalid(field.NewPath("spec").Child("replicas"), *replicas, "replicas cannot be negative")
allErrs = append(allErrs, e)
}

if maxReplicas != nil && replicas != nil && *replicas > *maxReplicas {
e := field.Invalid(field.NewPath("spec").Child("maxReplicas"), *maxReplicas,
"maxReplicas must be greater than or equal to replicas")
allErrs = append(allErrs, e)
if maxReplicas != nil && replicas != nil {
if *replicas > *maxReplicas {
e := field.Invalid(field.NewPath("spec").Child("maxReplicas"), *maxReplicas,
"maxReplicas must be greater than or equal to replicas")
allErrs = append(allErrs, e)
}

if *replicas == 0 {
e := field.Invalid(field.NewPath("spec").Child("replicas"), *replicas,
"replicas cannot be zero or negative when HPA is enabled")
allErrs = append(allErrs, e)
}
}

if minReplicas != nil && *minReplicas <= 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ spec:
minReplicas:
default: 1
format: int32
minimum: 1
minimum: 0
type: integer
name:
type: string
Expand Down Expand Up @@ -3193,7 +3193,7 @@ spec:
replicas:
default: 1
format: int32
minimum: 1
minimum: 0
type: integer
resources:
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ spec:
minReplicas:
default: 1
format: int32
minimum: 1
minimum: 0
type: integer
name:
type: string
Expand Down Expand Up @@ -3212,7 +3212,7 @@ spec:
replicas:
default: 1
format: int32
minimum: 1
minimum: 0
type: integer
resources:
properties:
Expand Down
12 changes: 6 additions & 6 deletions config/crd/bases/compute.functionmesh.io_functionmeshes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ spec:
minReplicas:
default: 1
format: int32
minimum: 1
minimum: 0
type: integer
name:
type: string
Expand Down Expand Up @@ -3194,7 +3194,7 @@ spec:
replicas:
default: 1
format: int32
minimum: 1
minimum: 0
type: integer
resources:
properties:
Expand Down Expand Up @@ -3504,7 +3504,7 @@ spec:
minReplicas:
default: 1
format: int32
minimum: 1
minimum: 0
type: integer
name:
type: string
Expand Down Expand Up @@ -6413,7 +6413,7 @@ spec:
replicas:
default: 1
format: int32
minimum: 1
minimum: 0
type: integer
resources:
properties:
Expand Down Expand Up @@ -6628,7 +6628,7 @@ spec:
minReplicas:
default: 1
format: int32
minimum: 1
minimum: 0
type: integer
name:
type: string
Expand Down Expand Up @@ -9593,7 +9593,7 @@ spec:
replicas:
default: 1
format: int32
minimum: 1
minimum: 0
type: integer
resources:
properties:
Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/compute.functionmesh.io_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ spec:
minReplicas:
default: 1
format: int32
minimum: 1
minimum: 0
type: integer
name:
type: string
Expand Down Expand Up @@ -3191,7 +3191,7 @@ spec:
replicas:
default: 1
format: int32
minimum: 1
minimum: 0
type: integer
resources:
properties:
Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/compute.functionmesh.io_sinks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ spec:
minReplicas:
default: 1
format: int32
minimum: 1
minimum: 0
type: integer
name:
type: string
Expand Down Expand Up @@ -3125,7 +3125,7 @@ spec:
replicas:
default: 1
format: int32
minimum: 1
minimum: 0
type: integer
resources:
properties:
Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/compute.functionmesh.io_sources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ spec:
minReplicas:
default: 1
format: int32
minimum: 1
minimum: 0
type: integer
name:
type: string
Expand Down Expand Up @@ -3112,7 +3112,7 @@ spec:
replicas:
default: 1
format: int32
minimum: 1
minimum: 0
type: integer
resources:
properties:
Expand Down