forked from syndesisio/syndesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syndesis_controller.go
201 lines (172 loc) · 6.79 KB
/
syndesis_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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package syndesis
import (
"context"
"reflect"
"time"
synpkg "github.com/syndesisio/syndesis/install/operator/pkg"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
synapi "github.com/syndesisio/syndesis/install/operator/pkg/apis/syndesis/v1beta3"
"github.com/syndesisio/syndesis/install/operator/pkg/syndesis/action"
"github.com/syndesisio/syndesis/install/operator/pkg/syndesis/clienttools"
"github.com/syndesisio/syndesis/install/operator/pkg/syndesis/configuration"
"github.com/syndesisio/syndesis/install/operator/pkg/syndesis/olm"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var log = logf.Log.WithName("controller")
var (
actions []action.SyndesisOperatorAction
)
// Add creates a new Syndesis 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) error {
reconciler, err := newReconciler(mgr)
if err != nil {
return err
}
return add(mgr, reconciler)
}
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) (*ReconcileSyndesis, error) {
clientTools := &clienttools.ClientTools{}
clientTools.SetRuntimeClient(mgr.GetClient())
return &ReconcileSyndesis{
clientTools: clientTools,
scheme: mgr.GetScheme(),
}, nil
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r *ReconcileSyndesis) error {
// Create a new controller
c, err := controller.New("syndesis-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to primary resource Syndesis
err = c.Watch(&source.Kind{Type: &synapi.Syndesis{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return err
}
actions = action.NewOperatorActions(mgr, r.clientTools)
return nil
}
var _ reconcile.Reconciler = &ReconcileSyndesis{}
// ReconcileSyndesis reconciles a Syndesis object
type ReconcileSyndesis struct {
// This client kit contains a split client, initialized using mgr.Client() above,
// that reads objects from the cache and writes to the apiserver
clientTools *clienttools.ClientTools
scheme *runtime.Scheme
}
// Reconcile the state of the Syndesis infrastructure elements
// 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 *ReconcileSyndesis) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.V(2).Info("Reconciling Syndesis")
// Fetch the Syndesis syndesis
syndesis := &synapi.Syndesis{}
client, _ := r.clientTools.RuntimeClient()
productName, err := configuration.GetProductName(configuration.TemplateConfig)
if err != nil {
log.Error(err, "Failed to determine product name")
}
err = client.Get(ctx, request.NamespacedName, syndesis)
if err != nil {
if errors.IsNotFound(err) {
//
// Allow the operator to be upgradeable if no syndesis resource is installed
//
state := olm.ConditionState{
Status: metav1.ConditionTrue,
Reason: "Ready",
Message: "No Syndesis Resource installed so operator can be upgraded",
}
if upgErr := olm.SetUpgradeCondition(ctx, r.clientTools, request.Namespace, productName, state); upgErr != nil {
log.Error(upgErr, "Failed to set the upgrade condition on the operator")
}
// 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 - requeue the request.
log.Error(err, "Cannot read object", request.NamespacedName)
//
// Stop the operator being upgradeable until a state can be read
//
state := olm.ConditionState{
Status: metav1.ConditionFalse,
Reason: "NotReady",
Message: "Read error detecting syndesis resource",
}
if upgErr := olm.SetUpgradeCondition(ctx, r.clientTools, request.Namespace, productName, state); upgErr != nil {
log.Error(upgErr, "Failed to set the upgrade condition on the operator")
}
return reconcile.Result{
Requeue: true,
RequeueAfter: 10 * time.Second,
}, err
}
//
// Check the syndesis resource allows for upgrades. Otherwise disable
//
stateMsg := ""
if syndesis.Status.Version != synpkg.DefaultOperatorTag {
stateMsg = "Not upgradeable due to version mismatch of operator and Syndesis Resource"
} else if syndesis.Status.Phase != synapi.SyndesisPhaseInstalled {
stateMsg = "Not upgradeable due to Syndesis Resource phase not reaching 'Installed'"
}
if len(stateMsg) > 0 {
state := olm.ConditionState{
Status: metav1.ConditionFalse,
Reason: "NotReady",
Message: stateMsg,
}
if upgErr := olm.SetUpgradeCondition(ctx, r.clientTools, request.Namespace, productName, state); upgErr != nil {
log.Error(upgErr, "Failed to set the upgrade condition on the operator")
}
}
for _, a := range actions {
// Don't want to do anything if the syndesis resource has been updated in the meantime
// This happens when a processing takes more tha the resync period
if latest, err := r.isLatestVersion(ctx, syndesis); err != nil || !latest {
log.Info("syndesis resource changed in the meantime, requeue and rerun in 5 seconds", "name", syndesis.Name)
return reconcile.Result{
Requeue: true,
RequeueAfter: 5 * time.Second,
}, nil
}
if a.CanExecute(syndesis) {
log.V(synpkg.DEBUG_LOGGING_LVL).Info("Running action", "action", reflect.TypeOf(a))
if err := a.Execute(ctx, syndesis, request.Namespace, productName); err != nil {
log.Error(err, "Error reconciling", "action", reflect.TypeOf(a), "phase", syndesis.Status.Phase)
return reconcile.Result{
Requeue: true,
RequeueAfter: 10 * time.Second,
}, nil
}
}
}
// Requeuing because actions expect this behaviour
return reconcile.Result{
Requeue: true,
RequeueAfter: 15 * time.Second,
}, nil
}
func (r *ReconcileSyndesis) isLatestVersion(ctx context.Context, syndesis *synapi.Syndesis) (bool, error) {
refreshed := syndesis.DeepCopy()
client, _ := r.clientTools.RuntimeClient()
if err := client.Get(ctx, types.NamespacedName{Name: refreshed.Name, Namespace: refreshed.Namespace}, refreshed); err != nil {
return false, err
}
return refreshed.ResourceVersion == syndesis.ResourceVersion, nil
}