Skip to content

Commit

Permalink
Make decoding of Raw to configs consistent (#209)
Browse files Browse the repository at this point in the history
* make decoding consistent

* remove decodeRsyslogRelpConfig

* remove unnecessary line

Co-authored-by: Plamen Kokanov <[email protected]>

* cosmetic nits

---------

Co-authored-by: Plamen Kokanov <[email protected]>
  • Loading branch information
RadaBDimitrova and plkokanov authored Dec 13, 2024
1 parent 02d7198 commit 3d13281
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 24 deletions.
28 changes: 9 additions & 19 deletions pkg/admission/validator/shoot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/rsyslogrelp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/lifecycle/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook/operatingsystemconfig/auditd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook/operatingsystemconfig/ensurer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 3d13281

Please sign in to comment.