Skip to content

Commit

Permalink
Merge pull request #16 from Gympass/support-v1beta1
Browse files Browse the repository at this point in the history
Support networking v1beta1 only
  • Loading branch information
caiofralmeida authored Aug 26, 2021
2 parents c8e8802 + e808f11 commit 5d5e4db
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions controllers/cdn_origin_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
12 changes: 6 additions & 6 deletions controllers/cloudfront.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -38,23 +38,23 @@ 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)...)
}
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
}
Expand Down
94 changes: 47 additions & 47 deletions controllers/cloudfront_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -37,22 +37,22 @@ 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),
},
},
},
},
},
}

status := networkingv1.IngressStatus{
status := networkingv1beta1.IngressStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{
Expand All @@ -69,26 +69,26 @@ 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),
},
},
},
},
},
}

status := networkingv1.IngressStatus{
status := networkingv1beta1.IngressStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{
Expand All @@ -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{
{
Expand All @@ -160,22 +160,22 @@ 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),
},
},
},
},
},
}

status := networkingv1.IngressStatus{
status := networkingv1beta1.IngressStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{
Expand All @@ -193,22 +193,22 @@ 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),
},
},
},
},
},
}

status := networkingv1.IngressStatus{
status := networkingv1beta1.IngressStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{
Expand All @@ -227,22 +227,22 @@ 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),
},
},
},
},
},
}

status := networkingv1.IngressStatus{
status := networkingv1beta1.IngressStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions controllers/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions controllers/predicate_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down

0 comments on commit 5d5e4db

Please sign in to comment.