Skip to content

Commit

Permalink
fix kubebuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
npolshakova committed Feb 3, 2025
1 parent 2ca3af4 commit cec8c6d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,6 @@ spec:
traceableFilter:
type: object
type: object
x-kubernetes-validations:
- message: There must one and only one AccessLogFilter
type set
rule: 1 == (self.statusCodeFilter != null?1:0) +
(self.durationFilter != null?1:0) + (self.notHealthCheckFilter
!= null?1:0) + (self.traceableFilter != null?1:0)
+ (self.runtimeFilter != null?1:0) + (self.headerFilter
!= null?1:0) + (self.responseFlagFilter != null?1:0)
+ (self.grpcStatusFilter != null?1:0) + (self.celFilter
!= null?1:0)
type: array
celFilter:
properties:
Expand Down Expand Up @@ -458,16 +448,6 @@ spec:
traceableFilter:
type: object
type: object
x-kubernetes-validations:
- message: There must one and only one AccessLogFilter
type set
rule: 1 == (self.statusCodeFilter != null?1:0) +
(self.durationFilter != null?1:0) + (self.notHealthCheckFilter
!= null?1:0) + (self.traceableFilter != null?1:0)
+ (self.runtimeFilter != null?1:0) + (self.headerFilter
!= null?1:0) + (self.responseFlagFilter != null?1:0)
+ (self.grpcStatusFilter != null?1:0) + (self.celFilter
!= null?1:0)
type: array
responseFlagFilter:
properties:
Expand Down Expand Up @@ -524,23 +504,6 @@ spec:
traceableFilter:
type: object
type: object
x-kubernetes-validations:
- message: There must one and only one AccessLogFilter type
set
rule: 1 == (self.statusCodeFilter != null?1:0) + (self.durationFilter
!= null?1:0) + (self.notHealthCheckFilter != null?1:0)
+ (self.traceableFilter != null?1:0) + (self.runtimeFilter
!= null?1:0) + (self.andFilter.items > 0?1:0) + (self.orFilter.items
> 0?1:0) + (self.headerFilter != null?1:0) + (self.responseFlagFilter
!= null?1:0) + (self.grpcStatusFilter != null?1:0)
- message: There must one and only one AccessLogFilter type
set
rule: 1 == (self.statusCodeFilter != null?1:0) + (self.durationFilter
!= null?1:0) + (self.notHealthCheckFilter != null?1:0)
+ (self.traceableFilter != null?1:0) + (self.runtimeFilter
!= null?1:0) + (self.headerFilter != null?1:0) + (self.responseFlagFilter
!= null?1:0) + (self.grpcStatusFilter != null?1:0) +
(self.celFilter != null?1:0)
grpcService:
properties:
additionalRequestHeadersToLog:
Expand Down
2 changes: 0 additions & 2 deletions projects/gateway2/api/v1alpha1/http_listener_policy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ type GrpcService struct {
}

// AccessLogFilter represents the top-level filter structure.
// +kubebuilder:validation:XValidation:message="There must one and only one AccessLogFilter type set",rule="1 == (self.statusCodeFilter != null?1:0) + (self.durationFilter != null?1:0) + (self.notHealthCheckFilter != null?1:0) + (self.traceableFilter != null?1:0) + (self.runtimeFilter != null?1:0) + (self.andFilter.items > 0?1:0) + (self.orFilter.items > 0?1:0) + (self.headerFilter != null?1:0) + (self.responseFlagFilter != null?1:0) + (self.grpcStatusFilter != null?1:0)"
type AccessLogFilter struct {
*FilterType `json:",inline"` // embedded to allow for validation
// +kube:validation:MinItems=2
Expand All @@ -106,7 +105,6 @@ type AccessLogFilter struct {
}

// FilterType represents the type of filter to apply (only one of these should be set).
// +kubebuilder:validation:XValidation:message="There must one and only one AccessLogFilter type set",rule="1 == (self.statusCodeFilter != null?1:0) + (self.durationFilter != null?1:0) + (self.notHealthCheckFilter != null?1:0) + (self.traceableFilter != null?1:0) + (self.runtimeFilter != null?1:0) + (self.headerFilter != null?1:0) + (self.responseFlagFilter != null?1:0) + (self.grpcStatusFilter != null?1:0) + (self.celFilter != null?1:0)"
type FilterType struct {
StatusCodeFilter *StatusCodeFilter `json:"statusCodeFilter,omitempty"`
DurationFilter *DurationFilter `json:"durationFilter,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ func convertAccessLogConfig(ctx context.Context, config *v1alpha1.AccessLoggingC
filters := make([]*v33.AccessLogFilter, len(log.Filter.OrFilter))
for _, filter := range log.Filter.OrFilter {
// Process OrFilter
cfg := translateFilter(logger, filter)
cfg, err := translateFilter(logger, filter)
if err != nil {
// exit and report error on resource
return nil, err
}
filters = append(filters, cfg)
}
accessLogCfg.GetFilter().FilterSpecifier = &v33.AccessLogFilter_OrFilter{
Expand All @@ -124,7 +128,11 @@ func convertAccessLogConfig(ctx context.Context, config *v1alpha1.AccessLoggingC
filters := make([]*v33.AccessLogFilter, len(log.Filter.OrFilter))
for _, filter := range log.Filter.OrFilter {
// Process OrFilter
cfg := translateFilter(logger, filter)
cfg, err := translateFilter(logger, filter)
if err != nil {
// exit and report error on resource
return nil, err
}
filters = append(filters, cfg)
}
accessLogCfg.GetFilter().FilterSpecifier = &v33.AccessLogFilter_AndFilter{
Expand All @@ -133,7 +141,11 @@ func convertAccessLogConfig(ctx context.Context, config *v1alpha1.AccessLoggingC
},
}
} else if log.Filter.FilterType != nil {
accessLogCfg.Filter = translateFilter(logger, log.Filter.FilterType)
accessLogCfg.Filter, err = translateFilter(logger, log.Filter.FilterType)
if err != nil {
// exit and report error on resource
return nil, err
}
}
}
results = append(results, &accessLogCfg)
Expand All @@ -142,7 +154,48 @@ func convertAccessLogConfig(ctx context.Context, config *v1alpha1.AccessLoggingC
return results, nil
}

func translateFilter(logger *zap.Logger, filter *v1alpha1.FilterType) *v33.AccessLogFilter {
func validateFilter(filter *v1alpha1.FilterType) error {
count := 0
if filter.StatusCodeFilter != nil {
count++
}
if filter.DurationFilter != nil {
count++
}
if filter.NotHealthCheckFilter != nil {
count++
}
if filter.TraceableFilter != nil {
count++
}
if filter.RuntimeFilter != nil {
count++
}
if filter.HeaderFilter != nil {
count++
}
if filter.ResponseFlagFilter != nil {
count++
}
if filter.GrpcStatusFilter != nil {
count++
}
if filter.CELFilter != nil {
count++
}

if count != 1 {
return fmt.Errorf("exactly one AccessLogFilter type must be set")
}

return nil
}

func translateFilter(logger *zap.Logger, filter *v1alpha1.FilterType) (*v33.AccessLogFilter, error) {
if err := validateFilter(filter); err != nil {
return nil, err
}

alCfg := &v33.AccessLogFilter{}
if filter.StatusCodeFilter != nil {
alCfg = &v33.AccessLogFilter{
Expand Down Expand Up @@ -278,7 +331,7 @@ func translateFilter(logger *zap.Logger, filter *v1alpha1.FilterType) *v33.Acces
},
}
}
return alCfg
return alCfg, nil
}

func convertJsonFormat(jsonFormat *runtime.RawExtension) *structpb.Struct {
Expand Down

0 comments on commit cec8c6d

Please sign in to comment.