Skip to content
This repository was archived by the owner on Nov 7, 2019. It is now read-only.

Commit 8317409

Browse files
author
Christopher Hein
authored
Merge pull request #72 from christopherhein/feature/71-static-site-s3
Adding Support for Creating Static S3 Sites
2 parents 29b2737 + 8866dc1 commit 8317409

File tree

7 files changed

+140
-1
lines changed

7 files changed

+140
-1
lines changed

cloudformation/s3bucket.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ Parameters:
7373
- BucketOwnerFullControl
7474
- AwsExecRead
7575
Default: "Private"
76+
EnableStaticSite:
77+
Description: enable static site
78+
Type: String
79+
AllowedValues:
80+
- 'true'
81+
- 'false'
82+
Default: 'false'
83+
StaticSiteIndex:
84+
Description: Index document for the S3 bucket static site
85+
Type: String
86+
Default: 'index.html'
87+
StaticSiteError:
88+
Description: Error document for the S3 bucket static site
89+
Type: String
90+
Default: "500.html"
7691
Mappings: {}
7792
Conditions:
7893
UseLogging: !Equals
@@ -84,12 +99,20 @@ Conditions:
8499
UseVersioning: !Equals
85100
- !Ref EnableVersioning
86101
- 'true'
102+
UseAsStaticSite: !Equals
103+
- !Ref EnableStaticSite
104+
- 'true'
87105
Resources:
88106
S3bucket:
89107
Type: 'AWS::S3::Bucket'
90108
Properties:
91109
BucketName: !Ref BucketName
92110
AccessControl: !Ref BucketAccessControl
111+
WebsiteConfiguration: !If
112+
- UseAsStaticSite
113+
- IndexDocument: !Ref StaticSiteIndex
114+
ErrorDocument: !Ref StaticSiteError
115+
- !Ref 'AWS::NoValue'
93116
LifecycleConfiguration:
94117
Rules:
95118
- Id: GlacierRule
@@ -139,3 +162,8 @@ Outputs:
139162
- S3bucket
140163
- Arn
141164
Description: Name of the Amazon S3 bucket
165+
WebsiteURL:
166+
Value: !GetAtt
167+
- S3bucket
168+
- WebsiteURL
169+
Description: URL of the bucket that was created

examples/s3bucket.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ metadata:
44
name: s3bucket.aws-operator.com
55
spec:
66
versioning: true
7-
accessControl: Private
7+
accessControl: PublicRead
8+
website:
9+
enabled: true
10+
indexPage: index.html
11+
errorPage: 500.html
812
logging:
913
enabled: false
1014
prefix: "archive"

models/s3bucket.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,34 @@ spec:
5959
Logging prefix to attach to all logs in the bucket.
6060
structKey: Prefix
6161
templateKey: LoggingPrefix
62+
- key: website
63+
type: object
64+
description: |
65+
Defines the options for creating a static website
66+
structKey: Website
67+
templateKey: Website
68+
properties:
69+
- key: enabled
70+
type: bool
71+
false: true
72+
description: |
73+
Website Enabled configures the S3 Bucket as a static site
74+
structKey: Enabled
75+
templateKey: EnableStaticSite
76+
- key: indexPage
77+
type: string
78+
default: "index.html"
79+
description: |
80+
Index document for the S3 bucket
81+
structKey: IndexPage
82+
templateKey: StaticSiteIndex
83+
- key: errorPage
84+
type: string
85+
default: "500.html"
86+
description: |
87+
Error document for the S3 bucket
88+
structKey: ErrorPage
89+
templateKey: StaticSiteError
6290
output:
6391
schema:
6492
type: object
@@ -75,6 +103,13 @@ spec:
75103
BucketARN is the full Amazon ARN for the bucket created
76104
structKey: BucketARN
77105
templateKey: BucketArn
106+
- key: websiteURL
107+
type: string
108+
description: |
109+
WebsiteURL is the DNS record for the static site
110+
structKey: WebsiteURL
111+
templateKey: WebsiteURL
112+
78113
additionalResources:
79114
services:
80115
- name: s3BucketSvc
@@ -95,4 +130,6 @@ spec:
95130
value: "{{.Config.Region}}"
96131
- key: bucketURL
97132
value: "{{.Obj.Name}}.s3-{{.Config.Region}}.amazonaws.com"
133+
- key: websiteURL
134+
value: "{{.Obj.Output.WebsiteURL}}"
98135

pkg/apis/operator.aws/v1alpha1/s3bucket.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,21 @@ type S3BucketSpec struct {
3333
Versioning bool `json:"versioning"`
3434
AccessControl string `json:"accessControl"`
3535
Logging S3BucketLogging `json:"logging"`
36+
Website S3BucketWebsite `json:"website"`
37+
}
38+
39+
// S3BucketWebsite defines the Website resource for S3Bucket
40+
type S3BucketWebsite struct {
41+
Enabled bool `json:"enabled"`
42+
IndexPage string `json:"indexPage"`
43+
ErrorPage string `json:"errorPage"`
3644
}
3745

3846
// S3BucketOutput defines the output resource for S3Bucket
3947
type S3BucketOutput struct {
4048
BucketName string `json:"bucketName"`
4149
BucketARN string `json:"bucketARN"`
50+
WebsiteURL string `json:"websiteURL"`
4251
}
4352

4453
// S3BucketStatus holds the status of the Cloudformation template

pkg/apis/operator.aws/v1alpha1/zz_generated.deepcopy.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/operator/s3bucket/cft.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,24 @@ func (s *Cloudformation) CreateStack() (output *cloudformation.CreateStackOutput
105105
return output, err
106106
}
107107
loggingprefix := helpers.CreateParam("LoggingPrefix", helpers.Stringify(loggingprefixValue))
108+
websiteenabledTemp := "{{.Obj.Spec.Website.Enabled}}"
109+
websiteenabledValue, err := helpers.Templatize(websiteenabledTemp, helpers.Data{Obj: s.S3Bucket, Config: s.config, Helpers: helpers.New()})
110+
if err != nil {
111+
return output, err
112+
}
113+
websiteenabled := helpers.CreateParam("EnableStaticSite", helpers.Stringify(websiteenabledValue))
114+
websiteindexPageTemp := "{{.Obj.Spec.Website.IndexPage}}"
115+
websiteindexPageValue, err := helpers.Templatize(websiteindexPageTemp, helpers.Data{Obj: s.S3Bucket, Config: s.config, Helpers: helpers.New()})
116+
if err != nil {
117+
return output, err
118+
}
119+
websiteindexPage := helpers.CreateParam("StaticSiteIndex", helpers.Stringify(websiteindexPageValue))
120+
websiteerrorPageTemp := "{{.Obj.Spec.Website.ErrorPage}}"
121+
websiteerrorPageValue, err := helpers.Templatize(websiteerrorPageTemp, helpers.Data{Obj: s.S3Bucket, Config: s.config, Helpers: helpers.New()})
122+
if err != nil {
123+
return output, err
124+
}
125+
websiteerrorPage := helpers.CreateParam("StaticSiteError", helpers.Stringify(websiteerrorPageValue))
108126

109127
parameters := []*cloudformation.Parameter{}
110128
parameters = append(parameters, resourceName)
@@ -116,6 +134,9 @@ func (s *Cloudformation) CreateStack() (output *cloudformation.CreateStackOutput
116134
parameters = append(parameters, accessControl)
117135
parameters = append(parameters, loggingenabled)
118136
parameters = append(parameters, loggingprefix)
137+
parameters = append(parameters, websiteenabled)
138+
parameters = append(parameters, websiteindexPage)
139+
parameters = append(parameters, websiteerrorPage)
119140

120141
stackInputs.SetParameters(parameters)
121142

@@ -180,6 +201,24 @@ func (s *Cloudformation) UpdateStack(updated *awsV1alpha1.S3Bucket) (output *clo
180201
return output, err
181202
}
182203
loggingprefix := helpers.CreateParam("LoggingPrefix", helpers.Stringify(loggingprefixValue))
204+
websiteenabledTemp := "{{.Obj.Spec.Website.Enabled}}"
205+
websiteenabledValue, err := helpers.Templatize(websiteenabledTemp, helpers.Data{Obj: updated, Config: s.config, Helpers: helpers.New()})
206+
if err != nil {
207+
return output, err
208+
}
209+
websiteenabled := helpers.CreateParam("EnableStaticSite", helpers.Stringify(websiteenabledValue))
210+
websiteindexPageTemp := "{{.Obj.Spec.Website.IndexPage}}"
211+
websiteindexPageValue, err := helpers.Templatize(websiteindexPageTemp, helpers.Data{Obj: updated, Config: s.config, Helpers: helpers.New()})
212+
if err != nil {
213+
return output, err
214+
}
215+
websiteindexPage := helpers.CreateParam("StaticSiteIndex", helpers.Stringify(websiteindexPageValue))
216+
websiteerrorPageTemp := "{{.Obj.Spec.Website.ErrorPage}}"
217+
websiteerrorPageValue, err := helpers.Templatize(websiteerrorPageTemp, helpers.Data{Obj: updated, Config: s.config, Helpers: helpers.New()})
218+
if err != nil {
219+
return output, err
220+
}
221+
websiteerrorPage := helpers.CreateParam("StaticSiteError", helpers.Stringify(websiteerrorPageValue))
183222

184223
parameters := []*cloudformation.Parameter{}
185224
parameters = append(parameters, resourceName)
@@ -191,6 +230,9 @@ func (s *Cloudformation) UpdateStack(updated *awsV1alpha1.S3Bucket) (output *clo
191230
parameters = append(parameters, accessControl)
192231
parameters = append(parameters, loggingenabled)
193232
parameters = append(parameters, loggingprefix)
233+
parameters = append(parameters, websiteenabled)
234+
parameters = append(parameters, websiteindexPage)
235+
parameters = append(parameters, websiteerrorPage)
194236

195237
stackInputs.SetParameters(parameters)
196238

pkg/operator/s3bucket/controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ func updateStatus(config *config.Config, name string, namespace string, stackID
216216
}
217217
resourceCopy.Output.BucketName = outputs["BucketName"]
218218
resourceCopy.Output.BucketARN = outputs["BucketArn"]
219+
resourceCopy.Output.WebsiteURL = outputs["WebsiteURL"]
219220
}
220221

221222
_, err = clientSet.S3Buckets(namespace).Update(resourceCopy)
@@ -272,6 +273,7 @@ func syncAdditionalResources(config *config.Config, s *awsV1alpha1.S3Bucket) (er
272273
"serviceName": "{{call .Helpers.KubernetesResourceName .Obj.Name}}",
273274
"region": "{{.Config.Region}}",
274275
"bucketURL": "{{.Obj.Name}}.s3-{{.Config.Region}}.amazonaws.com",
276+
"websiteURL": "{{.Obj.Output.WebsiteURL}}",
275277
}
276278
s3BucketCM := helpers.CreateConfigMap(config, s, s.Name, s.Namespace, s3BucketCMData)
277279
configmaps = append(configmaps, s3BucketCM)

0 commit comments

Comments
 (0)