Skip to content

Commit 7189552

Browse files
pPrecelMichalKalke
andauthored
Remove profiles from webhook (#18163)
Co-authored-by: MichalKalke <[email protected]>
1 parent bff2f21 commit 7189552

File tree

13 files changed

+15
-196
lines changed

13 files changed

+15
-196
lines changed

components/function-controller/cmd/webhook/main.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,7 @@ func main() {
129129
logWithCtx.Named("defaulting-webhook")),
130130
})
131131

132-
validationCfg, err := webhookCfg.ToValidationConfig()
133-
if err != nil {
134-
setupLog.Error(err, "while creating of validation configuration")
135-
os.Exit(1)
136-
}
132+
validationCfg := webhookCfg.ToValidationConfig()
137133
whs.Register(resources.FunctionValidationWebhookPath, &ctrlwebhook.Admission{
138134
Handler: webhook.NewValidatingWebhook(
139135
&validationCfg,

components/function-controller/config/crd/bases/serverless.kyma-project.io_functions.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ spec:
190190
by the relevant HorizontalPodAutoscaler to control the number of
191191
active replicas.
192192
format: int32
193+
minimum: 0
193194
type: integer
194195
resourceConfiguration:
195196
description: Specifies resources requested by the Function and the

components/function-controller/hack/webhook_config.yaml

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ reservedEnvs:
66
- "NODE_PATH"
77
- "PYTHONPATH" # https://github.com/kubeless/runtimes/blob/master/stable/nodejs/kubeless.js;https://github.com/kubeless/runtimes/tree/master/stable/python
88
function:
9-
replicas:
10-
minValue: "1"
119
resources:
1210
minRequestCpu: "10m"
1311
minRequestMemory: "16Mi"

components/function-controller/internal/webhook/validating_webhook_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,6 @@ func TestValidatingWebHook_Handle(t *testing.T) {
223223
func fixValidationConfig() serverlessv1alpha2.ValidationConfig {
224224
return serverlessv1alpha2.ValidationConfig{
225225
Function: serverlessv1alpha2.MinFunctionValues{
226-
Replicas: serverlessv1alpha2.MinFunctionReplicasValues{
227-
MinValue: int32(1),
228-
},
229226
Resources: serverlessv1alpha2.MinFunctionResourcesValues{
230227
MinRequestCPU: "10m",
231228
MinRequestMemory: "16Mi",

components/function-controller/internal/webhook/webhook_config.go

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package webhook
22

33
import (
4+
"os"
5+
"path/filepath"
6+
47
"github.com/kyma-project/kyma/components/function-controller/pkg/apis/serverless/v1alpha2"
58
"github.com/pkg/errors"
69
"gopkg.in/yaml.v2"
710
"k8s.io/apimachinery/pkg/util/json"
8-
"os"
9-
"path/filepath"
10-
"strconv"
1111
)
1212

1313
type Replicas struct {
@@ -97,17 +97,10 @@ func (rp *RuntimePreset) UnmarshalYAML(unmarshal func(interface{}) error) error
9797
return nil
9898
}
9999

100-
func (wc WebhookConfig) ToValidationConfig() (v1alpha2.ValidationConfig, error) {
101-
minReplicas, err := strconv.Atoi(wc.Function.Replicas.MinValue)
102-
if err != nil {
103-
return v1alpha2.ValidationConfig{}, nil
104-
}
105-
cfg := v1alpha2.ValidationConfig{
100+
func (wc WebhookConfig) ToValidationConfig() v1alpha2.ValidationConfig {
101+
return v1alpha2.ValidationConfig{
106102
ReservedEnvs: wc.ReservedEnvs,
107103
Function: v1alpha2.MinFunctionValues{
108-
Replicas: v1alpha2.MinFunctionReplicasValues{
109-
MinValue: int32(minReplicas),
110-
},
111104
Resources: v1alpha2.MinFunctionResourcesValues{
112105
MinRequestCPU: wc.Function.Resources.MinRequestCpu,
113106
MinRequestMemory: wc.Function.Resources.MinRequestMemory,
@@ -120,7 +113,6 @@ func (wc WebhookConfig) ToValidationConfig() (v1alpha2.ValidationConfig, error)
120113
},
121114
},
122115
}
123-
return cfg, nil
124116
}
125117

126118
func (wc WebhookConfig) ToDefaultingConfig() (v1alpha2.DefaultingConfig, error) {

components/function-controller/pkg/apis/serverless/v1alpha2/function_types.go

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ type FunctionSpec struct {
182182
// Defines the exact number of Function's Pods to run at a time.
183183
// If **ScaleConfig** is configured, or if the Function is targeted by an external scaler,
184184
// then the **Replicas** field is used by the relevant HorizontalPodAutoscaler to control the number of active replicas.
185+
// +kubebuilder:validation:Minimum=0
185186
// +kubebuilder:default:=1
186187
// +optional
187188
Replicas *int32 `json:"replicas,omitempty"`

components/function-controller/pkg/apis/serverless/v1alpha2/function_validation.go

+2-36
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package v1alpha2
22

33
import (
44
"fmt"
5-
"k8s.io/apimachinery/pkg/labels"
65
"net/url"
76
"regexp"
87
"strings"
98

9+
"k8s.io/apimachinery/pkg/labels"
10+
1011
"github.com/pkg/errors"
1112
corev1 "k8s.io/api/core/v1"
1213
"k8s.io/apimachinery/pkg/api/resource"
@@ -19,10 +20,6 @@ import (
1920

2021
const ValidationConfigKey = "validation-config"
2122

22-
type MinFunctionReplicasValues struct {
23-
MinValue int32
24-
}
25-
2623
type MinFunctionResourcesValues struct {
2724
MinRequestCPU string
2825
MinRequestMemory string
@@ -34,7 +31,6 @@ type MinBuildJobResourcesValues struct {
3431
}
3532

3633
type MinFunctionValues struct {
37-
Replicas MinFunctionReplicasValues
3834
Resources MinFunctionResourcesValues
3935
}
4036

@@ -57,7 +53,6 @@ func (fn *Function) getBasicValidations() []validationFunction {
5753
fn.Spec.validateEnv,
5854
fn.Spec.validateLabels,
5955
fn.Spec.validateAnnotations,
60-
fn.Spec.validateReplicas,
6156
fn.Spec.validateFunctionResources,
6257
fn.Spec.validateBuildResources,
6358
fn.Spec.validateSources,
@@ -290,35 +285,6 @@ func validateLimits(resources corev1.ResourceRequirements, minMemory, minCPU res
290285
return allErrs
291286
}
292287

293-
func (spec *FunctionSpec) validateReplicas(vc *ValidationConfig) error {
294-
minValue := vc.Function.Replicas.MinValue
295-
var maxReplicas *int32
296-
var minReplicas *int32
297-
if spec.ScaleConfig == nil {
298-
return nil
299-
}
300-
maxReplicas = spec.ScaleConfig.MaxReplicas
301-
minReplicas = spec.ScaleConfig.MinReplicas
302-
allErrs := []string{}
303-
304-
if spec.Replicas == nil && spec.ScaleConfig == nil {
305-
allErrs = append(allErrs, "spec.replicas and spec.scaleConfig are empty at the same time")
306-
}
307-
if maxReplicas != nil && minReplicas != nil && *minReplicas > *maxReplicas {
308-
allErrs = append(allErrs, fmt.Sprintf("spec.maxReplicas(%d) is less than spec.minReplicas(%d)",
309-
*maxReplicas, *minReplicas))
310-
}
311-
if minReplicas != nil && *minReplicas < minValue {
312-
allErrs = append(allErrs, fmt.Sprintf("spec.minReplicas(%d) is less than the smallest allowed value(%d)",
313-
*minReplicas, minValue))
314-
}
315-
if maxReplicas != nil && *maxReplicas < minValue {
316-
allErrs = append(allErrs, fmt.Sprintf("spec.maxReplicas(%d) is less than the smallest allowed value(%d)",
317-
*maxReplicas, minValue))
318-
}
319-
return returnAllErrs("invalid values", allErrs)
320-
}
321-
322288
func (spec *FunctionSpec) validateLabels(_ *ValidationConfig) error {
323289
var templateLabels map[string]string
324290
if spec.Template != nil {

components/function-controller/pkg/apis/serverless/v1alpha2/function_validation_test.go

-116
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
corev1 "k8s.io/api/core/v1"
88
"k8s.io/apimachinery/pkg/api/resource"
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10-
"k8s.io/utils/pointer"
1110
)
1211

1312
func TestFunctionSpec_validateResources(t *testing.T) {
@@ -37,10 +36,6 @@ func TestFunctionSpec_validateResources(t *testing.T) {
3736
Source: "test-source",
3837
},
3938
},
40-
ScaleConfig: &ScaleConfig{
41-
MinReplicas: pointer.Int32(1),
42-
MaxReplicas: pointer.Int32(1),
43-
},
4439
Runtime: NodeJs18,
4540
ResourceConfiguration: &ResourceConfiguration{
4641
Function: &ResourceRequirements{
@@ -98,10 +93,6 @@ func TestFunctionSpec_validateResources(t *testing.T) {
9893
"test": "test",
9994
},
10095
},
101-
ScaleConfig: &ScaleConfig{
102-
MinReplicas: pointer.Int32(1),
103-
MaxReplicas: pointer.Int32(1),
104-
},
10596
ResourceConfiguration: &ResourceConfiguration{
10697
Function: &ResourceRequirements{
10798
Resources: &corev1.ResourceRequirements{
@@ -457,52 +448,6 @@ func TestFunctionSpec_validateResources(t *testing.T) {
457448
),
458449
),
459450
},
460-
"Should return error on replicas validation": {
461-
givenFunc: Function{
462-
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "test"},
463-
Spec: FunctionSpec{
464-
Runtime: NodeJs18,
465-
Source: Source{
466-
Inline: &InlineSource{
467-
Source: "test-source",
468-
},
469-
},
470-
ScaleConfig: &ScaleConfig{
471-
MinReplicas: pointer.Int32(1),
472-
MaxReplicas: pointer.Int32(-1),
473-
},
474-
},
475-
},
476-
expectedError: gomega.HaveOccurred(),
477-
specifiedExpectedError: gomega.And(
478-
gomega.ContainSubstring(
479-
"spec.maxReplicas",
480-
),
481-
),
482-
},
483-
"Should return error on replicas validation on 0 minReplicas set": {
484-
givenFunc: Function{
485-
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "test"},
486-
Spec: FunctionSpec{
487-
Runtime: NodeJs18,
488-
Source: Source{
489-
Inline: &InlineSource{
490-
Source: "test-source",
491-
},
492-
},
493-
ScaleConfig: &ScaleConfig{
494-
MinReplicas: pointer.Int32(0), // HPA needs this value to be greater then 0
495-
MaxReplicas: pointer.Int32(1),
496-
},
497-
},
498-
},
499-
expectedError: gomega.HaveOccurred(),
500-
specifiedExpectedError: gomega.And(
501-
gomega.ContainSubstring(
502-
"spec.minReplicas",
503-
),
504-
),
505-
},
506451
"Should return error on function resources validation": {
507452
givenFunc: Function{
508453
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "test"},
@@ -589,10 +534,6 @@ func TestFunctionSpec_validateResources(t *testing.T) {
589534
givenFunc: Function{
590535
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "test"},
591536
Spec: FunctionSpec{
592-
ScaleConfig: &ScaleConfig{
593-
MinReplicas: pointer.Int32(0),
594-
MaxReplicas: pointer.Int32(0),
595-
},
596537
Runtime: NodeJs18,
597538
Source: Source{
598539
Inline: &InlineSource{
@@ -629,8 +570,6 @@ func TestFunctionSpec_validateResources(t *testing.T) {
629570
},
630571
expectedError: gomega.HaveOccurred(),
631572
specifiedExpectedError: gomega.And(
632-
gomega.ContainSubstring("spec.minReplicas"),
633-
gomega.ContainSubstring("spec.maxReplicas"),
634573
gomega.ContainSubstring("spec.resourceConfiguration.function.resources.requests.cpu"),
635574
gomega.ContainSubstring("spec.resourceConfiguration.function.resources.requests.memory"),
636575
gomega.ContainSubstring("spec.resourceConfiguration.function.resources.limits.cpu"),
@@ -680,10 +619,6 @@ func TestFunctionSpec_validateResources(t *testing.T) {
680619
},
681620
},
682621
},
683-
ScaleConfig: &ScaleConfig{
684-
MinReplicas: pointer.Int32(1),
685-
MaxReplicas: pointer.Int32(1),
686-
},
687622
Runtime: NodeJs18,
688623
},
689624
},
@@ -724,10 +659,6 @@ func TestFunctionSpec_validateResources(t *testing.T) {
724659
},
725660
},
726661
},
727-
ScaleConfig: &ScaleConfig{
728-
MinReplicas: pointer.Int32(1),
729-
MaxReplicas: pointer.Int32(1),
730-
},
731662
Runtime: NodeJs18,
732663
},
733664
},
@@ -737,50 +668,6 @@ func TestFunctionSpec_validateResources(t *testing.T) {
737668
),
738669
expectedError: gomega.HaveOccurred(),
739670
},
740-
"Should not return error when replicas field is use together with scaleConfig": {
741-
givenFunc: Function{
742-
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "test"},
743-
Spec: FunctionSpec{
744-
Source: Source{
745-
Inline: &InlineSource{
746-
Source: "test-source",
747-
},
748-
},
749-
Replicas: pointer.Int32(1),
750-
ScaleConfig: &ScaleConfig{
751-
MinReplicas: pointer.Int32(1),
752-
MaxReplicas: pointer.Int32(1),
753-
},
754-
Runtime: NodeJs18,
755-
ResourceConfiguration: &ResourceConfiguration{
756-
Function: &ResourceRequirements{
757-
Resources: &corev1.ResourceRequirements{Limits: corev1.ResourceList{
758-
corev1.ResourceCPU: resource.MustParse("100m"),
759-
corev1.ResourceMemory: resource.MustParse("128Mi"),
760-
},
761-
Requests: corev1.ResourceList{
762-
corev1.ResourceCPU: resource.MustParse("50m"),
763-
corev1.ResourceMemory: resource.MustParse("64Mi"),
764-
},
765-
},
766-
},
767-
Build: &ResourceRequirements{
768-
Resources: &corev1.ResourceRequirements{
769-
Limits: corev1.ResourceList{
770-
corev1.ResourceCPU: resource.MustParse("300m"),
771-
corev1.ResourceMemory: resource.MustParse("300Mi"),
772-
},
773-
Requests: corev1.ResourceList{
774-
corev1.ResourceCPU: resource.MustParse("200m"),
775-
corev1.ResourceMemory: resource.MustParse("200Mi"),
776-
},
777-
},
778-
},
779-
},
780-
},
781-
},
782-
expectedError: gomega.BeNil(),
783-
},
784671
"Should validate without error Resources and Profile occurring at once in ResourceConfiguration.Function/Build": {
785672
givenFunc: Function{
786673
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "test"},
@@ -1000,9 +887,6 @@ func fixValidationConfig() *ValidationConfig {
1000887
return &ValidationConfig{
1001888
ReservedEnvs: []string{"K_CONFIGURATION"},
1002889
Function: MinFunctionValues{
1003-
Replicas: MinFunctionReplicasValues{
1004-
MinValue: int32(1),
1005-
},
1006890
Resources: MinFunctionResourcesValues{
1007891
MinRequestCPU: "10m",
1008892
MinRequestMemory: "16Mi",

components/function-controller/pkg/apis/serverless/v1alpha2/zz_generated.deepcopy.go

-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

installation/resources/crds/serverless/crd-serverless.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ spec:
188188
by the relevant HorizontalPodAutoscaler to control the number of
189189
active replicas.
190190
format: int32
191+
minimum: 0
191192
type: integer
192193
resourceConfiguration:
193194
description: Specifies resources requested by the Function and the

0 commit comments

Comments
 (0)