Skip to content

Commit dbaefc8

Browse files
committed
Ensure webhook validation ingress has a PathTypePrefix
1 parent bae9043 commit dbaefc8

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

internal/ingress/controller/controller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/mitchellh/hashstructure"
2727
apiv1 "k8s.io/api/core/v1"
2828
networking "k8s.io/api/networking/v1beta1"
29+
apiequality "k8s.io/apimachinery/pkg/api/equality"
2930
"k8s.io/apimachinery/pkg/util/intstr"
3031
"k8s.io/apimachinery/pkg/util/sets"
3132
"k8s.io/apimachinery/pkg/util/wait"
@@ -219,6 +220,8 @@ func (n *NGINXController) CheckIngress(ing *networking.Ingress) error {
219220
toCheck.ObjectMeta.Name == ing.ObjectMeta.Name
220221
}
221222

223+
k8s.SetDefaultPathTypeIfEmpty(ing)
224+
222225
ings := n.store.ListIngresses(filter)
223226
ings = append(ings, &ingress.Ingress{
224227
Ingress: *ing,
@@ -529,7 +532,7 @@ func (n *NGINXController) getBackendServers(ingresses []*ingress.Ingress) ([]*in
529532
if loc.Path == nginxPath {
530533
// Same paths but different types are allowed
531534
// (same type means overlap in the path definition)
532-
if *loc.PathType != *path.PathType {
535+
if !apiequality.Semantic.DeepEqual(loc.PathType, path.PathType) {
533536
break
534537
}
535538

internal/ingress/controller/store/store.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -963,28 +963,14 @@ func toIngress(obj interface{}) (*networkingv1beta1.Ingress, bool) {
963963
return nil, false
964964
}
965965

966-
setDefaultPathTypeIfEmpty(ing)
966+
k8s.SetDefaultPathTypeIfEmpty(ing)
967967
return ing, true
968968
}
969969

970970
if ing, ok := obj.(*networkingv1beta1.Ingress); ok {
971-
setDefaultPathTypeIfEmpty(ing)
971+
k8s.SetDefaultPathTypeIfEmpty(ing)
972972
return ing, true
973973
}
974974

975975
return nil, false
976976
}
977-
978-
func setDefaultPathTypeIfEmpty(ing *networkingv1beta1.Ingress) {
979-
for _, rule := range ing.Spec.Rules {
980-
if rule.IngressRuleValue.HTTP == nil {
981-
continue
982-
}
983-
984-
for _, path := range rule.IngressRuleValue.HTTP.Paths {
985-
if path.PathType == nil {
986-
path.PathType = &defaultPathType
987-
}
988-
}
989-
}
990-
}

internal/k8s/main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,22 @@ func NetworkingIngressAvailable(client clientset.Interface) (bool, bool) {
149149

150150
return runningVersion.AtLeast(version114), runningVersion.AtLeast(version118)
151151
}
152+
153+
// Default path type is Prefix to not break existing definitions
154+
var defaultPathType = networkingv1beta1.PathTypePrefix
155+
156+
// SetDefaultPathTypeIfEmpty sets a default PathType when is not defined.
157+
func SetDefaultPathTypeIfEmpty(ing *networkingv1beta1.Ingress) {
158+
for _, rule := range ing.Spec.Rules {
159+
if rule.IngressRuleValue.HTTP == nil {
160+
continue
161+
}
162+
163+
for idx := range rule.IngressRuleValue.HTTP.Paths {
164+
p := &rule.IngressRuleValue.HTTP.Paths[idx]
165+
if p.PathType == nil {
166+
p.PathType = &defaultPathType
167+
}
168+
}
169+
}
170+
}

0 commit comments

Comments
 (0)