This repository has been archived by the owner on Jul 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
input_converter.go
179 lines (149 loc) · 5.79 KB
/
input_converter.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
package provisioning
import (
"fmt"
"strings"
"github.com/kyma-incubator/compass/components/provisioner/internal/installation/release"
"github.com/kyma-incubator/compass/components/provisioner/internal/persistence/dberrors"
"github.com/kyma-incubator/compass/components/provisioner/internal/util"
"github.com/pkg/errors"
"github.com/kyma-incubator/compass/components/provisioner/internal/model"
"github.com/kyma-incubator/compass/components/provisioner/internal/uuid"
"github.com/kyma-incubator/compass/components/provisioner/pkg/gqlschema"
)
type InputConverter interface {
ProvisioningInputToCluster(runtimeID string, input gqlschema.ProvisionRuntimeInput, tenant, subAccountId string) (model.Cluster, error)
KymaConfigFromInput(runtimeID string, input gqlschema.KymaConfigInput) (model.KymaConfig, error)
}
func NewInputConverter(uuidGenerator uuid.UUIDGenerator, releaseRepo release.Provider, gardenerProject string) InputConverter {
return &converter{
uuidGenerator: uuidGenerator,
releaseRepo: releaseRepo,
gardenerProject: gardenerProject,
}
}
type converter struct {
uuidGenerator uuid.UUIDGenerator
releaseRepo release.Provider
gardenerProject string
}
func (c converter) ProvisioningInputToCluster(runtimeID string, input gqlschema.ProvisionRuntimeInput, tenant, subAccountId string) (model.Cluster, error) {
var err error
var kymaConfig model.KymaConfig
if input.KymaConfig != nil {
kymaConfig, err = c.KymaConfigFromInput(runtimeID, *input.KymaConfig)
if err != nil {
return model.Cluster{}, err
}
}
if input.ClusterConfig == nil {
return model.Cluster{}, fmt.Errorf("error: ClusterConfig not provided")
}
gardenerConfig, err := c.gardenerConfigFromInput(runtimeID, input.ClusterConfig.GardenerConfig)
if err != nil {
return model.Cluster{}, err
}
return model.Cluster{
ID: runtimeID,
KymaConfig: kymaConfig,
ClusterConfig: gardenerConfig,
Tenant: tenant,
SubAccountId: &subAccountId,
}, nil
}
func (c converter) gardenerConfigFromInput(runtimeID string, input *gqlschema.GardenerConfigInput) (model.GardenerConfig, error) {
if input == nil {
return model.GardenerConfig{}, fmt.Errorf("error: GardenerConfig not provided")
}
providerSpecificConfig, err := c.providerSpecificConfigFromInput(input.ProviderSpecificConfig)
if err != nil {
return model.GardenerConfig{}, err
}
return model.GardenerConfig{
ID: c.uuidGenerator.New(),
Name: c.createGardenerClusterName(),
ProjectName: c.gardenerProject,
KubernetesVersion: input.KubernetesVersion,
VolumeSizeGB: input.VolumeSizeGb,
DiskType: input.DiskType,
MachineType: input.MachineType,
Provider: input.Provider,
Purpose: input.Purpose,
LicenceType: input.LicenceType,
Seed: util.UnwrapStr(input.Seed),
TargetSecret: input.TargetSecret,
WorkerCidr: input.WorkerCidr,
Region: input.Region,
AutoScalerMin: input.AutoScalerMin,
AutoScalerMax: input.AutoScalerMax,
MaxSurge: input.MaxSurge,
MaxUnavailable: input.MaxUnavailable,
ClusterID: runtimeID,
GardenerProviderConfig: providerSpecificConfig,
}, nil
}
func (c converter) createGardenerClusterName() string {
id := c.uuidGenerator.New()
name := strings.ReplaceAll(id, "-", "")
name = fmt.Sprintf("%.7s", name)
name = util.StartWithLetter(name)
name = strings.ToLower(name)
return name
}
func (c converter) providerSpecificConfigFromInput(input *gqlschema.ProviderSpecificInput) (model.GardenerProviderConfig, error) {
if input == nil {
return nil, errors.New("provider config not specified")
}
if input.GcpConfig != nil {
return model.NewGCPGardenerConfig(input.GcpConfig)
}
if input.AzureConfig != nil {
return model.NewAzureGardenerConfig(input.AzureConfig)
}
if input.AwsConfig != nil {
return model.NewAWSGardenerConfig(input.AwsConfig)
}
return nil, errors.New("provider config not specified")
}
func (c converter) KymaConfigFromInput(runtimeID string, input gqlschema.KymaConfigInput) (model.KymaConfig, error) {
kymaRelease, err := c.releaseRepo.GetReleaseByVersion(input.Version)
if err != nil {
if err.Code() == dberrors.CodeNotFound {
return model.KymaConfig{}, errors.Errorf("Kyma Release %s not found", input.Version)
}
return model.KymaConfig{}, errors.WithMessagef(err, "Failed to get Kyma Release with version %s", input.Version)
}
var components []model.KymaComponentConfig
kymaConfigID := c.uuidGenerator.New()
for i, component := range input.Components {
id := c.uuidGenerator.New()
kymaConfigModule := model.KymaComponentConfig{
ID: id,
Component: model.KymaComponent(component.Component),
Namespace: component.Namespace,
SourceURL: component.SourceURL,
Configuration: c.configurationFromInput(component.Configuration),
ComponentOrder: i + 1,
KymaConfigID: kymaConfigID,
}
components = append(components, kymaConfigModule)
}
return model.KymaConfig{
ID: kymaConfigID,
Release: kymaRelease,
Components: components,
ClusterID: runtimeID,
GlobalConfiguration: c.configurationFromInput(input.Configuration),
}, nil
}
func (c converter) configurationFromInput(input []*gqlschema.ConfigEntryInput) model.Configuration {
configuration := model.Configuration{
ConfigEntries: make([]model.ConfigEntry, 0, len(input)),
}
for _, ce := range input {
configuration.ConfigEntries = append(configuration.ConfigEntries, configEntryFromInput(ce))
}
return configuration
}
func configEntryFromInput(entry *gqlschema.ConfigEntryInput) model.ConfigEntry {
return model.NewConfigEntry(entry.Key, entry.Value, util.BoolFromPtr(entry.Secret))
}