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

Clean up defaulting webhook code #18164

Merged
merged 3 commits into from
Sep 21, 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
6 changes: 0 additions & 6 deletions components/function-controller/cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,8 @@ func main() {
whs.CertName = resources.CertFile
whs.KeyName = resources.KeyFile

defaultCfg, err := webhookCfg.ToDefaultingConfig()
if err != nil {
setupLog.Error(err, "while creating of defaulting configuration")
os.Exit(1)
}
whs.Register(resources.FunctionDefaultingWebhookPath, &ctrlwebhook.Admission{
Handler: webhook.NewDefaultingWebhook(
&defaultCfg,
mgr.GetClient(),
logWithCtx.Named("defaulting-webhook")),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ import (
)

type DefaultingWebHook struct {
configAlphaV2 *serverlessv1alpha2.DefaultingConfig
client ctrlclient.Client
decoder *admission.Decoder
log *zap.SugaredLogger
client ctrlclient.Client
decoder *admission.Decoder
log *zap.SugaredLogger
}

func NewDefaultingWebhook(configV1Alpha2 *serverlessv1alpha2.DefaultingConfig, client ctrlclient.Client, log *zap.SugaredLogger) *DefaultingWebHook {
func NewDefaultingWebhook(client ctrlclient.Client, log *zap.SugaredLogger) *DefaultingWebHook {
return &DefaultingWebHook{
configAlphaV2: configV1Alpha2,
client: client,
log: log,
client: client,
log: log,
}
}

Expand All @@ -39,11 +37,6 @@ func (w *DefaultingWebHook) Handle(_ context.Context, req admission.Request) adm
log.Debug("defaulting finished for function")
return res
}
if req.Kind.Kind == "GitRepository" {
res := w.handleGitRepoDefaulting()
log.Debug("defaulting finished for gitrepository")
return res
}

log.Debug("request object invalid kind")
return admission.Errored(http.StatusBadRequest, fmt.Errorf("invalid kind: %v", req.Kind.Kind))
Expand All @@ -63,7 +56,6 @@ func (w *DefaultingWebHook) handleFunctionDefaulting(req admission.Request) admi
if err := w.decoder.Decode(req, fn); err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
fn.Default(w.configAlphaV2)
f = fn
}
default:
Expand All @@ -76,7 +68,3 @@ func (w *DefaultingWebHook) handleFunctionDefaulting(req admission.Request) admi
}
return admission.PatchResponseFromRaw(req.Object.Raw, fBytes)
}

func (w *DefaultingWebHook) handleGitRepoDefaulting() admission.Response {
return admission.Allowed("")
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,20 @@ import (
"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"
)

type Replicas struct {
MinValue string `yaml:"minValue"`
}

type ResourcePreset map[string]struct {
RequestCpu string `yaml:"requestCpu"`
RequestMemory string `yaml:"requestMemory"`
LimitMemory string `yaml:"limitMemory"`
LimitCpu string `yaml:"limitCpu"`
}

type RuntimePreset map[string]string

type FunctionResources struct {
MinRequestCpu string `yaml:"minRequestCpu"`
MinRequestMemory string `yaml:"minRequestMemory"`
DefaultPreset string `yaml:"defaultPreset"`
Presets ResourcePreset `yaml:"presets"`
RuntimePresets RuntimePreset `yaml:"runtimePresets"`
MinRequestCpu string `yaml:"minRequestCpu"`
MinRequestMemory string `yaml:"minRequestMemory"`
}

type FunctionCfg struct {
Replicas Replicas `yaml:"replicas"`
Resources FunctionResources `yaml:"resources"`
}

type BuildResources struct {
MinRequestCpu string `yaml:"minRequestCpu"`
MinRequestMemory string `yaml:"minRequestMemory"`
DefaultPreset string `yaml:"defaultPreset"`
Presets ResourcePreset `yaml:"presets"`
MinRequestCpu string `yaml:"minRequestCpu"`
MinRequestMemory string `yaml:"minRequestMemory"`
}

type BuildJob struct {
Expand All @@ -55,12 +35,7 @@ type WebhookConfig struct {
}

func LoadWebhookCfg(path string) (WebhookConfig, error) {
cfg := WebhookConfig{
DefaultRuntime: string(v1alpha2.NodeJs18),
Function: FunctionCfg{
Resources: FunctionResources{DefaultPreset: "M"}},
BuildJob: BuildJob{Resources: BuildResources{DefaultPreset: "normal"}},
}
cfg := WebhookConfig{DefaultRuntime: string(v1alpha2.NodeJs18)}

cleanPath := filepath.Clean(path)
yamlFile, err := os.ReadFile(cleanPath)
Expand All @@ -72,31 +47,6 @@ func LoadWebhookCfg(path string) (WebhookConfig, error) {
return cfg, errors.Wrap(err, "while unmarshalling yaml")
}

func (r *ResourcePreset) UnmarshalYAML(unmarshal func(interface{}) error) error {
rawPresets := ""
err := unmarshal(&rawPresets)
if err != nil {
return err
}

if err := json.Unmarshal([]byte(rawPresets), r); err != nil {
return err
}
return nil
}

func (rp *RuntimePreset) 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 (wc WebhookConfig) ToValidationConfig() v1alpha2.ValidationConfig {
return v1alpha2.ValidationConfig{
ReservedEnvs: wc.ReservedEnvs,
Expand All @@ -114,36 +64,3 @@ func (wc WebhookConfig) ToValidationConfig() v1alpha2.ValidationConfig {
},
}
}

func (wc WebhookConfig) ToDefaultingConfig() (v1alpha2.DefaultingConfig, error) {
cfg := v1alpha2.DefaultingConfig{
Runtime: v1alpha2.Runtime(wc.DefaultRuntime),
Function: v1alpha2.FunctionDefaulting{
Resources: v1alpha2.FunctionResourcesDefaulting{
DefaultPreset: wc.Function.Resources.DefaultPreset,
Presets: wc.Function.Resources.Presets.toDefaultingResourcePreset(),
RuntimePresets: wc.Function.Resources.RuntimePresets,
},
},
BuildJob: v1alpha2.BuildJobDefaulting{
Resources: v1alpha2.BuildJobResourcesDefaulting{
DefaultPreset: wc.BuildJob.Resources.DefaultPreset,
Presets: wc.BuildJob.Resources.Presets.toDefaultingResourcePreset(),
},
},
}
return cfg, nil
}

func (rp ResourcePreset) toDefaultingResourcePreset() map[string]v1alpha2.ResourcesPreset {
out := map[string]v1alpha2.ResourcesPreset{}
for k, v := range rp {
out[k] = v1alpha2.ResourcesPreset{
RequestCPU: v.RequestCpu,
RequestMemory: v.RequestMemory,
LimitCPU: v.LimitCpu,
LimitMemory: v.LimitMemory,
}
}
return out
}
Loading