Skip to content

Commit 1758d17

Browse files
committed
Allow resource backends in Ingress
1 parent 8f7b810 commit 1758d17

File tree

2 files changed

+9
-182
lines changed

2 files changed

+9
-182
lines changed

pkg/apis/networking/validation/validation.go

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ var ValidateIngressName = apimachineryvalidation.NameIsDNSSubdomain
197197

198198
// IngressValidationOptions cover beta to GA transitions for HTTP PathType
199199
type IngressValidationOptions struct {
200-
allowResourceBackend bool
201200
}
202201

203202
// ValidateIngress validates Ingresses on create and update.
@@ -211,10 +210,7 @@ func validateIngress(ingress *networking.Ingress, opts IngressValidationOptions,
211210
func ValidateIngressCreate(ingress *networking.Ingress, requestGV schema.GroupVersion) field.ErrorList {
212211
allErrs := field.ErrorList{}
213212
var opts IngressValidationOptions
214-
opts = IngressValidationOptions{
215-
// TODO(cmluciano): Allow resource backend for 1.19.
216-
allowResourceBackend: false,
217-
}
213+
opts = IngressValidationOptions{}
218214
allErrs = append(allErrs, validateIngress(ingress, opts, requestGV)...)
219215
annotationVal, annotationIsSet := ingress.Annotations[annotationIngressClass]
220216
if annotationIsSet && ingress.Spec.IngressClassName != nil {
@@ -228,9 +224,7 @@ func ValidateIngressCreate(ingress *networking.Ingress, requestGV schema.GroupVe
228224
func ValidateIngressUpdate(ingress, oldIngress *networking.Ingress, requestGV schema.GroupVersion) field.ErrorList {
229225
allErrs := apivalidation.ValidateObjectMetaUpdate(&ingress.ObjectMeta, &oldIngress.ObjectMeta, field.NewPath("metadata"))
230226
var opts IngressValidationOptions
231-
opts = IngressValidationOptions{
232-
allowResourceBackend: resourceBackendPresent(oldIngress),
233-
}
227+
opts = IngressValidationOptions{}
234228

235229
allErrs = append(allErrs, validateIngress(ingress, opts, requestGV)...)
236230
return allErrs
@@ -383,8 +377,6 @@ func validateIngressBackend(backend *networking.IngressBackend, fldPath *field.P
383377
switch {
384378
case hasResourceBackend && hasServiceBackend:
385379
return append(allErrs, field.Invalid(fldPath, "", "cannot set both resource and service backends"))
386-
case hasResourceBackend && !opts.allowResourceBackend:
387-
return append(allErrs, field.Forbidden(fldPath.Child("resource"), "not supported; only service backends are supported in this version"))
388380
case hasResourceBackend:
389381
allErrs = append(allErrs, validateIngressTypedLocalObjectReference(backend.Resource, fldPath.Child("resource"))...)
390382
default:
@@ -468,20 +460,3 @@ func validateIngressTypedLocalObjectReference(params *api.TypedLocalObjectRefere
468460

469461
return allErrs
470462
}
471-
472-
func resourceBackendPresent(ingress *networking.Ingress) bool {
473-
if ingress.Spec.Backend != nil && ingress.Spec.Backend.Resource != nil {
474-
return true
475-
}
476-
for _, rule := range ingress.Spec.Rules {
477-
if rule.HTTP == nil {
478-
continue
479-
}
480-
for _, path := range rule.HTTP.Paths {
481-
if path.Backend.Resource != nil {
482-
return true
483-
}
484-
}
485-
}
486-
return false
487-
}

pkg/apis/networking/validation/validation_test.go

Lines changed: 7 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,14 +1336,14 @@ func TestValidateIngressCreate(t *testing.T) {
13361336
},
13371337
expectedErrs: field.ErrorList{},
13381338
},
1339-
"Spec.Backend.Resource field not allowed on create": {
1339+
"Spec.Backend.Resource field allowed on create": {
13401340
tweakIngress: func(ingress *networking.Ingress) {
13411341
ingress.Spec.Backend = &networking.IngressBackend{
13421342
Resource: resourceBackend}
13431343
},
1344-
expectedErrs: field.ErrorList{field.Forbidden(field.NewPath("spec.backend.resource"), "not supported; only service backends are supported in this version")},
1344+
expectedErrs: field.ErrorList{},
13451345
},
1346-
"Paths.Backend.Resource field not allowed on create": {
1346+
"Paths.Backend.Resource field allowed on create": {
13471347
tweakIngress: func(ingress *networking.Ingress) {
13481348
ingress.Spec.Rules = []networking.IngressRule{{
13491349
Host: "foo.bar.com",
@@ -1359,7 +1359,7 @@ func TestValidateIngressCreate(t *testing.T) {
13591359
},
13601360
}}
13611361
},
1362-
expectedErrs: field.ErrorList{field.Forbidden(field.NewPath("spec.rules[0].http.paths[0].backend.resource"), "not supported; only service backends are supported in this version")},
1362+
expectedErrs: field.ErrorList{},
13631363
},
13641364
}
13651365

@@ -1543,13 +1543,13 @@ func TestValidateIngressUpdate(t *testing.T) {
15431543
},
15441544
expectedErrs: field.ErrorList{},
15451545
},
1546-
"new Backend.Resource not allowed on update": {
1546+
"new Backend.Resource allowed on update": {
15471547
tweakIngresses: func(newIngress, oldIngress *networking.Ingress) {
15481548
oldIngress.Spec.Backend = &defaultBackend
15491549
newIngress.Spec.Backend = &networking.IngressBackend{
15501550
Resource: resourceBackend}
15511551
},
1552-
expectedErrs: field.ErrorList{field.Forbidden(field.NewPath("spec.backend.resource"), "not supported; only service backends are supported in this version")},
1552+
expectedErrs: field.ErrorList{},
15531553
},
15541554
"old Backend.Resource allowed on update": {
15551555
tweakIngresses: func(newIngress, oldIngress *networking.Ingress) {
@@ -1686,7 +1686,7 @@ func TestValidateIngressUpdate(t *testing.T) {
16861686
},
16871687
}}
16881688
},
1689-
expectedErrs: field.ErrorList{field.Forbidden(field.NewPath("spec.rules[0].http.paths[0].backend.resource"), "not supported; only service backends are supported in this version")},
1689+
expectedErrs: field.ErrorList{},
16901690
},
16911691
}
16921692

@@ -2077,151 +2077,3 @@ func TestValidateIngressStatusUpdate(t *testing.T) {
20772077
}
20782078
}
20792079
}
2080-
2081-
func TestValidateResourceBackendPresent(t *testing.T) {
2082-
implementationPathType := networking.PathTypeImplementationSpecific
2083-
defaultBackend := networking.IngressBackend{
2084-
ServiceName: "default-backend",
2085-
ServicePort: intstr.FromInt(80),
2086-
}
2087-
resourceBackend := &api.TypedLocalObjectReference{
2088-
APIGroup: utilpointer.StringPtr("example.com"),
2089-
Kind: "foo",
2090-
Name: "bar",
2091-
}
2092-
baseIngress := networking.Ingress{
2093-
ObjectMeta: metav1.ObjectMeta{
2094-
Name: "foo",
2095-
Namespace: metav1.NamespaceDefault,
2096-
},
2097-
Spec: networking.IngressSpec{
2098-
Backend: &networking.IngressBackend{
2099-
ServiceName: "default-backend",
2100-
ServicePort: intstr.FromInt(80),
2101-
},
2102-
Rules: []networking.IngressRule{
2103-
{
2104-
Host: "foo.bar.com",
2105-
IngressRuleValue: networking.IngressRuleValue{
2106-
HTTP: &networking.HTTPIngressRuleValue{
2107-
Paths: []networking.HTTPIngressPath{
2108-
{
2109-
Path: "/foo",
2110-
PathType: &implementationPathType,
2111-
Backend: defaultBackend,
2112-
},
2113-
},
2114-
},
2115-
},
2116-
},
2117-
},
2118-
},
2119-
Status: networking.IngressStatus{
2120-
LoadBalancer: api.LoadBalancerStatus{
2121-
Ingress: []api.LoadBalancerIngress{
2122-
{IP: "127.0.0.1"},
2123-
},
2124-
},
2125-
},
2126-
}
2127-
testCases := map[string]struct {
2128-
groupVersion *schema.GroupVersion
2129-
tweakIngress func(ing *networking.Ingress)
2130-
expectResourceBackend bool
2131-
}{
2132-
"nil spec.Backend and no paths": {
2133-
tweakIngress: func(ing *networking.Ingress) {
2134-
ing.Spec.Backend = nil
2135-
ing.Spec.Rules[0].IngressRuleValue.HTTP.Paths[0].Path = ""
2136-
},
2137-
expectResourceBackend: false,
2138-
},
2139-
"nil spec.Backend.Resource and no paths": {
2140-
tweakIngress: func(ing *networking.Ingress) {
2141-
ing.Spec.Backend = nil
2142-
ing.Spec.Rules[0].IngressRuleValue.HTTP.Paths = []networking.HTTPIngressPath{}
2143-
},
2144-
expectResourceBackend: false,
2145-
},
2146-
"non-nil spec.Backend.Resource and no paths": {
2147-
tweakIngress: func(ing *networking.Ingress) {
2148-
ing.Spec.Backend = &networking.IngressBackend{
2149-
Resource: resourceBackend,
2150-
}
2151-
ing.Spec.Rules[0].IngressRuleValue.HTTP.Paths[0].Path = ""
2152-
},
2153-
expectResourceBackend: true,
2154-
},
2155-
"nil spec.Backend, one rule with nil HTTP ": {
2156-
tweakIngress: func(ing *networking.Ingress) {
2157-
ing.Spec.Backend = nil
2158-
ing.Spec.Rules[0].IngressRuleValue.HTTP = nil
2159-
},
2160-
expectResourceBackend: false,
2161-
},
2162-
"nil spec.Backend, one rule with non-nil HTTP, no paths": {
2163-
tweakIngress: func(ing *networking.Ingress) {
2164-
ing.Spec.Backend = nil
2165-
ing.Spec.Rules[0].IngressRuleValue = networking.IngressRuleValue{
2166-
HTTP: &networking.HTTPIngressRuleValue{
2167-
Paths: []networking.HTTPIngressPath{},
2168-
},
2169-
}
2170-
},
2171-
expectResourceBackend: false,
2172-
},
2173-
"nil spec.Backend, one rule with non-nil HTTP, one path with nil Backend.Resource": {
2174-
tweakIngress: func(ing *networking.Ingress) {
2175-
ing.Spec.Backend = nil
2176-
ing.Spec.Rules[0].IngressRuleValue = networking.IngressRuleValue{
2177-
HTTP: &networking.HTTPIngressRuleValue{
2178-
Paths: []networking.HTTPIngressPath{
2179-
{
2180-
Path: "/foo",
2181-
PathType: &implementationPathType,
2182-
Backend: networking.IngressBackend{
2183-
Resource: nil,
2184-
},
2185-
},
2186-
},
2187-
},
2188-
}
2189-
},
2190-
expectResourceBackend: false,
2191-
},
2192-
"nil spec.Backend, one rule with non-nil HTTP, one path with non-nil Backend.Resource": {
2193-
tweakIngress: func(ing *networking.Ingress) {
2194-
ing.Spec.Backend = nil
2195-
ing.Spec.Rules[0].IngressRuleValue = networking.IngressRuleValue{
2196-
HTTP: &networking.HTTPIngressRuleValue{
2197-
Paths: []networking.HTTPIngressPath{
2198-
{
2199-
Path: "/foo",
2200-
PathType: &implementationPathType,
2201-
Backend: networking.IngressBackend{
2202-
Resource: resourceBackend,
2203-
},
2204-
},
2205-
},
2206-
},
2207-
}
2208-
},
2209-
expectResourceBackend: true,
2210-
},
2211-
}
2212-
2213-
for name, testCase := range testCases {
2214-
t.Run(name, func(t *testing.T) {
2215-
ingress := baseIngress.DeepCopy()
2216-
testCase.tweakIngress(ingress)
2217-
gv := testCase.groupVersion
2218-
if gv == nil {
2219-
gv = &networkingv1.SchemeGroupVersion
2220-
}
2221-
isBackendAllowed := resourceBackendPresent(ingress)
2222-
if isBackendAllowed != testCase.expectResourceBackend {
2223-
t.Errorf("Expected resourceBackendPresent to return: %v, got: %v", testCase.expectResourceBackend, isBackendAllowed)
2224-
}
2225-
})
2226-
}
2227-
}

0 commit comments

Comments
 (0)