Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PE1-1809] feat(k8s): support function-associations for user-origins #106

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/cloudfront/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (s *Service) desiredIngresses(ctx context.Context, reconciling k8s.CDNIngre
func (s *Service) isPartOfDesiredState(reconciling k8s.CDNIngress) func(k8s.CDNIngress) bool {
return func(ing k8s.CDNIngress) bool {
isPartOfGroup := ing.Group == reconciling.Group
hasBeenProvisioned := len(ing.LoadBalancerHost) > 0
hasBeenProvisioned := len(ing.OriginHost) > 0
return !ing.IsBeingRemoved && isPartOfGroup && hasBeenProvisioned
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cloudfront/service_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ func renderDescription(template, group string) string {
}

func newOrigin(ing k8s.CDNIngress, cfg config.Config, shared k8s.SharedIngressParams) Origin {
builder := NewOriginBuilder(ing.Group, ing.LoadBalancerHost, ing.OriginAccess, cfg).
builder := NewOriginBuilder(ing.Group, ing.OriginHost, ing.OriginAccess, cfg).
WithResponseTimeout(ing.OriginRespTimeout).
WithRequestPolicy(ing.OriginReqPolicy).
WithCachePolicy(ing.CachePolicy)

for _, p := range shared.PathsFromIngress(ing.NamespacedName) {
for _, p := range shared.PathsFromOrigin(ing.OriginHost) {
for _, pp := range pathPatternsForPath(p) {
builder = builder.WithBehavior(pp, NewFunctions(p.FunctionAssociations)...)
}
Expand Down
7 changes: 7 additions & 0 deletions internal/k8s/function_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ func (fa FunctionAssociations) deepCopy() FunctionAssociations {
return copied
}

func (fa FunctionAssociations) IsEmpty() bool {
return fa.ViewerRequest == nil &&
fa.ViewerResponse == nil &&
fa.OriginRequest == nil &&
fa.OriginResponse == nil
}

type ViewerFunction struct {
ARN string `yaml:"arn"`
FunctionType FunctionType `yaml:"functionType"`
Expand Down
38 changes: 19 additions & 19 deletions internal/k8s/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Path struct {
// CDNIngress represents an Ingress within the bounded context of cdn-origin-controller
type CDNIngress struct {
types.NamespacedName
LoadBalancerHost string
OriginHost string
Group string
UnmergedPaths []Path
OriginReqPolicy string
Expand Down Expand Up @@ -95,7 +95,7 @@ var (
// SharedIngressParams represents parameters which might be specified in multiple Ingresses
type SharedIngressParams struct {
WebACLARN string
paths map[types.NamespacedName][]Path
paths map[string][]Path // map[originHost][]Path
}

// NewSharedIngressParams creates a new SharedIngressParams from a slice of CDNIngress
Expand All @@ -116,11 +116,11 @@ func NewSharedIngressParams(ingresses []CDNIngress) (SharedIngressParams, error)
}, nil
}

func (sp SharedIngressParams) PathsFromIngress(ing types.NamespacedName) []Path {
return sp.paths[ing]
func (sp SharedIngressParams) PathsFromOrigin(originHost string) []Path {
return sp.paths[originHost]
}

func mergedPaths(ingresses []CDNIngress) (map[types.NamespacedName][]Path, error) {
func mergedPaths(ingresses []CDNIngress) (map[string][]Path, error) {
// Let's put it all in a map of Paths to easily check if we already saw this
// Path before and whether to merge it, but we must avoid checking equality of
// Paths by also checking Path.FunctionAssociations, because the same Path might
Expand All @@ -130,27 +130,27 @@ func mergedPaths(ingresses []CDNIngress) (map[types.NamespacedName][]Path, error
// instead of just using the input Path, with an empty FunctionAssociations.
// This way we ensure we only check equality via PathPattern and PathType.
//
// We're also going to create a map[ingressNamespacedName] because this
// struct will later also need to filter paths by Ingress, so it comes in handy
// We're also going to create a map[originHostname] because this
// struct will later also need to filter paths by origin, so it comes in handy
// to filter easily.
//
// The resulting map of all of this is a map of Path indexed by ingress name.

mergedPaths := make(map[types.NamespacedName]map[Path]FunctionAssociations)
mergedPaths := make(map[string]map[Path]FunctionAssociations)
for _, ing := range ingresses {
_, ok := mergedPaths[ing.NamespacedName]
_, ok := mergedPaths[ing.OriginHost]
if !ok {
mergedPaths[ing.NamespacedName] = make(map[Path]FunctionAssociations)
mergedPaths[ing.OriginHost] = make(map[Path]FunctionAssociations)
}

for _, p := range ing.UnmergedPaths {
pKey := Path{
PathPattern: p.PathPattern,
PathType: p.PathType,
}
existingFA, ok := mergedPaths[ing.NamespacedName][pKey]
existingFA, ok := mergedPaths[ing.OriginHost][pKey]
if !ok {
mergedPaths[ing.NamespacedName][pKey] = p.FunctionAssociations
mergedPaths[ing.OriginHost][pKey] = p.FunctionAssociations
continue
}

Expand All @@ -159,23 +159,23 @@ func mergedPaths(ingresses []CDNIngress) (map[types.NamespacedName][]Path, error
return nil, fmt.Errorf("conflicting function associations on %q: %v", p.PathPattern, err)
}

mergedPaths[ing.NamespacedName][pKey] = mergedFA
mergedPaths[ing.OriginHost][pKey] = mergedFA
}
}

return mapOfNamespacedNameToPath(mergedPaths), nil
return mapOfOriginHostToPath(mergedPaths), nil
}

func mapOfNamespacedNameToPath(ingsPathsAndFAs map[types.NamespacedName]map[Path]FunctionAssociations) map[types.NamespacedName][]Path {
s := make(map[types.NamespacedName][]Path)
for ing, pathsAndFAs := range ingsPathsAndFAs {
func mapOfOriginHostToPath(ingsPathsAndFAs map[string]map[Path]FunctionAssociations) map[string][]Path {
s := make(map[string][]Path)
for originHost, pathsAndFAs := range ingsPathsAndFAs {
for p, fa := range pathsAndFAs {
path := Path{
PathPattern: p.PathPattern,
PathType: p.PathType,
FunctionAssociations: fa,
}
s[ing] = append(s[ing], path)
s[originHost] = append(s[originHost], path)
}
}
return s
Expand Down Expand Up @@ -228,7 +228,7 @@ func NewCDNIngressFromV1(ctx context.Context, ing *networkingv1.Ingress, class C
}

if len(ing.Status.LoadBalancer.Ingress) > 0 {
result.LoadBalancerHost = ing.Status.LoadBalancer.Ingress[0].Hostname
result.OriginHost = ing.Status.LoadBalancer.Ingress[0].Hostname
}

return result, nil
Expand Down
38 changes: 18 additions & 20 deletions internal/k8s/ingress_fetcher_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ func (s *IngressFetcherV1TestSuite) TestFetchBy_SuccessWithUserOrigins() {
- /foo/*`,
expectedIngs: []CDNIngress{
{
NamespacedName: types.NamespacedName{Name: "name", Namespace: "namespace"},
Group: "group",
LoadBalancerHost: "host",
UnmergedPaths: []Path{{PathPattern: "/foo"}, {PathPattern: "/foo/*"}},
OriginAccess: "Public",
NamespacedName: types.NamespacedName{Name: "name", Namespace: "namespace"},
Group: "group",
OriginHost: "host",
UnmergedPaths: []Path{{PathPattern: "/foo"}, {PathPattern: "/foo/*"}},
OriginAccess: "Public",
},
},
},
Expand All @@ -128,11 +128,11 @@ func (s *IngressFetcherV1TestSuite) TestFetchBy_SuccessWithUserOrigins() {
originAccess: Bucket`,
expectedIngs: []CDNIngress{
{
NamespacedName: types.NamespacedName{Name: "name", Namespace: "namespace"},
Group: "group",
LoadBalancerHost: "host",
UnmergedPaths: []Path{{PathPattern: "/foo"}, {PathPattern: "/foo/*"}},
OriginAccess: "Bucket",
NamespacedName: types.NamespacedName{Name: "name", Namespace: "namespace"},
Group: "group",
OriginHost: "host",
UnmergedPaths: []Path{{PathPattern: "/foo"}, {PathPattern: "/foo/*"}},
OriginAccess: "Bucket",
},
},
},
Expand All @@ -144,13 +144,12 @@ func (s *IngressFetcherV1TestSuite) TestFetchBy_SuccessWithUserOrigins() {
paths:
- /foo
- /foo/*
viewerFunctionARN: foo
originRequestPolicy: None`,
expectedIngs: []CDNIngress{
{
NamespacedName: types.NamespacedName{Name: "name", Namespace: "namespace"},
Group: "group",
LoadBalancerHost: "host",
OriginHost: "host",
UnmergedPaths: []Path{{PathPattern: "/foo"}, {PathPattern: "/foo/*"}},
OriginRespTimeout: int64(35),
OriginReqPolicy: "None",
Expand All @@ -164,7 +163,6 @@ func (s *IngressFetcherV1TestSuite) TestFetchBy_SuccessWithUserOrigins() {
- host: host
paths:
- /foo
viewerFunctionARN: foo
originRequestPolicy: None
originAccess: Bucket
- host: host
Expand All @@ -173,17 +171,17 @@ func (s *IngressFetcherV1TestSuite) TestFetchBy_SuccessWithUserOrigins() {
- /bar`,
expectedIngs: []CDNIngress{
{
NamespacedName: types.NamespacedName{Name: "name", Namespace: "namespace"},
Group: "group",
LoadBalancerHost: "host",
UnmergedPaths: []Path{{PathPattern: "/foo"}},
OriginReqPolicy: "None",
OriginAccess: "Bucket",
NamespacedName: types.NamespacedName{Name: "name", Namespace: "namespace"},
Group: "group",
OriginHost: "host",
UnmergedPaths: []Path{{PathPattern: "/foo"}},
OriginReqPolicy: "None",
OriginAccess: "Bucket",
},
{
NamespacedName: types.NamespacedName{Name: "name", Namespace: "namespace"},
Group: "group",
LoadBalancerHost: "host",
OriginHost: "host",
UnmergedPaths: []Path{{PathPattern: "/bar"}},
OriginRespTimeout: int64(35),
OriginAccess: "Public",
Expand Down
Loading
Loading