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

Commit

Permalink
Merge pull request #72 from christopherhein/feature/71-static-site-s3
Browse files Browse the repository at this point in the history
Adding Support for Creating Static S3 Sites
  • Loading branch information
Christopher Hein committed Sep 7, 2018
2 parents 29b2737 + 8866dc1 commit 8317409
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 1 deletion.
28 changes: 28 additions & 0 deletions cloudformation/s3bucket.yaml
Expand Up @@ -73,6 +73,21 @@ Parameters:
- BucketOwnerFullControl
- AwsExecRead
Default: "Private"
EnableStaticSite:
Description: enable static site
Type: String
AllowedValues:
- 'true'
- 'false'
Default: 'false'
StaticSiteIndex:
Description: Index document for the S3 bucket static site
Type: String
Default: 'index.html'
StaticSiteError:
Description: Error document for the S3 bucket static site
Type: String
Default: "500.html"
Mappings: {}
Conditions:
UseLogging: !Equals
Expand All @@ -84,12 +99,20 @@ Conditions:
UseVersioning: !Equals
- !Ref EnableVersioning
- 'true'
UseAsStaticSite: !Equals
- !Ref EnableStaticSite
- 'true'
Resources:
S3bucket:
Type: 'AWS::S3::Bucket'
Properties:
BucketName: !Ref BucketName
AccessControl: !Ref BucketAccessControl
WebsiteConfiguration: !If
- UseAsStaticSite
- IndexDocument: !Ref StaticSiteIndex
ErrorDocument: !Ref StaticSiteError
- !Ref 'AWS::NoValue'
LifecycleConfiguration:
Rules:
- Id: GlacierRule
Expand Down Expand Up @@ -139,3 +162,8 @@ Outputs:
- S3bucket
- Arn
Description: Name of the Amazon S3 bucket
WebsiteURL:
Value: !GetAtt
- S3bucket
- WebsiteURL
Description: URL of the bucket that was created
6 changes: 5 additions & 1 deletion examples/s3bucket.yaml
Expand Up @@ -4,7 +4,11 @@ metadata:
name: s3bucket.aws-operator.com
spec:
versioning: true
accessControl: Private
accessControl: PublicRead
website:
enabled: true
indexPage: index.html
errorPage: 500.html
logging:
enabled: false
prefix: "archive"
37 changes: 37 additions & 0 deletions models/s3bucket.yaml
Expand Up @@ -59,6 +59,34 @@ spec:
Logging prefix to attach to all logs in the bucket.
structKey: Prefix
templateKey: LoggingPrefix
- key: website
type: object
description: |
Defines the options for creating a static website
structKey: Website
templateKey: Website
properties:
- key: enabled
type: bool
false: true
description: |
Website Enabled configures the S3 Bucket as a static site
structKey: Enabled
templateKey: EnableStaticSite
- key: indexPage
type: string
default: "index.html"
description: |
Index document for the S3 bucket
structKey: IndexPage
templateKey: StaticSiteIndex
- key: errorPage
type: string
default: "500.html"
description: |
Error document for the S3 bucket
structKey: ErrorPage
templateKey: StaticSiteError
output:
schema:
type: object
Expand All @@ -75,6 +103,13 @@ spec:
BucketARN is the full Amazon ARN for the bucket created
structKey: BucketARN
templateKey: BucketArn
- key: websiteURL
type: string
description: |
WebsiteURL is the DNS record for the static site
structKey: WebsiteURL
templateKey: WebsiteURL

additionalResources:
services:
- name: s3BucketSvc
Expand All @@ -95,4 +130,6 @@ spec:
value: "{{.Config.Region}}"
- key: bucketURL
value: "{{.Obj.Name}}.s3-{{.Config.Region}}.amazonaws.com"
- key: websiteURL
value: "{{.Obj.Output.WebsiteURL}}"

9 changes: 9 additions & 0 deletions pkg/apis/operator.aws/v1alpha1/s3bucket.go
Expand Up @@ -33,12 +33,21 @@ type S3BucketSpec struct {
Versioning bool `json:"versioning"`
AccessControl string `json:"accessControl"`
Logging S3BucketLogging `json:"logging"`
Website S3BucketWebsite `json:"website"`
}

// S3BucketWebsite defines the Website resource for S3Bucket
type S3BucketWebsite struct {
Enabled bool `json:"enabled"`
IndexPage string `json:"indexPage"`
ErrorPage string `json:"errorPage"`
}

// S3BucketOutput defines the output resource for S3Bucket
type S3BucketOutput struct {
BucketName string `json:"bucketName"`
BucketARN string `json:"bucketARN"`
WebsiteURL string `json:"websiteURL"`
}

// S3BucketStatus holds the status of the Cloudformation template
Expand Down
17 changes: 17 additions & 0 deletions pkg/apis/operator.aws/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions pkg/operator/s3bucket/cft.go
Expand Up @@ -105,6 +105,24 @@ func (s *Cloudformation) CreateStack() (output *cloudformation.CreateStackOutput
return output, err
}
loggingprefix := helpers.CreateParam("LoggingPrefix", helpers.Stringify(loggingprefixValue))
websiteenabledTemp := "{{.Obj.Spec.Website.Enabled}}"
websiteenabledValue, err := helpers.Templatize(websiteenabledTemp, helpers.Data{Obj: s.S3Bucket, Config: s.config, Helpers: helpers.New()})
if err != nil {
return output, err
}
websiteenabled := helpers.CreateParam("EnableStaticSite", helpers.Stringify(websiteenabledValue))
websiteindexPageTemp := "{{.Obj.Spec.Website.IndexPage}}"
websiteindexPageValue, err := helpers.Templatize(websiteindexPageTemp, helpers.Data{Obj: s.S3Bucket, Config: s.config, Helpers: helpers.New()})
if err != nil {
return output, err
}
websiteindexPage := helpers.CreateParam("StaticSiteIndex", helpers.Stringify(websiteindexPageValue))
websiteerrorPageTemp := "{{.Obj.Spec.Website.ErrorPage}}"
websiteerrorPageValue, err := helpers.Templatize(websiteerrorPageTemp, helpers.Data{Obj: s.S3Bucket, Config: s.config, Helpers: helpers.New()})
if err != nil {
return output, err
}
websiteerrorPage := helpers.CreateParam("StaticSiteError", helpers.Stringify(websiteerrorPageValue))

parameters := []*cloudformation.Parameter{}
parameters = append(parameters, resourceName)
Expand All @@ -116,6 +134,9 @@ func (s *Cloudformation) CreateStack() (output *cloudformation.CreateStackOutput
parameters = append(parameters, accessControl)
parameters = append(parameters, loggingenabled)
parameters = append(parameters, loggingprefix)
parameters = append(parameters, websiteenabled)
parameters = append(parameters, websiteindexPage)
parameters = append(parameters, websiteerrorPage)

stackInputs.SetParameters(parameters)

Expand Down Expand Up @@ -180,6 +201,24 @@ func (s *Cloudformation) UpdateStack(updated *awsV1alpha1.S3Bucket) (output *clo
return output, err
}
loggingprefix := helpers.CreateParam("LoggingPrefix", helpers.Stringify(loggingprefixValue))
websiteenabledTemp := "{{.Obj.Spec.Website.Enabled}}"
websiteenabledValue, err := helpers.Templatize(websiteenabledTemp, helpers.Data{Obj: updated, Config: s.config, Helpers: helpers.New()})
if err != nil {
return output, err
}
websiteenabled := helpers.CreateParam("EnableStaticSite", helpers.Stringify(websiteenabledValue))
websiteindexPageTemp := "{{.Obj.Spec.Website.IndexPage}}"
websiteindexPageValue, err := helpers.Templatize(websiteindexPageTemp, helpers.Data{Obj: updated, Config: s.config, Helpers: helpers.New()})
if err != nil {
return output, err
}
websiteindexPage := helpers.CreateParam("StaticSiteIndex", helpers.Stringify(websiteindexPageValue))
websiteerrorPageTemp := "{{.Obj.Spec.Website.ErrorPage}}"
websiteerrorPageValue, err := helpers.Templatize(websiteerrorPageTemp, helpers.Data{Obj: updated, Config: s.config, Helpers: helpers.New()})
if err != nil {
return output, err
}
websiteerrorPage := helpers.CreateParam("StaticSiteError", helpers.Stringify(websiteerrorPageValue))

parameters := []*cloudformation.Parameter{}
parameters = append(parameters, resourceName)
Expand All @@ -191,6 +230,9 @@ func (s *Cloudformation) UpdateStack(updated *awsV1alpha1.S3Bucket) (output *clo
parameters = append(parameters, accessControl)
parameters = append(parameters, loggingenabled)
parameters = append(parameters, loggingprefix)
parameters = append(parameters, websiteenabled)
parameters = append(parameters, websiteindexPage)
parameters = append(parameters, websiteerrorPage)

stackInputs.SetParameters(parameters)

Expand Down
2 changes: 2 additions & 0 deletions pkg/operator/s3bucket/controller.go
Expand Up @@ -216,6 +216,7 @@ func updateStatus(config *config.Config, name string, namespace string, stackID
}
resourceCopy.Output.BucketName = outputs["BucketName"]
resourceCopy.Output.BucketARN = outputs["BucketArn"]
resourceCopy.Output.WebsiteURL = outputs["WebsiteURL"]
}

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

0 comments on commit 8317409

Please sign in to comment.