forked from kyma-project/lifecycle-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_to_module.go
235 lines (212 loc) · 7.37 KB
/
template_to_module.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
package parser
import (
"context"
"encoding/json"
"errors"
"fmt"
machineryruntime "k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"github.com/kyma-project/lifecycle-manager/api/shared"
"github.com/kyma-project/lifecycle-manager/api/v1beta2"
"github.com/kyma-project/lifecycle-manager/internal/descriptor/provider"
"github.com/kyma-project/lifecycle-manager/internal/manifest/img"
"github.com/kyma-project/lifecycle-manager/pkg/log"
"github.com/kyma-project/lifecycle-manager/pkg/module/common"
"github.com/kyma-project/lifecycle-manager/pkg/templatelookup"
)
type Parser struct {
client.Client
descriptorProvider *provider.CachedDescriptorProvider
inKCPMode bool
remoteSyncNamespace string
}
func NewParser(clnt client.Client,
descriptorProvider *provider.CachedDescriptorProvider,
inKCPMode bool,
remoteSyncNamespace string,
) *Parser {
return &Parser{
Client: clnt,
descriptorProvider: descriptorProvider,
inKCPMode: inKCPMode,
remoteSyncNamespace: remoteSyncNamespace,
}
}
func (p *Parser) GenerateModulesFromTemplates(kyma *v1beta2.Kyma, templates templatelookup.ModuleTemplatesByModuleName,
) common.Modules {
// First, we fetch the module spec from the template and use it to resolve it into an arbitrary object
// (since we do not know which module we are dealing with)
modules := make(common.Modules, 0)
for _, module := range templatelookup.FetchModuleInfo(kyma) {
template := templates[module.Name]
modules = p.appendModuleWithInformation(module, kyma, template, modules)
}
return modules
}
func (p *Parser) GenerateMandatoryModulesFromTemplates(ctx context.Context,
kyma *v1beta2.Kyma,
templates templatelookup.ModuleTemplatesByModuleName,
) common.Modules {
modules := make(common.Modules, 0)
for _, template := range templates {
moduleName, ok := template.ObjectMeta.Labels[shared.ModuleName]
if !ok {
logf.FromContext(ctx).V(log.InfoLevel).Info("ModuleTemplate does not contain Module Name as label, "+
"will fallback to use ModuleTemplate name as Module name",
"template", template.Name)
moduleName = template.Name
}
modules = p.appendModuleWithInformation(templatelookup.ModuleInfo{
Module: v1beta2.Module{
Name: moduleName,
CustomResourcePolicy: v1beta2.CustomResourcePolicyCreateAndDelete,
},
Enabled: true,
}, kyma, template, modules)
}
return modules
}
func (p *Parser) appendModuleWithInformation(module templatelookup.ModuleInfo, kyma *v1beta2.Kyma,
template *templatelookup.ModuleTemplateInfo, modules common.Modules,
) common.Modules {
if template.Err != nil && !errors.Is(template.Err, templatelookup.ErrTemplateNotAllowed) {
modules = append(modules, &common.Module{
ModuleName: module.Name,
Template: template,
Enabled: module.Enabled,
IsUnmanaged: module.Unmanaged,
})
return modules
}
descriptor, err := p.descriptorProvider.GetDescriptor(template.ModuleTemplate)
if err != nil {
template.Err = err
modules = append(modules, &common.Module{
ModuleName: module.Name,
Template: template,
Enabled: module.Enabled,
IsUnmanaged: module.Unmanaged,
})
return modules
}
fqdn := descriptor.GetName()
name := common.CreateModuleName(fqdn, kyma.Name, module.Name)
setNameAndNamespaceIfEmpty(template, name, p.remoteSyncNamespace)
var manifest *v1beta2.Manifest
if manifest, err = p.newManifestFromTemplate(module.Module,
template.ModuleTemplate); err != nil {
template.Err = err
modules = append(modules, &common.Module{
ModuleName: module.Name,
Template: template,
Enabled: module.Enabled,
IsUnmanaged: module.Unmanaged,
})
return modules
}
// we name the manifest after the module name
manifest.SetName(name)
// to have correct owner references, the manifest must always have the same namespace as kyma
manifest.SetNamespace(kyma.GetNamespace())
modules = append(modules, &common.Module{
ModuleName: module.Name,
FQDN: fqdn,
Template: template,
Manifest: manifest,
Enabled: module.Enabled,
IsUnmanaged: module.Unmanaged,
})
return modules
}
func setNameAndNamespaceIfEmpty(template *templatelookup.ModuleTemplateInfo, name, namespace string) {
if template.ModuleTemplate.Spec.Data == nil {
return
}
// if the default data does not contain a name, default it to the module name
if template.ModuleTemplate.Spec.Data.GetName() == "" {
template.ModuleTemplate.Spec.Data.SetName(name)
}
// if the default data does not contain a namespace, default it to the provided namespace
if template.ModuleTemplate.Spec.Data.GetNamespace() == "" {
template.ModuleTemplate.Spec.Data.SetNamespace(namespace)
}
}
func (p *Parser) newManifestFromTemplate(
module v1beta2.Module,
template *v1beta2.ModuleTemplate,
) (*v1beta2.Manifest, error) {
manifest := &v1beta2.Manifest{}
manifest.Spec.Remote = p.inKCPMode
switch module.CustomResourcePolicy {
case v1beta2.CustomResourcePolicyIgnore:
manifest.Spec.Resource = nil
case v1beta2.CustomResourcePolicyCreateAndDelete:
fallthrough
default:
if template.Spec.Data != nil {
manifest.Spec.Resource = template.Spec.Data.DeepCopy()
}
}
var layers img.Layers
var err error
descriptor, err := p.descriptorProvider.GetDescriptor(template)
if err != nil {
return nil, fmt.Errorf("failed to get descriptor from template: %w", err)
}
if layers, err = img.Parse(descriptor.ComponentDescriptor); err != nil {
return nil, fmt.Errorf("could not parse descriptor: %w", err)
}
if err := translateLayersAndMergeIntoManifest(manifest, layers); err != nil {
return nil, fmt.Errorf("could not translate layers and merge them: %w", err)
}
if err := appendOptionalCustomStateCheck(manifest, template.Spec.CustomStateCheck); err != nil {
return nil, fmt.Errorf("could not translate custom state check: %w", err)
}
manifest.Spec.Version = descriptor.Version
return manifest, nil
}
func appendOptionalCustomStateCheck(manifest *v1beta2.Manifest, stateCheck []*v1beta2.CustomStateCheck) error {
if manifest.Spec.Resource == nil || stateCheck == nil {
return nil
}
if manifest.Annotations == nil {
manifest.Annotations = make(map[string]string)
}
stateCheckByte, err := json.Marshal(stateCheck)
if err != nil {
return fmt.Errorf("failed to marshal stateCheck: %w", err)
}
manifest.Annotations[shared.CustomStateCheckAnnotation] = string(stateCheckByte)
return nil
}
func translateLayersAndMergeIntoManifest(manifest *v1beta2.Manifest, layers img.Layers) error {
for _, layer := range layers {
if err := insertLayerIntoManifest(manifest, layer); err != nil {
return fmt.Errorf("error in layer %s: %w", layer.LayerName, err)
}
}
return nil
}
func insertLayerIntoManifest(manifest *v1beta2.Manifest, layer img.Layer) error {
switch layer.LayerName {
case v1beta2.DefaultCRLayer:
// default CR layer is not relevant for the manifest
case v1beta2.ConfigLayer:
imageSpec, err := layer.ConvertToImageSpec()
if err != nil {
return fmt.Errorf("error while parsing config layer: %w", err)
}
manifest.Spec.Config = imageSpec
case v1beta2.RawManifestLayer:
installRaw, err := layer.ToInstallRaw()
if err != nil {
return fmt.Errorf("error while merging the generic install representation: %w", err)
}
manifest.Spec.Install = v1beta2.InstallInfo{
Source: machineryruntime.RawExtension{Raw: installRaw},
Name: string(layer.LayerName),
}
}
return nil
}