Skip to content

Commit

Permalink
Ensure webhook validation ingress has a PathTypePrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
aledbf committed Apr 27, 2020
1 parent bae9043 commit 45ce28b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
2 changes: 2 additions & 0 deletions internal/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ func (n *NGINXController) CheckIngress(ing *networking.Ingress) error {
toCheck.ObjectMeta.Name == ing.ObjectMeta.Name
}

k8s.SetDefaultPathTypeIfEmpty(ing)

ings := n.store.ListIngresses(filter)
ings = append(ings, &ingress.Ingress{
Ingress: *ing,
Expand Down
18 changes: 2 additions & 16 deletions internal/ingress/controller/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,28 +963,14 @@ func toIngress(obj interface{}) (*networkingv1beta1.Ingress, bool) {
return nil, false
}

setDefaultPathTypeIfEmpty(ing)
k8s.SetDefaultPathTypeIfEmpty(ing)
return ing, true
}

if ing, ok := obj.(*networkingv1beta1.Ingress); ok {
setDefaultPathTypeIfEmpty(ing)
k8s.SetDefaultPathTypeIfEmpty(ing)
return ing, true
}

return nil, false
}

func setDefaultPathTypeIfEmpty(ing *networkingv1beta1.Ingress) {
for _, rule := range ing.Spec.Rules {
if rule.IngressRuleValue.HTTP == nil {
continue
}

for _, path := range rule.IngressRuleValue.HTTP.Paths {
if path.PathType == nil {
path.PathType = &defaultPathType
}
}
}
}
18 changes: 18 additions & 0 deletions internal/k8s/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,21 @@ func NetworkingIngressAvailable(client clientset.Interface) (bool, bool) {

return runningVersion.AtLeast(version114), runningVersion.AtLeast(version118)
}

// Default path type is Prefix to not break existing definitions
var defaultPathType = networkingv1beta1.PathTypePrefix

// SetDefaultPathTypeIfEmpty sets a default PathType when is not defined.
func SetDefaultPathTypeIfEmpty(ing *networkingv1beta1.Ingress) {
for _, rule := range ing.Spec.Rules {
if rule.IngressRuleValue.HTTP == nil {
continue
}

for _, path := range rule.IngressRuleValue.HTTP.Paths {
if path.PathType == nil {
path.PathType = &defaultPathType
}
}
}
}

0 comments on commit 45ce28b

Please sign in to comment.