From a6c03f93a419555139c29e42dcded870d0aaefd4 Mon Sep 17 00:00:00 2001 From: Lucas Caparelli Date: Thu, 26 Aug 2021 18:16:38 -0300 Subject: [PATCH 1/2] Support networking v1beta1 only Signed-off-by: Lucas Caparelli --- controllers/cdn_origin_controller.go | 6 +- controllers/cloudfront.go | 12 ++-- controllers/cloudfront_test.go | 94 +++++++++++++------------- controllers/predicate.go | 4 +- controllers/predicate_internal_test.go | 10 +-- 5 files changed, 63 insertions(+), 63 deletions(-) diff --git a/controllers/cdn_origin_controller.go b/controllers/cdn_origin_controller.go index a3fd3d2..6baa9a2 100644 --- a/controllers/cdn_origin_controller.go +++ b/controllers/cdn_origin_controller.go @@ -25,7 +25,7 @@ import ( "github.com/go-logr/logr" corev1 "k8s.io/api/core/v1" - networkingv1 "k8s.io/api/networking/v1" + networkingv1beta1 "k8s.io/api/networking/v1beta1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/tools/record" @@ -62,7 +62,7 @@ type Reconciler struct { func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { r.log = r.OriginalLog.WithValues("Ingress", req.NamespacedName) - ingress := &networkingv1.Ingress{} + ingress := &networkingv1beta1.Ingress{} err := r.Client.Get(ctx, req.NamespacedName, ingress) if err != nil { if errors.IsNotFound(err) { @@ -92,6 +92,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). WithEventFilter(&hasCdnAnnotationPredicate{}). - For(&networkingv1.Ingress{}). + For(&networkingv1beta1.Ingress{}). Complete(r) } diff --git a/controllers/cloudfront.go b/controllers/cloudfront.go index b8bbb8e..fe34436 100644 --- a/controllers/cloudfront.go +++ b/controllers/cloudfront.go @@ -22,12 +22,12 @@ package controllers import ( "strings" - networkingv1 "k8s.io/api/networking/v1" + networkingv1beta1 "k8s.io/api/networking/v1beta1" "github.com/Gympass/cdn-origin-controller/internal/cloudfront" ) -func newOrigin(rules []networkingv1.IngressRule, status networkingv1.IngressStatus) cloudfront.Origin { +func newOrigin(rules []networkingv1beta1.IngressRule, status networkingv1beta1.IngressStatus) cloudfront.Origin { h := originHost(status) builder := cloudfront.NewOriginBuilder(h) patterns := pathPatterns(rules) @@ -38,11 +38,11 @@ func newOrigin(rules []networkingv1.IngressRule, status networkingv1.IngressStat return builder.Build() } -func originHost(status networkingv1.IngressStatus) string { +func originHost(status networkingv1beta1.IngressStatus) string { return status.LoadBalancer.Ingress[0].Hostname } -func pathPatterns(rules []networkingv1.IngressRule) []string { +func pathPatterns(rules []networkingv1beta1.IngressRule) []string { var patterns []string for _, r := range rules { patterns = append(patterns, pathPatternsForRule(r)...) @@ -50,11 +50,11 @@ func pathPatterns(rules []networkingv1.IngressRule) []string { return patterns } -func pathPatternsForRule(rule networkingv1.IngressRule) []string { +func pathPatternsForRule(rule networkingv1beta1.IngressRule) []string { var paths []string for _, p := range rule.HTTP.Paths { pattern := p.Path - if *p.PathType == networkingv1.PathTypePrefix { + if *p.PathType == networkingv1beta1.PathTypePrefix { paths = append(paths, buildPatternsForPrefix(pattern)...) continue } diff --git a/controllers/cloudfront_test.go b/controllers/cloudfront_test.go index 3738712..c2df932 100644 --- a/controllers/cloudfront_test.go +++ b/controllers/cloudfront_test.go @@ -24,7 +24,7 @@ import ( "github.com/stretchr/testify/suite" corev1 "k8s.io/api/core/v1" - networkingv1 "k8s.io/api/networking/v1" + networkingv1beta1 "k8s.io/api/networking/v1beta1" ) func TestRunIngressConverterTestSuite(t *testing.T) { @@ -37,14 +37,14 @@ type IngressConverterSuite struct { } func (s *IngressConverterSuite) TestNewOrigin_SingleBehaviorAndRule() { - rules := []networkingv1.IngressRule{ + rules := []networkingv1beta1.IngressRule{ { - IngressRuleValue: networkingv1.IngressRuleValue{ - HTTP: &networkingv1.HTTPIngressRuleValue{ - Paths: []networkingv1.HTTPIngressPath{ + IngressRuleValue: networkingv1beta1.IngressRuleValue{ + HTTP: &networkingv1beta1.HTTPIngressRuleValue{ + Paths: []networkingv1beta1.HTTPIngressPath{ { Path: "/", - PathType: pathTypePointer(networkingv1.PathTypeExact), + PathType: pathTypePointer(networkingv1beta1.PathTypeExact), }, }, }, @@ -52,7 +52,7 @@ func (s *IngressConverterSuite) TestNewOrigin_SingleBehaviorAndRule() { }, } - status := networkingv1.IngressStatus{ + status := networkingv1beta1.IngressStatus{ LoadBalancer: corev1.LoadBalancerStatus{ Ingress: []corev1.LoadBalancerIngress{ { @@ -69,18 +69,18 @@ func (s *IngressConverterSuite) TestNewOrigin_SingleBehaviorAndRule() { } func (s *IngressConverterSuite) TestNewOrigins_MultipleBehaviorsSingleRule() { - rules := []networkingv1.IngressRule{ + rules := []networkingv1beta1.IngressRule{ { - IngressRuleValue: networkingv1.IngressRuleValue{ - HTTP: &networkingv1.HTTPIngressRuleValue{ - Paths: []networkingv1.HTTPIngressPath{ + IngressRuleValue: networkingv1beta1.IngressRuleValue{ + HTTP: &networkingv1beta1.HTTPIngressRuleValue{ + Paths: []networkingv1beta1.HTTPIngressPath{ { Path: "/", - PathType: pathTypePointer(networkingv1.PathTypeExact), + PathType: pathTypePointer(networkingv1beta1.PathTypeExact), }, { Path: "/foo", - PathType: pathTypePointer(networkingv1.PathTypeExact), + PathType: pathTypePointer(networkingv1beta1.PathTypeExact), }, }, }, @@ -88,7 +88,7 @@ func (s *IngressConverterSuite) TestNewOrigins_MultipleBehaviorsSingleRule() { }, } - status := networkingv1.IngressStatus{ + status := networkingv1beta1.IngressStatus{ LoadBalancer: corev1.LoadBalancerStatus{ Ingress: []corev1.LoadBalancerIngress{ { @@ -105,41 +105,41 @@ func (s *IngressConverterSuite) TestNewOrigins_MultipleBehaviorsSingleRule() { s.Equal("/foo", origin.Behaviors[1].PathPattern) } func (s *IngressConverterSuite) TestNewOrigins_MultipleBehaviorsMultipleRules() { - rule1 := networkingv1.IngressRule{ - IngressRuleValue: networkingv1.IngressRuleValue{ - HTTP: &networkingv1.HTTPIngressRuleValue{ - Paths: []networkingv1.HTTPIngressPath{ + rule1 := networkingv1beta1.IngressRule{ + IngressRuleValue: networkingv1beta1.IngressRuleValue{ + HTTP: &networkingv1beta1.HTTPIngressRuleValue{ + Paths: []networkingv1beta1.HTTPIngressPath{ { Path: "/", - PathType: pathTypePointer(networkingv1.PathTypeExact), + PathType: pathTypePointer(networkingv1beta1.PathTypeExact), }, { Path: "/foo", - PathType: pathTypePointer(networkingv1.PathTypeExact), + PathType: pathTypePointer(networkingv1beta1.PathTypeExact), }, }, }, }, } - rule2 := networkingv1.IngressRule{ - IngressRuleValue: networkingv1.IngressRuleValue{ - HTTP: &networkingv1.HTTPIngressRuleValue{ - Paths: []networkingv1.HTTPIngressPath{ + rule2 := networkingv1beta1.IngressRule{ + IngressRuleValue: networkingv1beta1.IngressRuleValue{ + HTTP: &networkingv1beta1.HTTPIngressRuleValue{ + Paths: []networkingv1beta1.HTTPIngressPath{ { Path: "/foo/bar", - PathType: pathTypePointer(networkingv1.PathTypeExact), + PathType: pathTypePointer(networkingv1beta1.PathTypeExact), }, { Path: "/bar", - PathType: pathTypePointer(networkingv1.PathTypeExact), + PathType: pathTypePointer(networkingv1beta1.PathTypeExact), }, }, }, }, } - rules := []networkingv1.IngressRule{rule1, rule2} + rules := []networkingv1beta1.IngressRule{rule1, rule2} - status := networkingv1.IngressStatus{ + status := networkingv1beta1.IngressStatus{ LoadBalancer: corev1.LoadBalancerStatus{ Ingress: []corev1.LoadBalancerIngress{ { @@ -160,14 +160,14 @@ func (s *IngressConverterSuite) TestNewOrigins_MultipleBehaviorsMultipleRules() // https://kubernetes.io/docs/concepts/services-networking/ingress/#examples func (s *IngressConverterSuite) TestNewCloudFrontOrigins_PrefixPathType_SingleSlashSpecialCase() { - rules := []networkingv1.IngressRule{ + rules := []networkingv1beta1.IngressRule{ { - IngressRuleValue: networkingv1.IngressRuleValue{ - HTTP: &networkingv1.HTTPIngressRuleValue{ - Paths: []networkingv1.HTTPIngressPath{ + IngressRuleValue: networkingv1beta1.IngressRuleValue{ + HTTP: &networkingv1beta1.HTTPIngressRuleValue{ + Paths: []networkingv1beta1.HTTPIngressPath{ { Path: "/", - PathType: pathTypePointer(networkingv1.PathTypePrefix), + PathType: pathTypePointer(networkingv1beta1.PathTypePrefix), }, }, }, @@ -175,7 +175,7 @@ func (s *IngressConverterSuite) TestNewCloudFrontOrigins_PrefixPathType_SingleSl }, } - status := networkingv1.IngressStatus{ + status := networkingv1beta1.IngressStatus{ LoadBalancer: corev1.LoadBalancerStatus{ Ingress: []corev1.LoadBalancerIngress{ { @@ -193,14 +193,14 @@ func (s *IngressConverterSuite) TestNewCloudFrontOrigins_PrefixPathType_SingleSl // https://kubernetes.io/docs/concepts/services-networking/ingress/#examples func (s *IngressConverterSuite) TestNewCloudFrontOrigins_PrefixPathType_EndsWithSlash() { - rules := []networkingv1.IngressRule{ + rules := []networkingv1beta1.IngressRule{ { - IngressRuleValue: networkingv1.IngressRuleValue{ - HTTP: &networkingv1.HTTPIngressRuleValue{ - Paths: []networkingv1.HTTPIngressPath{ + IngressRuleValue: networkingv1beta1.IngressRuleValue{ + HTTP: &networkingv1beta1.HTTPIngressRuleValue{ + Paths: []networkingv1beta1.HTTPIngressPath{ { Path: "/foo/", - PathType: pathTypePointer(networkingv1.PathTypePrefix), + PathType: pathTypePointer(networkingv1beta1.PathTypePrefix), }, }, }, @@ -208,7 +208,7 @@ func (s *IngressConverterSuite) TestNewCloudFrontOrigins_PrefixPathType_EndsWith }, } - status := networkingv1.IngressStatus{ + status := networkingv1beta1.IngressStatus{ LoadBalancer: corev1.LoadBalancerStatus{ Ingress: []corev1.LoadBalancerIngress{ { @@ -227,14 +227,14 @@ func (s *IngressConverterSuite) TestNewCloudFrontOrigins_PrefixPathType_EndsWith // https://kubernetes.io/docs/concepts/services-networking/ingress/#examples func (s *IngressConverterSuite) TestNewCloudFrontOrigins_PrefixPathType_DoesNotEndWithSlash() { - rules := []networkingv1.IngressRule{ + rules := []networkingv1beta1.IngressRule{ { - IngressRuleValue: networkingv1.IngressRuleValue{ - HTTP: &networkingv1.HTTPIngressRuleValue{ - Paths: []networkingv1.HTTPIngressPath{ + IngressRuleValue: networkingv1beta1.IngressRuleValue{ + HTTP: &networkingv1beta1.HTTPIngressRuleValue{ + Paths: []networkingv1beta1.HTTPIngressPath{ { Path: "/foo", - PathType: pathTypePointer(networkingv1.PathTypePrefix), + PathType: pathTypePointer(networkingv1beta1.PathTypePrefix), }, }, }, @@ -242,7 +242,7 @@ func (s *IngressConverterSuite) TestNewCloudFrontOrigins_PrefixPathType_DoesNotE }, } - status := networkingv1.IngressStatus{ + status := networkingv1beta1.IngressStatus{ LoadBalancer: corev1.LoadBalancerStatus{ Ingress: []corev1.LoadBalancerIngress{ { @@ -259,6 +259,6 @@ func (s *IngressConverterSuite) TestNewCloudFrontOrigins_PrefixPathType_DoesNotE s.Equal("/foo/*", origin.Behaviors[1].PathPattern) } -func pathTypePointer(pt networkingv1.PathType) *networkingv1.PathType { +func pathTypePointer(pt networkingv1beta1.PathType) *networkingv1beta1.PathType { return &pt } diff --git a/controllers/predicate.go b/controllers/predicate.go index 09183c7..db15b0f 100644 --- a/controllers/predicate.go +++ b/controllers/predicate.go @@ -20,7 +20,7 @@ package controllers import ( - networkingv1 "k8s.io/api/networking/v1" + networkingv1beta1 "k8s.io/api/networking/v1beta1" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/predicate" @@ -51,7 +51,7 @@ func hasCdnAnnotation(o client.Object) bool { } func hasLoadBalancer(o client.Object) bool { - ing, ok := o.(*networkingv1.Ingress) + ing, ok := o.(*networkingv1beta1.Ingress) if !ok { return false } diff --git a/controllers/predicate_internal_test.go b/controllers/predicate_internal_test.go index 56dc204..2632c02 100644 --- a/controllers/predicate_internal_test.go +++ b/controllers/predicate_internal_test.go @@ -24,7 +24,7 @@ import ( "github.com/stretchr/testify/suite" corev1 "k8s.io/api/core/v1" - networkingv1 "k8s.io/api/networking/v1" + networkingv1beta1 "k8s.io/api/networking/v1beta1" "sigs.k8s.io/controller-runtime/pkg/event" ) @@ -38,19 +38,19 @@ type PredicateSuite struct { } var ( - baseIngress = &networkingv1.Ingress{} - annotatedIngress = func() *networkingv1.Ingress { + baseIngress = &networkingv1beta1.Ingress{} + annotatedIngress = func() *networkingv1beta1.Ingress { i := baseIngress.DeepCopy() i.Annotations = make(map[string]string) i.Annotations[cdnIDAnnotation] = "some value" return i }() - provisionedIngress = func() *networkingv1.Ingress { + provisionedIngress = func() *networkingv1beta1.Ingress { i := baseIngress.DeepCopy() i.Status.LoadBalancer.Ingress = []corev1.LoadBalancerIngress{{Hostname: "some value"}} return i }() - annotatedAndProvisionedIngress = func() *networkingv1.Ingress { + annotatedAndProvisionedIngress = func() *networkingv1beta1.Ingress { i := baseIngress.DeepCopy() i.Annotations = annotatedIngress.Annotations i.Status = provisionedIngress.Status From e808f11ae3bbebb81feb60b7d1cf18e8b6bfea41 Mon Sep 17 00:00:00 2001 From: Lucas Caparelli Date: Thu, 26 Aug 2021 18:24:34 -0300 Subject: [PATCH 2/2] Update K8s requirement Signed-off-by: Lucas Caparelli --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f33bb3e..c6d8faa 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Currently, the controller only supports adding origins to AWS CloudFront. Other Requirements: - - Kubernetes v1.19 or higher + - Kubernetes with Ingresses on networking.k8s.io/v1beta1 (< v1.22) # AWS CloudFront