@@ -19,7 +19,6 @@ package controllers
19
19
import (
20
20
"context"
21
21
"fmt"
22
- "os"
23
22
24
23
routev1 "github.com/openshift/api/route/v1"
25
24
"gopkg.in/yaml.v2"
@@ -63,6 +62,8 @@ type MyControllerReconciler struct {
63
62
func (r * MyControllerReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
64
63
log := ctrllog .FromContext (ctx )
65
64
65
+ log .Info ("reconcile triggered" , "namespace" , req .Namespace , "name" , req .Name )
66
+
66
67
// fetch the MyController instance
67
68
myController := & mycontrollerv1.MyController {}
68
69
err := r .Get (ctx , req .NamespacedName , myController )
@@ -83,30 +84,58 @@ func (r *MyControllerReconciler) Reconcile(ctx context.Context, req ctrl.Request
83
84
found := & appsv1.Deployment {}
84
85
err = r .Get (ctx , types.NamespacedName {Name : myController .Name , Namespace : myController .Namespace }, found )
85
86
if err != nil && errors .IsNotFound (err ) {
87
+ err = r .removeMyController (ctx , myController )
88
+ if err != nil {
89
+ log .Error (err , "failed to remove existing resources" )
90
+ return ctrl.Result {}, err
91
+ }
86
92
err = r .setupMyController (ctx , myController )
87
93
if err != nil {
88
94
log .Error (err , "failed to setup mycontroller" )
89
95
return ctrl.Result {}, err
90
96
}
97
+
98
+ myController .Status .Phase = mycontrollerv1 .MyControllerPhaseRunning
99
+ if err = r .Client .Status ().Update (ctx , myController ); err != nil {
100
+ log .Error (err , "failed to update status" )
101
+ }
91
102
// deployed successfully - return and requeue
92
103
return ctrl.Result {Requeue : true }, nil
93
104
} else if err != nil {
94
105
log .Error (err , "failed to get Deployment" )
95
106
return ctrl.Result {}, err
96
107
}
97
108
109
+ // TODO: verify the existing installation status and reconcile, if there is a need
110
+
98
111
return ctrl.Result {}, nil
99
112
}
100
113
101
114
// SetupWithManager sets up the controller with the Manager.
102
115
func (r * MyControllerReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
116
+ log := mgr .GetLogger ()
117
+
118
+ // update platform
119
+ err := updatePlatform (mgr )
120
+ if err != nil {
121
+ return err
122
+ }
123
+
124
+ // Adding the routev1
125
+ if isOpenshift () {
126
+ if err := routev1 .AddToScheme (mgr .GetScheme ()); err != nil {
127
+ log .Error (err , "" )
128
+ return err
129
+ }
130
+ }
131
+
103
132
return ctrl .NewControllerManagedBy (mgr ).
104
133
For (& mycontrollerv1.MyController {}).
105
134
Owns (& appsv1.Deployment {}).
106
135
Complete (r )
107
136
}
108
137
109
- func (r * MyControllerReconciler ) setupMyController (ctx context.Context , m * mycontrollerv1.MyController ) error {
138
+ func (r * MyControllerReconciler ) removeMyController (ctx context.Context , m * mycontrollerv1.MyController ) error {
110
139
// delete existing resources and create new resources
111
140
// delete deployment
112
141
err := r .Delete (ctx , & appsv1.Deployment {ObjectMeta : metav1.ObjectMeta {Namespace : m .Namespace , Name : m .Name }})
@@ -119,7 +148,7 @@ func (r *MyControllerReconciler) setupMyController(ctx context.Context, m *mycon
119
148
return err
120
149
}
121
150
// delete route
122
- if os . Getenv ( "is_opensift" ) == "true" {
151
+ if isOpenshift () {
123
152
err = r .Delete (ctx , & routev1.Route {ObjectMeta : metav1.ObjectMeta {Namespace : m .Namespace , Name : m .Name }})
124
153
if err != nil && ! errors .IsNotFound (err ) {
125
154
return err
@@ -130,9 +159,12 @@ func (r *MyControllerReconciler) setupMyController(ctx context.Context, m *mycon
130
159
if err != nil && ! errors .IsNotFound (err ) {
131
160
return err
132
161
}
162
+ return nil
163
+ }
133
164
165
+ func (r * MyControllerReconciler ) setupMyController (ctx context.Context , m * mycontrollerv1.MyController ) error {
134
166
// create configmap
135
- err = r .createConfigMap (ctx , m )
167
+ err : = r .createConfigMap (ctx , m )
136
168
if err != nil {
137
169
return err
138
170
}
@@ -204,27 +236,7 @@ func (r *MyControllerReconciler) deployMyController(ctx context.Context, m *myco
204
236
Labels : ls ,
205
237
},
206
238
Spec : corev1.PodSpec {
207
- Containers : []corev1.Container {{
208
- Image : "quay.io/mycontroller/server:master" ,
209
- Name : "mycontroller" ,
210
- Command : []string {"/app/mycontroller-server" , "-config" , "/app/mycontroller.yaml" },
211
- Ports : []corev1.ContainerPort {{
212
- ContainerPort : 8080 ,
213
- Name : "mycontroller" ,
214
- }},
215
- VolumeMounts : []corev1.VolumeMount {
216
- {
217
- Name : "myc-data-dir" ,
218
- MountPath : "/mc_home" ,
219
- },
220
- {
221
- Name : "myc-config" ,
222
- MountPath : "/app/mycontroller.yaml" ,
223
- SubPath : "mycontroller.yaml" ,
224
- ReadOnly : true ,
225
- },
226
- },
227
- },
239
+ Containers : []corev1.Container {
228
240
{
229
241
Name : "influxdb" ,
230
242
Image : "influxdb:1.8.4" ,
@@ -236,6 +248,47 @@ func (r *MyControllerReconciler) deployMyController(ctx context.Context, m *myco
236
248
Name : "myc-metric-dir" ,
237
249
MountPath : "/var/lib/influxdb" ,
238
250
}},
251
+ LivenessProbe : & corev1.Probe {
252
+ InitialDelaySeconds : 7000 ,
253
+ Handler : corev1.Handler {
254
+ HTTPGet : & corev1.HTTPGetAction {
255
+ Path : "health" ,
256
+ Port : intstr .FromInt (8086 ),
257
+ Scheme : corev1 .URISchemeHTTP ,
258
+ },
259
+ },
260
+ },
261
+ },
262
+ {
263
+ Image : "quay.io/mycontroller/server:master" ,
264
+ Name : "mycontroller" ,
265
+ Command : []string {"/app/mycontroller-server" , "-config" , "/app/mycontroller.yaml" },
266
+ Ports : []corev1.ContainerPort {{
267
+ ContainerPort : 8080 ,
268
+ Name : "mycontroller" ,
269
+ }},
270
+ VolumeMounts : []corev1.VolumeMount {
271
+ {
272
+ Name : "myc-data-dir" ,
273
+ MountPath : "/mc_home" ,
274
+ },
275
+ {
276
+ Name : "myc-config" ,
277
+ MountPath : "/app/mycontroller.yaml" ,
278
+ SubPath : "mycontroller.yaml" ,
279
+ ReadOnly : true ,
280
+ },
281
+ },
282
+ LivenessProbe : & corev1.Probe {
283
+ InitialDelaySeconds : 5000 ,
284
+ Handler : corev1.Handler {
285
+ HTTPGet : & corev1.HTTPGetAction {
286
+ Path : "api/status" ,
287
+ Port : intstr .FromInt (8080 ),
288
+ Scheme : corev1 .URISchemeHTTP ,
289
+ },
290
+ },
291
+ },
239
292
}},
240
293
Volumes : []corev1.Volume {
241
294
dataVolume ,
@@ -251,7 +304,7 @@ func (r *MyControllerReconciler) deployMyController(ctx context.Context, m *myco
251
304
},
252
305
},
253
306
}
254
- // Set Memcached instance as the owner and controller
307
+ // Set MyController instance as the owner and controller
255
308
ctrl .SetControllerReference (m , mycDeployment , r .Scheme )
256
309
err := r .Create (ctx , mycDeployment )
257
310
if err != nil {
@@ -418,7 +471,7 @@ func (r *MyControllerReconciler) createService(ctx context.Context, m *mycontrol
418
471
419
472
func (r * MyControllerReconciler ) createRoute (ctx context.Context , m * mycontrollerv1.MyController ) error {
420
473
// if it is a openshift platform continue or exit
421
- if os . Getenv ( "is_openshift" ) != "true" {
474
+ if ! isOpenshift () {
422
475
return nil
423
476
}
424
477
@@ -434,7 +487,7 @@ func (r *MyControllerReconciler) createRoute(ctx context.Context, m *mycontrolle
434
487
TLS : & routev1.TLSConfig {},
435
488
},
436
489
}
437
- // Set Memcached instance as the owner and controller
490
+ // Set MyController instance as the owner and controller
438
491
ctrl .SetControllerReference (m , mycRoute , r .Scheme )
439
492
440
493
err := r .Create (ctx , mycRoute )
0 commit comments