-
Notifications
You must be signed in to change notification settings - Fork 947
/
manager.go
246 lines (196 loc) · 6.67 KB
/
manager.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
/*
Copyright 2024 The Crossplane Authors.
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 validate
import (
"fmt"
"io"
"github.com/spf13/afero"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/yaml"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
v1 "github.com/crossplane/crossplane/apis/apiextensions/v1"
metav1 "github.com/crossplane/crossplane/apis/pkg/meta/v1"
"github.com/crossplane/crossplane/internal/xcrd"
)
const (
defaultCacheDir = ".crossplane/cache"
packageFileName = "package.yaml"
baseLayerLabel = "base"
refFmt = "%s@%s"
imageFmt = "%s:%s"
)
// Manager defines a Manager for preparing Crossplane packages for validation.
type Manager struct {
fetcher ImageFetcher
cache Cache
writer io.Writer
crds []*extv1.CustomResourceDefinition
deps map[string]bool // One level dependency images
confs map[string]bool // Configuration images
}
// NewManager returns a new Manager.
func NewManager(cacheDir string, fs afero.Fs, w io.Writer) *Manager {
m := &Manager{}
m.cache = &LocalCache{
fs: fs,
cacheDir: cacheDir,
}
m.fetcher = &Fetcher{}
m.writer = w
m.crds = make([]*extv1.CustomResourceDefinition, 0)
m.deps = make(map[string]bool)
m.confs = make(map[string]bool)
return m
}
// PrepExtensions converts the unstructured XRDs/CRDs to CRDs and extract package images to add as a dependency.
func (m *Manager) PrepExtensions(extensions []*unstructured.Unstructured) error { //nolint:gocognit // the function itself is not that complex, it just has different cases
for _, e := range extensions {
switch e.GroupVersionKind().GroupKind() {
case schema.GroupKind{Group: "apiextensions.k8s.io", Kind: "CustomResourceDefinition"}:
crd := &extv1.CustomResourceDefinition{}
bytes, err := e.MarshalJSON()
if err != nil {
return errors.Wrap(err, "cannot marshal CRD to JSON")
}
if err := yaml.Unmarshal(bytes, crd); err != nil {
return errors.Wrap(err, "cannot unmarshal CRD YAML")
}
m.crds = append(m.crds, crd)
case schema.GroupKind{Group: "apiextensions.crossplane.io", Kind: "CompositeResourceDefinition"}:
xrd := &v1.CompositeResourceDefinition{}
bytes, err := e.MarshalJSON()
if err != nil {
return errors.Wrap(err, "cannot marshal XRD to JSON")
}
if err := yaml.Unmarshal(bytes, xrd); err != nil {
return errors.Wrap(err, "cannot unmarshal XRD YAML")
}
crd, err := xcrd.ForCompositeResource(xrd)
if err != nil {
return errors.Wrapf(err, "cannot derive composite CRD from XRD %q", xrd.GetName())
}
m.crds = append(m.crds, crd)
if xrd.Spec.ClaimNames != nil {
claimCrd, err := xcrd.ForCompositeResourceClaim(xrd)
if err != nil {
return errors.Wrapf(err, "cannot derive claim CRD from XRD %q", xrd.GetName())
}
m.crds = append(m.crds, claimCrd)
}
case schema.GroupKind{Group: "pkg.crossplane.io", Kind: "Provider"}:
paved := fieldpath.Pave(e.Object)
image, err := paved.GetString("spec.package")
if err != nil {
return errors.Wrapf(err, "cannot get package image")
}
m.deps[image] = true
case schema.GroupKind{Group: "pkg.crossplane.io", Kind: "Configuration"}:
paved := fieldpath.Pave(e.Object)
image, err := paved.GetString("spec.package")
if err != nil {
return errors.Wrapf(err, "cannot get package image")
}
m.confs[image] = true
default:
continue
}
}
return nil
}
// CacheAndLoad finds and caches dependencies and loads them as CRDs.
func (m *Manager) CacheAndLoad(cleanCache bool) error {
if cleanCache {
if err := m.cache.Flush(); err != nil {
return errors.Wrapf(err, "cannot flush cache directory")
}
}
if err := m.cache.Init(); err != nil {
return errors.Wrapf(err, "cannot initialize cache directory")
}
if err := m.addDependencies(); err != nil {
return errors.Wrapf(err, "cannot add package dependencies")
}
if err := m.cacheDependencies(); err != nil {
return errors.Wrapf(err, "cannot cache package dependencies")
}
schemas, err := m.cache.Load()
if err != nil {
return errors.Wrapf(err, "cannot load cache")
}
return m.PrepExtensions(schemas)
}
func (m *Manager) addDependencies() error {
for image := range m.confs {
m.deps[image] = true // we need to download the configuration package for the XRDs
layer, err := m.fetcher.FetchBaseLayer(image)
if err != nil {
return errors.Wrapf(err, "cannot download package %s", image)
}
_, meta, err := extractPackageContent(*layer)
if err != nil {
return errors.Wrapf(err, "cannot extract package file and meta")
}
cfg := &metav1.Configuration{}
if err := yaml.Unmarshal(meta, cfg); err != nil {
return errors.Wrapf(err, "cannot unmarshal configuration YAML")
}
deps := cfg.Spec.MetaSpec.DependsOn
for _, dep := range deps {
image := ""
if dep.Configuration != nil { //nolint:gocritic // switch is not suitable here
image = *dep.Configuration
} else if dep.Provider != nil {
image = *dep.Provider
} else if dep.Function != nil {
image = *dep.Function
}
if len(image) > 0 {
image = fmt.Sprintf(imageFmt, image, dep.Version)
m.deps[image] = true
}
}
}
return nil
}
func (m *Manager) cacheDependencies() error {
if err := m.cache.Init(); err != nil {
return errors.Wrapf(err, "cannot initialize cache directory")
}
for image := range m.deps {
path, err := m.cache.Exists(image) // returns the path if the image is not cached
if err != nil {
return errors.Wrapf(err, "cannot check if cache exists for %s", image)
}
if path == "" {
continue
}
if _, err := fmt.Fprintln(m.writer, "package schemas does not exist, downloading: ", image); err != nil {
return errors.Wrapf(err, errWriteOutput)
}
layer, err := m.fetcher.FetchBaseLayer(image)
if err != nil {
return errors.Wrapf(err, "cannot download package %s", image)
}
schemas, _, err := extractPackageContent(*layer)
if err != nil {
return errors.Wrapf(err, "cannot extract package file and meta")
}
if err := m.cache.Store(schemas, path); err != nil {
return errors.Wrapf(err, "cannot store base layer")
}
}
return nil
}