-
Notifications
You must be signed in to change notification settings - Fork 38
/
semeru_compiler.go
591 lines (524 loc) · 21.9 KB
/
semeru_compiler.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
Copyright contributors to the WASdev project.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllers
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"time"
openlibertyv1 "github.com/OpenLiberty/open-liberty-operator/api/v1"
lutils "github.com/OpenLiberty/open-liberty-operator/utils"
"github.com/application-stacks/runtime-component-operator/common"
utils "github.com/application-stacks/runtime-component-operator/utils"
certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
certmanagermetav1 "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
const (
SemeruLabelNameSuffix = "-semeru-compiler"
SemeruLabelName = "semeru-compiler"
SemeruGenerationLabelNameSuffix = "/semeru-compiler-generation"
StatusReferenceSemeruGeneration = "semeruGeneration"
StatusReferenceSemeruInstancesCompleted = "semeruInstancesCompleted"
)
// Create the Deployment and Service objects for a Semeru Compiler used by an Open Liberty Application
func (r *ReconcileOpenLiberty) reconcileSemeruCompiler(ola *openlibertyv1.OpenLibertyApplication) (error, string, bool) {
compilerMeta := metav1.ObjectMeta{
Name: getSemeruCompilerNameWithGeneration(ola),
Namespace: ola.GetNamespace(),
}
currentGeneration := getGeneration(ola)
if r.isSemeruEnabled(ola) {
cmPresent, _ := r.IsGroupVersionSupported(certmanagerv1.SchemeGroupVersion.String(), "Certificate")
// Create the Semeru Service object
semsvc := &corev1.Service{ObjectMeta: compilerMeta}
tlsSecretName := ""
err := r.CreateOrUpdate(semsvc, ola, func() error {
reconcileSemeruService(semsvc, ola)
if r.IsOpenShift() {
if _, ok := semsvc.Annotations["service.beta.openshift.io/serving-cert-secret-name"]; !ok {
if _, ok = semsvc.Annotations["service.alpha.openshift.io/serving-cert-secret-name"]; !ok {
if semsvc.Annotations == nil {
semsvc.Annotations = map[string]string{}
}
tlsSecretName = semsvc.GetName() + "-tls-ocp"
semsvc.Annotations["service.beta.openshift.io/serving-cert-secret-name"] = tlsSecretName
if ola.Status.SemeruCompiler == nil {
ola.Status.SemeruCompiler = &openlibertyv1.SemeruCompilerStatus{}
}
ola.Status.SemeruCompiler.TLSSecretName = tlsSecretName
}
}
}
return nil
})
if err != nil {
return err, "Failed to reconcile the Semeru Compiler Service", false
}
//create certmanager issuer and certificate if necessary
if !r.IsOpenShift() || cmPresent {
err = r.GenerateCMIssuer(ola.Namespace, "olo", "Open Liberty Operator", "open-liberty-operator")
if err != nil {
return err, "Failed to reconcile Certificate Issuer", false
}
err = r.reconcileSemeruCMCertificate(ola)
if err != nil {
return err, "Failed to reconcile Semeru Compiler Certificate", false
}
}
//TLS Secret
semeruTLSSecret := &corev1.Secret{}
err = r.GetClient().Get(context.TODO(), types.NamespacedName{Name: ola.Status.SemeruCompiler.TLSSecretName, Namespace: ola.Namespace}, semeruTLSSecret)
if err != nil {
return err, "Failed to reconcile Semeru Compiler TLS Secret", false
}
//Deployment
semeruDeployment := &appsv1.Deployment{ObjectMeta: compilerMeta}
err = r.CreateOrUpdate(semeruDeployment, ola, func() error {
r.reconcileSemeruDeployment(ola, semeruDeployment)
return nil
})
if err != nil {
return err, "Failed to reconcile Deployment : " + semeruDeployment.Name, false
}
// Add the new generation number to .status.reference.semeruInstancesCompleted as a comma-separated string
areCompletedSemeruInstancesMarkedToBeDeleted := false
if ola.Status.References != nil {
if completedInstances, ok := ola.Status.References[StatusReferenceSemeruInstancesCompleted]; ok {
if completedInstances != currentGeneration {
// Mark old Semeru Cloud Compiler instances for deletion
areCompletedSemeruInstancesMarkedToBeDeleted = true
if !strings.Contains(completedInstances, currentGeneration) {
ola.Status.References[StatusReferenceSemeruInstancesCompleted] += "," + currentGeneration
}
}
} else {
ola.Status.References[StatusReferenceSemeruInstancesCompleted] = currentGeneration
}
}
return nil, "", areCompletedSemeruInstancesMarkedToBeDeleted
} else {
semsvc := &corev1.Service{ObjectMeta: compilerMeta}
semeruDeployment := &appsv1.Deployment{ObjectMeta: compilerMeta}
if err := r.DeleteResources([]client.Object{semsvc, semeruDeployment}); err != nil {
return err, "Failed to delete Semeru Compiler resources", false
}
ola.Status.SemeruCompiler = nil
return nil, "", false
}
}
// Returns the one-based index generation indicated by .status.references.semeruGeneration if it exists, otherwise defaults to 1
func getGeneration(ola *openlibertyv1.OpenLibertyApplication) string {
if ola.Status.References != nil {
if semeruGeneration, ok := ola.Status.References[StatusReferenceSemeruGeneration]; ok {
return semeruGeneration
}
ola.Status.References[StatusReferenceSemeruGeneration] = fmt.Sprint(1)
}
return "1"
}
// Increments the generation number at .status.references.semeruGeneration if it exists, otherwise if possible, initializes the generation to 1
func createNewSemeruGeneration(ola *openlibertyv1.OpenLibertyApplication) {
if ola.Status.References != nil {
if semeruGeneration, ok := ola.Status.References[StatusReferenceSemeruGeneration]; ok {
if generation, err := strconv.Atoi(semeruGeneration); err == nil {
ola.Status.References[StatusReferenceSemeruGeneration] = fmt.Sprint(generation + 1)
} else {
ola.Status.References[StatusReferenceSemeruGeneration] = fmt.Sprint(1)
}
} else {
ola.Status.References[StatusReferenceSemeruGeneration] = fmt.Sprint(1)
}
}
}
func getSemeruGenerationLabelName(ola *openlibertyv1.OpenLibertyApplication) string {
return ola.GetGroupName() + SemeruGenerationLabelNameSuffix
}
// Deletes old Semeru Cloud Compiler instances that have been marked as completed (instances that underwent at least one reconcile)
func (r *ReconcileOpenLiberty) deleteCompletedSemeruInstances(ola *openlibertyv1.OpenLibertyApplication) error {
if semeruInstancesCompleted, ok := ola.Status.References[StatusReferenceSemeruInstancesCompleted]; ok {
generationsMarkedForDeletion := make([]string, 0)
cmPresent, _ := r.IsGroupVersionSupported(certmanagerv1.SchemeGroupVersion.String(), "Certificate")
useCertManager := !r.IsOpenShift() || cmPresent
// For each completed Semeru Cloud Compiler generation
for _, completedGenerationStr := range strings.Split(semeruInstancesCompleted, ",") {
completedGeneration, _ := strconv.Atoi(completedGenerationStr)
currentGeneration, _ := strconv.Atoi(ola.Status.References[StatusReferenceSemeruGeneration])
// Delete the older generation's resources and mark the status reference field for deletion
if completedGeneration < currentGeneration {
resourceName := getSemeruCompilerName(ola) + "-" + completedGenerationStr
resourceNamespace := ola.GetNamespace()
resourceLabels := map[string]string{
getSemeruGenerationLabelName(ola): completedGenerationStr,
"app.kubernetes.io/name": getSemeruCompilerName(ola),
}
// Delete Deployment
deployment := &appsv1.Deployment{}
deployment.Name = resourceName
deployment.Namespace = resourceNamespace
deployment.Labels = resourceLabels
err := r.DeleteResource(deployment)
if err != nil {
return err
}
// Delete Service
service := &corev1.Service{}
service.Name = resourceName
service.Namespace = resourceNamespace
service.Labels = resourceLabels
err = r.DeleteResource(service)
if err != nil {
return err
}
// Remove CertManager Certificate and Secret if necessary
if useCertManager {
cmCertificate := &certmanagerv1.Certificate{}
cmCertificate.Name = resourceName
cmCertificate.Namespace = resourceNamespace
cmCertificate.Labels = resourceLabels
err = r.DeleteResource(cmCertificate)
if err != nil {
return err
}
cmSecret := &corev1.Secret{}
cmSecret.Name = resourceName + "-tls-cm"
cmSecret.Namespace = resourceNamespace
err = r.DeleteResource(cmSecret)
if err != nil {
return err
}
}
// On successful cleanup, mark the generation for deletion from the status reference field
generationsMarkedForDeletion = append(generationsMarkedForDeletion, completedGenerationStr)
}
}
// Remove deleted generations from the status reference field
for _, deletedGeneration := range generationsMarkedForDeletion {
oldInstancesCompleted := ola.Status.References[StatusReferenceSemeruInstancesCompleted]
ola.Status.References[StatusReferenceSemeruInstancesCompleted] = strings.Replace(oldInstancesCompleted, deletedGeneration+",", "", 1)
// Corner case: The new generation completed before the old generation completed
if oldInstancesCompleted == ola.Status.References[StatusReferenceSemeruInstancesCompleted] {
ola.Status.References[StatusReferenceSemeruInstancesCompleted] = strings.Replace(oldInstancesCompleted, ","+deletedGeneration, "", 1)
}
}
}
return nil
}
func (r *ReconcileOpenLiberty) reconcileSemeruDeployment(ola *openlibertyv1.OpenLibertyApplication, deploy *appsv1.Deployment) {
deploy.Labels = getLabels(ola)
deploy.Spec.Strategy.Type = appsv1.RecreateDeploymentStrategyType
if deploy.Spec.Selector == nil {
deploy.Spec.Selector = &metav1.LabelSelector{
MatchLabels: getSelectors(ola),
}
}
semeruCloudCompiler := ola.GetSemeruCloudCompiler()
deploy.Spec.Replicas = semeruCloudCompiler.GetReplicas()
// Get Semeru resources config
instanceResources := semeruCloudCompiler.Resources
requestsMemory := getQuantityFromRequestsOrDefault(instanceResources, corev1.ResourceMemory, "800Mi")
requestsCPU := getQuantityFromRequestsOrDefault(instanceResources, corev1.ResourceCPU, "100m")
limitsMemory := getQuantityFromLimitsOrDefault(instanceResources, corev1.ResourceMemory, "1200Mi")
limitsCPU := getQuantityFromLimitsOrDefault(instanceResources, corev1.ResourceCPU, "2000m")
// Liveness probe
livenessProbe := corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
TCPSocket: &corev1.TCPSocketAction{
Port: intstr.FromInt(38400),
},
},
InitialDelaySeconds: 10,
PeriodSeconds: 10,
}
// Readiness probe
readinessProbe := corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
TCPSocket: &corev1.TCPSocketAction{
Port: intstr.FromInt(38400),
},
},
InitialDelaySeconds: 5,
PeriodSeconds: 5,
}
deploy.Spec.Template = corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: getLabels(ola),
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "compiler",
Image: ola.Status.GetImageReference(),
ImagePullPolicy: *ola.GetPullPolicy(),
Command: []string{"jitserver"},
Ports: []corev1.ContainerPort{
{
ContainerPort: 38400,
Protocol: corev1.ProtocolTCP,
},
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceMemory: requestsMemory,
corev1.ResourceCPU: requestsCPU,
},
Limits: corev1.ResourceList{
corev1.ResourceMemory: limitsMemory,
corev1.ResourceCPU: limitsCPU,
},
},
Env: []corev1.EnvVar{
{Name: "OPENJ9_JAVA_OPTIONS", Value: "-XX:+JITServerLogConnections" +
" -XX:+JITServerShareROMClasses" +
" -XX:JITServerSSLKey=/etc/x509/certs/tls.key" +
" -XX:JITServerSSLCert=/etc/x509/certs/tls.crt"},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "certs",
ReadOnly: true,
MountPath: "/etc/x509/certs",
},
},
LivenessProbe: &livenessProbe,
ReadinessProbe: &readinessProbe,
},
},
Volumes: []corev1.Volume{{
Name: "certs",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: ola.Status.SemeruCompiler.TLSSecretName,
},
},
}},
},
}
// Copy the service account from the OpenLibertyApplcation CR
if ola.GetServiceAccountName() != nil && *ola.GetServiceAccountName() != "" {
deploy.Spec.Template.Spec.ServiceAccountName = *ola.GetServiceAccountName()
} else {
deploy.Spec.Template.Spec.ServiceAccountName = ola.GetName()
}
// This ensures that the semeru pod(s) are updated if the service account is updated
saRV := ola.GetStatus().GetReferences()[common.StatusReferenceSAResourceVersion]
if saRV != "" {
deploy.Spec.Template.Spec.Containers[0].Env = append(deploy.Spec.Template.Spec.Containers[0].Env, corev1.EnvVar{Name: "SA_RESOURCE_VERSION", Value: saRV})
}
// Copy the securityContext from the OpenLibertyApplcation CR
deploy.Spec.Template.Spec.Containers[0].SecurityContext = utils.GetSecurityContext(ola)
lutils.AddSecretResourceVersionAsEnvVar(&deploy.Spec.Template, ola, r.GetClient(), ola.Status.SemeruCompiler.TLSSecretName, "TLS")
}
func reconcileSemeruService(svc *corev1.Service, ola *openlibertyv1.OpenLibertyApplication) {
var port int32 = 38400
var timeout int32 = 86400
svc.Labels = getLabels(ola)
svc.Spec.Selector = getSelectors(ola)
if len(svc.Spec.Ports) == 0 {
svc.Spec.Ports = append(svc.Spec.Ports, corev1.ServicePort{})
}
svc.Spec.Ports[0].Protocol = corev1.ProtocolTCP
svc.Spec.Ports[0].Port = port
svc.Spec.Ports[0].TargetPort = intstr.FromInt(int(port))
svc.Spec.SessionAffinity = corev1.ServiceAffinityClientIP
svc.Spec.SessionAffinityConfig = &corev1.SessionAffinityConfig{
ClientIP: &corev1.ClientIPConfig{
TimeoutSeconds: &timeout,
},
}
if ola.Status.SemeruCompiler == nil {
ola.Status.SemeruCompiler = &openlibertyv1.SemeruCompilerStatus{}
}
ola.Status.SemeruCompiler.ServiceHostname = svc.GetName() + "." + svc.GetNamespace() + ".svc"
}
func (r *ReconcileOpenLiberty) reconcileSemeruCMCertificate(ola *openlibertyv1.OpenLibertyApplication) error {
svcCert := &certmanagerv1.Certificate{}
svcCert.Name = getSemeruCompilerNameWithGeneration(ola)
svcCert.Namespace = ola.GetNamespace()
customIssuer := &certmanagerv1.Issuer{ObjectMeta: metav1.ObjectMeta{
Name: "olo-" + "-custom-issuer",
Namespace: svcCert.Namespace,
}}
customIssuerFound := false
err := r.GetClient().Get(context.Background(), types.NamespacedName{Name: customIssuer.Name,
Namespace: customIssuer.Namespace}, customIssuer)
if err == nil {
customIssuerFound = true
}
shouldRefreshCertSecret := false
err = r.CreateOrUpdate(svcCert, ola, func() error {
svcCert.Labels = ola.GetLabels()
svcCert.Labels[getSemeruGenerationLabelName(ola)] = getGeneration(ola)
svcCert.Spec.IssuerRef = certmanagermetav1.ObjectReference{
Name: "olo-ca-issuer",
}
if customIssuerFound {
svcCert.Spec.IssuerRef.Name = customIssuer.Name
}
rVersion, _ := utils.GetIssuerResourceVersion(r.GetClient(), svcCert)
if svcCert.Spec.SecretTemplate == nil {
svcCert.Spec.SecretTemplate = &certmanagerv1.CertificateSecretTemplate{
Annotations: map[string]string{},
}
}
if svcCert.Spec.SecretTemplate.Annotations[ola.GetGroupName()+"/cm-issuer-version"] != rVersion {
if svcCert.Spec.SecretTemplate.Annotations == nil {
svcCert.Spec.SecretTemplate.Annotations = map[string]string{}
}
svcCert.Spec.SecretTemplate.Annotations[ola.GetGroupName()+"/cm-issuer-version"] = rVersion
shouldRefreshCertSecret = true
}
svcCert.Spec.SecretName = svcCert.Name + "-tls-cm"
svcCert.Spec.DNSNames = make([]string, 2)
svcCert.Spec.DNSNames[0] = svcCert.Name + "." + ola.Namespace + ".svc"
svcCert.Spec.DNSNames[1] = svcCert.Name + "." + ola.Namespace + ".svc.cluster.local"
svcCert.Spec.CommonName = svcCert.Name
duration, err := time.ParseDuration(common.Config[common.OpConfigCMCertDuration])
if err != nil {
return err
}
svcCert.Spec.Duration = &metav1.Duration{Duration: duration}
return nil
})
if err != nil {
return err
}
if shouldRefreshCertSecret {
r.DeleteResource(&corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: svcCert.Spec.SecretName, Namespace: svcCert.Namespace}})
}
if ola.Status.SemeruCompiler == nil {
ola.Status.SemeruCompiler = &openlibertyv1.SemeruCompilerStatus{}
}
ola.Status.SemeruCompiler.TLSSecretName = svcCert.Spec.SecretName
return nil
}
func getSemeruCompilerNameWithGeneration(ola *openlibertyv1.OpenLibertyApplication) string {
return getSemeruCompilerName(ola) + "-" + getGeneration(ola)
}
func getSemeruCompilerName(ola *openlibertyv1.OpenLibertyApplication) string {
return ola.GetName() + SemeruLabelNameSuffix
}
// Create the Selector map for a Semeru Compiler
func getSelectors(ola *openlibertyv1.OpenLibertyApplication) map[string]string {
requiredSelector := make(map[string]string)
requiredSelector["app.kubernetes.io/component"] = SemeruLabelName
requiredSelector["app.kubernetes.io/instance"] = getSemeruCompilerNameWithGeneration(ola)
requiredSelector["app.kubernetes.io/part-of"] = ola.GetName()
return requiredSelector
}
// Create the Labels map for a Semeru Compiler
func getLabels(ola *openlibertyv1.OpenLibertyApplication) map[string]string {
requiredLabels := make(map[string]string)
requiredLabels["app.kubernetes.io/name"] = getSemeruCompilerName(ola)
requiredLabels["app.kubernetes.io/instance"] = getSemeruCompilerNameWithGeneration(ola)
requiredLabels["app.kubernetes.io/managed-by"] = OperatorName
requiredLabels["app.kubernetes.io/component"] = SemeruLabelName
requiredLabels["app.kubernetes.io/part-of"] = ola.GetName()
requiredLabels[getSemeruGenerationLabelName(ola)] = getGeneration(ola)
return requiredLabels
}
// Returns quantity at resourceRequirements.Requests[resourceName] if it exists, otherwise return the parsed defaultQuantity
func getQuantityFromRequestsOrDefault(resourceRequirements *corev1.ResourceRequirements, resourceName corev1.ResourceName, defaultQuantity string) resource.Quantity {
if resourceRequirements != nil && resourceRequirements.Requests != nil {
if mapValue, ok := resourceRequirements.Requests[resourceName]; ok {
return mapValue
}
}
return resource.MustParse(defaultQuantity)
}
// Returns quantity at resourceRequirements.Limits[resourceName] if it exists, otherwise return the parsed defaultQuantity
func getQuantityFromLimitsOrDefault(resourceRequirements *corev1.ResourceRequirements, resourceName corev1.ResourceName, defaultQuantity string) resource.Quantity {
if resourceRequirements != nil && resourceRequirements.Limits != nil {
if mapValue, ok := resourceRequirements.Limits[resourceName]; ok {
return mapValue
}
}
return resource.MustParse(defaultQuantity)
}
func getSemeruCertVolumeMount(ola *openlibertyv1.OpenLibertyApplication) corev1.VolumeMount {
return corev1.VolumeMount{
Name: "semeru-certs",
MountPath: "/etc/x509/semeru-certs",
ReadOnly: true,
}
}
func getSemeruCertVolume(ola *openlibertyv1.OpenLibertyApplication) *corev1.Volume {
if ola.Status.SemeruCompiler == nil || ola.Status.SemeruCompiler.TLSSecretName == "" ||
strings.HasSuffix(ola.Status.SemeruCompiler.TLSSecretName, "-ocp") {
return nil
}
return &corev1.Volume{
Name: "semeru-certs",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: ola.Status.SemeruCompiler.TLSSecretName,
},
},
}
}
func (r *ReconcileOpenLiberty) getSemeruJavaOptions(instance *openlibertyv1.OpenLibertyApplication) []string {
if r.isSemeruEnabled(instance) {
certificateLocation := "/etc/x509/semeru-certs/ca.crt"
if instance.Status.SemeruCompiler != nil && strings.HasSuffix(instance.Status.SemeruCompiler.TLSSecretName, "-ocp") {
certificateLocation = "/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt"
}
jitServerAddress := instance.Status.SemeruCompiler.ServiceHostname
jitSeverOptions := fmt.Sprintf("-XX:+UseJITServer -XX:+JITServerLogConnections -XX:JITServerAddress=%v -XX:JITServerSSLRootCerts=%v",
jitServerAddress, certificateLocation)
args := []string{
"/bin/bash",
"-c",
"export OPENJ9_JAVA_OPTIONS=\"$OPENJ9_JAVA_OPTIONS " +
jitSeverOptions +
"\" && server run",
}
return args
}
return nil
}
func (r *ReconcileOpenLiberty) areSemeruCompilerResourcesReady(ola *openlibertyv1.OpenLibertyApplication) error {
var replicas, readyReplicas, updatedReplicas int32
namespacedName := types.NamespacedName{Name: getSemeruCompilerNameWithGeneration(ola), Namespace: ola.GetNamespace()}
// Check if deployment exists
deployment := &appsv1.Deployment{}
err := r.GetClient().Get(context.TODO(), namespacedName, deployment)
if err != nil {
return errors.New("Semeru Cloud Compiler is not ready: Deployment is not created.")
}
// Get replicas
expectedReplicas := ola.GetSemeruCloudCompiler().GetReplicas()
ds := deployment.Status
replicas, readyReplicas, updatedReplicas = ds.Replicas, ds.ReadyReplicas, ds.UpdatedReplicas
// Check if all replicas are equal to the expected replicas
if replicas == *expectedReplicas && readyReplicas == *expectedReplicas && updatedReplicas == *expectedReplicas {
return nil // Semeru ready
} else if replicas > *expectedReplicas {
return errors.New("Semeru Cloud Compiler is not ready: Replica set is progressing.")
}
return errors.New("Semeru Cloud Compiler is not ready: Deployment is not ready.")
}
func (r *ReconcileOpenLiberty) isSemeruEnabled(ola *openlibertyv1.OpenLibertyApplication) bool {
if ola.GetSemeruCloudCompiler() != nil && ola.GetSemeruCloudCompiler().Enable == true {
return true
} else {
return false
}
}