Skip to content

Commit

Permalink
Use route/v1 types for route warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
benluddy committed Sep 30, 2022
1 parent 5d29c08 commit fdfbac2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 14 deletions.
18 changes: 18 additions & 0 deletions pkg/route/apis/route/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,21 @@ func validateWildcardPolicy(host string, policy routev1.WildcardPolicyType, fldP

return nil
}

func Warnings(route *routeapi.Route) []string {
external, errs := toRouteV1(route)
if len(errs) > 0 {
return nil
}

return warningsV1(external)
}

func warningsV1(route *routev1.Route) []string {
if len(route.Spec.Host) != 0 && len(route.Spec.Subdomain) != 0 {
var warnings []string
warnings = append(warnings, "spec.host is set; spec.subdomain may be ignored")
return warnings
}
return nil
}
44 changes: 44 additions & 0 deletions pkg/route/apis/route/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1316,3 +1316,47 @@ func TestValidateEdgeReencryptInsecureEdgeTerminationPolicy(t *testing.T) {
}
}
}

func TestWarnings(t *testing.T) {
for _, tc := range []struct {
name string
host string
subdomain string
expected []string
}{
{
name: "both host and subdomain set",
host: "foo",
subdomain: "bar",
expected: []string{"spec.host is set; spec.subdomain may be ignored"},
},
{
name: "only host set",
host: "foo",
},
{
name: "only subdomain set",
subdomain: "bar",
},
{
name: "both host and subdomain unset",
},
} {
t.Run(tc.name, func(t *testing.T) {
actual := warningsV1(&routev1.Route{
Spec: routev1.RouteSpec{
Host: tc.host,
Subdomain: tc.subdomain,
},
})
if len(actual) != len(tc.expected) {
t.Fatalf("expected %#v, got %#v", tc.expected, actual)
}
for i := range actual {
if actual[i] != tc.expected[i] {
t.Fatalf("expected %#v, got %#v", tc.expected, actual)
}
}
})
}
}
16 changes: 2 additions & 14 deletions pkg/route/apiserver/registry/route/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (s routeStrategy) Validate(ctx context.Context, obj runtime.Object) field.E

// WarningsOnCreate returns warnings for the creation of the given object.
func (routeStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
return hostAndSubdomainBothSetWarning(obj)
return validation.Warnings(obj.(*routeapi.Route))
}

func (routeStrategy) AllowCreateOnUpdate() bool {
Expand All @@ -96,7 +96,7 @@ func (s routeStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Obje

// WarningsOnUpdate returns warnings for the given update.
func (routeStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
return hostAndSubdomainBothSetWarning(obj)
return validation.Warnings(obj.(*routeapi.Route))
}

func (routeStrategy) AllowUnconditionalUpdate() bool {
Expand Down Expand Up @@ -168,15 +168,3 @@ func DecorateLegacyRouteWithEmptyDestinationCACertificates(obj runtime.Object) e
return fmt.Errorf("unknown type passed to %T", obj)
}
}

// hostAndSubdomainBothSetWarning returns a warning if a route has both
// spec.host and spec.subdomain set.
func hostAndSubdomainBothSetWarning(obj runtime.Object) []string {
newRoute := obj.(*routeapi.Route)
if len(newRoute.Spec.Host) != 0 && len(newRoute.Spec.Subdomain) != 0 {
var warnings []string
warnings = append(warnings, "spec.host is set; spec.subdomain may be ignored")
return warnings
}
return nil
}

0 comments on commit fdfbac2

Please sign in to comment.