Skip to content

Commit

Permalink
chore: add validation to error out early for accessLog
Browse files Browse the repository at this point in the history
  • Loading branch information
ardikabs committed Aug 31, 2023
1 parent 51d3b99 commit 87c955b
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
57 changes: 57 additions & 0 deletions api/config/v1alpha1/validation/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ func validateEnvoyProxySpec(spec *egcfgv1a1.EnvoyProxySpec) error {
errs = append(errs, err)
}
}

validateProxyTelemetryErrs := validateProxyTelemetry(spec)
if len(validateProxyTelemetryErrs) != 0 {
errs = append(errs, validateProxyTelemetryErrs...)
}

return utilerrors.NewAggregate(errs)
}

Expand Down Expand Up @@ -156,3 +162,54 @@ func validateBootstrap(boostrapConfig *egcfgv1a1.ProxyBootstrap) error {

return nil
}

func validateProxyTelemetry(spec *egcfgv1a1.EnvoyProxySpec) []error {
var errs []error

if spec != nil && spec.Telemetry.AccessLog != nil {
accessLogErrs := validateProxyAccessLog(spec.Telemetry.AccessLog)
if len(accessLogErrs) > 0 {
errs = append(errs, accessLogErrs...)
}
}

return errs
}

func validateProxyAccessLog(accessLog *egcfgv1a1.ProxyAccessLog) []error {
if accessLog.Disable {
return nil
}

Check warning on line 182 in api/config/v1alpha1/validation/validate.go

View check run for this annotation

Codecov / codecov/patch

api/config/v1alpha1/validation/validate.go#L181-L182

Added lines #L181 - L182 were not covered by tests

var errs []error

for _, setting := range accessLog.Settings {
switch setting.Format.Type {
case egcfgv1a1.ProxyAccessLogFormatTypeText:
if setting.Format.Text == nil {
err := fmt.Errorf("unable to configure access log when using Text format but \"text\" field being empty")
errs = append(errs, err)
}
case egcfgv1a1.ProxyAccessLogFormatTypeJSON:
if setting.Format.JSON == nil {
err := fmt.Errorf("unable to configure access log when using JSON format but \"json\" field being empty")
errs = append(errs, err)
}

Check warning on line 197 in api/config/v1alpha1/validation/validate.go

View check run for this annotation

Codecov / codecov/patch

api/config/v1alpha1/validation/validate.go#L193-L197

Added lines #L193 - L197 were not covered by tests
}

for _, sink := range setting.Sinks {
switch sink.Type {
case egcfgv1a1.ProxyAccessLogSinkTypeFile:
if sink.File == nil {
err := fmt.Errorf("unable to configure access log when using File sink type but \"file\" field being empty")
errs = append(errs, err)
}
case egcfgv1a1.ProxyAccessLogSinkTypeOpenTelemetry:
err := fmt.Errorf("unable to configure access log when using OpenTelemetry sink type but \"openTelemetry\" field being empty")
errs = append(errs, err)

Check warning on line 209 in api/config/v1alpha1/validation/validate.go

View check run for this annotation

Codecov / codecov/patch

api/config/v1alpha1/validation/validate.go#L207-L209

Added lines #L207 - L209 were not covered by tests
}
}
}

return errs
}
52 changes: 52 additions & 0 deletions api/config/v1alpha1/validation/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,58 @@ func TestValidateEnvoyProxy(t *testing.T) {
},
expected: false,
},
{
name: "should invalid when accesslog enabled using Text format, but `text` field being empty",
proxy: &egcfgv1a1.EnvoyProxy{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "test",
},
Spec: egcfgv1a1.EnvoyProxySpec{
Telemetry: egcfgv1a1.ProxyTelemetry{
AccessLog: &egcfgv1a1.ProxyAccessLog{
Settings: []egcfgv1a1.ProxyAccessLogSetting{
{
Format: egcfgv1a1.ProxyAccessLogFormat{
Type: egcfgv1a1.ProxyAccessLogFormatTypeText,
},
},
},
},
},
},
},
expected: false,
},
{
name: "should invalid when accesslog enabled using File sink, but `file` field being empty",
proxy: &egcfgv1a1.EnvoyProxy{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "test",
},
Spec: egcfgv1a1.EnvoyProxySpec{
Telemetry: egcfgv1a1.ProxyTelemetry{
AccessLog: &egcfgv1a1.ProxyAccessLog{
Settings: []egcfgv1a1.ProxyAccessLogSetting{
{
Format: egcfgv1a1.ProxyAccessLogFormat{
Type: egcfgv1a1.ProxyAccessLogFormatTypeText,
Text: pointer.String("[%START_TIME%]"),
},
Sinks: []egcfgv1a1.ProxyAccessLogSink{
{
Type: egcfgv1a1.ProxyAccessLogSinkTypeFile,
},
},
},
},
},
},
},
},
expected: false,
},
}

for i := range testCases {
Expand Down

0 comments on commit 87c955b

Please sign in to comment.