[Refactoring] Extract validation to a separate function
@@ -14,6 +14,7 @@ import (
apierror "github.com/epinio/epinio/pkg/api/core/v1/errors"
"github.com/epinio/epinio/pkg/api/core/v1/models"
"github.com/gin-gonic/gin"
+ networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
@@ -154,18 +155,35 @@ func validateRoutes(ctx context.Context, cluster *kubernetes.Cluster, appName, n
issues := []apierror.APIError{}
for _, ingress := range ingressList.Items {
+ ingressIssues := validateIngress(desiredRoutesMap, appName, namespace, ingress)
+ if len(ingressIssues) > 0 {
+ issues = append(issues, ingressIssues...)
+ }
+ }
+
+ if len(issues) > 0 {
+ return apierror.NewMultiError(issues)
+ }
+ return nil
+}
+
+// validateIngress checks if the desiredRoutesMap is in conflict with the passed
+// ingress object. Conflict means, the ingress already defines one of the desired
+// routes and it belongs to another or and unknown app.
+func validateIngress(desiredRoutesMap map[string]struct{}, appName, namespace string, ingress networkingv1.Ingress) []apierror.APIError {
+ issues := []apierror.APIError{}
+
routes, err := routes.FromIngress(ingress)
if err != nil {
- issues = append(issues, apierror.InternalError(err))
- continue
+ return append(issues, apierror.InternalError(err))
}
- // TODO: Extract to method
for _, route := range routes {
- ingressRoute := route.String()
+ routeStr := route.String()
- // if a desired route is present within the ingresses then we have to check if it's already owned by the same app
- if _, found := desiredRoutesMap[ingressRoute]; found {
+ // if a desired route is present within the ingresses then we have to check
+ // if it's already owned by the same app
+ if _, found := desiredRoutesMap[routeStr]; found {
ingressAppName, found := ingress.GetLabels()["app.kubernetes.io/name"]
if !found {
err := apierror.NewBadRequestErrorf("route is already owned by an unknown app").
@@ -176,16 +194,12 @@ func validateRoutes(ctx context.Context, cluster *kubernetes.Cluster, appName, n
// the ingress route is owned by another app
if appName != ingressAppName || namespace != ingress.Namespace {
- err := apierror.NewBadRequestErrorf("route '%s' already exists", ingressRoute).
+ err := apierror.NewBadRequestErrorf("route '%s' already exists", route).
WithDetailsf("route is already owned by app [%s] in namespace [%s]", ingressAppName, ingress.Namespace)
issues = append(issues, err)
}
}
}
- }
- if len(issues) > 0 {
- return apierror.NewMultiError(issues)
- }
- return nil
+ return issues
}
