Skip to content

Commit 111f338

Browse files
authored
Merge pull request #272 from aledbf/refactor-annotation-parsers
Fix error getting class information from Ingress annotations
2 parents 9bba947 + 5c9bf12 commit 111f338

File tree

7 files changed

+46
-5
lines changed

7 files changed

+46
-5
lines changed

controllers/nginx/pkg/cmd/controller/nginx.go

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
"github.com/golang/glog"
3232
"github.com/mitchellh/mapstructure"
33+
"github.com/spf13/pflag"
3334

3435
"k8s.io/kubernetes/pkg/api"
3536

@@ -251,6 +252,11 @@ func (n NGINXController) Info() *ingress.BackendInfo {
251252
}
252253
}
253254

255+
// OverrideFlags customize NGINX controller flags
256+
func (n NGINXController) OverrideFlags(flags *pflag.FlagSet) {
257+
flags.Set("ingress-class", "nginx")
258+
}
259+
254260
// testTemplate checks if the NGINX configuration inside the byte array is valid
255261
// running the command "nginx -t" using a temporal file.
256262
func (n NGINXController) testTemplate(cfg []byte) error {

core/pkg/ingress/annotations/authreq/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ func (a authReq) Parse(ing *extensions.Ingress) (interface{}, error) {
9292
return nil, ing_errors.NewLocationDenied("invalid url host")
9393
}
9494

95-
m, _ := parser.GetStringAnnotation(authMethod, ing)
95+
m, err := parser.GetStringAnnotation(authMethod, ing)
96+
if err != nil {
97+
return nil, err
98+
}
99+
96100
if len(m) != 0 && !validMethod(m) {
97101
return nil, ing_errors.NewLocationDenied("invalid HTTP method")
98102
}

core/pkg/ingress/controller/controller.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func newIngressController(config *Configuration) *GenericController {
173173
DeleteFunc: func(obj interface{}) {
174174
delIng := obj.(*extensions.Ingress)
175175
if !IsValidClass(delIng, config.IngressClass) {
176-
glog.Infof("ignoring add for ingress %v based on annotation %v", delIng.Name, ingressClassKey)
176+
glog.Infof("ignoring delete for ingress %v based on annotation %v", delIng.Name, ingressClassKey)
177177
return
178178
}
179179
ic.recorder.Eventf(delIng, api.EventTypeNormal, "DELETE", fmt.Sprintf("Ingress %s/%s", delIng.Namespace, delIng.Name))
@@ -182,7 +182,7 @@ func newIngressController(config *Configuration) *GenericController {
182182
UpdateFunc: func(old, cur interface{}) {
183183
oldIng := old.(*extensions.Ingress)
184184
curIng := cur.(*extensions.Ingress)
185-
if !IsValidClass(curIng, config.IngressClass) {
185+
if !IsValidClass(curIng, config.IngressClass) && !IsValidClass(oldIng, config.IngressClass) {
186186
return
187187
}
188188

@@ -564,6 +564,10 @@ func (ic *GenericController) getBackendServers() ([]*ingress.Backend, []*ingress
564564
for _, ingIf := range ings {
565565
ing := ingIf.(*extensions.Ingress)
566566

567+
if !IsValidClass(ing, ic.cfg.IngressClass) {
568+
continue
569+
}
570+
567571
anns := ic.annotations.Extract(ing)
568572

569573
for _, rule := range ing.Spec.Rules {
@@ -698,6 +702,10 @@ func (ic *GenericController) createUpstreams(data []interface{}) map[string]*ing
698702
for _, ingIf := range data {
699703
ing := ingIf.(*extensions.Ingress)
700704

705+
if !IsValidClass(ing, ic.cfg.IngressClass) {
706+
continue
707+
}
708+
701709
secUpstream := ic.annotations.SecureUpstream(ing)
702710
hz := ic.annotations.HealthCheck(ing)
703711

@@ -840,6 +848,10 @@ func (ic *GenericController) createServers(data []interface{}, upstreams map[str
840848
// initialize all the servers
841849
for _, ingIf := range data {
842850
ing := ingIf.(*extensions.Ingress)
851+
if !IsValidClass(ing, ic.cfg.IngressClass) {
852+
continue
853+
}
854+
843855
// check if ssl passthrough is configured
844856
sslpt := ic.annotations.SSLPassthrough(ing)
845857

@@ -868,6 +880,9 @@ func (ic *GenericController) createServers(data []interface{}, upstreams map[str
868880
// configure default location and SSL
869881
for _, ingIf := range data {
870882
ing := ingIf.(*extensions.Ingress)
883+
if !IsValidClass(ing, ic.cfg.IngressClass) {
884+
continue
885+
}
871886

872887
for _, rule := range ing.Spec.Rules {
873888
host := rule.Host

core/pkg/ingress/controller/launch.go

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ func NewIngressController(backend ingress.Controller) *GenericController {
8484
ingress controller should update the Ingress status IP/hostname. Default is true`)
8585
)
8686

87+
backend.OverrideFlags(flags)
88+
8789
flags.AddGoFlagSet(flag.CommandLine)
8890
flags.Parse(os.Args)
8991

core/pkg/ingress/controller/util.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"k8s.io/ingress/core/pkg/ingress"
2828
"k8s.io/ingress/core/pkg/ingress/annotations/parser"
29+
"k8s.io/ingress/core/pkg/ingress/errors"
2930
)
3031

3132
// DeniedKeyName name of the key that contains the reason to deny a location
@@ -92,7 +93,10 @@ func IsValidClass(ing *extensions.Ingress, class string) bool {
9293
return true
9394
}
9495

95-
cc, _ := parser.GetStringAnnotation(ingressClassKey, ing)
96+
cc, err := parser.GetStringAnnotation(ingressClassKey, ing)
97+
if err != nil && !errors.IsMissingAnnotations(err) {
98+
glog.Warningf("unexpected error reading ingress annotation: %v", err)
99+
}
96100
if cc == "" {
97101
return true
98102
}

core/pkg/ingress/controller/util_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package controller
1919
import (
2020
"testing"
2121

22+
"reflect"
23+
2224
"k8s.io/ingress/core/pkg/ingress"
2325
"k8s.io/ingress/core/pkg/ingress/annotations/auth"
2426
"k8s.io/ingress/core/pkg/ingress/annotations/authreq"
@@ -29,7 +31,6 @@ import (
2931
"k8s.io/ingress/core/pkg/ingress/resolver"
3032
"k8s.io/kubernetes/pkg/api"
3133
"k8s.io/kubernetes/pkg/apis/extensions"
32-
"reflect"
3334
)
3435

3536
type fakeError struct{}
@@ -54,6 +55,7 @@ func TestIsValidClass(t *testing.T) {
5455
data := map[string]string{}
5556
data[ingressClassKey] = "custom"
5657
ing.SetAnnotations(data)
58+
5759
b = IsValidClass(ing, "custom")
5860
if !b {
5961
t.Errorf("Expected valid class but %v returned", b)
@@ -62,6 +64,10 @@ func TestIsValidClass(t *testing.T) {
6264
if b {
6365
t.Errorf("Expected invalid class but %v returned", b)
6466
}
67+
b = IsValidClass(ing, "")
68+
if !b {
69+
t.Errorf("Expected invalid class but %v returned", b)
70+
}
6571
}
6672

6773
func TestIsHostValid(t *testing.T) {

core/pkg/ingress/types.go

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package ingress
1818

1919
import (
20+
"github.com/spf13/pflag"
21+
2022
"k8s.io/kubernetes/pkg/api"
2123
"k8s.io/kubernetes/pkg/healthz"
2224

@@ -86,6 +88,8 @@ type Controller interface {
8688
BackendDefaults() defaults.Backend
8789
// Info returns information about the ingress controller
8890
Info() *BackendInfo
91+
// OverrideFlags allow the customization of the flags in the backend
92+
OverrideFlags(*pflag.FlagSet)
8993
}
9094

9195
// BackendInfo returns information about the backend.

0 commit comments

Comments
 (0)