This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
/
config.go
212 lines (175 loc) · 5.93 KB
/
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package model
import (
"fmt"
"path/filepath"
"strings"
"unicode/utf8"
"github.com/kubernetes-incubator/kube-aws/builtin"
"github.com/kubernetes-incubator/kube-aws/gzipcompressor"
"github.com/kubernetes-incubator/kube-aws/logger"
"github.com/kubernetes-incubator/kube-aws/pkg/api"
"github.com/kubernetes-incubator/kube-aws/provisioner"
)
const (
vpcLogicalName = "VPC"
internetGatewayLogicalName = "InternetGateway"
)
// Config contains configuration parameters available when rendering userdata injected into a controller or an etcd node from golang text templates
type Config struct {
*api.Cluster
AdminAPIEndpoint APIEndpoint
APIEndpoints APIEndpoints
// EtcdNodes is the golang-representation of etcd nodes, which is used to differentiate unique etcd nodes
// This is used to simplify templating of the control-plane stack template.
EtcdNodes []EtcdNode
APIServerVolumes api.APIServerVolumes
APIServerFlags api.CommandLineFlags
ControllerFlags api.CommandLineFlags
KubeSchedulerFlags api.CommandLineFlags
KubernetesManifestFiles []*provisioner.RemoteFile
HelmReleaseFilesets []api.HelmReleaseFileset
}
func (c *Config) EtcdCluster() EtcdCluster {
etcdNetwork := NewNetwork(c.Etcd.Subnets, c.NATGateways())
return NewEtcdCluster(c.Etcd.Cluster, c.Region, etcdNetwork, c.Etcd.Count)
}
// AdminAPIEndpointURL is the url of the API endpoint which is written in kubeconfig and used to by admins
func (c *Config) AdminAPIEndpointURL() string {
return fmt.Sprintf("https://%s", c.AdminAPIEndpoint.DNSName)
}
func (c *Config) APIEndpointURLPort() string {
return fmt.Sprintf("https://%s:443", c.APIEndpoints.GetDefault().DNSName)
}
func (c *Config) AWSIAMAuthenticatorClusterIDRef() string {
var rawClusterId string
if c.Kubernetes.Authentication.AWSIAM.ClusterID != "" {
rawClusterId = c.Kubernetes.Authentication.AWSIAM.ClusterID
} else {
rawClusterId = c.ClusterName
}
return fmt.Sprintf(`"%s"`, rawClusterId)
}
func (c *Config) IAMRoleARNs() []string {
arns := []string{
`{"Fn::GetAtt": ["IAMRoleController", "Arn"]}`,
}
for _, np := range c.NodePools {
arns = append(arns, fmt.Sprintf(`{"Fn::ImportValue" : {"Fn::Sub" : "${NetworkStackName}-%sIAMRoleWorkerArn"}}`, np.NodePoolName))
}
return arns
}
func (c Config) VPCLogicalName() (string, error) {
if c.VPC.HasIdentifier() {
return "", fmt.Errorf("[BUG] .VPCLogicalName should not be called in stack template when vpc id is specified")
}
return vpcLogicalName, nil
}
func (c Config) VPCID() (string, error) {
logger.Warn(".VPCID in stack template is deprecated and will be removed in v0.9.9. Please use .VPC.ID instead")
if !c.VPC.HasIdentifier() {
return "", fmt.Errorf("[BUG] .VPCID should not be called in stack template when vpc.id(FromStackOutput) is specified. Use .VPCManaged instead.")
}
return c.VPC.ID, nil
}
func (c Config) VPCManaged() bool {
return !c.VPC.HasIdentifier()
}
func (c Config) VPCRef() (string, error) {
return c.VPC.RefOrError(c.VPCLogicalName)
}
func (c Config) VPCRefFromNetworkStack() (string, error) {
i := c.VPC
if i.IDFromStackOutput != "" {
return fmt.Sprintf(`{ "Fn::ImportValue" : %q }`, i.IDFromStackOutput), nil
} else if i.ID != "" {
return fmt.Sprintf(`"%s"`, i.ID), nil
} else {
logicalName, err := c.VPCLogicalName()
if err != nil {
return "", fmt.Errorf("failed to get id or ref: %v", err)
}
return fmt.Sprintf(`{ "Ref" : %q }`, logicalName), nil
}
}
func (c Config) InternetGatewayLogicalName() string {
return internetGatewayLogicalName
}
func (c Config) InternetGatewayRef() string {
return c.InternetGateway.Ref(c.InternetGatewayLogicalName)
}
// Etcdadm returns the content of the etcdadm script to be embedded into cloud-config-etcd
func (c *Config) Etcdadm() (string, error) {
return gzipcompressor.BytesToGzippedBase64String(builtin.Bytes("etcdadm/etcdadm"))
}
// ManageELBLogicalNames returns all the logical names of the cfn resources corresponding to ELBs managed by kube-aws for API endpoints
func (c *Config) ManagedELBLogicalNames() []string {
return c.APIEndpoints.ManagedELBLogicalNames()
}
type kubernetesManifestPlugin struct {
Manifests []*provisioner.RemoteFile
}
func (p kubernetesManifestPlugin) ManifestListFile() *provisioner.RemoteFile {
paths := []string{}
for _, m := range p.Manifests {
paths = append(paths, m.Path)
}
bytes := []byte(strings.Join(paths, "\n"))
return provisioner.NewRemoteFileAtPath(p.listFilePath(), bytes)
}
func (p kubernetesManifestPlugin) listFilePath() string {
return "/srv/kube-aws/plugins/kubernetes-manifests"
}
func (p kubernetesManifestPlugin) Directory() string {
return filepath.Dir(p.listFilePath())
}
type helmReleasePlugin struct {
Releases []api.HelmReleaseFileset
}
func (p helmReleasePlugin) ReleaseListFile() *provisioner.RemoteFile {
paths := []string{}
for _, r := range p.Releases {
paths = append(paths, r.ReleaseFile.Path)
}
bytes := []byte(strings.Join(paths, "\n"))
return provisioner.NewRemoteFileAtPath(p.listFilePath(), bytes)
}
func (p helmReleasePlugin) listFilePath() string {
return "/srv/kube-aws/plugins/helm-releases"
}
func (p helmReleasePlugin) Directory() string {
return filepath.Dir(p.listFilePath())
}
func (c *Config) KubernetesManifestPlugin() kubernetesManifestPlugin {
p := kubernetesManifestPlugin{
Manifests: c.KubernetesManifestFiles,
}
return p
}
func (c *Config) HelmReleasePlugin() helmReleasePlugin {
p := helmReleasePlugin{
Releases: c.HelmReleaseFilesets,
}
return p
}
func WithTrailingDot(s string) string {
if s == "" {
return s
}
lastRune, _ := utf8.DecodeLastRuneInString(s)
if lastRune != rune('.') {
return s + "."
}
return s
}
func (c Config) NetworkStackName() string {
if c.CloudFormation.StackNameOverrides.Network != "" {
return c.CloudFormation.StackNameOverrides.Network
}
return networkStackName
}
func (c Config) EtcdStackName() string {
if c.CloudFormation.StackNameOverrides.Etcd != "" {
return c.CloudFormation.StackNameOverrides.Etcd
}
return etcdStackName
}