-
Notifications
You must be signed in to change notification settings - Fork 30
/
moduletemplate_types.go
296 lines (241 loc) · 9.71 KB
/
moduletemplate_types.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
Copyright 2022.
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 v1beta2
import (
"errors"
"fmt"
"strings"
"github.com/Masterminds/semver/v3"
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
machineryruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"ocm.software/ocm/api/ocm/compdesc"
"github.com/kyma-project/lifecycle-manager/api/shared"
)
// ModuleTemplate is a representation of a Template used for creating Module Instances within the Module Lifecycle.
// It is generally loosely defined within the Kubernetes Specification, however it has a strict enforcement of
// OCM guidelines as it serves an active role in maintaining a list of available Modules within a cluster.
//
// +genclient
// +kubebuilder:object:root=true
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
// +kubebuilder:storageversion
type ModuleTemplate struct {
apimetav1.TypeMeta `json:",inline"`
apimetav1.ObjectMeta `json:"metadata,omitempty"`
Spec ModuleTemplateSpec `json:"spec,omitempty"`
}
// +k8s:deepcopy-gen=false
type Descriptor struct {
*compdesc.ComponentDescriptor
}
func (d *Descriptor) SetGroupVersionKind(kind schema.GroupVersionKind) {
d.Version = kind.Version
}
func (d *Descriptor) GroupVersionKind() schema.GroupVersionKind {
return schema.GroupVersionKind{
Group: "ocm.kyma-project.io",
Version: d.Metadata.ConfiguredVersion,
Kind: "Descriptor",
}
}
func (d *Descriptor) GetObjectKind() schema.ObjectKind {
return d
}
func (d *Descriptor) DeepCopyObject() machineryruntime.Object {
return &Descriptor{ComponentDescriptor: d.Copy()}
}
// ModuleTemplateSpec defines the desired state of ModuleTemplate.
type ModuleTemplateSpec struct {
// Channel is the targeted channel of the ModuleTemplate. It will be used to directly assign a Template
// to a target channel. It has to be provided at any given time.
// Deprecated: This field is deprecated and will be removed in a future release.
// +optional
// +kubebuilder:deprecatedversion
// +kubebuilder:validation:Pattern:=`^$|^[a-z]{3,}$`
// +kubebuilder:validation:MaxLength:=32
Channel string `json:"channel"`
// Version identifies the version of the Module. Can be empty, or a semantic version.
// +optional
// +kubebuilder:validation:Pattern:=`^((0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z-][0-9a-zA-Z-]*)?)?$`
// +kubebuilder:validation:MaxLength:=32
Version string `json:"version"`
// ModuleName is the name of the Module. Can be empty.
// +optional
// +kubebuilder:validation:Pattern:=`^([a-z]{3,}(-[a-z]{3,})*)?$`
// +kubebuilder:validation:MaxLength:=64
ModuleName string `json:"moduleName"`
// Mandatory indicates whether the module is mandatory. It is used to enforce the installation of the module with
// its configuration in all runtime clusters.
// +optional
Mandatory bool `json:"mandatory"`
// Data is the default set of attributes that are used to generate the Module. It contains a default set of values
// for a given channel, and is thus different from default values allocated during struct parsing of the Module.
// While Data can change after the initial creation of ModuleTemplate, it is not expected to be propagated to
// downstream modules as it is considered a set of default values. This means that an update of the data block
// will only propagate to new Modules created form ModuleTemplate, not any existing Module.
//
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:XEmbeddedResource
Data *unstructured.Unstructured `json:"data,omitempty"`
// The Descriptor is the Open Component Model Descriptor of a Module, containing all relevant information
// to correctly initialize a module (e.g. Manifests, References to Binaries and/or configuration)
// Name more information on Component Descriptors, see
// https://github.com/open-component-model/ocm
//
// It is translated inside the Lifecycle of the Cluster and will be used by downstream controllers
// to bootstrap and manage the module. This part is also propagated for every change of the template.
// This means for upgrades of the Descriptor, downstream controllers will also update the dependant modules
// (e.g. by updating the controller binary linked in a chart referenced in the descriptor)
//
// NOTE: Only Raw Rendering is Supported for the layers. So previously used "config" layers for the helm
// charts and kustomize renderers are deprecated and ignored.
//
// +kubebuilder:pruning:PreserveUnknownFields
Descriptor machineryruntime.RawExtension `json:"descriptor"`
// CustomStateCheck is deprecated.
CustomStateCheck []*CustomStateCheck `json:"customStateCheck,omitempty"`
// Resources is a list of additional resources of the module that can be fetched, e.g., the raw manifest.
// +optional
// +listType=map
// +listMapKey=name
Resources []Resource `json:"resources,omitempty"`
// Info contains metadata about the module.
// +optional
Info *ModuleInfo `json:"info,omitempty"`
// AssociatedResources is a list of module related resources that usually must be cleaned when uninstalling a module. Informational purpose only.
// +optional
AssociatedResources []apimetav1.GroupVersionKind `json:"associatedResources,omitempty"`
// Manager contains information for identifying a module's resource that can be used as indicator for the installation readiness of the module. Typically, this is the manager Deployment of the module. In exceptional cases, it may also be another resource.
// +optional
Manager *Manager `json:"manager,omitempty"`
}
// Manager defines the structure for the manager field in ModuleTemplateSpec.
type Manager struct {
apimetav1.GroupVersionKind `json:",inline"`
// Namespace is the namespace of the manager. It is optional.
// +optional
Namespace string `json:"namespace,omitempty"`
// Name is the name of the manager.
Name string `json:"name"`
}
type ModuleInfo struct {
// Repository is the link to the repository of the module.
Repository string `json:"repository"`
// Documentation is the link to the documentation of the module.
Documentation string `json:"documentation"`
// Icons is a list of icons of the module.
// +optional
// +listType=map
// +listMapKey=name
Icons []ModuleIcon `json:"icons,omitempty"`
}
type ModuleIcon struct {
// Name is the name of the icon.
Name string `json:"name"`
// Link is the link to the icon.
Link string `json:"link"`
}
type CustomStateCheck struct {
// JSONPath specifies the JSON path to the state variable in the Module CR
JSONPath string `json:"jsonPath" yaml:"jsonPath"`
// Value is the value at the JSONPath for which the Module CR state should map with MappedState
Value string `json:"value" yaml:"value"`
// MappedState is the Kyma CR State
MappedState shared.State `json:"mappedState" yaml:"mappedState"`
}
// +kubebuilder:object:root=true
// ModuleTemplateList contains a list of ModuleTemplate.
type ModuleTemplateList struct {
apimetav1.TypeMeta `json:",inline"`
apimetav1.ListMeta `json:"metadata,omitempty"`
Items []ModuleTemplate `json:"items"`
}
type Resource struct {
// Name is the name of the resource.
Name string `json:"name"`
// Link is the URL to the resource.
// +kubebuilder:validation:Format=uri
Link string `json:"link"`
}
//nolint:gochecknoinits // registers ModuleTemplate CRD on startup
func init() {
SchemeBuilder.Register(&ModuleTemplate{}, &ModuleTemplateList{}, &Descriptor{})
}
func (m *ModuleTemplate) SyncEnabled(betaEnabled, internalEnabled bool) bool {
if m.syncDisabled() {
return false
}
if m.IsBeta() && !betaEnabled {
return false
}
if m.IsInternal() && !internalEnabled {
return false
}
if m.IsMandatory() {
return false
}
return true
}
func (m *ModuleTemplate) syncDisabled() bool {
if isSync, found := m.Labels[shared.SyncLabel]; found {
return strings.ToLower(isSync) == shared.DisableLabelValue
}
return false
}
func (m *ModuleTemplate) IsInternal() bool {
if isInternal, found := m.Labels[shared.InternalLabel]; found {
return strings.ToLower(isInternal) == shared.EnableLabelValue
}
return false
}
var ErrInvalidVersion = errors.New("can't find valid semantic version")
// getVersionLegacy() returns the version of the ModuleTemplate from the annotation on the object.
// Remove once shared.ModuleVersionAnnotation is removed.
func (m *ModuleTemplate) getVersionLegacy() (string, error) {
if m.Annotations != nil {
moduleVersion, found := m.Annotations[shared.ModuleVersionAnnotation]
if found {
return moduleVersion, nil
}
}
return "", ErrInvalidVersion
}
// GetVersion returns the declared version of the ModuleTemplate from it's Spec.
func (m *ModuleTemplate) GetVersion() (*semver.Version, error) {
var versionValue string
var err error
if m.Spec.Version == "" {
versionValue, err = m.getVersionLegacy()
if err != nil {
return nil, err
}
} else {
versionValue = m.Spec.Version
}
version, err := semver.NewVersion(versionValue)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrInvalidVersion, err.Error())
}
return version, nil
}
func (m *ModuleTemplate) IsBeta() bool {
if isBeta, found := m.Labels[shared.BetaLabel]; found {
return strings.ToLower(isBeta) == shared.EnableLabelValue
}
return false
}
func (m *ModuleTemplate) IsMandatory() bool {
return m.Spec.Mandatory
}