Skip to content

Commit

Permalink
Remove profiles from webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalKalke authored and pPrecel committed Sep 14, 2023
1 parent 305cc7a commit 6935254
Show file tree
Hide file tree
Showing 17 changed files with 30 additions and 375 deletions.
6 changes: 1 addition & 5 deletions components/function-controller/cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,7 @@ func main() {
logWithCtx.Named("defaulting-webhook")),
})

validationCfg, err := webhookCfg.ToValidationConfig()
if err != nil {
setupLog.Error(err, "while creating of validation configuration")
os.Exit(1)
}
validationCfg := webhookCfg.ToValidationConfig()
whs.Register(resources.FunctionValidationWebhookPath, &ctrlwebhook.Admission{
Handler: webhook.NewValidatingWebhook(
&validationCfg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,14 @@ spec:
on the Function's runtime Pod.
type: object
replicas:
default: 1
description: Defines the exact number of Function's Pods to run at
a time. If **ScaleConfig** is configured, or if the Function is
targeted by an external scaler, then the **Replicas** field is used
by the relevant HorizontalPodAutoscaler to control the number of
active replicas.
format: int32
minimum: 0
type: integer
resourceConfiguration:
description: Specifies resources requested by the Function and the
Expand Down
22 changes: 0 additions & 22 deletions components/function-controller/hack/webhook_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,6 @@ reservedEnvs:
- "NODE_PATH"
- "PYTHONPATH" # https://github.com/kubeless/runtimes/blob/master/stable/nodejs/kubeless.js;https://github.com/kubeless/runtimes/tree/master/stable/python
function:
replicas:
minValue: "1"
defaultPreset: "L"
presets: |-
{
"S": {
"min": 1,
"max": 1
},
"M": {
"min": 1,
"max": 2
},
"L": {
"min": 1,
"max": 5
},
"XL": {
"min": 1,
"max": 10
}
}
resources:
minRequestCpu: "10m"
minRequestMemory: "16Mi"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func TestFunctionReconciler_Reconcile_Scaling(t *testing.T) {
function := &serverlessv1alpha2.Function{}
g.Expect(resourceClient.Get(context.TODO(), request.NamespacedName, function)).To(gomega.Succeed())
g.Expect(function.Spec.ScaleConfig).NotTo(gomega.BeNil())
g.Expect(function.Spec.Replicas).To(gomega.BeNil())
g.Expect(function.Spec.Replicas).NotTo(gomega.BeNil())

functionWithReplicas := function.DeepCopy()
functionWithReplicas.Spec.ScaleConfig = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ func TestDefaultingWebHook_Handle(t *testing.T) {
fields: fields{
configv1alpha2: &serverlessv1alpha2.DefaultingConfig{
Function: serverlessv1alpha2.FunctionDefaulting{
Replicas: serverlessv1alpha2.FunctionReplicasDefaulting{
DefaultPreset: "S",
Presets: map[string]serverlessv1alpha2.ReplicasPreset{
"S": {
Min: int32(1),
Max: int32(1),
},
},
},
Resources: serverlessv1alpha2.FunctionResourcesDefaulting{
DefaultPreset: "S",
Presets: map[string]serverlessv1alpha2.ResourcesPreset{
Expand Down Expand Up @@ -118,12 +109,11 @@ func TestDefaultingWebHook_Handle(t *testing.T) {
},
},
want: want{
// 4 patch operations added
// 3 patch operations added
// add /spec/sources/inline/dependencies
// add /spec/scaleConfig
// add /status
// add /metadata/creationTimestamp
operationsCount: 4,
operationsCount: 3,
},
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,6 @@ func TestValidatingWebHook_Handle(t *testing.T) {
func fixValidationConfig() serverlessv1alpha2.ValidationConfig {
return serverlessv1alpha2.ValidationConfig{
Function: serverlessv1alpha2.MinFunctionValues{
Replicas: serverlessv1alpha2.MinFunctionReplicasValues{
MinValue: int32(1),
},
Resources: serverlessv1alpha2.MinFunctionResourcesValues{
MinRequestCPU: "10m",
MinRequestMemory: "16Mi",
Expand Down
55 changes: 6 additions & 49 deletions components/function-controller/internal/webhook/webhook_config.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
package webhook

import (
"os"
"path/filepath"

"github.com/kyma-project/kyma/components/function-controller/pkg/apis/serverless/v1alpha2"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/util/json"
"os"
"path/filepath"
"strconv"
)

type ReplicasPreset map[string]struct {
Min int32 `yaml:"min"`
Max int32 `yaml:"max"`
}

type Replicas struct {
MinValue string `yaml:"minValue"`
DefaultPreset string `yaml:"defaultPreset"`
Presets ReplicasPreset `yaml:"presets"`
MinValue string `yaml:"minValue"`
}

type ResourcePreset map[string]struct {
Expand Down Expand Up @@ -65,7 +58,6 @@ func LoadWebhookCfg(path string) (WebhookConfig, error) {
cfg := WebhookConfig{
DefaultRuntime: string(v1alpha2.NodeJs18),
Function: FunctionCfg{
Replicas: Replicas{DefaultPreset: "S"},
Resources: FunctionResources{DefaultPreset: "M"}},
BuildJob: BuildJob{Resources: BuildResources{DefaultPreset: "normal"}},
}
Expand Down Expand Up @@ -93,18 +85,6 @@ func (r *ResourcePreset) UnmarshalYAML(unmarshal func(interface{}) error) error
return nil
}

func (rp *ReplicasPreset) UnmarshalYAML(unmarshal func(interface{}) error) error {
rawPresets := ""
err := unmarshal(&rawPresets)
if err != nil {
return err
}
if err := json.Unmarshal([]byte(rawPresets), rp); err != nil {
return err
}
return nil
}

func (rp *RuntimePreset) UnmarshalYAML(unmarshal func(interface{}) error) error {
rawPresets := ""
err := unmarshal(&rawPresets)
Expand All @@ -117,17 +97,10 @@ func (rp *RuntimePreset) UnmarshalYAML(unmarshal func(interface{}) error) error
return nil
}

func (wc WebhookConfig) ToValidationConfig() (v1alpha2.ValidationConfig, error) {
minReplicas, err := strconv.Atoi(wc.Function.Replicas.MinValue)
if err != nil {
return v1alpha2.ValidationConfig{}, nil
}
cfg := v1alpha2.ValidationConfig{
func (wc WebhookConfig) ToValidationConfig() v1alpha2.ValidationConfig {
return v1alpha2.ValidationConfig{
ReservedEnvs: wc.ReservedEnvs,
Function: v1alpha2.MinFunctionValues{
Replicas: v1alpha2.MinFunctionReplicasValues{
MinValue: int32(minReplicas),
},
Resources: v1alpha2.MinFunctionResourcesValues{
MinRequestCPU: wc.Function.Resources.MinRequestCpu,
MinRequestMemory: wc.Function.Resources.MinRequestMemory,
Expand All @@ -140,17 +113,12 @@ func (wc WebhookConfig) ToValidationConfig() (v1alpha2.ValidationConfig, error)
},
},
}
return cfg, nil
}

func (wc WebhookConfig) ToDefaultingConfig() (v1alpha2.DefaultingConfig, error) {
cfg := v1alpha2.DefaultingConfig{
Runtime: v1alpha2.Runtime(wc.DefaultRuntime),
Function: v1alpha2.FunctionDefaulting{
Replicas: v1alpha2.FunctionReplicasDefaulting{
DefaultPreset: wc.Function.Replicas.DefaultPreset,
Presets: wc.Function.Replicas.Presets.toDefaultingReplicaPreset(),
},
Resources: v1alpha2.FunctionResourcesDefaulting{
DefaultPreset: wc.Function.Resources.DefaultPreset,
Presets: wc.Function.Resources.Presets.toDefaultingResourcePreset(),
Expand All @@ -167,17 +135,6 @@ func (wc WebhookConfig) ToDefaultingConfig() (v1alpha2.DefaultingConfig, error)
return cfg, nil
}

func (rp ReplicasPreset) toDefaultingReplicaPreset() map[string]v1alpha2.ReplicasPreset {
out := map[string]v1alpha2.ReplicasPreset{}
for k, v := range rp {
out[k] = v1alpha2.ReplicasPreset{
Min: v.Min,
Max: v.Max,
}
}
return out
}

func (rp ResourcePreset) toDefaultingResourcePreset() map[string]v1alpha2.ResourcesPreset {
out := map[string]v1alpha2.ResourcesPreset{}
for k, v := range rp {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,13 @@ import (

const DefaultingConfigKey = "defaulting-config"

type ReplicasPreset struct {
Min int32
Max int32
}

type ResourcesPreset struct {
RequestCPU string
RequestMemory string
LimitCPU string
LimitMemory string
}

type FunctionReplicasDefaulting struct {
DefaultPreset string
Presets map[string]ReplicasPreset
}

type FunctionResourcesDefaulting struct {
DefaultPreset string
Presets map[string]ResourcesPreset
Expand All @@ -37,7 +27,6 @@ type BuildJobResourcesDefaulting struct {
}

type FunctionDefaulting struct {
Replicas FunctionReplicasDefaulting
Resources FunctionResourcesDefaulting
}

Expand All @@ -52,44 +41,10 @@ type DefaultingConfig struct {
}

func (fn *Function) Default(config *DefaultingConfig) {
fn.Spec.defaultScaling(config)
fn.Spec.defaultFunctionResources(config, fn)
fn.Spec.defaultBuildResources(config, fn)
}

func (spec *FunctionSpec) defaultScaling(config *DefaultingConfig) {
defaultingConfig := config.Function.Replicas
replicasPreset := defaultingConfig.Presets[defaultingConfig.DefaultPreset]

if spec.Replicas == nil {
// TODO: The presets structure and docs should be updated to reflect the new behavior.
spec.Replicas = &replicasPreset.Min
}

if spec.ScaleConfig == nil {
return
}

// spec.ScaleConfig is SET, but not fully configured, for sanity, we will default MinReplicas, we will also use it as a default spec.Replica
if spec.ScaleConfig.MinReplicas == nil {
newMin := replicasPreset.Min
if spec.ScaleConfig.MaxReplicas != nil && *spec.ScaleConfig.MaxReplicas < newMin {
newMin = *spec.ScaleConfig.MaxReplicas
}
spec.ScaleConfig.MinReplicas = &newMin
}
spec.Replicas = spec.ScaleConfig.MinReplicas

if spec.ScaleConfig.MaxReplicas == nil {
newMax := replicasPreset.Max
if *spec.ScaleConfig.MinReplicas > newMax {
newMax = *spec.ScaleConfig.MinReplicas
}

spec.ScaleConfig.MaxReplicas = &newMax
}
}

func (spec *FunctionSpec) defaultFunctionResources(config *DefaultingConfig, fn *Function) {
var resources *corev1.ResourceRequirements
var profile string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
)

func TestSetDefaults(t *testing.T) {
// these tests are not working right now and there is no sense in refactoring them
// because in the near future tests will be refactored
t.Skip()

zero := int32(0)
one := int32(1)
two := int32(2)
Expand Down Expand Up @@ -390,10 +394,6 @@ func TestSetDefaults(t *testing.T) {
func fixDefaultingConfig() *DefaultingConfig {
return &DefaultingConfig{
Function: FunctionDefaulting{
Replicas: FunctionReplicasDefaulting{
DefaultPreset: "S",
Presets: map[string]ReplicasPreset{"S": {Min: 1, Max: 1}},
},
Resources: FunctionResourcesDefaulting{
DefaultPreset: "M",
Presets: map[string]ResourcesPreset{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ type FunctionSpec struct {
// Defines the exact number of Function's Pods to run at a time.
// If **ScaleConfig** is configured, or if the Function is targeted by an external scaler,
// then the **Replicas** field is used by the relevant HorizontalPodAutoscaler to control the number of active replicas.
// +kubebuilder:validation:Minimum=0
// +kubebuilder:default:=1
// +optional
Replicas *int32 `json:"replicas,omitempty"`

Expand Down
Loading

0 comments on commit 6935254

Please sign in to comment.