From 3d132814e6a4fb02148326f3be2c9663ec5cea48 Mon Sep 17 00:00:00 2001 From: Rada Dimitrova <117522044+RadaBDimitrova@users.noreply.github.com> Date: Fri, 13 Dec 2024 13:18:20 +0200 Subject: [PATCH] Make decoding of `Raw` to configs consistent (#209) * make decoding consistent * remove decodeRsyslogRelpConfig * remove unnecessary line Co-authored-by: Plamen Kokanov <35485709+plkokanov@users.noreply.github.com> * cosmetic nits --------- Co-authored-by: Plamen Kokanov <35485709+plkokanov@users.noreply.github.com> --- pkg/admission/validator/shoot.go | 28 +++++++------------- pkg/cmd/rsyslogrelp/options.go | 3 +-- pkg/controller/lifecycle/actuator.go | 2 +- pkg/webhook/operatingsystemconfig/auditd.go | 2 +- pkg/webhook/operatingsystemconfig/ensurer.go | 2 +- 5 files changed, 13 insertions(+), 24 deletions(-) diff --git a/pkg/admission/validator/shoot.go b/pkg/admission/validator/shoot.go index b042c0b3..0fc9f6e6 100644 --- a/pkg/admission/validator/shoot.go +++ b/pkg/admission/validator/shoot.go @@ -59,12 +59,16 @@ func (s *shoot) Validate(ctx context.Context, new, _ client.Object) error { } providerConfigPath := fldPath.Child("providerConfig") - rsyslogRelpConfig, err := decodeRsyslogRelpConfig(s.decoder, ext.ProviderConfig, providerConfigPath) - if err != nil { - return err + if ext.ProviderConfig == nil { + return field.Required(providerConfigPath, "Rsyslog relp configuration is required when using gardener-extension-shoot-rsyslog-relp") + } + + rsyslogRelpConfig := &rsyslog.RsyslogRelpConfig{} + if err := runtime.DecodeInto(s.decoder, ext.ProviderConfig.Raw, rsyslogRelpConfig); err != nil { + return fmt.Errorf("could not decode rsyslog relp configuration: %w", err) } - if err = validation.ValidateRsyslogRelpConfig(rsyslogRelpConfig, providerConfigPath).ToAggregate(); err != nil { + if err := validation.ValidateRsyslogRelpConfig(rsyslogRelpConfig, providerConfigPath).ToAggregate(); err != nil { return err } @@ -165,8 +169,7 @@ func validateAuditConfigMap(decoder runtime.Decoder, configMap *corev1.ConfigMap auditdConfig := &rsyslog.Auditd{} - _, _, err := decoder.Decode([]byte(auditdConfigString), nil, auditdConfig) - if err != nil { + if err := runtime.DecodeInto(decoder, []byte(auditdConfigString), auditdConfig); err != nil { return fmt.Errorf("could not decode 'data.%s' field of configMap %s: %w", constants.AuditdConfigMapDataKey, configMapKey.String(), err) } if err := validation.ValidateAuditd(auditdConfig).ToAggregate(); err != nil { @@ -187,19 +190,6 @@ func isExtensionEnabled(ext *core.Extension) bool { return true } -func decodeRsyslogRelpConfig(decoder runtime.Decoder, config *runtime.RawExtension, fldPath *field.Path) (*rsyslog.RsyslogRelpConfig, error) { - if config == nil { - return nil, field.Required(fldPath, "Rsyslog relp configuration is required when using gardener-extension-shoot-rsyslog-relp") - } - - rsyslogRelpConfig := &rsyslog.RsyslogRelpConfig{} - if err := runtime.DecodeInto(decoder, config.Raw, rsyslogRelpConfig); err != nil { - return nil, fmt.Errorf("could not decode rsyslog relp configuration: %w", err) - } - - return rsyslogRelpConfig, nil -} - func getReferencedResourceName(shoot *core.Shoot, resourceKind, resourceName string) (string, error) { if shoot != nil { for _, ref := range shoot.Spec.Resources { diff --git a/pkg/cmd/rsyslogrelp/options.go b/pkg/cmd/rsyslogrelp/options.go index 82be108c..4578bc68 100644 --- a/pkg/cmd/rsyslogrelp/options.go +++ b/pkg/cmd/rsyslogrelp/options.go @@ -59,8 +59,7 @@ func (o *Options) Complete() error { } configuration := apisconfig.Configuration{} - _, _, err = decoder.Decode(data, nil, &configuration) - if err != nil { + if err = runtime.DecodeInto(decoder, data, &configuration); err != nil { return err } diff --git a/pkg/controller/lifecycle/actuator.go b/pkg/controller/lifecycle/actuator.go index 3c1ce271..e8aa3f4c 100644 --- a/pkg/controller/lifecycle/actuator.go +++ b/pkg/controller/lifecycle/actuator.go @@ -58,7 +58,7 @@ func (a *actuator) Reconcile(ctx context.Context, _ logr.Logger, ex *extensionsv namespace := ex.GetNamespace() rsyslogRelpConfig := &api.RsyslogRelpConfig{} - if _, _, err := a.decoder.Decode(ex.Spec.ProviderConfig.Raw, nil, rsyslogRelpConfig); err != nil { + if err := runtime.DecodeInto(a.decoder, ex.Spec.ProviderConfig.Raw, rsyslogRelpConfig); err != nil { return fmt.Errorf("failed to decode provider config: %w", err) } diff --git a/pkg/webhook/operatingsystemconfig/auditd.go b/pkg/webhook/operatingsystemconfig/auditd.go index b822e43a..4ef23dc7 100644 --- a/pkg/webhook/operatingsystemconfig/auditd.go +++ b/pkg/webhook/operatingsystemconfig/auditd.go @@ -72,7 +72,7 @@ func getAuditConfigFromConfigMap(ctx context.Context, c client.Client, decoder r } auditdConfig := &rsyslog.Auditd{} - _, _, err := decoder.Decode([]byte(auditdConfigString), nil, auditdConfig) + err := runtime.DecodeInto(decoder, []byte(auditdConfigString), auditdConfig) if err != nil { return nil, err } diff --git a/pkg/webhook/operatingsystemconfig/ensurer.go b/pkg/webhook/operatingsystemconfig/ensurer.go index f8ff3bc8..c8138a12 100644 --- a/pkg/webhook/operatingsystemconfig/ensurer.go +++ b/pkg/webhook/operatingsystemconfig/ensurer.go @@ -65,7 +65,7 @@ func (e *ensurer) EnsureAdditionalFiles(ctx context.Context, gctx gcontext.Garde shootRsyslogRelpConfig := &rsyslog.RsyslogRelpConfig{} if extension.Spec.ProviderConfig != nil { - if _, _, err := e.decoder.Decode(extension.Spec.ProviderConfig.Raw, nil, shootRsyslogRelpConfig); err != nil { + if err := runtime.DecodeInto(e.decoder, extension.Spec.ProviderConfig.Raw, shootRsyslogRelpConfig); err != nil { return fmt.Errorf("failed to decode provider config: %w", err) } }