/
catalogsourceconfig_controller.go
124 lines (107 loc) · 4.83 KB
/
catalogsourceconfig_controller.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
package catalogsourceconfig
import (
"context"
v2 "github.com/operator-framework/operator-marketplace/pkg/apis/operators/v2"
catalogsourceconfighandler "github.com/operator-framework/operator-marketplace/pkg/catalogsourceconfig"
"github.com/operator-framework/operator-marketplace/pkg/controller/options"
"github.com/operator-framework/operator-marketplace/pkg/metrics"
"github.com/operator-framework/operator-marketplace/pkg/status"
"github.com/operator-framework/operator-marketplace/pkg/watches"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
// Add creates a new CatalogSourceConfig Controller and adds it to the Manager. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager, o options.ControllerOptions) error {
reconciler, err := newReconciler(mgr, o)
if err != nil {
return err
}
return add(mgr, reconciler)
}
// newReconciler returns a new ReconcileCatalogSourceConfig
func newReconciler(mgr manager.Manager, o options.ControllerOptions) (*ReconcileCatalogSourceConfig, error) {
// The default client serves read requests from the cache which contains
// objects only from the namespace the operator is watching. Given we need
// to query other namespaces for CatalogSources, we create our own client
// and pass it the manager's scheme which has all our registered types
cfg, err := config.GetConfig()
if err != nil {
return nil, err
}
client, err := client.New(cfg, client.Options{Scheme: mgr.GetScheme()})
if err != nil {
return nil, err
}
return &ReconcileCatalogSourceConfig{
CatalogSourceConfigHandler: catalogsourceconfighandler.NewHandler(mgr, client, o.SyncSender),
client: client,
syncSender: o.SyncSender,
}, nil
}
// add adds a new Controller to mgr with r as the ReconcileCatalogSourceConfig
func add(mgr manager.Manager, r *ReconcileCatalogSourceConfig) error {
// Create a new controller
c, err := controller.New("catalogsourceconfig-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to primary resource CatalogSourceConfig
err = c.Watch(&source.Kind{Type: &v2.CatalogSourceConfig{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return err
}
// Watch for child resource deletions
err = watches.WatchChildResourcesDeletionEvents(c, r.client, v2.CatalogSourceConfigKind)
if err != nil {
return err
}
return nil
}
var _ reconcile.Reconciler = &ReconcileCatalogSourceConfig{}
// ReconcileCatalogSourceConfig reconciles a CatalogSourceConfig object
type ReconcileCatalogSourceConfig struct {
CatalogSourceConfigHandler catalogsourceconfighandler.Handler
client client.Client
syncSender status.SyncSender
}
// Reconcile reads that state of the cluster for a CatalogSourceConfig object and makes changes based on the state read
// and what is in the CatalogSourceConfig.Spec
// Note:
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (r *ReconcileCatalogSourceConfig) Reconcile(request reconcile.Request) (reconcile.Result, error) {
log.Printf("Reconciling CatalogSourceConfig %s/%s\n", request.Namespace, request.Name)
log.Warning("DEPRECATION NOTICE: The CatalogSourceConfig API is deprecated in future versions. Please visit this link for futher details: https://docs.openshift.com/container-platform/4.4/release_notes/ocp-4-4-release-notes.html#ocp-4-4-marketplace-apis-deprecated")
metrics.RegisterCustomResource(metrics.ResourceTypeCSC)
// Reconcile kicked off, message Sync Channel
r.syncSender.SendSyncMessage(nil)
// Fetch the CatalogSourceConfig instance
instance := &v2.CatalogSourceConfig{}
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
return reconcile.Result{}, nil
}
// Error reading the object - report a failed sync and requeue the request.
r.syncSender.SendSyncMessage(err)
return reconcile.Result{}, err
}
// Needed because sdk does not get the gvk
instance.EnsureGVK()
err = r.CatalogSourceConfigHandler.Handle(context.TODO(), instance)
if err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{}, err
}