forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
258 lines (237 loc) · 10.6 KB
/
convert.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 machine
import (
"fmt"
apiv1 "github.com/kubermatic/kubermatic/api/pkg/api/v1"
clusterv1alpha1 "github.com/kubermatic/machine-controller/pkg/apis/cluster/v1alpha1"
alibaba "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/alibaba/types"
aws "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/aws/types"
azure "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/azure/types"
digitalocean "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/digitalocean/types"
gce "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/gce/types"
hetzner "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/hetzner/types"
kubevirt "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/kubevirt/types"
openstack "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/openstack/types"
packet "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/packet/types"
vsphere "github.com/kubermatic/machine-controller/pkg/cloudprovider/provider/vsphere/types"
providerconfig "github.com/kubermatic/machine-controller/pkg/providerconfig/types"
"github.com/kubermatic/machine-controller/pkg/userdata/centos"
"github.com/kubermatic/machine-controller/pkg/userdata/coreos"
"github.com/kubermatic/machine-controller/pkg/userdata/flatcar"
"github.com/kubermatic/machine-controller/pkg/userdata/rhel"
"github.com/kubermatic/machine-controller/pkg/userdata/sles"
"github.com/kubermatic/machine-controller/pkg/userdata/ubuntu"
"k8s.io/apimachinery/pkg/util/json"
)
// GetAPIV1OperatingSystemSpec returns the api compatible OperatingSystemSpec for the given machine
func GetAPIV1OperatingSystemSpec(machineSpec clusterv1alpha1.MachineSpec) (*apiv1.OperatingSystemSpec, error) {
decodedProviderSpec, err := providerconfig.GetConfig(machineSpec.ProviderSpec)
if err != nil {
return nil, fmt.Errorf("failed to get machine providerConfig: %v", err)
}
operatingSystemSpec := &apiv1.OperatingSystemSpec{}
switch decodedProviderSpec.OperatingSystem {
case providerconfig.OperatingSystemCoreos:
config := &coreos.Config{}
if err := json.Unmarshal(decodedProviderSpec.OperatingSystemSpec.Raw, &config); err != nil {
return nil, fmt.Errorf("failed to parse coreos config: %v", err)
}
operatingSystemSpec.ContainerLinux = &apiv1.ContainerLinuxSpec{
DisableAutoUpdate: config.DisableAutoUpdate,
}
case providerconfig.OperatingSystemFlatcar:
config := &flatcar.Config{}
if err := json.Unmarshal(decodedProviderSpec.OperatingSystemSpec.Raw, &config); err != nil {
return nil, fmt.Errorf("failed to parse flatcar config: %v", err)
}
operatingSystemSpec.Flatcar = &apiv1.FlatcarSpec{
DisableAutoUpdate: config.DisableAutoUpdate,
}
case providerconfig.OperatingSystemUbuntu:
config := &ubuntu.Config{}
if err := json.Unmarshal(decodedProviderSpec.OperatingSystemSpec.Raw, &config); err != nil {
return nil, fmt.Errorf("failed to parse ubuntu config: %v", err)
}
operatingSystemSpec.Ubuntu = &apiv1.UbuntuSpec{
DistUpgradeOnBoot: config.DistUpgradeOnBoot,
}
case providerconfig.OperatingSystemCentOS:
config := ¢os.Config{}
if err := json.Unmarshal(decodedProviderSpec.OperatingSystemSpec.Raw, &config); err != nil {
return nil, fmt.Errorf("failed to parse centos config: %v", err)
}
operatingSystemSpec.CentOS = &apiv1.CentOSSpec{
DistUpgradeOnBoot: config.DistUpgradeOnBoot,
}
case providerconfig.OperatingSystemSLES:
config := &sles.Config{}
if err := json.Unmarshal(decodedProviderSpec.OperatingSystemSpec.Raw, &config); err != nil {
return nil, fmt.Errorf("failed to parse sles config: %v", err)
}
operatingSystemSpec.SLES = &apiv1.SLESSpec{
DistUpgradeOnBoot: config.DistUpgradeOnBoot,
}
case providerconfig.OperatingSystemRHEL:
config := &rhel.Config{}
if err := json.Unmarshal(decodedProviderSpec.OperatingSystemSpec.Raw, &config); err != nil {
return nil, fmt.Errorf("failed to parse rhel config: %v", err)
}
operatingSystemSpec.RHEL = &apiv1.RHELSpec{
DistUpgradeOnBoot: config.DistUpgradeOnBoot,
RHELSubscriptionManagerUser: config.RHELSubscriptionManagerUser,
RHELSubscriptionManagerPassword: config.RHELSubscriptionManagerPassword,
RHSMOfflineToken: config.RHSMOfflineToken,
}
}
return operatingSystemSpec, nil
}
// GetAPIV2NodeCloudSpec returns the api compatible NodeCloudSpec for the given machine
func GetAPIV2NodeCloudSpec(machineSpec clusterv1alpha1.MachineSpec) (*apiv1.NodeCloudSpec, error) {
decodedProviderSpec, err := providerconfig.GetConfig(machineSpec.ProviderSpec)
if err != nil {
return nil, fmt.Errorf("failed to get machine providerConfig: %v", err)
}
cloudSpec := &apiv1.NodeCloudSpec{}
switch decodedProviderSpec.CloudProvider {
case providerconfig.CloudProviderAWS:
config := &aws.RawConfig{}
if err := json.Unmarshal(decodedProviderSpec.CloudProviderSpec.Raw, &config); err != nil {
return nil, fmt.Errorf("failed to parse aws config: %v", err)
}
cloudSpec.AWS = &apiv1.AWSNodeSpec{
Tags: config.Tags,
VolumeSize: config.DiskSize,
VolumeType: config.DiskType.Value,
InstanceType: config.InstanceType.Value,
AMI: config.AMI.Value,
AvailabilityZone: config.AvailabilityZone.Value,
SubnetID: config.SubnetID.Value,
AssignPublicIP: config.AssignPublicIP,
}
case providerconfig.CloudProviderAzure:
config := &azure.RawConfig{}
if err := json.Unmarshal(decodedProviderSpec.CloudProviderSpec.Raw, &config); err != nil {
return nil, fmt.Errorf("failed to parse Azure config: %v", err)
}
cloudSpec.Azure = &apiv1.AzureNodeSpec{
Size: config.VMSize.Value,
AssignPublicIP: config.AssignPublicIP.Value,
Tags: config.Tags,
ImageID: config.ImageID.Value,
}
case providerconfig.CloudProviderDigitalocean:
config := &digitalocean.RawConfig{}
if err := json.Unmarshal(decodedProviderSpec.CloudProviderSpec.Raw, &config); err != nil {
return nil, fmt.Errorf("failed to parse digitalocean config: %v", err)
}
cloudSpec.Digitalocean = &apiv1.DigitaloceanNodeSpec{
IPv6: config.IPv6.Value,
Size: config.Size.Value,
Backups: config.Backups.Value,
Monitoring: config.Monitoring.Value,
}
for _, v := range config.Tags {
cloudSpec.Digitalocean.Tags = append(cloudSpec.Digitalocean.Tags, v.Value)
}
case providerconfig.CloudProviderOpenstack:
config := &openstack.RawConfig{}
if err := json.Unmarshal(decodedProviderSpec.CloudProviderSpec.Raw, &config); err != nil {
return nil, fmt.Errorf("failed to parse openstack config: %v", err)
}
cloudSpec.Openstack = &apiv1.OpenstackNodeSpec{
Flavor: config.Flavor.Value,
Image: config.Image.Value,
Tags: config.Tags,
}
cloudSpec.Openstack.UseFloatingIP = config.FloatingIPPool.Value != ""
if config.RootDiskSizeGB != nil && *config.RootDiskSizeGB > 0 {
cloudSpec.Openstack.RootDiskSizeGB = config.RootDiskSizeGB
}
case providerconfig.CloudProviderHetzner:
config := &hetzner.RawConfig{}
if err := json.Unmarshal(decodedProviderSpec.CloudProviderSpec.Raw, &config); err != nil {
return nil, fmt.Errorf("failed to parse hetzner config: %v", err)
}
cloudSpec.Hetzner = &apiv1.HetznerNodeSpec{
Type: config.ServerType.Value,
}
case providerconfig.CloudProviderVsphere:
config := &vsphere.RawConfig{}
if err := json.Unmarshal(decodedProviderSpec.CloudProviderSpec.Raw, &config); err != nil {
return nil, fmt.Errorf("failed to parse vsphere config: %v", err)
}
cloudSpec.VSphere = &apiv1.VSphereNodeSpec{
CPUs: int(config.CPUs),
Memory: int(config.MemoryMB),
DiskSizeGB: config.DiskSizeGB,
Template: config.TemplateVMName.Value,
}
case providerconfig.CloudProviderPacket:
config := &packet.RawConfig{}
if err := json.Unmarshal(decodedProviderSpec.CloudProviderSpec.Raw, &config); err != nil {
return nil, fmt.Errorf("failed to parse packet config: %v", err)
}
cloudSpec.Packet = &apiv1.PacketNodeSpec{
InstanceType: config.InstanceType.Value,
}
for _, v := range config.Tags {
cloudSpec.Packet.Tags = append(cloudSpec.Packet.Tags, v.Value)
}
case providerconfig.CloudProviderGoogle:
config := &gce.CloudProviderSpec{}
if err := json.Unmarshal(decodedProviderSpec.CloudProviderSpec.Raw, &config); err != nil {
return nil, fmt.Errorf("failed to parse gcp config: %v", err)
}
cloudSpec.GCP = &apiv1.GCPNodeSpec{
Zone: config.Zone.Value,
MachineType: config.MachineType.Value,
DiskSize: config.DiskSize,
DiskType: config.DiskType.Value,
Preemptible: config.Preemptible.Value,
Labels: config.Labels,
Tags: config.Tags,
CustomImage: config.CustomImage.Value,
}
case providerconfig.CloudProviderKubeVirt:
config := &kubevirt.RawConfig{}
if err := json.Unmarshal(decodedProviderSpec.CloudProviderSpec.Raw, &config); err != nil {
return nil, fmt.Errorf("failed to parse kubevirt config: %v", err)
}
cloudSpec.Kubevirt = &apiv1.KubevirtNodeSpec{
CPUs: config.CPUs.Value,
Memory: config.Memory.Value,
Namespace: config.Namespace.Value,
SourceURL: config.SourceURL.Value,
StorageClassName: config.StorageClassName.Value,
PVCSize: config.PVCSize.Value,
}
case providerconfig.CloudProviderAlibaba:
config := &alibaba.RawConfig{}
if err := json.Unmarshal(decodedProviderSpec.CloudProviderSpec.Raw, &config); err != nil {
return nil, fmt.Errorf("failed to parse alibaba config: %v", err)
}
cloudSpec.Alibaba = &apiv1.AlibabaNodeSpec{
InstanceType: config.InstanceType.Value,
DiskSize: config.DiskSize.Value,
DiskType: config.DiskType.Value,
VSwitchID: config.VSwitchID.Value,
InternetMaxBandwidthOut: config.InternetMaxBandwidthOut.Value,
Labels: config.Labels,
ZoneID: config.ZoneID.Value,
}
default:
return nil, fmt.Errorf("unknown cloud provider %q", decodedProviderSpec.CloudProvider)
}
return cloudSpec, nil
}