@@ -199,6 +199,8 @@ var ValidateIngressName = apimachineryvalidation.NameIsDNSSubdomain
199
199
200
200
// IngressValidationOptions cover beta to GA transitions for HTTP PathType
201
201
type IngressValidationOptions struct {
202
+ // AllowInvalidSecretName indicates whether spec.tls[*].secretName values that are not valid Secret names should be allowed
203
+ AllowInvalidSecretName bool
202
204
}
203
205
204
206
// ValidateIngress validates Ingresses on create and update.
@@ -212,7 +214,9 @@ func validateIngress(ingress *networking.Ingress, opts IngressValidationOptions,
212
214
func ValidateIngressCreate (ingress * networking.Ingress , requestGV schema.GroupVersion ) field.ErrorList {
213
215
allErrs := field.ErrorList {}
214
216
var opts IngressValidationOptions
215
- opts = IngressValidationOptions {}
217
+ opts = IngressValidationOptions {
218
+ AllowInvalidSecretName : allowInvalidSecretName (requestGV , nil ),
219
+ }
216
220
allErrs = append (allErrs , validateIngress (ingress , opts , requestGV )... )
217
221
annotationVal , annotationIsSet := ingress .Annotations [annotationIngressClass ]
218
222
if annotationIsSet && ingress .Spec .IngressClassName != nil {
@@ -226,26 +230,34 @@ func ValidateIngressCreate(ingress *networking.Ingress, requestGV schema.GroupVe
226
230
func ValidateIngressUpdate (ingress , oldIngress * networking.Ingress , requestGV schema.GroupVersion ) field.ErrorList {
227
231
allErrs := apivalidation .ValidateObjectMetaUpdate (& ingress .ObjectMeta , & oldIngress .ObjectMeta , field .NewPath ("metadata" ))
228
232
var opts IngressValidationOptions
229
- opts = IngressValidationOptions {}
233
+ opts = IngressValidationOptions {
234
+ AllowInvalidSecretName : allowInvalidSecretName (requestGV , oldIngress ),
235
+ }
230
236
231
237
allErrs = append (allErrs , validateIngress (ingress , opts , requestGV )... )
232
238
return allErrs
233
239
}
234
240
235
- func validateIngressTLS (spec * networking.IngressSpec , fldPath * field.Path ) field.ErrorList {
241
+ func validateIngressTLS (spec * networking.IngressSpec , fldPath * field.Path , opts IngressValidationOptions ) field.ErrorList {
236
242
allErrs := field.ErrorList {}
237
243
// TODO: Perform a more thorough validation of spec.TLS.Hosts that takes
238
244
// the wildcard spec from RFC 6125 into account.
239
- for _ , itls := range spec .TLS {
245
+ for tlsIndex , itls := range spec .TLS {
240
246
for i , host := range itls .Hosts {
241
247
if strings .Contains (host , "*" ) {
242
248
for _ , msg := range validation .IsWildcardDNS1123Subdomain (host ) {
243
- allErrs = append (allErrs , field .Invalid (fldPath .Index (i ).Child ("hosts" ), host , msg ))
249
+ allErrs = append (allErrs , field .Invalid (fldPath .Index (tlsIndex ).Child ("hosts" ). Index ( i ), host , msg ))
244
250
}
245
251
continue
246
252
}
247
253
for _ , msg := range validation .IsDNS1123Subdomain (host ) {
248
- allErrs = append (allErrs , field .Invalid (fldPath .Index (i ).Child ("hosts" ), host , msg ))
254
+ allErrs = append (allErrs , field .Invalid (fldPath .Index (tlsIndex ).Child ("hosts" ).Index (i ), host , msg ))
255
+ }
256
+ }
257
+
258
+ if ! opts .AllowInvalidSecretName {
259
+ for _ , msg := range validateTLSSecretName (itls .SecretName ) {
260
+ allErrs = append (allErrs , field .Invalid (fldPath .Index (tlsIndex ).Child ("secretName" ), itls .SecretName , msg ))
249
261
}
250
262
}
251
263
}
@@ -278,7 +290,7 @@ func ValidateIngressSpec(spec *networking.IngressSpec, fldPath *field.Path, opts
278
290
allErrs = append (allErrs , validateIngressRules (spec .Rules , fldPath .Child ("rules" ), opts , requestGV )... )
279
291
}
280
292
if len (spec .TLS ) > 0 {
281
- allErrs = append (allErrs , validateIngressTLS (spec , fldPath .Child ("tls" ))... )
293
+ allErrs = append (allErrs , validateIngressTLS (spec , fldPath .Child ("tls" ), opts )... )
282
294
}
283
295
if spec .IngressClassName != nil {
284
296
for _ , msg := range ValidateIngressClassName (* spec .IngressClassName , false ) {
@@ -523,3 +535,26 @@ func validateIngressTypedLocalObjectReference(params *api.TypedLocalObjectRefere
523
535
524
536
return allErrs
525
537
}
538
+
539
+ func allowInvalidSecretName (gv schema.GroupVersion , oldIngress * networking.Ingress ) bool {
540
+ if gv == networkingv1beta1 .SchemeGroupVersion || gv == extensionsv1beta1 .SchemeGroupVersion {
541
+ // backwards compatibility with released API versions that allowed invalid names
542
+ return true
543
+ }
544
+ if oldIngress != nil {
545
+ for _ , tls := range oldIngress .Spec .TLS {
546
+ if len (validateTLSSecretName (tls .SecretName )) > 0 {
547
+ // backwards compatibility with existing persisted object
548
+ return true
549
+ }
550
+ }
551
+ }
552
+ return false
553
+ }
554
+
555
+ func validateTLSSecretName (name string ) []string {
556
+ if len (name ) == 0 {
557
+ return nil
558
+ }
559
+ return apivalidation .ValidateSecretName (name , false )
560
+ }
0 commit comments