/
reconciler.go
298 lines (247 loc) · 11.4 KB
/
reconciler.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package ups
import (
"context"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
corev1 "k8s.io/api/core/v1"
"github.com/integr8ly/integreatly-operator/pkg/resources/events"
"github.com/integr8ly/integreatly-operator/pkg/resources/owner"
"github.com/integr8ly/cloud-resource-operator/pkg/apis/integreatly/v1alpha1/types"
"github.com/sirupsen/logrus"
"github.com/integr8ly/integreatly-operator/pkg/products/monitoring"
upsv1alpha1 "github.com/aerogear/unifiedpush-operator/pkg/apis/push/v1alpha1"
monitoringv1alpha1 "github.com/integr8ly/application-monitoring-operator/pkg/apis/applicationmonitoring/v1alpha1"
croUtil "github.com/integr8ly/cloud-resource-operator/pkg/resources"
integreatlyv1alpha1 "github.com/integr8ly/integreatly-operator/pkg/apis/integreatly/v1alpha1"
"github.com/integr8ly/integreatly-operator/pkg/config"
"github.com/integr8ly/integreatly-operator/pkg/resources"
"github.com/integr8ly/integreatly-operator/pkg/resources/marketplace"
routev1 "github.com/openshift/api/route/v1"
appsv1 "k8s.io/api/apps/v1"
k8serr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
)
const (
defaultInstallationNamespace = "ups"
defaultUpsName = "ups"
defaultSubscriptionName = "rhmi-unifiedpush"
defaultRoutename = defaultUpsName + "-unifiedpush-proxy"
manifestPackage = "integreatly-unifiedpush"
tier = "production"
)
type Reconciler struct {
Config *config.Ups
ConfigManager config.ConfigReadWriter
mpm marketplace.MarketplaceInterface
logger *logrus.Entry
*resources.Reconciler
recorder record.EventRecorder
}
func NewReconciler(configManager config.ConfigReadWriter, installation *integreatlyv1alpha1.RHMI, mpm marketplace.MarketplaceInterface, recorder record.EventRecorder) (*Reconciler, error) {
config, err := configManager.ReadUps()
if err != nil {
return nil, fmt.Errorf("could not retrieve ups config: %w", err)
}
if config.GetNamespace() == "" {
config.SetNamespace(installation.Spec.NamespacePrefix + defaultInstallationNamespace)
configManager.WriteConfig(config)
}
if config.GetOperatorNamespace() == "" {
if installation.Spec.OperatorsInProductNamespace {
config.SetOperatorNamespace(config.GetNamespace())
} else {
config.SetOperatorNamespace(config.GetNamespace() + "-operator")
}
configManager.WriteConfig(config)
}
config.SetBlackboxTargetPath("/rest/auth/config/")
logger := logrus.NewEntry(logrus.StandardLogger())
return &Reconciler{
ConfigManager: configManager,
Config: config,
mpm: mpm,
logger: logger,
Reconciler: resources.NewReconciler(mpm),
recorder: recorder,
}, nil
}
func (r *Reconciler) GetPreflightObject(ns string) runtime.Object {
return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "unifiedpush-operator",
Namespace: ns,
},
}
}
func (r *Reconciler) Reconcile(ctx context.Context, installation *integreatlyv1alpha1.RHMI, product *integreatlyv1alpha1.RHMIProductStatus, serverClient k8sclient.Client) (integreatlyv1alpha1.StatusPhase, error) {
logrus.Infof("Reconciling %s", defaultUpsName)
phase, err := r.ReconcileFinalizer(ctx, serverClient, installation, string(r.Config.GetProductName()), func() (integreatlyv1alpha1.StatusPhase, error) {
phase, err := resources.RemoveNamespace(ctx, installation, serverClient, r.Config.GetNamespace())
if err != nil || phase != integreatlyv1alpha1.PhaseCompleted {
return phase, err
}
phase, err = resources.RemoveNamespace(ctx, installation, serverClient, r.Config.GetOperatorNamespace())
if err != nil || phase != integreatlyv1alpha1.PhaseCompleted {
return phase, err
}
return integreatlyv1alpha1.PhaseCompleted, nil
})
if err != nil || phase != integreatlyv1alpha1.PhaseCompleted {
events.HandleError(r.recorder, installation, phase, "Failed to reconcile finalizer", err)
return phase, err
}
phase, err = r.ReconcileNamespace(ctx, r.Config.GetOperatorNamespace(), installation, serverClient)
if err != nil || phase != integreatlyv1alpha1.PhaseCompleted {
events.HandleError(r.recorder, installation, phase, fmt.Sprintf("Failed to reconcile %s namespace", r.Config.GetOperatorNamespace()), err)
return phase, err
}
ns := r.Config.GetNamespace()
phase, err = r.ReconcileNamespace(ctx, ns, installation, serverClient)
if err != nil || phase != integreatlyv1alpha1.PhaseCompleted {
events.HandleError(r.recorder, installation, phase, fmt.Sprintf("Failed to reconcile %s namespace", ns), err)
return phase, err
}
namespace, err := resources.GetNS(ctx, ns, serverClient)
if err != nil {
events.HandleError(r.recorder, installation, integreatlyv1alpha1.PhaseFailed, fmt.Sprintf("Failed to retrieve %s namespace", ns), err)
return integreatlyv1alpha1.PhaseFailed, err
}
phase, err = r.ReconcileSubscription(ctx, namespace, marketplace.Target{Pkg: defaultSubscriptionName, Namespace: r.Config.GetOperatorNamespace(), Channel: marketplace.IntegreatlyChannel, ManifestPackage: manifestPackage}, []string{ns}, serverClient)
if err != nil || phase != integreatlyv1alpha1.PhaseCompleted {
events.HandleError(r.recorder, installation, phase, fmt.Sprintf("Failed to reconcile %s subscription", defaultSubscriptionName), err)
return phase, err
}
phase, err = r.reconcileComponents(ctx, installation, serverClient)
if err != nil || phase != integreatlyv1alpha1.PhaseCompleted {
events.HandleError(r.recorder, installation, phase, "Failed to reconcile components", err)
return phase, err
}
phase, err = r.reconcileHost(ctx, serverClient)
if err != nil || phase != integreatlyv1alpha1.PhaseCompleted {
events.HandleError(r.recorder, installation, phase, "Failed to reconcile host", err)
return phase, err
}
phase, err = r.reconcileBlackboxTargets(ctx, installation, serverClient)
if err != nil || phase != integreatlyv1alpha1.PhaseCompleted {
events.HandleError(r.recorder, installation, phase, "Failed to reconcile blackbox targets", err)
return phase, err
}
product.Host = r.Config.GetHost()
product.Version = r.Config.GetProductVersion()
product.OperatorVersion = r.Config.GetOperatorVersion()
events.HandleProductComplete(r.recorder, installation, integreatlyv1alpha1.ProductsStage, r.Config.GetProductName())
logrus.Infof("%s is successfully reconciled", defaultUpsName)
return integreatlyv1alpha1.PhaseCompleted, nil
}
func (r *Reconciler) reconcileComponents(ctx context.Context, installation *integreatlyv1alpha1.RHMI, client k8sclient.Client) (integreatlyv1alpha1.StatusPhase, error) {
logrus.Info("Reconciling external postgres")
ns := installation.Namespace
// setup postgres custom resource
// this will be used by the cloud resources operator to provision a postgres instance
postgresName := fmt.Sprintf("ups-postgres-%s", installation.Name)
postgres, err := croUtil.ReconcilePostgres(ctx, client, defaultInstallationNamespace, installation.Spec.Type, tier, postgresName, ns, postgresName, ns, func(cr metav1.Object) error {
owner.AddIntegreatlyOwnerAnnotations(cr, installation)
return nil
})
if err != nil {
return integreatlyv1alpha1.PhaseFailed, fmt.Errorf("failed to reconcile postgres request: %w", err)
}
// wait for the postgres instance to reconcile
if postgres.Status.Phase != types.PhaseComplete {
return integreatlyv1alpha1.PhaseAwaitingComponents, nil
}
// create the prometheus availability rule
_, err = resources.CreatePostgresAvailabilityAlert(ctx, client, installation, postgres)
if err != nil {
return integreatlyv1alpha1.PhaseFailed, fmt.Errorf("failed to create postgres prometheus alert for ups: %w", err)
}
// get the secret created by the cloud resources operator
// containing postgres connection details
connSec := &corev1.Secret{}
err = client.Get(ctx, k8sclient.ObjectKey{Name: postgres.Status.SecretRef.Name, Namespace: postgres.Status.SecretRef.Namespace}, connSec)
if err != nil {
return integreatlyv1alpha1.PhaseFailed, fmt.Errorf("failed to get postgres credential secret: %w", err)
}
postgresSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: postgres.Status.SecretRef.Name,
Namespace: r.Config.GetNamespace(),
},
}
controllerutil.CreateOrUpdate(ctx, client, postgresSecret, func() error {
postgresSecret.StringData = map[string]string{
"POSTGRES_DATABASE": string(connSec.Data["database"]),
"POSTGRES_HOST": string(connSec.Data["host"]),
"POSTGRES_PORT": string(connSec.Data["port"]),
"POSTGRES_USERNAME": string(connSec.Data["username"]),
"POSTGRES_PASSWORD": string(connSec.Data["password"]),
"POSTGRES_SUPERUSER": "false",
"POSTGRES_VERSION": "10",
}
return nil
})
// Reconcile ups custom resource
logrus.Info("Reconciling unified push server cr")
cr := &upsv1alpha1.UnifiedPushServer{
ObjectMeta: metav1.ObjectMeta{
Name: defaultUpsName,
Namespace: r.Config.GetNamespace(),
},
Spec: upsv1alpha1.UnifiedPushServerSpec{
ExternalDB: true,
DatabaseSecret: postgres.Status.SecretRef.Name,
},
}
err = client.Get(ctx, k8sclient.ObjectKey{Name: cr.Name, Namespace: cr.Namespace}, cr)
if err != nil {
// If the error is not an isNotFound error
if !k8serr.IsNotFound(err) {
return integreatlyv1alpha1.PhaseFailed, err
}
// Otherwise create the cr
owner.AddIntegreatlyOwnerAnnotations(cr, installation)
if err := client.Create(ctx, cr); err != nil {
return integreatlyv1alpha1.PhaseFailed, fmt.Errorf("failed to create unifiedpush server custom resource during reconcile: %w", err)
}
}
// Wait till the ups cr status is complete
if cr.Status.Phase != upsv1alpha1.PhaseReconciling {
logrus.Info("Waiting for unified push server cr phase to complete")
return integreatlyv1alpha1.PhaseInProgress, nil
}
logrus.Info("Successfully reconciled unified push server custom resource")
return integreatlyv1alpha1.PhaseCompleted, nil
}
func (r *Reconciler) reconcileHost(ctx context.Context, serverClient k8sclient.Client) (integreatlyv1alpha1.StatusPhase, error) {
// Setting host on config to exposed route
logrus.Info("Setting unified push server config host")
upsRoute := &routev1.Route{}
err := serverClient.Get(ctx, k8sclient.ObjectKey{Name: defaultRoutename, Namespace: r.Config.GetNamespace()}, upsRoute)
if err != nil {
return integreatlyv1alpha1.PhaseFailed, fmt.Errorf("failed to get route for unified push server: %w", err)
}
r.Config.SetHost("https://" + upsRoute.Spec.Host)
err = r.ConfigManager.WriteConfig(r.Config)
if err != nil {
return integreatlyv1alpha1.PhaseFailed, fmt.Errorf("could not update unified push server config: %w", err)
}
logrus.Info("Successfully set unified push server host")
return integreatlyv1alpha1.PhaseCompleted, nil
}
func (r *Reconciler) reconcileBlackboxTargets(ctx context.Context, installation *integreatlyv1alpha1.RHMI, client k8sclient.Client) (integreatlyv1alpha1.StatusPhase, error) {
cfg, err := r.ConfigManager.ReadMonitoring()
if err != nil {
return integreatlyv1alpha1.PhaseFailed, fmt.Errorf("error reading monitoring config: %w", err)
}
err = monitoring.CreateBlackboxTarget(ctx, "integreatly-ups", monitoringv1alpha1.BlackboxtargetData{
Url: r.Config.GetHost() + "/" + r.Config.GetBlackboxTargetPath(),
Service: "ups-ui",
}, cfg, installation, client)
if err != nil {
return integreatlyv1alpha1.PhaseFailed, fmt.Errorf("error creating ups blackbox target: %w", err)
}
return integreatlyv1alpha1.PhaseCompleted, nil
}