Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure webhook validation ingress has a PathType #5445

Merged
merged 1 commit into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion internal/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/mitchellh/hashstructure"
apiv1 "k8s.io/api/core/v1"
networking "k8s.io/api/networking/v1beta1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -219,6 +220,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 Expand Up @@ -529,7 +532,7 @@ func (n *NGINXController) getBackendServers(ingresses []*ingress.Ingress) ([]*in
if loc.Path == nginxPath {
// Same paths but different types are allowed
// (same type means overlap in the path definition)
if *loc.PathType != *path.PathType {
if !apiequality.Semantic.DeepEqual(loc.PathType, path.PathType) {
break
}

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
}
}
}
}
19 changes: 19 additions & 0 deletions internal/k8s/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,22 @@ 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 idx := range rule.IngressRuleValue.HTTP.Paths {
p := &rule.IngressRuleValue.HTTP.Paths[idx]
if p.PathType == nil {
p.PathType = &defaultPathType
}
}
}
}