Repository: traefik/traefik
File: pkg/provider/kubernetes/gateway/kubernetes.go (Go)

Code before:
		return nil, fmt.Errorf("invalid listener protocol %s", listener.Protocol)
	}

	for i, filter := range filters {
		if filter.Type != v1alpha2.HTTPRouteFilterRequestRedirect {
			// As per the spec:
			// https://gateway-api.sigs.k8s.io/api-types/httproute/#filters-optional
			// In all cases where incompatible or unsupported filters are
			// specified, implementations MUST add a warning condition to
			// status.
			return nil, fmt.Errorf("unsupported filter %s", filter.Type)
		}

		middleware, err := createRedirectSchemeMiddleware(listenerScheme, filter.RequestRedirect)
		if err != nil {
			return nil, err
		}

		middlewareName := provider.Normalize(fmt.Sprintf("%s-%s-%d", prefix, strings.ToLower(string(filter.Type)), i))
		middlewares[middlewareName] = middleware
	}

	return middlewares, nil
}

func createRedirectSchemeMiddleware(scheme string, filter *v1alpha2.HTTPRequestRedirectFilter) (*dynamic.Middleware, error) {
	// Use the HTTPRequestRedirectFilter scheme if defined.
	filterScheme := scheme
	if filter.Scheme != nil {
		filterScheme = *filter.Scheme
	}

	if filterScheme != "http" && filterScheme != "https" {
		return nil, fmt.Errorf("invalid scheme %s", filterScheme)
	}

	statusCode := http.StatusFound
	if filter.StatusCode != nil {
		statusCode = *filter.StatusCode
	}

	if statusCode != http.StatusMovedPermanently && statusCode != http.StatusFound {
		return nil, fmt.Errorf("invalid status code %d", statusCode)
	}

	port := "${port}"
	if filter.Port != nil {
		port = fmt.Sprintf(":%d", *filter.Port)
	}

	hostname := "${hostname}"

Review comment (suggestion):
```suggestion
func createRedirectRegexMiddleware(scheme string, filter *v1alpha2.HTTPRequestRedirectFilter) (*dynamic.Middleware, error) {
```

Code after:
	}

	return wrrSvc, services, nil
}

func loadMiddlewares(listener v1alpha2.Listener, prefix string, filters []v1alpha2.HTTPRouteFilter) (map[string]*dynamic.Middleware, error) {
	middlewares := make(map[string]*dynamic.Middleware)

	// The spec allows for an empty string in which case we should use the
	// scheme of the request which in this case is the listener scheme.
	var listenerScheme string
	switch listener.Protocol {
	case v1alpha2.HTTPProtocolType:
		listenerScheme = "http"
	case v1alpha2.HTTPSProtocolType:
		listenerScheme = "https"
	default:
		return nil, fmt.Errorf("invalid listener protocol %s", listener.Protocol)
	}

	for i, filter := range filters {
		var middleware *dynamic.Middleware
		switch filter.Type {
		case v1alpha2.HTTPRouteFilterRequestRedirect:
			var err error
			middleware, err = createRedirectRegexMiddleware(listenerScheme, filter.RequestRedirect)
			if err != nil {
				return nil, fmt.Errorf("creating RedirectRegex middleware: %w", err)
			}
		default:
			// As per the spec:
			// https://gateway-api.sigs.k8s.io/api-types/httproute/#filters-optional
			// In all cases where incompatible or unsupported filters are
			// specified, implementations MUST add a warning condition to
			// status.
			return nil, fmt.Errorf("unsupported filter %s", filter.Type)
		}

		middlewareName := provider.Normalize(fmt.Sprintf("%s-%s-%d", prefix, strings.ToLower(string(filter.Type)), i))
		middlewares[middlewareName] = middleware
	}

	return middlewares, nil
}

func createRedirectRegexMiddleware(scheme string, filter *v1alpha2.HTTPRequestRedirectFilter) (*dynamic.Middleware, error) {
	// Use the HTTPRequestRedirectFilter scheme if defined.
	filterScheme := scheme
	if filter.Scheme != nil {
		filterScheme = *filter.Scheme
	}