forked from kubernetes-retired/kube-aws
/
stack_config.go
67 lines (56 loc) · 2.2 KB
/
stack_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package config
import (
"fmt"
"net/url"
"strings"
"github.com/kubernetes-incubator/kube-aws/filereader/jsontemplate"
"github.com/kubernetes-incubator/kube-aws/model"
)
// StackConfig contains configuration parameters available when rendering CFN stack template from golang text templates
type StackConfig struct {
*Config
StackTemplateOptions
UserDataController model.UserData
UserDataEtcd model.UserData
ControllerSubnetIndex int
ExtraCfnResources map[string]interface{}
}
func (c *StackConfig) s3Folders() model.S3Folders {
return model.NewS3Folders(c.S3URI, c.ClusterName)
}
func (c *StackConfig) ClusterS3URI() string {
return c.s3Folders().Cluster().URI()
}
func (c *StackConfig) ClusterExportedStacksS3URI() string {
return c.s3Folders().ClusterExportedStacks().URI()
}
// EtcdSnapshotsS3Path is a pair of a S3 bucket and a key of an S3 object containing an etcd cluster snapshot
func (c StackConfig) EtcdSnapshotsS3PathRef() (string, error) {
s3uri, err := url.Parse(c.ClusterS3URI())
if err != nil {
return "", fmt.Errorf("Error in EtcdSnapshotsS3PathRef : %v", err)
}
return fmt.Sprintf(`{ "Fn::Join" : [ "", [ "%s%s/instances/", { "Fn::Select" : [ "2", { "Fn::Split": [ "/", { "Ref": "AWS::StackId" }]} ]}, "/etcd-snapshots" ]]}`, s3uri.Host, s3uri.Path), nil
}
func (c StackConfig) EtcdSnapshotsS3Bucket() (string, error) {
s3uri, err := url.Parse(c.ClusterS3URI())
if err != nil {
return "", fmt.Errorf("Error in EtcdSnapshotsS3Bucket : %v", err)
}
return s3uri.Host, nil
}
func (c StackConfig) EtcdSnapshotsS3PrefixRef() (string, error) {
s3uri, err := url.Parse(c.ClusterS3URI())
if err != nil {
return "", fmt.Errorf("Error in EtcdSnapshotsS3Prefix : %v", err)
}
s3path := fmt.Sprintf(`{ "Fn::Join" : [ "", [ "%s/instances/", { "Fn::Select" : [ "2", { "Fn::Split": [ "/", { "Ref": "AWS::StackId" }]} ]}, "/etcd-snapshots" ]]}`, strings.TrimLeft(s3uri.Path, "/"))
return s3path, nil
}
func (c *StackConfig) RenderStackTemplateAsBytes() ([]byte, error) {
return jsontemplate.GetBytes(c.StackTemplateTmplFile, *c, c.PrettyPrint)
}
func (c *StackConfig) RenderStackTemplateAsString() (string, error) {
bytes, err := c.RenderStackTemplateAsBytes()
return string(bytes), err
}