Skip to content

Commit

Permalink
Merge pull request #17 from Gympass/rn225-support-v1
Browse files Browse the repository at this point in the history
[RN-225] Support v1beta1 and v1 ingresses
  • Loading branch information
LCaparelli authored Sep 3, 2021
2 parents 5d5e4db + c8f95ec commit 6f07cf1
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 218 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 with Ingresses on networking.k8s.io/v1beta1 (< v1.22)
- Kubernetes with Ingress support for networking.k8s.io/v1 or networking.k8s.io/v1beta1

# AWS CloudFront

Expand Down
33 changes: 12 additions & 21 deletions controllers/cloudfront.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,31 @@ import (
"github.com/Gympass/cdn-origin-controller/internal/cloudfront"
)

func newOrigin(rules []networkingv1beta1.IngressRule, status networkingv1beta1.IngressStatus) cloudfront.Origin {
h := originHost(status)
builder := cloudfront.NewOriginBuilder(h)
patterns := pathPatterns(rules)
const prefixPathType = string(networkingv1beta1.PathTypePrefix)

func newOrigin(dto ingressDTO) cloudfront.Origin {
builder := cloudfront.NewOriginBuilder(dto.host)
patterns := pathPatterns(dto.paths)
for _, p := range patterns {
builder = builder.WithBehavior(p)
}

return builder.Build()
}

func originHost(status networkingv1beta1.IngressStatus) string {
return status.LoadBalancer.Ingress[0].Hostname
}

func pathPatterns(rules []networkingv1beta1.IngressRule) []string {
func pathPatterns(paths []path) []string {
var patterns []string
for _, r := range rules {
patterns = append(patterns, pathPatternsForRule(r)...)
for _, p := range paths {
patterns = append(patterns, pathPatternsForPath(p)...)
}
return patterns
}

func pathPatternsForRule(rule networkingv1beta1.IngressRule) []string {
var paths []string
for _, p := range rule.HTTP.Paths {
pattern := p.Path
if *p.PathType == networkingv1beta1.PathTypePrefix {
paths = append(paths, buildPatternsForPrefix(pattern)...)
continue
}
paths = append(paths, pattern)
func pathPatternsForPath(p path) []string {
if p.pathType == prefixPathType {
return buildPatternsForPrefix(p.pathPattern)
}
return paths
return []string{p.pathPattern}
}

// https://github.com/kubernetes-sigs/aws-load-balancer-controller/pull/1772/files#diff-ab931de004b4ee78d1a27f20f37cc9acaf851ab150094ec8baa1fdbcf5ca67f1R163-R174
Expand Down
212 changes: 55 additions & 157 deletions controllers/cloudfront_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"testing"

"github.com/stretchr/testify/suite"
corev1 "k8s.io/api/core/v1"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
)

Expand All @@ -37,119 +36,67 @@ type IngressConverterSuite struct {
}

func (s *IngressConverterSuite) TestNewOrigin_SingleBehaviorAndRule() {
rules := []networkingv1beta1.IngressRule{
{
IngressRuleValue: networkingv1beta1.IngressRuleValue{
HTTP: &networkingv1beta1.HTTPIngressRuleValue{
Paths: []networkingv1beta1.HTTPIngressPath{
{
Path: "/",
PathType: pathTypePointer(networkingv1beta1.PathTypeExact),
},
},
},
dto := ingressDTO{
host: "origin1",
paths: []path{
{
pathPattern: "/",
pathType: string(networkingv1beta1.PathTypeExact),
},
},
}

status := networkingv1beta1.IngressStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{
Hostname: "origin1",
},
},
},
}

origin := newOrigin(rules, status)
origin := newOrigin(dto)
s.Equal("origin1", origin.Host)
s.Len(origin.Behaviors, 1)
s.Equal("/", origin.Behaviors[0].PathPattern)
}

func (s *IngressConverterSuite) TestNewOrigins_MultipleBehaviorsSingleRule() {
rules := []networkingv1beta1.IngressRule{
{
IngressRuleValue: networkingv1beta1.IngressRuleValue{
HTTP: &networkingv1beta1.HTTPIngressRuleValue{
Paths: []networkingv1beta1.HTTPIngressPath{
{
Path: "/",
PathType: pathTypePointer(networkingv1beta1.PathTypeExact),
},
{
Path: "/foo",
PathType: pathTypePointer(networkingv1beta1.PathTypeExact),
},
},
},
dto := ingressDTO{
host: "origin1",
paths: []path{
{
pathPattern: "/",
pathType: string(networkingv1beta1.PathTypeExact),
},
},
}

status := networkingv1beta1.IngressStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{
Hostname: "origin1",
},
{
pathPattern: "/foo",
pathType: string(networkingv1beta1.PathTypeExact),
},
},
}

origin := newOrigin(rules, status)
origin := newOrigin(dto)
s.Equal("origin1", origin.Host)
s.Len(origin.Behaviors, 2)
s.Equal("/", origin.Behaviors[0].PathPattern)
s.Equal("/foo", origin.Behaviors[1].PathPattern)
}
func (s *IngressConverterSuite) TestNewOrigins_MultipleBehaviorsMultipleRules() {
rule1 := networkingv1beta1.IngressRule{
IngressRuleValue: networkingv1beta1.IngressRuleValue{
HTTP: &networkingv1beta1.HTTPIngressRuleValue{
Paths: []networkingv1beta1.HTTPIngressPath{
{
Path: "/",
PathType: pathTypePointer(networkingv1beta1.PathTypeExact),
},
{
Path: "/foo",
PathType: pathTypePointer(networkingv1beta1.PathTypeExact),
},
},
dto := ingressDTO{
host: "origin1",
paths: []path{
{
pathPattern: "/",
pathType: string(networkingv1beta1.PathTypeExact),
},
},
}
rule2 := networkingv1beta1.IngressRule{
IngressRuleValue: networkingv1beta1.IngressRuleValue{
HTTP: &networkingv1beta1.HTTPIngressRuleValue{
Paths: []networkingv1beta1.HTTPIngressPath{
{
Path: "/foo/bar",
PathType: pathTypePointer(networkingv1beta1.PathTypeExact),
},
{
Path: "/bar",
PathType: pathTypePointer(networkingv1beta1.PathTypeExact),
},
},
{
pathPattern: "/foo",
pathType: string(networkingv1beta1.PathTypeExact),
},
},
}
rules := []networkingv1beta1.IngressRule{rule1, rule2}

status := networkingv1beta1.IngressStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{
Hostname: "origin1",
},
{
pathPattern: "/foo/bar",
pathType: string(networkingv1beta1.PathTypeExact),
},
{
pathPattern: "/bar",
pathType: string(networkingv1beta1.PathTypeExact),
},
},
}

origin := newOrigin(rules, status)
origin := newOrigin(dto)
s.Equal("origin1", origin.Host)
s.Len(origin.Behaviors, 4)
s.Equal("/", origin.Behaviors[0].PathPattern)
Expand All @@ -160,65 +107,35 @@ func (s *IngressConverterSuite) TestNewOrigins_MultipleBehaviorsMultipleRules()

// https://kubernetes.io/docs/concepts/services-networking/ingress/#examples
func (s *IngressConverterSuite) TestNewCloudFrontOrigins_PrefixPathType_SingleSlashSpecialCase() {
rules := []networkingv1beta1.IngressRule{
{
IngressRuleValue: networkingv1beta1.IngressRuleValue{
HTTP: &networkingv1beta1.HTTPIngressRuleValue{
Paths: []networkingv1beta1.HTTPIngressPath{
{
Path: "/",
PathType: pathTypePointer(networkingv1beta1.PathTypePrefix),
},
},
},
dto := ingressDTO{
host: "origin1",
paths: []path{
{
pathPattern: "/",
pathType: string(networkingv1beta1.PathTypePrefix),
},
},
}

status := networkingv1beta1.IngressStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{
Hostname: "origin1",
},
},
},
}

origin := newOrigin(rules, status)
origin := newOrigin(dto)
s.Equal("origin1", origin.Host)
s.Len(origin.Behaviors, 1)
s.Equal("/*", origin.Behaviors[0].PathPattern)
}

// https://kubernetes.io/docs/concepts/services-networking/ingress/#examples
func (s *IngressConverterSuite) TestNewCloudFrontOrigins_PrefixPathType_EndsWithSlash() {
rules := []networkingv1beta1.IngressRule{
{
IngressRuleValue: networkingv1beta1.IngressRuleValue{
HTTP: &networkingv1beta1.HTTPIngressRuleValue{
Paths: []networkingv1beta1.HTTPIngressPath{
{
Path: "/foo/",
PathType: pathTypePointer(networkingv1beta1.PathTypePrefix),
},
},
},
},
},
}

status := networkingv1beta1.IngressStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{
Hostname: "origin1",
},
dto := ingressDTO{
host: "origin1",
paths: []path{
{
pathPattern: "/foo/",
pathType: string(networkingv1beta1.PathTypePrefix),
},
},
}

origin := newOrigin(rules, status)
origin := newOrigin(dto)
s.Equal("origin1", origin.Host)
s.Len(origin.Behaviors, 2)
s.Equal("/foo", origin.Behaviors[0].PathPattern)
Expand All @@ -227,38 +144,19 @@ func (s *IngressConverterSuite) TestNewCloudFrontOrigins_PrefixPathType_EndsWith

// https://kubernetes.io/docs/concepts/services-networking/ingress/#examples
func (s *IngressConverterSuite) TestNewCloudFrontOrigins_PrefixPathType_DoesNotEndWithSlash() {
rules := []networkingv1beta1.IngressRule{
{
IngressRuleValue: networkingv1beta1.IngressRuleValue{
HTTP: &networkingv1beta1.HTTPIngressRuleValue{
Paths: []networkingv1beta1.HTTPIngressPath{
{
Path: "/foo",
PathType: pathTypePointer(networkingv1beta1.PathTypePrefix),
},
},
},
dto := ingressDTO{
host: "origin1",
paths: []path{
{
pathPattern: "/foo",
pathType: string(networkingv1beta1.PathTypePrefix),
},
},
}

status := networkingv1beta1.IngressStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{
Hostname: "origin1",
},
},
},
}

origin := newOrigin(rules, status)
origin := newOrigin(dto)
s.Equal("origin1", origin.Host)
s.Len(origin.Behaviors, 2)
s.Equal("/foo", origin.Behaviors[0].PathPattern)
s.Equal("/foo/*", origin.Behaviors[1].PathPattern)
}

func pathTypePointer(pt networkingv1beta1.PathType) *networkingv1beta1.PathType {
return &pt
}
Loading

0 comments on commit 6f07cf1

Please sign in to comment.