1- package stub
1+ package catalogsourceconfig
22
33import (
4+ "context"
5+
46 olm "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
57 "github.com/operator-framework/operator-marketplace/pkg/apis/marketplace/v1alpha1"
68 "github.com/operator-framework/operator-sdk/pkg/sdk"
@@ -10,27 +12,58 @@ import (
1012 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1113)
1214
15+ // Handler is the interface that wraps the Handle method
16+ type Handler interface {
17+ Handle (ctx context.Context , event sdk.Event ) error
18+ }
19+
20+ type handler struct {
21+ }
22+
23+ var log * logrus.Entry
24+
25+ // Handle handles a new event associated with CatalogSourceConfig type
26+ func (h * handler ) Handle (ctx context.Context , event sdk.Event ) error {
27+ csc := event .Object .(* v1alpha1.CatalogSourceConfig )
28+ log = getLoggerWithCatalogSourceConfigTypeFields (csc )
29+ // Ignore the delete event as the garbage collector will clean up the created resources as per the OwnerReference
30+ if event .Deleted {
31+ log .Infof ("Deleted" )
32+ return nil
33+ }
34+ return createCatalogSource (csc )
35+ }
36+
1337// createCatalogSource creates a new CatalogSource CR and all the resources it requires
1438func createCatalogSource (cr * v1alpha1.CatalogSourceConfig ) error {
1539 // Create the ConfigMap that will be used by the CatalogSource
1640 catalogConfigMap := newConfigMap (cr )
17- logrus .Infof ("Creating %s ConfigMap in %s namespace " , catalogConfigMap .Name , cr . Spec . TargetNamespace )
41+ log .Infof ("Creating %s ConfigMap" , catalogConfigMap .Name )
1842 err := sdk .Create (catalogConfigMap )
1943 if err != nil && ! errors .IsAlreadyExists (err ) {
20- logrus .Errorf ("Failed to create catalog source : %v" , err )
44+ log .Errorf ("Failed to create ConfigMap : %v" , err )
2145 return err
2246 }
2347
2448 catalogSource := newCatalogSource (cr , catalogConfigMap .Name )
25- logrus .Infof ("Creating %s CatalogSource in %s namespace" , catalogSource .Name , cr .Spec .TargetNamespace )
2649 err = sdk .Create (catalogSource )
2750 if err != nil && ! errors .IsAlreadyExists (err ) {
28- logrus .Errorf ("Failed to create catalog source : %v" , err )
51+ log .Errorf ("Failed to create CatalogSource : %v" , err )
2952 return err
3053 }
54+ log .Infof ("Created" )
3155 return nil
3256}
3357
58+ // getLoggerWithCatalogSourceConfigTypeFields returns a logger entry that can be used for consistent logging
59+ func getLoggerWithCatalogSourceConfigTypeFields (csc * v1alpha1.CatalogSourceConfig ) * logrus.Entry {
60+ return logrus .WithFields (logrus.Fields {
61+ "type" : csc .TypeMeta .Kind ,
62+ "targetNamespace" : csc .Spec .TargetNamespace ,
63+ "name" : csc .GetName (),
64+ })
65+ }
66+
3467// newCatalogSource returns a CatalogSource object
3568func newCatalogSource (cr * v1alpha1.CatalogSourceConfig , configMapName string ) * olm.CatalogSource {
3669 name := v1alpha1 .CatalogSourcePrefix + cr .Name
0 commit comments