-
Notifications
You must be signed in to change notification settings - Fork 239
/
configmap.go
187 lines (163 loc) · 6.36 KB
/
configmap.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
package sqlite
import (
"encoding/json"
"fmt"
"strings"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"sigs.k8s.io/yaml"
"github.com/operator-framework/operator-registry/pkg/registry"
)
const (
ConfigMapCRDName = "customResourceDefinitions"
ConfigMapCSVName = "clusterServiceVersions"
ConfigMapPackageName = "packages"
)
// ConfigMapLoader loads a configmap of resources into the database
// entries under "customResourceDefinitions" will be parsed as CRDs
// entries under "clusterServiceVersions" will be parsed as CSVs
// entries under "packages" will be parsed as Packages
type ConfigMapLoader struct {
log *logrus.Entry
store registry.Load
configMapData map[string]string
crds map[registry.APIKey]*unstructured.Unstructured
}
var _ SQLPopulator = &ConfigMapLoader{}
// NewSQLLoaderForConfigMapData is useful when the operator manifest(s)
// originate from a different source than a configMap. For example, operator
// manifest(s) can be downloaded from a remote registry like quay.io.
func NewSQLLoaderForConfigMapData(logger *logrus.Entry, store registry.Load, configMapData map[string]string) *ConfigMapLoader {
return &ConfigMapLoader{
log: logger,
store: store,
configMapData: configMapData,
crds: map[registry.APIKey]*unstructured.Unstructured{},
}
}
func NewSQLLoaderForConfigMap(store registry.Load, configMap v1.ConfigMap) *ConfigMapLoader {
logger := logrus.WithFields(logrus.Fields{"configmap": configMap.GetName(), "ns": configMap.GetNamespace()})
return &ConfigMapLoader{
log: logger,
store: store,
configMapData: configMap.Data,
crds: map[registry.APIKey]*unstructured.Unstructured{},
}
}
func (c *ConfigMapLoader) Populate() error {
c.log.Info("loading CRDs")
// first load CRDs into memory; these will be added to the bundle that owns them
crdListYaml, ok := c.configMapData[ConfigMapCRDName]
if !ok {
return fmt.Errorf("couldn't find expected key %s in configmap", ConfigMapCRDName)
}
crdListJson, err := yaml.YAMLToJSON([]byte(crdListYaml))
if err != nil {
c.log.WithError(err).Debug("error loading CRD list")
return err
}
var parsedCRDList []v1beta1.CustomResourceDefinition
if err := json.Unmarshal(crdListJson, &parsedCRDList); err != nil {
c.log.WithError(err).Debug("error parsing CRD list")
return err
}
var errs []error
for _, crd := range parsedCRDList {
if crd.Spec.Versions == nil && crd.Spec.Version != "" {
crd.Spec.Versions = []v1beta1.CustomResourceDefinitionVersion{{Name: crd.Spec.Version, Served: true, Storage: true}}
}
for _, version := range crd.Spec.Versions {
gvk := registry.APIKey{Group: crd.Spec.Group, Version: version.Name, Kind: crd.Spec.Names.Kind, Plural: crd.Spec.Names.Plural}
c.log.WithField("gvk", gvk).Debug("loading CRD")
if _, ok := c.crds[gvk]; ok {
c.log.WithField("gvk", gvk).Debug("crd added twice")
errs = append(errs, fmt.Errorf("can't add the same CRD twice in one configmap: %v", gvk))
continue
}
crdUnst, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&crd)
if err != nil {
errs = append(errs, fmt.Errorf("error marshaling crd: %s", err))
continue
}
c.crds[gvk] = &unstructured.Unstructured{Object: crdUnst}
}
}
c.log.Info("loading Bundles")
csvListYaml, ok := c.configMapData[ConfigMapCSVName]
if !ok {
errs = append(errs, fmt.Errorf("couldn't find expected key %s in configmap", ConfigMapCSVName))
return utilerrors.NewAggregate(errs)
}
csvListJson, err := yaml.YAMLToJSON([]byte(csvListYaml))
if err != nil {
errs = append(errs, fmt.Errorf("error loading CSV list: %s", err))
return utilerrors.NewAggregate(errs)
}
var parsedCSVList []registry.ClusterServiceVersion
err = json.Unmarshal(csvListJson, &parsedCSVList)
if err != nil {
errs = append(errs, fmt.Errorf("error parsing CSV list: %s", err))
return utilerrors.NewAggregate(errs)
}
for _, csv := range parsedCSVList {
c.log.WithField("csv", csv.GetName()).Debug("loading CSV")
csvUnst, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&csv)
if err != nil {
errs = append(errs, fmt.Errorf("error marshaling csv: %s", err))
continue
}
bundle := registry.NewBundle(csv.GetName(), ®istry.Annotations{}, &unstructured.Unstructured{Object: csvUnst})
ownedCRDs, _, err := csv.GetCustomResourceDefintions()
if err != nil {
errs = append(errs, err)
continue
}
for _, owned := range ownedCRDs {
split := strings.SplitN(owned.Name, ".", 2)
if len(split) < 2 {
c.log.WithError(err).Debug("error parsing owned name")
errs = append(errs, fmt.Errorf("error parsing owned name: %s", err))
continue
}
gvk := registry.APIKey{Group: split[1], Version: owned.Version, Kind: owned.Kind, Plural: split[0]}
crdUnst, ok := c.crds[gvk]
if !ok {
errs = append(errs, fmt.Errorf("couldn't find owned CRD in crd list %v: %s", gvk, err))
continue
}
bundle.Add(crdUnst)
}
if err := c.store.AddOperatorBundle(bundle); err != nil {
version, _ := bundle.Version()
errs = append(errs, fmt.Errorf("error adding operator bundle %s/%s/%s: %s", csv.GetName(), version, bundle.BundleImage, err))
}
}
c.log.Info("loading Packages")
packageListYaml, ok := c.configMapData[ConfigMapPackageName]
if !ok {
errs = append(errs, fmt.Errorf("couldn't find expected key %s in configmap", ConfigMapPackageName))
return utilerrors.NewAggregate(errs)
}
packageListJson, err := yaml.YAMLToJSON([]byte(packageListYaml))
if err != nil {
errs = append(errs, fmt.Errorf("error loading package list: %s", err))
return utilerrors.NewAggregate(errs)
}
var parsedPackageManifests []registry.PackageManifest
err = json.Unmarshal(packageListJson, &parsedPackageManifests)
if err != nil {
errs = append(errs, fmt.Errorf("error parsing package list: %s", err))
return utilerrors.NewAggregate(errs)
}
for _, packageManifest := range parsedPackageManifests {
c.log.WithField("package", packageManifest.PackageName).Debug("loading package")
if err := c.store.AddPackageChannels(packageManifest); err != nil {
errs = append(errs, fmt.Errorf("error loading package %s: %s", packageManifest.PackageName, err))
}
}
return utilerrors.NewAggregate(errs)
}