generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 10
/
create.go
258 lines (216 loc) · 8.97 KB
/
create.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
package create
import (
"fmt"
"ocm.software/ocm/api/ocm/compdesc"
"ocm.software/ocm/api/ocm/cpi"
"ocm.software/ocm/api/ocm/extensions/repositories/comparch"
commonerrors "github.com/kyma-project/modulectl/internal/common/errors"
"github.com/kyma-project/modulectl/internal/service/componentarchive"
"github.com/kyma-project/modulectl/internal/service/componentdescriptor"
"github.com/kyma-project/modulectl/internal/service/contentprovider"
)
type ModuleConfigService interface {
ParseAndValidateModuleConfig(moduleConfigFile string) (*contentprovider.ModuleConfig, error)
}
type FileSystem interface {
ReadFile(path string) ([]byte, error)
}
type FileResolver interface {
Resolve(file string) (string, error)
CleanupTempFiles() []error
}
type SecurityConfigService interface {
ParseSecurityConfigData(securityConfigFile string) (*contentprovider.SecurityScanConfig, error)
AppendSecurityScanConfig(descriptor *compdesc.ComponentDescriptor,
securityConfig contentprovider.SecurityScanConfig) error
}
type GitSourcesService interface {
AddGitSources(componentDescriptor *compdesc.ComponentDescriptor, gitRepoURL, moduleVersion string) error
}
type ComponentArchiveService interface {
CreateComponentArchive(componentDescriptor *compdesc.ComponentDescriptor) (*comparch.ComponentArchive,
error)
AddModuleResourcesToArchive(componentArchive componentarchive.ComponentArchive,
moduleResources []componentdescriptor.Resource) error
}
type RegistryService interface {
PushComponentVersion(archive *comparch.ComponentArchive, insecure bool, credentials, registryURL string) error
GetComponentVersion(archive *comparch.ComponentArchive, insecure bool,
userPasswordCreds, registryURL string) (cpi.ComponentVersionAccess, error)
}
type ModuleTemplateService interface {
GenerateModuleTemplate(moduleConfig *contentprovider.ModuleConfig,
descriptor *compdesc.ComponentDescriptor,
data []byte,
isCrdClusterScoped bool,
templateOutput string) error
}
type CRDParserService interface {
IsCRDClusterScoped(crPath, manifestPath string) (bool, error)
}
type Service struct {
moduleConfigService ModuleConfigService
gitSourcesService GitSourcesService
securityConfigService SecurityConfigService
componentArchiveService ComponentArchiveService
registryService RegistryService
moduleTemplateService ModuleTemplateService
crdParserService CRDParserService
manifestFileResolver FileResolver
defaultCRFileResolver FileResolver
fileSystem FileSystem
}
func NewService(moduleConfigService ModuleConfigService,
gitSourcesService GitSourcesService,
securityConfigService SecurityConfigService,
componentArchiveService ComponentArchiveService,
registryService RegistryService,
moduleTemplateService ModuleTemplateService,
crdParserService CRDParserService,
manifestFileResolver FileResolver,
defaultCRFileResolver FileResolver,
fileSystem FileSystem,
) (*Service, error) {
if moduleConfigService == nil {
return nil, fmt.Errorf("moduleConfigService must not be nil: %w", commonerrors.ErrInvalidArg)
}
if gitSourcesService == nil {
return nil, fmt.Errorf("gitSourcesService must not be nil: %w", commonerrors.ErrInvalidArg)
}
if securityConfigService == nil {
return nil, fmt.Errorf("securityConfigService must not be nil: %w", commonerrors.ErrInvalidArg)
}
if componentArchiveService == nil {
return nil, fmt.Errorf("componentArchiveService must not be nil: %w", commonerrors.ErrInvalidArg)
}
if registryService == nil {
return nil, fmt.Errorf("registryService must not be nil: %w", commonerrors.ErrInvalidArg)
}
if moduleTemplateService == nil {
return nil, fmt.Errorf("moduleTemplateService must not be nil: %w", commonerrors.ErrInvalidArg)
}
if crdParserService == nil {
return nil, fmt.Errorf("crdParserService must not be nil: %w", commonerrors.ErrInvalidArg)
}
if manifestFileResolver == nil {
return nil, fmt.Errorf("manifestFileResolver must not be nil: %w", commonerrors.ErrInvalidArg)
}
if defaultCRFileResolver == nil {
return nil, fmt.Errorf("defaultCRFileResolver must not be nil: %w", commonerrors.ErrInvalidArg)
}
if fileSystem == nil {
return nil, fmt.Errorf("fileSystem must not be nil: %w", commonerrors.ErrInvalidArg)
}
return &Service{
moduleConfigService: moduleConfigService,
gitSourcesService: gitSourcesService,
securityConfigService: securityConfigService,
componentArchiveService: componentArchiveService,
registryService: registryService,
moduleTemplateService: moduleTemplateService,
crdParserService: crdParserService,
manifestFileResolver: manifestFileResolver,
defaultCRFileResolver: defaultCRFileResolver,
fileSystem: fileSystem,
}, nil
}
func (s *Service) Run(opts Options) error {
if err := opts.Validate(); err != nil {
return err
}
defer func() {
if err := s.defaultCRFileResolver.CleanupTempFiles(); err != nil {
opts.Out.Write(fmt.Sprintf("failed to cleanup temporary default CR files: %v\n", err))
}
if err := s.manifestFileResolver.CleanupTempFiles(); err != nil {
opts.Out.Write(fmt.Sprintf("failed to cleanup temporary manifest files: %v\n", err))
}
}()
moduleConfig, err := s.moduleConfigService.ParseAndValidateModuleConfig(opts.ConfigFile)
if err != nil {
return fmt.Errorf("failed to parse module config: %w", err)
}
manifestFilePath, err := s.manifestFileResolver.Resolve(moduleConfig.Manifest)
if err != nil {
return fmt.Errorf("failed to resolve manifest file: %w", err)
}
defaultCRFilePath := moduleConfig.DefaultCR
if moduleConfig.DefaultCR != "" {
defaultCRFilePath, err = s.defaultCRFileResolver.Resolve(moduleConfig.DefaultCR)
if err != nil {
return fmt.Errorf("failed to resolve default CR file: %w", err)
}
}
descriptor, err := componentdescriptor.InitializeComponentDescriptor(moduleConfig.Name, moduleConfig.Version)
if err != nil {
return fmt.Errorf("failed to populate component descriptor metadata: %w", err)
}
moduleResources, err := componentdescriptor.GenerateModuleResources(moduleConfig.Version, manifestFilePath,
defaultCRFilePath, opts.RegistryCredSelector)
if err != nil {
return fmt.Errorf("failed to generate module resources: %w", err)
}
if err = s.gitSourcesService.AddGitSources(descriptor, moduleConfig.Repository,
moduleConfig.Version); err != nil {
return fmt.Errorf("failed to add git sources: %w", err)
}
if moduleConfig.Security != "" {
err = s.configureSecScannerConf(descriptor, moduleConfig, opts)
if err != nil {
return fmt.Errorf("failed to configure security scanners: %w", err)
}
}
opts.Out.Write("- Creating component archive\n")
archive, err := s.componentArchiveService.CreateComponentArchive(descriptor)
if err != nil {
return fmt.Errorf("failed to create component archive: %w", err)
}
if err = s.componentArchiveService.AddModuleResourcesToArchive(archive,
moduleResources); err != nil {
return fmt.Errorf("failed to add module resources to component archive: %w", err)
}
if opts.RegistryURL != "" {
return s.pushImgAndCreateTemplate(archive, moduleConfig, manifestFilePath, defaultCRFilePath, opts)
}
return nil
}
func (s *Service) pushImgAndCreateTemplate(archive *comparch.ComponentArchive, moduleConfig *contentprovider.ModuleConfig, manifestFilePath, defaultCRFilePath string, opts Options) error {
opts.Out.Write("- Pushing component version\n")
isCRDClusterScoped, err := s.crdParserService.IsCRDClusterScoped(defaultCRFilePath, manifestFilePath)
if err != nil {
return fmt.Errorf("failed to determine if CRD is cluster scoped: %w", err)
}
if err := s.registryService.PushComponentVersion(archive, opts.Insecure, opts.Credentials,
opts.RegistryURL); err != nil {
return fmt.Errorf("failed to push component archive: %w", err)
}
componentVersionAccess, err := s.registryService.GetComponentVersion(archive, opts.Insecure, opts.Credentials, opts.RegistryURL)
if err != nil {
return fmt.Errorf("failed to get component version: %w", err)
}
var crData []byte
if defaultCRFilePath != "" {
crData, err = s.fileSystem.ReadFile(defaultCRFilePath)
if err != nil {
return fmt.Errorf("failed to get default CR data: %w", err)
}
}
opts.Out.Write("- Generating ModuleTemplate\n")
descriptor := componentVersionAccess.GetDescriptor()
if err = s.moduleTemplateService.GenerateModuleTemplate(moduleConfig, descriptor,
crData, isCRDClusterScoped, opts.TemplateOutput); err != nil {
return fmt.Errorf("failed to generate module template: %w", err)
}
return nil
}
func (s *Service) configureSecScannerConf(descriptor *compdesc.ComponentDescriptor, moduleConfig *contentprovider.ModuleConfig, opts Options) error {
opts.Out.Write("- Configuring security scanners config\n")
securityConfig, err := s.securityConfigService.ParseSecurityConfigData(moduleConfig.Security)
if err != nil {
return fmt.Errorf("failed to parse security config data: %w", err)
}
if err = s.securityConfigService.AppendSecurityScanConfig(descriptor, *securityConfig); err != nil {
return fmt.Errorf("failed to append security scan config: %w", err)
}
return nil
}