Skip to content

Commit

Permalink
follow golang name convention: change Cors to CORS
Browse files Browse the repository at this point in the history
Signed-off-by: huabing zhao <[email protected]>
  • Loading branch information
zhaohuabing committed Oct 25, 2023
1 parent 1856fb1 commit ec1822c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 57 deletions.
14 changes: 7 additions & 7 deletions internal/gatewayapi/securitypolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func resolveSecurityPolicyRouteTargetRef(policy *egv1a1.SecurityPolicy, routes m

func (t *Translator) translateSecurityPolicyForRoute(policy *egv1a1.SecurityPolicy, route RouteContext, xdsIR XdsIRMap) {
// Build IR
var cors *ir.Cors
var cors *ir.CORS
if policy.Spec.CORS != nil {
cors = t.buildCORS(policy)
}
Expand All @@ -234,7 +234,7 @@ func (t *Translator) translateSecurityPolicyForRoute(policy *egv1a1.SecurityPoli
for _, r := range http.Routes {
// Apply if there is a match
if strings.HasPrefix(r.Name, prefix) {
r.Cors = cors
r.CORS = cors
}
}
}
Expand All @@ -244,7 +244,7 @@ func (t *Translator) translateSecurityPolicyForRoute(policy *egv1a1.SecurityPoli

func (t *Translator) translateSecurityPolicyForGateway(policy *egv1a1.SecurityPolicy, gateway *GatewayContext, xdsIR XdsIRMap) {
// Build IR
var cors *ir.Cors
var cors *ir.CORS
if policy.Spec.CORS != nil {
cors = t.buildCORS(policy)
}
Expand All @@ -259,15 +259,15 @@ func (t *Translator) translateSecurityPolicyForGateway(policy *egv1a1.SecurityPo
for _, http := range ir.HTTP {
for _, r := range http.Routes {
// Apply if not already set
if r.Cors == nil {
r.Cors = cors
if r.CORS == nil {
r.CORS = cors
}
}
}

}

func (t *Translator) buildCORS(policy *egv1a1.SecurityPolicy) *ir.Cors {
func (t *Translator) buildCORS(policy *egv1a1.SecurityPolicy) *ir.CORS {
var allowOrigins []*ir.StringMatch

for _, origin := range policy.Spec.CORS.AllowOrigins {
Expand Down Expand Up @@ -308,7 +308,7 @@ func (t *Translator) buildCORS(policy *egv1a1.SecurityPolicy) *ir.Cors {
}
}

return &ir.Cors{
return &ir.CORS{
AllowOrigins: allowOrigins,
AllowMethods: policy.Spec.CORS.AllowMethods,
AllowHeaders: policy.Spec.CORS.AllowHeaders,
Expand Down
8 changes: 4 additions & 4 deletions internal/ir/xds.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ type HTTPRoute struct {
Timeout *metav1.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"`
// load balancer policy to use when routing to the backend endpoints.
LoadBalancer *LoadBalancer `json:"loadBalancer,omitempty" yaml:"loadBalancer,omitempty"`
// Cors policy for the route.
Cors *Cors `json:"cors,omitempty" yaml:"cors,omitempty"`
// CORS policy for the route.
CORS *CORS `json:"cors,omitempty" yaml:"cors,omitempty"`
// ExtensionRefs holds unstructured resources that were introduced by an extension and used on the HTTPRoute as extensionRef filters
ExtensionRefs []*UnstructuredRef `json:"extensionRefs,omitempty" yaml:"extensionRefs,omitempty"`
}
Expand Down Expand Up @@ -314,10 +314,10 @@ type JwtRequestAuthentication struct {
Providers []egv1a1.JwtAuthenticationFilterProvider `json:"providers,omitempty" yaml:"providers,omitempty"`
}

// Cors holds the Cross-Origin Resource Sharing (CORS) policy for the route.
// CORS holds the Cross-Origin Resource Sharing (CORS) policy for the route.
//
// +k8s:deepcopy-gen=true
type Cors struct {
type CORS struct {
// AllowOrigins defines the origins that are allowed to make requests.
AllowOrigins []*StringMatch `json:"allowOrigins,omitempty" yaml:"allowOrigins,omitempty"`
// AllowMethods defines the methods that are allowed to make requests.
Expand Down
54 changes: 27 additions & 27 deletions internal/ir/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 17 additions & 17 deletions internal/xds/translator/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"github.com/envoyproxy/gateway/internal/ir"
)

// patchHCMWithCorsFilter builds and appends the Cors Filter to the HTTP
// patchHCMWithCORSFilter builds and appends the CORS Filter to the HTTP
// Connection Manager if applicable.
func patchHCMWithCorsFilter(mgr *hcmv3.HttpConnectionManager, irListener *ir.HTTPListener) error {
func patchHCMWithCORSFilter(mgr *hcmv3.HttpConnectionManager, irListener *ir.HTTPListener) error {
if mgr == nil {
return errors.New("hcm is nil")
}
Expand All @@ -33,7 +33,7 @@ func patchHCMWithCorsFilter(mgr *hcmv3.HttpConnectionManager, irListener *ir.HTT
return errors.New("ir listener is nil")
}

if !listenerContainsCors(irListener) {
if !listenerContainsCORS(irListener) {
return nil
}

Expand All @@ -44,7 +44,7 @@ func patchHCMWithCorsFilter(mgr *hcmv3.HttpConnectionManager, irListener *ir.HTT
}
}

corsFilter, err := buildHCMCorsFilter()
corsFilter, err := buildHCMCORSFilter()
if err != nil {
return err
}
Expand All @@ -55,8 +55,8 @@ func patchHCMWithCorsFilter(mgr *hcmv3.HttpConnectionManager, irListener *ir.HTT
return nil
}

// buildHCMCorsFilter returns a Cors filter from the provided IR listener.
func buildHCMCorsFilter() (*hcmv3.HttpFilter, error) {
// buildHCMCORSFilter returns a CORS filter from the provided IR listener.
func buildHCMCORSFilter() (*hcmv3.HttpFilter, error) {
corsProto := &corsv3.Cors{}

corsAny, err := anypb.New(corsProto)
Expand All @@ -72,32 +72,32 @@ func buildHCMCorsFilter() (*hcmv3.HttpFilter, error) {
}, nil
}

// listenerContainsCors returns true if the provided listener has Cors
// listenerContainsCORS returns true if the provided listener has CORS
// policies attached to its routes.
func listenerContainsCors(irListener *ir.HTTPListener) bool {
func listenerContainsCORS(irListener *ir.HTTPListener) bool {
if irListener == nil {
return false
}

for _, route := range irListener.Routes {
if route.Cors != nil {
if route.CORS != nil {
return true
}
}

return false
}

// patchRouteWithCorsConfig patches the provided route with the Cors config if
// patchRouteWithCORSConfig patches the provided route with the CORS config if
// applicable.
func patchRouteWithCorsConfig(route *routev3.Route, irRoute *ir.HTTPRoute) error {
func patchRouteWithCORSConfig(route *routev3.Route, irRoute *ir.HTTPRoute) error {
if route == nil {
return errors.New("xds route is nil")
}
if irRoute == nil {
return errors.New("ir route is nil")
}
if irRoute.Cors == nil {
if irRoute.CORS == nil {
return nil
}

Expand All @@ -119,14 +119,14 @@ func patchRouteWithCorsConfig(route *routev3.Route, irRoute *ir.HTTPRoute) error

//nolint:gocritic

for _, origin := range irRoute.Cors.AllowOrigins {
for _, origin := range irRoute.CORS.AllowOrigins {
allowOrigins = append(allowOrigins, buildXdsStringMatcher(origin))
}

allowMethods = strings.Join(irRoute.Cors.AllowMethods, ", ")
allowHeaders = strings.Join(irRoute.Cors.AllowHeaders, ", ")
exposeHeaders = strings.Join(irRoute.Cors.ExposeHeaders, ", ")
maxAge = strconv.Itoa(int(irRoute.Cors.MaxAge.Seconds()))
allowMethods = strings.Join(irRoute.CORS.AllowMethods, ", ")
allowHeaders = strings.Join(irRoute.CORS.AllowHeaders, ", ")
exposeHeaders = strings.Join(irRoute.CORS.ExposeHeaders, ", ")
maxAge = strconv.Itoa(int(irRoute.CORS.MaxAge.Seconds()))

routeCfgProto := &corsv3.CorsPolicy{
AllowOriginStringMatch: allowOrigins,
Expand Down
4 changes: 2 additions & 2 deletions internal/xds/translator/httpfilters.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (t *Translator) patchHCMWithFilters(
}

// Add the cors filter, if needed
if err := patchHCMWithCorsFilter(mgr, irListener); err != nil {
if err := patchHCMWithCORSFilter(mgr, irListener); err != nil {
return err
}

Expand Down Expand Up @@ -135,7 +135,7 @@ func patchRouteWithFilters(
}

// Add the cors per route config to the route, if needed.
if err := patchRouteWithCorsConfig(route, irRoute); err != nil {
if err := patchRouteWithCORSConfig(route, irRoute); err != nil {
return err
}
return nil
Expand Down

0 comments on commit ec1822c

Please sign in to comment.