Skip to content

Commit

Permalink
Update websiteConfig for routing rules and protocol
Browse files Browse the repository at this point in the history
- Ensures protocol can be set blank as per the description
- Fixes the website config to work when no routing rules are passed

Signed-off-by: jack1902 <39212456+jack1902@users.noreply.github.com>
  • Loading branch information
jack1902 committed May 5, 2021
1 parent 62405df commit 11f8c8f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
4 changes: 2 additions & 2 deletions apis/s3/v1beta1/websiteConfiguration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type RedirectAllRequestsTo struct {
// Protocol to use when redirecting requests. The default is the protocol that
// is used in the original request.
// +kubebuilder:validation:Enum=http;https
Protocol string `json:"protocol"`
Protocol string `json:"protocol,omitempty"`
}

// RoutingRule specifies the redirect behavior and when a redirect is applied.
Expand Down Expand Up @@ -114,7 +114,7 @@ type Redirect struct {

// Protocol to use when redirecting requests. The default is the protocol that
// is used in the original request.
Protocol string `json:"protocol"`
Protocol string `json:"protocol,omitempty"`

// The object key prefix to use in the redirect request. For example, to redirect
// requests for all pages with prefix docs/ (objects in the docs/ folder) to
Expand Down
3 changes: 0 additions & 3 deletions package/crds/s3.aws.crossplane.io_buckets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,6 @@ spec:
type: string
required:
- hostName
- protocol
type: object
routingRules:
description: Rules that define when a redirect is applied and the redirect behavior.
Expand Down Expand Up @@ -920,8 +919,6 @@ spec:
replaceKeyWith:
description: The specific object key to use in the redirect request. For example, redirect request to error.html. Not required if one of the siblings is present. Can be present only if ReplaceKeyPrefixWith is not provided.
type: string
required:
- protocol
type: object
required:
- redirect
Expand Down
61 changes: 36 additions & 25 deletions pkg/controller/s3/bucket/websiteConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,28 +139,35 @@ func GenerateWebsiteConfiguration(config *v1beta1.WebsiteConfiguration) *awss3.W
if config.RedirectAllRequestsTo != nil {
wi.RedirectAllRequestsTo = &awss3.RedirectAllRequestsTo{
HostName: awsclient.String(config.RedirectAllRequestsTo.HostName),
Protocol: awss3.Protocol(config.RedirectAllRequestsTo.Protocol),
}
}
wi.RoutingRules = make([]awss3.RoutingRule, len(config.RoutingRules))
for i, rule := range config.RoutingRules {
rr := awss3.RoutingRule{
Redirect: &awss3.Redirect{
HostName: rule.Redirect.HostName,
HttpRedirectCode: rule.Redirect.HTTPRedirectCode,
Protocol: awss3.Protocol(rule.Redirect.Protocol),
ReplaceKeyPrefixWith: rule.Redirect.ReplaceKeyPrefixWith,
ReplaceKeyWith: rule.Redirect.ReplaceKeyWith,
},
if config.RedirectAllRequestsTo.Protocol != "" {
wi.RedirectAllRequestsTo.Protocol = awss3.Protocol(config.RedirectAllRequestsTo.Protocol)
}
if rule.Condition != nil {
rr.Condition = &awss3.Condition{
HttpErrorCodeReturnedEquals: rule.Condition.HTTPErrorCodeReturnedEquals,
KeyPrefixEquals: rule.Condition.KeyPrefixEquals,
}
if config.RoutingRules != nil {
wi.RoutingRules = make([]awss3.RoutingRule, len(config.RoutingRules))
for i, rule := range config.RoutingRules {
rr := awss3.RoutingRule{
Redirect: &awss3.Redirect{
HostName: rule.Redirect.HostName,
HttpRedirectCode: rule.Redirect.HTTPRedirectCode,
ReplaceKeyPrefixWith: rule.Redirect.ReplaceKeyPrefixWith,
ReplaceKeyWith: rule.Redirect.ReplaceKeyWith,
},
}
if rule.Redirect.Protocol != "" {
rr.Redirect.Protocol = awss3.Protocol(rule.Redirect.Protocol)
}
if rule.Condition != nil {
rr.Condition = &awss3.Condition{
HttpErrorCodeReturnedEquals: rule.Condition.HTTPErrorCodeReturnedEquals,
KeyPrefixEquals: rule.Condition.KeyPrefixEquals,
}
}
wi.RoutingRules[i] = rr
}
wi.RoutingRules[i] = rr
}

return wi
}

Expand Down Expand Up @@ -190,10 +197,12 @@ func createWebsiteConfigFromExternal(external *awss3.GetBucketWebsiteOutput, con
if config.RedirectAllRequestsTo == nil {
config.RedirectAllRequestsTo = &v1beta1.RedirectAllRequestsTo{}
}
config.RedirectAllRequestsTo.Protocol = awsclient.LateInitializeString(
config.RedirectAllRequestsTo.Protocol,
awsclient.String(string(external.RedirectAllRequestsTo.Protocol)),
)
if external.RedirectAllRequestsTo.Protocol != "" {
config.RedirectAllRequestsTo.Protocol = awsclient.LateInitializeString(
config.RedirectAllRequestsTo.Protocol,
awsclient.String(string(external.RedirectAllRequestsTo.Protocol)),
)
}
config.RedirectAllRequestsTo.HostName = awsclient.LateInitializeString(
config.RedirectAllRequestsTo.HostName,
external.RedirectAllRequestsTo.HostName,
Expand All @@ -219,10 +228,12 @@ func createWebsiteConfigFromExternal(external *awss3.GetBucketWebsiteOutput, con
config.RoutingRules[i].Redirect.ReplaceKeyWith,
rr.Redirect.ReplaceKeyWith,
)
config.RoutingRules[i].Redirect.Protocol = awsclient.LateInitializeString(
config.RoutingRules[i].Redirect.Protocol,
awsclient.String(string(rr.Redirect.Protocol)),
)
if rr.Redirect.Protocol != "" {
config.RoutingRules[i].Redirect.Protocol = awsclient.LateInitializeString(
config.RoutingRules[i].Redirect.Protocol,
awsclient.String(string(rr.Redirect.Protocol)),
)
}
}
if rr.Condition != nil {
if config.RoutingRules[i].Condition == nil {
Expand Down

0 comments on commit 11f8c8f

Please sign in to comment.