@@ -21,26 +21,20 @@ const (
21
21
catalogReadyState string = "READY"
22
22
)
23
23
24
- type MagicCatalog interface {
25
- DeployCatalog (ctx context.Context ) error
26
- UpdateCatalog (ctx context.Context , provider FileBasedCatalogProvider ) error
27
- UndeployCatalog (ctx context.Context ) []error
28
- }
29
-
30
- type magicCatalog struct {
24
+ type MagicCatalog struct {
31
25
fileBasedCatalog FileBasedCatalogProvider
32
26
kubeClient k8scontrollerclient.Client
33
- namespace string
34
27
name string
28
+ namespace string
35
29
configMapName string
36
30
serviceName string
37
31
podName string
38
32
}
39
33
40
34
// NewMagicCatalog creates an object that can deploy an arbitrary file-based catalog given by the FileBasedCatalogProvider
41
35
// Keep in mind that there are limits to the configMaps. So, the catalogs need to be relatively simple
42
- func NewMagicCatalog (kubeClient k8scontrollerclient.Client , namespace string , catalogName string , provider FileBasedCatalogProvider ) MagicCatalog {
43
- return & magicCatalog {
36
+ func NewMagicCatalog (kubeClient k8scontrollerclient.Client , namespace string , catalogName string , provider FileBasedCatalogProvider ) * MagicCatalog {
37
+ return & MagicCatalog {
44
38
fileBasedCatalog : provider ,
45
39
kubeClient : kubeClient ,
46
40
namespace : namespace ,
@@ -51,53 +45,41 @@ func NewMagicCatalog(kubeClient k8scontrollerclient.Client, namespace string, ca
51
45
}
52
46
}
53
47
54
- func (c * magicCatalog ) DeployCatalog (ctx context.Context ) error {
55
- catalogSource := c .makeCatalogSource ()
48
+ func NewMagicCatalogFromFile (kubeClient k8scontrollerclient.Client , namespace string , catalogName string , fbcFilePath string ) (* MagicCatalog , error ) {
49
+ provider , err := NewFileBasedFiledBasedCatalogProvider (fbcFilePath )
50
+ if err != nil {
51
+ return nil , err
52
+ }
53
+ catalog := NewMagicCatalog (kubeClient , namespace , catalogName , provider )
54
+ return catalog , nil
55
+ }
56
+
57
+ func (c * MagicCatalog ) GetName () string {
58
+ return c .name
59
+ }
60
+
61
+ func (c * MagicCatalog ) GetNamespace () string {
62
+ return c .namespace
63
+ }
64
+
65
+ func (c * MagicCatalog ) DeployCatalog (ctx context.Context ) error {
56
66
resourcesInOrderOfDeployment := []k8scontrollerclient.Object {
57
67
c .makeConfigMap (),
58
68
c .makeCatalogSourcePod (),
59
69
c .makeCatalogService (),
60
- catalogSource ,
70
+ c . makeCatalogSource () ,
61
71
}
62
72
if err := c .deployCatalog (ctx , resourcesInOrderOfDeployment ); err != nil {
63
73
return err
64
74
}
65
- if err := catalogSourceIsReady (ctx , c . kubeClient , catalogSource ); err != nil {
66
- return c .cleanUpAfter (ctx , err )
75
+ if err := c . catalogSourceIsReady (ctx ); err != nil {
76
+ return c .cleanUpAfterError (ctx , err )
67
77
}
68
78
69
79
return nil
70
80
}
71
81
72
- func catalogSourceIsReady (ctx context.Context , c k8scontrollerclient.Client , cs * operatorsv1alpha1.CatalogSource ) error {
73
- // wait for catalog source to become ready
74
- return waitFor (func () (bool , error ) {
75
- err := c .Get (ctx , k8scontrollerclient.ObjectKey {
76
- Name : cs .GetName (),
77
- Namespace : cs .GetNamespace (),
78
- }, cs )
79
- if err != nil || cs .Status .GRPCConnectionState == nil {
80
- return false , err
81
- }
82
- state := cs .Status .GRPCConnectionState .LastObservedState
83
- if state != catalogReadyState {
84
- return false , nil
85
- }
86
- return true , nil
87
- })
88
- }
89
-
90
- func (c * magicCatalog ) deployCatalog (ctx context.Context , resources []k8scontrollerclient.Object ) error {
91
- for _ , res := range resources {
92
- err := c .kubeClient .Create (ctx , res )
93
- if err != nil {
94
- return c .cleanUpAfter (ctx , err )
95
- }
96
- }
97
- return nil
98
- }
99
-
100
- func (c * magicCatalog ) UpdateCatalog (ctx context.Context , provider FileBasedCatalogProvider ) error {
82
+ func (c * MagicCatalog ) UpdateCatalog (ctx context.Context , provider FileBasedCatalogProvider ) error {
101
83
resourcesInOrderOfDeletion := []k8scontrollerclient.Object {
102
84
c .makeCatalogSourcePod (),
103
85
c .makeConfigMap (),
@@ -132,14 +114,14 @@ func (c *magicCatalog) UpdateCatalog(ctx context.Context, provider FileBasedCata
132
114
if err := c .deployCatalog (ctx , resourcesInOrderOfCreation ); err != nil {
133
115
return err
134
116
}
135
- if err := catalogSourceIsReady ( ctx , c . kubeClient , c . makeCatalogSource () ); err != nil {
136
- return c .cleanUpAfter (ctx , err )
117
+ if err := c . catalogSourceIsReady ( ctx ); err != nil {
118
+ return c .cleanUpAfterError (ctx , err )
137
119
}
138
120
139
121
return nil
140
122
}
141
123
142
- func (c * magicCatalog ) UndeployCatalog (ctx context.Context ) []error {
124
+ func (c * MagicCatalog ) UndeployCatalog (ctx context.Context ) []error {
143
125
resourcesInOrderOfDeletion := []k8scontrollerclient.Object {
144
126
c .makeCatalogSource (),
145
127
c .makeCatalogService (),
@@ -149,7 +131,38 @@ func (c *magicCatalog) UndeployCatalog(ctx context.Context) []error {
149
131
return c .undeployCatalog (ctx , resourcesInOrderOfDeletion )
150
132
}
151
133
152
- func (c * magicCatalog ) undeployCatalog (ctx context.Context , resources []k8scontrollerclient.Object ) []error {
134
+ func (c * MagicCatalog ) catalogSourceIsReady (ctx context.Context ) error {
135
+ // wait for catalog source to become ready
136
+ key := k8scontrollerclient.ObjectKey {
137
+ Name : c .name ,
138
+ Namespace : c .namespace ,
139
+ }
140
+
141
+ return waitFor (func () (bool , error ) {
142
+ catalogSource := & operatorsv1alpha1.CatalogSource {}
143
+ err := c .kubeClient .Get (ctx , key , catalogSource )
144
+ if err != nil || catalogSource .Status .GRPCConnectionState == nil {
145
+ return false , err
146
+ }
147
+ state := catalogSource .Status .GRPCConnectionState .LastObservedState
148
+ if state != catalogReadyState {
149
+ return false , nil
150
+ }
151
+ return true , nil
152
+ })
153
+ }
154
+
155
+ func (c * MagicCatalog ) deployCatalog (ctx context.Context , resources []k8scontrollerclient.Object ) error {
156
+ for _ , res := range resources {
157
+ err := c .kubeClient .Create (ctx , res )
158
+ if err != nil {
159
+ return c .cleanUpAfterError (ctx , err )
160
+ }
161
+ }
162
+ return nil
163
+ }
164
+
165
+ func (c * MagicCatalog ) undeployCatalog (ctx context.Context , resources []k8scontrollerclient.Object ) []error {
153
166
var errors []error
154
167
// try to delete all resourcesInOrderOfDeletion even if errors are
155
168
// encountered through deletion.
@@ -167,15 +180,15 @@ func (c *magicCatalog) undeployCatalog(ctx context.Context, resources []k8scontr
167
180
return errors
168
181
}
169
182
170
- func (c * magicCatalog ) cleanUpAfter (ctx context.Context , err error ) error {
183
+ func (c * MagicCatalog ) cleanUpAfterError (ctx context.Context , err error ) error {
171
184
cleanupErr := c .UndeployCatalog (ctx )
172
185
if cleanupErr != nil {
173
186
return fmt .Errorf ("the following cleanup errors occurred: '%s' after an error deploying the configmap: '%s' " , cleanupErr , err )
174
187
}
175
188
return err
176
189
}
177
190
178
- func (c * magicCatalog ) makeCatalogService () * corev1.Service {
191
+ func (c * MagicCatalog ) makeCatalogService () * corev1.Service {
179
192
return & corev1.Service {
180
193
ObjectMeta : metav1.ObjectMeta {
181
194
Name : c .serviceName ,
@@ -195,7 +208,7 @@ func (c *magicCatalog) makeCatalogService() *corev1.Service {
195
208
}
196
209
}
197
210
198
- func (c * magicCatalog ) makeConfigMap () * corev1.ConfigMap {
211
+ func (c * MagicCatalog ) makeConfigMap () * corev1.ConfigMap {
199
212
isImmutable := true
200
213
return & corev1.ConfigMap {
201
214
ObjectMeta : metav1.ObjectMeta {
@@ -224,7 +237,7 @@ func (c *magicCatalog) makeConfigMap() *corev1.ConfigMap {
224
237
}
225
238
}
226
239
227
- func (c * magicCatalog ) makeCatalogSource () * operatorsv1alpha1.CatalogSource {
240
+ func (c * MagicCatalog ) makeCatalogSource () * operatorsv1alpha1.CatalogSource {
228
241
return & operatorsv1alpha1.CatalogSource {
229
242
ObjectMeta : metav1.ObjectMeta {
230
243
Name : c .name ,
@@ -237,7 +250,7 @@ func (c *magicCatalog) makeCatalogSource() *operatorsv1alpha1.CatalogSource {
237
250
}
238
251
}
239
252
240
- func (c * magicCatalog ) makeCatalogSourcePod () * corev1.Pod {
253
+ func (c * MagicCatalog ) makeCatalogSourcePod () * corev1.Pod {
241
254
242
255
const (
243
256
image = "quay.io/operator-framework/upstream-opm-builder"
@@ -320,7 +333,7 @@ func (c *magicCatalog) makeCatalogSourcePod() *corev1.Pod {
320
333
}
321
334
}
322
335
323
- func (c * magicCatalog ) makeCatalogSourcePodLabels () map [string ]string {
336
+ func (c * MagicCatalog ) makeCatalogSourcePodLabels () map [string ]string {
324
337
return map [string ]string {
325
338
olmCatalogLabel : c .name ,
326
339
}
0 commit comments