Skip to content

Commit f46165a

Browse files
authored
Merge pull request #123 from BuddhiWathsala/master
Refactor the code and address minor fixes
2 parents f35e80d + 5a2fe20 commit f46165a

File tree

5 files changed

+49
-53
lines changed

5 files changed

+49
-53
lines changed

pkg/controller/siddhiprocess/artifact/artifacts.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package artifacts
2121
import (
2222
"context"
2323
"errors"
24+
"path"
2425
"reflect"
2526
"strconv"
2627
"strings"
@@ -86,8 +87,7 @@ func (k *KubeClient) CreateOrUpdateIngress(
8687

8788
var ingressPaths []extensionsv1beta1.HTTPIngressPath
8889
for _, port := range containerPorts {
89-
path := "/" + strings.ToLower(serviceName) +
90-
"/" + strconv.Itoa(int(port.ContainerPort)) + "(/|$)(.*)"
90+
path := path.Join("/", strings.ToLower(serviceName), (strconv.Itoa(int(port.ContainerPort)) + "(/|$)(.*)"))
9191
ingressPath := extensionsv1beta1.HTTPIngressPath{
9292
Path: path,
9393
Backend: extensionsv1beta1.IngressBackend{
@@ -615,8 +615,7 @@ func IngressMutateFunc(
615615
ingress := obj.(*extensionsv1beta1.Ingress)
616616
var ingressPaths []extensionsv1beta1.HTTPIngressPath
617617
for _, port := range containerPorts {
618-
path := "/" + strings.ToLower(serviceName) +
619-
"/" + strconv.Itoa(int(port.ContainerPort)) + "(/|$)(.*)"
618+
path := path.Join("/", strings.ToLower(serviceName), (strconv.Itoa(int(port.ContainerPort)) + "(/|$)(.*)"))
620619
ingressPath := extensionsv1beta1.HTTPIngressPath{
621620
Path: path,
622621
Backend: extensionsv1beta1.IngressBackend{

pkg/controller/siddhiprocess/artifact/artifacts_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ package artifacts
2020

2121
import (
2222
"context"
23-
"strconv"
23+
"fmt"
2424
"testing"
2525

2626
natsv1alpha2 "github.com/siddhi-io/siddhi-operator/pkg/apis/nats/v1alpha2"
@@ -31,15 +31,14 @@ import (
3131
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3232

3333
"k8s.io/apimachinery/pkg/api/resource"
34-
"k8s.io/apimachinery/pkg/runtime"
3534
"k8s.io/apimachinery/pkg/types"
3635
"k8s.io/client-go/kubernetes/scheme"
3736
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3837
)
3938

4039
var kubeClient = KubeClient{
4140
Scheme: scheme.Scheme,
42-
Client: fake.NewFakeClient([]runtime.Object{}...),
41+
Client: fake.NewFakeClient(),
4342
}
4443

4544
// apps to run tests
@@ -115,7 +114,7 @@ func TestCreateOrUpdateIngress(t *testing.T) {
115114
t.Error(err)
116115
}
117116
if len(ingress.Spec.Rules[0].HTTP.Paths) != 2 {
118-
t.Error("Ingress update error. Expected entries 2, but found " + strconv.Itoa(len(ingress.Spec.Rules)))
117+
t.Error(fmt.Sprint("Ingress update error. Expected entries 2, but found", len(ingress.Spec.Rules)))
119118
}
120119
}
121120

pkg/controller/siddhiprocess/deploymanager/application.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,11 @@ func (d *DeployManager) Deploy() (operationResult controllerutil.OperationResult
115115
d.SiddhiProcess,
116116
)
117117
if err != nil {
118-
return
118+
return operationResult, err
119119
}
120-
mountPath := ""
121-
mountPath, err = populateMountPath(d.SiddhiProcess, d.Image.Home, d.Image.Profile)
120+
mountPath, err := populateMountPath(d.SiddhiProcess, d.Image.Home, d.Image.Profile)
122121
if err != nil {
123-
return
122+
return operationResult, err
124123
}
125124
volume, volumeMount := createPVCVolumes(pvcName, mountPath)
126125
volumes = append(volumes, volume)
@@ -136,7 +135,7 @@ func (d *DeployManager) Deploy() (operationResult controllerutil.OperationResult
136135
}
137136
err = d.KubeClient.CreateOrUpdateCM(deployYAMLCMName, d.SiddhiProcess.Namespace, data, d.SiddhiProcess)
138137
if err != nil {
139-
return
138+
return operationResult, err
140139
}
141140
mountPath := d.Image.Home + DepConfMountPath
142141
volume, volumeMount := createCMVolumes(deployYAMLCMName, mountPath)
@@ -150,7 +149,7 @@ func (d *DeployManager) Deploy() (operationResult controllerutil.OperationResult
150149
}
151150
err = d.KubeClient.CreateOrUpdateCM(deployYAMLCMName, d.SiddhiProcess.Namespace, data, d.SiddhiProcess)
152151
if err != nil {
153-
return
152+
return operationResult, err
154153
}
155154
mountPath := d.Image.Home + DepConfMountPath
156155
volume, volumeMount := createCMVolumes(deployYAMLCMName, mountPath)
@@ -184,7 +183,7 @@ func (d *DeployManager) Deploy() (operationResult controllerutil.OperationResult
184183
}
185184
err = d.KubeClient.CreateOrUpdateCM(appsCMName, d.SiddhiProcess.Namespace, appsMap, d.SiddhiProcess)
186185
if err != nil {
187-
return
186+
return operationResult, err
188187
}
189188
appsPath := d.Image.Home + SiddhiFilesDir
190189
volume, volumeMount := createCMVolumes(appsCMName, appsPath)
@@ -205,7 +204,7 @@ func (d *DeployManager) Deploy() (operationResult controllerutil.OperationResult
205204
ContainerName,
206205
[]string{Shell},
207206
[]string{
208-
d.Image.Home + SiddhiBin + "/" + d.Image.Profile + ".sh",
207+
filepath.Join(d.Image.Home, SiddhiBin, (d.Image.Profile + ".sh")),
209208
appParameter,
210209
configParameter,
211210
},
@@ -219,15 +218,14 @@ func (d *DeployManager) Deploy() (operationResult controllerutil.OperationResult
219218
depStrategy,
220219
d.SiddhiProcess,
221220
)
222-
return
221+
return operationResult, err
223222
}
224223

225224
// createLocalObjectReference creates a local object reference secret to download docker images from private registries.
226225
func createLocalObjectReference(secret string) (localObjectRef corev1.LocalObjectReference) {
227-
localObjectRef = corev1.LocalObjectReference{
226+
return corev1.LocalObjectReference{
228227
Name: secret,
229228
}
230-
return
231229
}
232230

233231
// populateMountPath reads the runner configs given by the user.
@@ -240,11 +238,11 @@ func populateMountPath(sp *siddhiv1alpha2.SiddhiProcess, home string, profile st
240238
if err != nil {
241239
return
242240
}
243-
mountPath = home + WSO2Dir + "/" + profile + "/" + FilePersistentDir
241+
mountPath = filepath.Join(home, WSO2Dir, profile, FilePersistentDir)
244242
if config.StatePersistence.SPConfig.Location != "" && filepath.IsAbs(config.StatePersistence.SPConfig.Location) {
245243
mountPath = config.StatePersistence.SPConfig.Location
246244
} else if config.StatePersistence.SPConfig.Location != "" {
247-
mountPath = home + WSO2Dir + "/" + profile + "/" + config.StatePersistence.SPConfig.Location
245+
mountPath = filepath.Join(home, WSO2Dir, profile, config.StatePersistence.SPConfig.Location)
248246
}
249247
return
250248
}

pkg/controller/siddhiprocess/parser/parser.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ func (p *Parser) Parse() (applications []deploymanager.Application, err error) {
135135
}
136136

137137
func (p *Parser) deploy() (err error) {
138+
generatedParserName := p.Name + ParserExtension
138139
containerPorts := []corev1.ContainerPort{
139140
corev1.ContainerPort{
140141
Name: ParserName,
@@ -143,7 +144,7 @@ func (p *Parser) deploy() (err error) {
143144
},
144145
}
145146
application := deploymanager.Application{
146-
Name: p.Name + ParserExtension,
147+
Name: generatedParserName,
147148
ContainerPorts: containerPorts,
148149
ServiceEnabled: true,
149150
Replicas: ParserReplicas,
@@ -160,7 +161,7 @@ func (p *Parser) deploy() (err error) {
160161
return
161162
}
162163
_, err = p.KubeClient.CreateOrUpdateService(
163-
p.Name+ParserExtension,
164+
generatedParserName,
164165
p.SiddhiProcess.Namespace,
165166
containerPorts,
166167
deployManeger.Labels,
@@ -170,7 +171,7 @@ func (p *Parser) deploy() (err error) {
170171
return
171172
}
172173

173-
url := ParserHTTP + p.Name + ParserExtension + "." + p.SiddhiProcess.Namespace + ParserHealth
174+
url := ParserHTTP + generatedParserName + "." + p.SiddhiProcess.Namespace + ParserHealth
174175
p.Logger.Info("Waiting for parser", "deployment", p.Name)
175176
err = waitForParser(url)
176177
if err != nil {

pkg/controller/siddhiprocess/siddhicontroller/siddhicontroller.go

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ func (sc *SiddhiController) CreateArtifacts(applications []deploymanager.Applica
182182

183183
sc.UpdatePartialAppStatus(applications)
184184
for _, application := range applications {
185-
if (eventType == controllerutil.OperationResultCreated) ||
186-
(eventType == controllerutil.OperationResultUpdated) {
185+
if eventType == controllerutil.OperationResultCreated ||
186+
eventType == controllerutil.OperationResultUpdated {
187187
deployManeger := deploymanager.DeployManager{
188188
Application: application,
189189
KubeClient: sc.KubeClient,
@@ -196,14 +196,14 @@ func (sc *SiddhiController) CreateArtifacts(applications []deploymanager.Applica
196196
sc.UpdateErrorStatus("AppDeploymentError", err)
197197
continue
198198
}
199-
if (eventType == controllerutil.OperationResultCreated) &&
200-
(operationResult == controllerutil.OperationResultCreated) {
199+
if eventType == controllerutil.OperationResultCreated &&
200+
operationResult == controllerutil.OperationResultCreated {
201201
sc.UpdateRunningStatus(
202202
"DeploymentCreated",
203203
(application.Name + " deployment created successfully"),
204204
)
205-
} else if (eventType == controllerutil.OperationResultUpdated) &&
206-
(operationResult == controllerutil.OperationResultUpdated) {
205+
} else if eventType == controllerutil.OperationResultUpdated &&
206+
operationResult == controllerutil.OperationResultUpdated {
207207
sc.UpdateRunningStatus(
208208
"DeploymentUpdated",
209209
(application.Name + " deployment updated successfully"),
@@ -222,14 +222,14 @@ func (sc *SiddhiController) CreateArtifacts(applications []deploymanager.Applica
222222
sc.UpdateErrorStatus("ServiceCreationError", err)
223223
continue
224224
}
225-
if (eventType == controllerutil.OperationResultCreated) &&
226-
(operationResult == controllerutil.OperationResultCreated) {
225+
if eventType == controllerutil.OperationResultCreated &&
226+
operationResult == controllerutil.OperationResultCreated {
227227
sc.UpdateRunningStatus(
228228
"ServiceCreated",
229229
(application.Name + " service created successfully"),
230230
)
231-
} else if (eventType == controllerutil.OperationResultUpdated) &&
232-
(operationResult == controllerutil.OperationResultUpdated) {
231+
} else if eventType == controllerutil.OperationResultUpdated &&
232+
operationResult == controllerutil.OperationResultUpdated {
233233
sc.UpdateRunningStatus(
234234
"ServiceUpdated",
235235
(application.Name + " service updated successfully"),
@@ -247,20 +247,19 @@ func (sc *SiddhiController) CreateArtifacts(applications []deploymanager.Applica
247247
sc.UpdateErrorStatus("IngressCreationError", err)
248248
continue
249249
}
250-
if (eventType == controllerutil.OperationResultCreated) &&
251-
(operationResult == controllerutil.OperationResultCreated) {
250+
if eventType == controllerutil.OperationResultCreated &&
251+
operationResult == controllerutil.OperationResultCreated {
252252
sc.Logger.Info("Ingress created", "Ingress.Name", artifact.IngressName)
253-
} else if (eventType == controllerutil.OperationResultUpdated) &&
254-
(operationResult == controllerutil.OperationResultUpdated) {
253+
} else if eventType == controllerutil.OperationResultUpdated &&
254+
operationResult == controllerutil.OperationResultUpdated {
255255
sc.Logger.Info("Ingress changed", "Ingress.Name", artifact.IngressName)
256256
}
257257
}
258258
}
259259
}
260260
}
261261
sc.SyncVersion()
262-
if (eventType == controllerutil.OperationResultCreated) ||
263-
(eventType == controllerutil.OperationResultUpdated) {
262+
if eventType == controllerutil.OperationResultCreated || eventType == controllerutil.OperationResultUpdated {
264263
sc.UpdateReadyDeployments(0, needDep)
265264
}
266265
return
@@ -433,32 +432,32 @@ func (sc *SiddhiController) UpdateDefaultConfigs() {
433432
configMap,
434433
)
435434
if err == nil {
436-
if configMap.Data["siddhiHome"] != "" {
437-
sc.Image.Home = configMap.Data["siddhiHome"]
435+
if v, ok := configMap.Data["siddhiHome"]; ok {
436+
sc.Image.Home = v
438437
}
439438

440-
if configMap.Data["siddhiImage"] != "" {
441-
sc.Image.Name = configMap.Data["siddhiImage"]
439+
if v, ok := configMap.Data["siddhiImage"]; ok {
440+
sc.Image.Name = v
442441
}
443442

444-
if configMap.Data["siddhiImageSecret"] != "" {
445-
sc.Image.Secret = configMap.Data["siddhiImageSecret"]
443+
if v, ok := configMap.Data["siddhiImageSecret"]; ok {
444+
sc.Image.Secret = v
446445
}
447446

448-
if configMap.Data["siddhiProfile"] != "" {
449-
sc.Image.Profile = configMap.Data["siddhiProfile"]
447+
if v, ok := configMap.Data["siddhiProfile"]; ok {
448+
sc.Image.Profile = v
450449
}
451450

452-
if configMap.Data["autoIngressCreation"] != "" {
453-
if configMap.Data["autoIngressCreation"] == "true" {
451+
if v, ok := configMap.Data["autoIngressCreation"]; ok {
452+
if v == "true" {
454453
sc.AutoCreateIngress = true
455454
} else {
456455
sc.AutoCreateIngress = false
457456
}
458457
}
459458

460-
if configMap.Data["ingressTLS"] != "" {
461-
sc.TLS = configMap.Data["ingressTLS"]
459+
if v, ok := configMap.Data["ingressTLS"]; ok {
460+
sc.TLS = v
462461
}
463462

464463
}
@@ -489,7 +488,7 @@ func (sc *SiddhiController) SetDefaultPendingState() {
489488
sc.SiddhiProcess.Status.CurrentVersion,
490489
sc.SiddhiProcess.Status.PreviousVersion,
491490
)
492-
if (eventType == controllerutil.OperationResultCreated) && (sc.SiddhiProcess.Status.Status == "") {
491+
if eventType == controllerutil.OperationResultCreated && sc.SiddhiProcess.Status.Status == "" {
493492
sc.UpdatePendingStatus()
494493
}
495494
}

0 commit comments

Comments
 (0)