forked from kubermatic/machine-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudconfig.go
144 lines (123 loc) · 5.17 KB
/
cloudconfig.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
/*
Copyright 2019 The Machine Controller Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package types
import (
"bytes"
"fmt"
"strconv"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/magnusr/kubermatic-machine-controller/pkg/ini"
)
// use-octavia is enabled by default in CCM since v1.17.0, and disabled by
// default with the in-tree cloud provider.
// https://v1-18.docs.kubernetes.io/docs/concepts/cluster-administration/cloud-providers/#load-balancer
const (
cloudConfigTpl = `[Global]
auth-url = {{ .Global.AuthURL | iniEscape }}
{{- if .Global.ApplicationCredentialID }}
application-credential-id = {{ .Global.ApplicationCredentialID | iniEscape }}
application-credential-secret = {{ .Global.ApplicationCredentialSecret | iniEscape }}
{{- else }}
username = {{ .Global.Username | iniEscape }}
password = {{ .Global.Password | iniEscape }}
tenant-name = {{ .Global.ProjectName | iniEscape }}
tenant-id = {{ .Global.ProjectID | iniEscape }}
domain-name = {{ .Global.DomainName | iniEscape }}
{{- end }}
region = {{ .Global.Region | iniEscape }}
[LoadBalancer]
lb-version = {{ default "v2" .LoadBalancer.LBVersion | iniEscape }}
subnet-id = {{ .LoadBalancer.SubnetID | iniEscape }}
floating-network-id = {{ .LoadBalancer.FloatingNetworkID | iniEscape }}
lb-method = {{ default "ROUND_ROBIN" .LoadBalancer.LBMethod | iniEscape }}
lb-provider = {{ .LoadBalancer.LBProvider | iniEscape }}
{{- if .LoadBalancer.UseOctavia }}
use-octavia = {{ .LoadBalancer.UseOctavia | boolPtr }}
{{- end }}
{{- if .LoadBalancer.CreateMonitor }}
create-monitor = {{ .LoadBalancer.CreateMonitor }}
monitor-delay = {{ .LoadBalancer.MonitorDelay }}
monitor-timeout = {{ .LoadBalancer.MonitorTimeout }}
monitor-max-retries = {{ .LoadBalancer.MonitorMaxRetries }}
{{- end}}
{{- if semverCompare "~1.9.10 || ~1.10.6 || ~1.11.1 || >=1.12.*" .Version }}
manage-security-groups = {{ .LoadBalancer.ManageSecurityGroups }}
{{- end }}
[BlockStorage]
{{- if semverCompare ">=1.9" .Version }}
ignore-volume-az = {{ .BlockStorage.IgnoreVolumeAZ }}
{{- end }}
trust-device-path = {{ .BlockStorage.TrustDevicePath }}
bs-version = {{ default "auto" .BlockStorage.BSVersion | iniEscape }}
{{- if .BlockStorage.NodeVolumeAttachLimit }}
node-volume-attach-limit = {{ .BlockStorage.NodeVolumeAttachLimit }}
{{- end }}
`
)
type LoadBalancerOpts struct {
LBVersion string `gcfg:"lb-version"`
SubnetID string `gcfg:"subnet-id"`
FloatingNetworkID string `gcfg:"floating-network-id"`
LBMethod string `gcfg:"lb-method"`
LBProvider string `gcfg:"lb-provider"`
CreateMonitor bool `gcfg:"create-monitor"`
MonitorDelay ini.Duration `gcfg:"monitor-delay"`
MonitorTimeout ini.Duration `gcfg:"monitor-timeout"`
MonitorMaxRetries uint `gcfg:"monitor-max-retries"`
ManageSecurityGroups bool `gcfg:"manage-security-groups"`
UseOctavia *bool `gcfg:"use-octavia"`
}
type BlockStorageOpts struct {
BSVersion string `gcfg:"bs-version"`
TrustDevicePath bool `gcfg:"trust-device-path"`
IgnoreVolumeAZ bool `gcfg:"ignore-volume-az"`
NodeVolumeAttachLimit uint `gcfg:"node-volume-attach-limit"`
}
type GlobalOpts struct {
AuthURL string `gcfg:"auth-url"`
Username string
Password string
ApplicationCredentialID string `gcfg:"application-credential-id"`
ApplicationCredentialSecret string `gcfg:"application-credential-secret"`
// project name formerly known as tenant name.
// it serialized as tenant-name because openstack CCM reads only tenant-name. In CCM, internally project and tenant
// are stored into tenant-name.
ProjectName string `gcfg:"tenant-name"`
// project id formerly known as tenant id.
// serialized as tenant-id for same reason as ProjectName
ProjectID string `gcfg:"tenant-id"`
DomainName string `gcfg:"domain-name"`
Region string
}
// CloudConfig is used to read and store information from the cloud configuration file.
type CloudConfig struct {
Global GlobalOpts
LoadBalancer LoadBalancerOpts
BlockStorage BlockStorageOpts
Version string
}
func CloudConfigToString(c *CloudConfig) (string, error) {
funcMap := sprig.TxtFuncMap()
funcMap["iniEscape"] = ini.Escape
funcMap["boolPtr"] = func(b *bool) string { return strconv.FormatBool(*b) }
tpl, err := template.New("cloud-config").Funcs(funcMap).Parse(cloudConfigTpl)
if err != nil {
return "", fmt.Errorf("failed to parse the cloud config template: %w", err)
}
buf := &bytes.Buffer{}
if err := tpl.Execute(buf, c); err != nil {
return "", fmt.Errorf("failed to execute cloud config template: %w", err)
}
return buf.String(), nil
}