@@ -21,26 +21,20 @@ const (
2121 catalogReadyState string = "READY"
2222)
2323
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 {
3125 fileBasedCatalog FileBasedCatalogProvider
3226 kubeClient k8scontrollerclient.Client
33- namespace string
3427 name string
28+ namespace string
3529 configMapName string
3630 serviceName string
3731 podName string
3832}
3933
4034// NewMagicCatalog creates an object that can deploy an arbitrary file-based catalog given by the FileBasedCatalogProvider
4135// 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 {
4438 fileBasedCatalog : provider ,
4539 kubeClient : kubeClient ,
4640 namespace : namespace ,
@@ -51,53 +45,41 @@ func NewMagicCatalog(kubeClient k8scontrollerclient.Client, namespace string, ca
5145 }
5246}
5347
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 {
5666 resourcesInOrderOfDeployment := []k8scontrollerclient.Object {
5767 c .makeConfigMap (),
5868 c .makeCatalogSourcePod (),
5969 c .makeCatalogService (),
60- catalogSource ,
70+ c . makeCatalogSource () ,
6171 }
6272 if err := c .deployCatalog (ctx , resourcesInOrderOfDeployment ); err != nil {
6373 return err
6474 }
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 )
6777 }
6878
6979 return nil
7080}
7181
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 {
10183 resourcesInOrderOfDeletion := []k8scontrollerclient.Object {
10284 c .makeCatalogSourcePod (),
10385 c .makeConfigMap (),
@@ -132,14 +114,14 @@ func (c *magicCatalog) UpdateCatalog(ctx context.Context, provider FileBasedCata
132114 if err := c .deployCatalog (ctx , resourcesInOrderOfCreation ); err != nil {
133115 return err
134116 }
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 )
137119 }
138120
139121 return nil
140122}
141123
142- func (c * magicCatalog ) UndeployCatalog (ctx context.Context ) []error {
124+ func (c * MagicCatalog ) UndeployCatalog (ctx context.Context ) []error {
143125 resourcesInOrderOfDeletion := []k8scontrollerclient.Object {
144126 c .makeCatalogSource (),
145127 c .makeCatalogService (),
@@ -149,7 +131,38 @@ func (c *magicCatalog) UndeployCatalog(ctx context.Context) []error {
149131 return c .undeployCatalog (ctx , resourcesInOrderOfDeletion )
150132}
151133
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 {
153166 var errors []error
154167 // try to delete all resourcesInOrderOfDeletion even if errors are
155168 // encountered through deletion.
@@ -167,15 +180,15 @@ func (c *magicCatalog) undeployCatalog(ctx context.Context, resources []k8scontr
167180 return errors
168181}
169182
170- func (c * magicCatalog ) cleanUpAfter (ctx context.Context , err error ) error {
183+ func (c * MagicCatalog ) cleanUpAfterError (ctx context.Context , err error ) error {
171184 cleanupErr := c .UndeployCatalog (ctx )
172185 if cleanupErr != nil {
173186 return fmt .Errorf ("the following cleanup errors occurred: '%s' after an error deploying the configmap: '%s' " , cleanupErr , err )
174187 }
175188 return err
176189}
177190
178- func (c * magicCatalog ) makeCatalogService () * corev1.Service {
191+ func (c * MagicCatalog ) makeCatalogService () * corev1.Service {
179192 return & corev1.Service {
180193 ObjectMeta : metav1.ObjectMeta {
181194 Name : c .serviceName ,
@@ -195,7 +208,7 @@ func (c *magicCatalog) makeCatalogService() *corev1.Service {
195208 }
196209}
197210
198- func (c * magicCatalog ) makeConfigMap () * corev1.ConfigMap {
211+ func (c * MagicCatalog ) makeConfigMap () * corev1.ConfigMap {
199212 isImmutable := true
200213 return & corev1.ConfigMap {
201214 ObjectMeta : metav1.ObjectMeta {
@@ -224,7 +237,7 @@ func (c *magicCatalog) makeConfigMap() *corev1.ConfigMap {
224237 }
225238}
226239
227- func (c * magicCatalog ) makeCatalogSource () * operatorsv1alpha1.CatalogSource {
240+ func (c * MagicCatalog ) makeCatalogSource () * operatorsv1alpha1.CatalogSource {
228241 return & operatorsv1alpha1.CatalogSource {
229242 ObjectMeta : metav1.ObjectMeta {
230243 Name : c .name ,
@@ -237,7 +250,7 @@ func (c *magicCatalog) makeCatalogSource() *operatorsv1alpha1.CatalogSource {
237250 }
238251}
239252
240- func (c * magicCatalog ) makeCatalogSourcePod () * corev1.Pod {
253+ func (c * MagicCatalog ) makeCatalogSourcePod () * corev1.Pod {
241254
242255 const (
243256 image = "quay.io/operator-framework/upstream-opm-builder"
@@ -320,7 +333,7 @@ func (c *magicCatalog) makeCatalogSourcePod() *corev1.Pod {
320333 }
321334}
322335
323- func (c * magicCatalog ) makeCatalogSourcePodLabels () map [string ]string {
336+ func (c * MagicCatalog ) makeCatalogSourcePodLabels () map [string ]string {
324337 return map [string ]string {
325338 olmCatalogLabel : c .name ,
326339 }
0 commit comments