This repository has been archived by the owner on Aug 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 139
/
v1.go
276 lines (254 loc) · 7.78 KB
/
v1.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package loaders
import (
"fmt"
"github.com/ghodss/yaml"
kfapis "github.com/kubeflow/kfctl/v3/pkg/apis"
kfdeftypes "github.com/kubeflow/kfctl/v3/pkg/apis/apps/kfdef/v1"
kfdefgcpplugin "github.com/kubeflow/kfctl/v3/pkg/apis/apps/plugins/gcp/v1alpha1"
"github.com/kubeflow/kfctl/v3/pkg/kfconfig"
)
// Empty struct - used to implement Converter interface.
type V1 struct {
}
func (v V1) LoadKfConfig(def interface{}) (*kfconfig.KfConfig, error) {
kfdef := &kfdeftypes.KfDef{}
if bytes, err := yaml.Marshal(def); err != nil {
return nil, &kfapis.KfError{
Code: int(kfapis.INTERNAL_ERROR),
Message: fmt.Sprintf("could not marshal kfdef into bytes: %v", err),
}
} else {
err = yaml.Unmarshal(bytes, kfdef)
if err != nil {
return nil, &kfapis.KfError{
Code: int(kfapis.INTERNAL_ERROR),
Message: fmt.Sprintf("could not unpack kfdef: %v", err),
}
}
}
// Set UseBasicAuth later.
config := &kfconfig.KfConfig{
Spec: kfconfig.KfConfigSpec{
UseBasicAuth: false,
},
}
config.Name = kfdef.Name
config.Namespace = kfdef.Namespace
config.APIVersion = kfdef.APIVersion
config.Kind = "KfConfig"
config.Labels = kfdef.Labels
config.Annotations = kfdef.Annotations
config.ClusterName = kfdef.ClusterName
config.Spec.Version = kfdef.Spec.Version
for i, app := range kfdef.Spec.Applications {
if app.Name == "" {
return nil, &kfapis.KfError{
Code: int(kfapis.INVALID_ARGUMENT),
Message: fmt.Sprintf("must have name for application. missing application name on application[%d] in kfdef", i),
}
}
application := kfconfig.Application{
Name: app.Name,
}
if app.KustomizeConfig != nil {
kconfig := &kfconfig.KustomizeConfig{
Overlays: app.KustomizeConfig.Overlays,
}
if app.KustomizeConfig.RepoRef != nil {
kref := &kfconfig.RepoRef{
Name: app.KustomizeConfig.RepoRef.Name,
Path: app.KustomizeConfig.RepoRef.Path,
}
kconfig.RepoRef = kref
// Use application to infer whether UseBasicAuth is true.
if kref.Path == "common/basic-auth" {
config.Spec.UseBasicAuth = true
}
}
for _, param := range app.KustomizeConfig.Parameters {
p := kfconfig.NameValue{
Name: param.Name,
Value: param.Value,
}
kconfig.Parameters = append(kconfig.Parameters, p)
}
application.KustomizeConfig = kconfig
}
config.Spec.Applications = append(config.Spec.Applications, application)
}
for _, plugin := range kfdef.Spec.Plugins {
p := kfconfig.Plugin{
Name: plugin.Name,
Namespace: kfdef.Namespace,
Kind: kfconfig.PluginKindType(plugin.Kind),
Spec: plugin.Spec,
}
config.Spec.Plugins = append(config.Spec.Plugins, p)
if plugin.Kind == string(kfconfig.GCP_PLUGIN_KIND) {
spec := kfdefgcpplugin.GcpPluginSpec{}
if err := kfdef.GetPluginSpec(plugin.Kind, &spec); err != nil {
return nil, &kfapis.KfError{
Code: int(kfapis.INTERNAL_ERROR),
Message: fmt.Sprintf("could not retrieve GCP plugin spec: %v", err),
}
}
config.Spec.Project = spec.Project
config.Spec.Email = spec.Email
config.Spec.IpName = spec.IpName
config.Spec.Hostname = spec.Hostname
config.Spec.SkipInitProject = spec.SkipInitProject
config.Spec.Zone = spec.Zone
config.Spec.DeleteStorage = spec.DeleteStorage
}
if p := maybeGetPlatform(plugin.Kind); p != "" {
config.Spec.Platform = p
}
}
for _, secret := range kfdef.Spec.Secrets {
s := kfconfig.Secret{
Name: secret.Name,
}
src := &kfconfig.SecretSource{}
// kfdef -> kfconfig should keep literalSource , becasue only kfdef should be checked into source control,
// We only filter secrets during kfconfig -> kfdef.
if secret.SecretSource.LiteralSource != nil {
src.LiteralSource = &kfconfig.LiteralSource{
Value: secret.SecretSource.LiteralSource.Value,
}
}
if secret.SecretSource.EnvSource != nil {
src.EnvSource = &kfconfig.EnvSource{
Name: secret.SecretSource.EnvSource.Name,
}
}
s.SecretSource = src
config.Spec.Secrets = append(config.Spec.Secrets, s)
}
for _, repo := range kfdef.Spec.Repos {
r := kfconfig.Repo{
Name: repo.Name,
URI: repo.URI,
}
config.Spec.Repos = append(config.Spec.Repos, r)
}
for _, cond := range kfdef.Status.Conditions {
c := kfconfig.Condition{
Type: kfconfig.ConditionType(cond.Type),
Status: cond.Status,
LastUpdateTime: cond.LastUpdateTime,
LastTransitionTime: cond.LastTransitionTime,
Reason: cond.Reason,
Message: cond.Message,
}
config.Status.Conditions = append(config.Status.Conditions, c)
}
for _, cache := range kfdef.Status.ReposCache {
c := kfconfig.Cache{
Name: cache.Name,
LocalPath: cache.LocalPath,
}
config.Status.Caches = append(config.Status.Caches, c)
}
return config, nil
}
func (v V1) LoadKfDef(config kfconfig.KfConfig, out interface{}) error {
kfdef := &kfdeftypes.KfDef{}
kfdef.Name = config.Name
kfdef.Namespace = config.Namespace
kfdef.APIVersion = config.APIVersion
kfdef.Kind = "KfDef"
kfdef.Labels = config.Labels
kfdef.Annotations = config.Annotations
kfdef.ClusterName = config.ClusterName
kfdef.Spec.Version = config.Spec.Version
for _, app := range config.Spec.Applications {
application := kfdeftypes.Application{
Name: app.Name,
}
if app.KustomizeConfig != nil {
kconfig := &kfdeftypes.KustomizeConfig{
Overlays: app.KustomizeConfig.Overlays,
}
if app.KustomizeConfig.RepoRef != nil {
kref := &kfdeftypes.RepoRef{
Name: app.KustomizeConfig.RepoRef.Name,
Path: app.KustomizeConfig.RepoRef.Path,
}
kconfig.RepoRef = kref
}
for _, param := range app.KustomizeConfig.Parameters {
p := kfdeftypes.NameValue{
Name: param.Name,
Value: param.Value,
}
kconfig.Parameters = append(kconfig.Parameters, p)
}
application.KustomizeConfig = kconfig
}
kfdef.Spec.Applications = append(kfdef.Spec.Applications, application)
}
for _, plugin := range config.Spec.Plugins {
p := kfdeftypes.Plugin{
Spec: plugin.Spec,
}
p.Name = plugin.Name
p.Kind = string(plugin.Kind)
kfdef.Spec.Plugins = append(kfdef.Spec.Plugins, p)
}
for _, secret := range config.Spec.Secrets {
s := kfdeftypes.Secret{
Name: secret.Name,
}
if secret.SecretSource != nil {
s.SecretSource = &kfdeftypes.SecretSource{}
// We don't want to store literalSource explictly, becasue we want the config to be checked into source control and don't want secrets in source control.
if secret.SecretSource.EnvSource != nil {
s.SecretSource.EnvSource = &kfdeftypes.EnvSource{
Name: secret.SecretSource.EnvSource.Name,
}
}
}
kfdef.Spec.Secrets = append(kfdef.Spec.Secrets, s)
}
for _, repo := range config.Spec.Repos {
r := kfdeftypes.Repo{
Name: repo.Name,
URI: repo.URI,
}
kfdef.Spec.Repos = append(kfdef.Spec.Repos, r)
}
for _, cond := range config.Status.Conditions {
c := kfdeftypes.KfDefCondition{
Type: kfdeftypes.KfDefConditionType(cond.Type),
Status: cond.Status,
LastUpdateTime: cond.LastUpdateTime,
LastTransitionTime: cond.LastTransitionTime,
Reason: cond.Reason,
Message: cond.Message,
}
kfdef.Status.Conditions = append(kfdef.Status.Conditions, c)
}
for _, cache := range config.Status.Caches {
c := kfdeftypes.RepoCache{
Name: cache.Name,
LocalPath: cache.LocalPath,
}
kfdef.Status.ReposCache = append(kfdef.Status.ReposCache, c)
}
kfdefBytes, err := yaml.Marshal(kfdef)
if err != nil {
return &kfapis.KfError{
Code: int(kfapis.INTERNAL_ERROR),
Message: fmt.Sprintf("error when marshaling to KfDef: %v", err),
}
}
err = yaml.Unmarshal(kfdefBytes, out)
if err == nil {
return nil
} else {
return &kfapis.KfError{
Code: int(kfapis.INTERNAL_ERROR),
Message: fmt.Sprintf("error when unmarshaling to KfDef: %v", err),
}
}
}