-
Notifications
You must be signed in to change notification settings - Fork 689
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for ingressv1.Spec.IngressClassName which allows for support …
…for ingress.class not in an annotation Signed-off-by: Steve Sloka <[email protected]>
- Loading branch information
1 parent
faf7340
commit e90e82a
Showing
16 changed files
with
413 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright © 2020 VMware | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package annotation | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
type Annotation struct { | ||
// The ingress class configured. | ||
// If not set, defaults to DEFAULT_INGRESS_CLASS. | ||
SystemIngressClass string | ||
|
||
logrus.FieldLogger | ||
} | ||
|
||
// MatchesIngressClass returns true if the given Kubernetes object | ||
// belongs to the Ingress class that this cache is using. | ||
func (a *Annotation) MatchesIngressClass(obj metav1.ObjectMetaAccessor, ingressClassName *string) bool { | ||
class := IngressClass(obj.GetObjectMeta().GetAnnotations()) | ||
|
||
// If a class is defined as an annotation | ||
if len(class) > 0 { | ||
// Ingress.Class annotation takes precedence over the Spec.IngressClassName | ||
if classesMatch(class, a.SystemIngressClass) { | ||
return true | ||
} | ||
|
||
// IngressClass did not match, log the annotation message | ||
a.WithField("name", obj.GetObjectMeta().GetName()). | ||
WithField("namespace", obj.GetObjectMeta().GetNamespace()). | ||
WithField("kind", "Ingress"). | ||
WithField("ingress.class", class). | ||
Debug("ignoring object with unmatched ingress class") | ||
return false | ||
} | ||
|
||
// Annotation does not match, check if IngressClassName matches | ||
if ingressClassName != nil { | ||
if classesMatch(*ingressClassName, a.SystemIngressClass) { | ||
return true | ||
} | ||
|
||
// IngressClassName did not match but was configured, log the ingressClassName message | ||
a.WithField("name", obj.GetObjectMeta().GetName()). | ||
WithField("namespace", obj.GetObjectMeta().GetNamespace()). | ||
WithField("kind", "HTTPProxy"). | ||
WithField("spec.ingressClassName", *ingressClassName). | ||
Debug("ignoring object with unmatched ingressClassName") | ||
return false | ||
} | ||
|
||
// If SystemIngressClass is defined, then the there should not be a match | ||
if len(a.SystemIngressClass) > 0 { | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
// Copyright © 2020 VMware | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package annotation | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
"k8s.io/api/networking/v1beta1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
utils "k8s.io/utils/pointer" | ||
) | ||
|
||
func TestMatchesIngressClass(t *testing.T) { | ||
tests := map[string]struct { | ||
obj *v1beta1.Ingress | ||
systemIngressClass string | ||
want bool | ||
}{ | ||
"matches default ingress class annotation": { | ||
obj: &v1beta1.Ingress{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "default", | ||
Namespace: "default", | ||
Annotations: map[string]string{ | ||
"projectcontour.io/ingress.class": DEFAULT_INGRESS_CLASS, | ||
}, | ||
}, | ||
}, | ||
want: true, | ||
}, | ||
"does not match specific ingress class annotation": { | ||
obj: &v1beta1.Ingress{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "default", | ||
Namespace: "default", | ||
Annotations: map[string]string{ | ||
"projectcontour.io/ingress.class": "mismatch", | ||
}, | ||
}, | ||
}, | ||
want: false, | ||
}, | ||
"matches ingress class annotation with custom ingress class": { | ||
obj: &v1beta1.Ingress{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "default", | ||
Namespace: "default", | ||
Annotations: map[string]string{ | ||
"projectcontour.io/ingress.class": "custom", | ||
}, | ||
}, | ||
}, | ||
systemIngressClass: "custom", | ||
want: true, | ||
}, | ||
"does not match ingress class annotation with custom ingress class": { | ||
obj: &v1beta1.Ingress{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "default", | ||
Namespace: "default", | ||
Annotations: map[string]string{ | ||
"projectcontour.io/ingress.class": "contour", | ||
}, | ||
}, | ||
}, | ||
systemIngressClass: "custom", | ||
want: false, | ||
}, | ||
"matches default ingress class name": { | ||
obj: &v1beta1.Ingress{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "default", | ||
Namespace: "default", | ||
}, | ||
Spec: v1beta1.IngressSpec{ | ||
IngressClassName: utils.StringPtr(DEFAULT_INGRESS_CLASS), | ||
}, | ||
}, | ||
want: true, | ||
}, | ||
"does not match default ingress class name": { | ||
obj: &v1beta1.Ingress{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "default", | ||
Namespace: "default", | ||
}, | ||
Spec: v1beta1.IngressSpec{ | ||
IngressClassName: utils.StringPtr("mismatch"), | ||
}, | ||
}, | ||
want: false, | ||
}, | ||
"matches ingress class name with custom ingress class": { | ||
obj: &v1beta1.Ingress{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "default", | ||
Namespace: "default", | ||
}, | ||
Spec: v1beta1.IngressSpec{ | ||
IngressClassName: utils.StringPtr("custom"), | ||
}, | ||
}, | ||
systemIngressClass: "custom", | ||
want: true, | ||
}, | ||
"does not match ingress class name with custom ingress class": { | ||
obj: &v1beta1.Ingress{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "default", | ||
Namespace: "default", | ||
}, | ||
Spec: v1beta1.IngressSpec{ | ||
IngressClassName: utils.StringPtr("contour"), | ||
}, | ||
}, | ||
systemIngressClass: "custom", | ||
want: false, | ||
}, | ||
"annotation matches before ingressClassName": { | ||
obj: &v1beta1.Ingress{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "default", | ||
Namespace: "default", | ||
Annotations: map[string]string{ | ||
"projectcontour.io/ingress.class": "contour", | ||
}, | ||
}, | ||
Spec: v1beta1.IngressSpec{ | ||
IngressClassName: utils.StringPtr("custom"), | ||
}, | ||
}, | ||
want: true, | ||
}, | ||
"does not match annotation when configured with ingressClassName": { | ||
obj: &v1beta1.Ingress{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "default", | ||
Namespace: "default", | ||
Annotations: map[string]string{ | ||
"projectcontour.io/ingress.class": "custom", | ||
}, | ||
}, | ||
Spec: v1beta1.IngressSpec{ | ||
IngressClassName: utils.StringPtr("contour"), | ||
}, | ||
}, | ||
want: false, | ||
}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
a := Annotation{ | ||
SystemIngressClass: tc.systemIngressClass, | ||
FieldLogger: testLogger(t), | ||
} | ||
|
||
got := a.MatchesIngressClass(tc.obj, tc.obj.Spec.IngressClassName) | ||
if got != tc.want { | ||
t.Fatalf("expected: %v, got %v", tc.want, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func testLogger(t *testing.T) logrus.FieldLogger { | ||
log := logrus.New() | ||
log.Out = &testWriter{t} | ||
return log | ||
} | ||
|
||
type testWriter struct { | ||
*testing.T | ||
} | ||
|
||
func (t *testWriter) Write(buf []byte) (int, error) { | ||
t.Logf("%s", buf) | ||
return len(buf), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.