Skip to content

Commit bb35118

Browse files
authored
Fr10 moving to module (#11)
* Shifting to module * Shifting to module
1 parent ecf352c commit bb35118

File tree

9 files changed

+636
-181
lines changed

9 files changed

+636
-181
lines changed

README.md

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,111 @@ The values for the __probes__, are calculated to help you to prevent Kubernetes
455455
You must use them and be sure they are correctly set in your CR or all the work done will be useless.
456456

457457
Resources are the cpu/memory dimension you should set. You will always have a LIMIT and a REQUEST for the resources. Keep in mind that whatever will push your pod above the memory limit will IMMEDIATELY trigger the OOM killer :) not a nice thing to have.
458-
458+
459+
# Module
460+
MySQLOperatorCalculator is also available as module.
461+
This is it you can include it in your code and query it directly getting back objects to browse or Json.
462+
The example directory contains a very simple example of code on how to do it, but mainly you have to:
463+
```go
464+
import (
465+
"bytes"
466+
"encoding/json"
467+
"fmt"
468+
MO "github.com/Tusamarco/mysqloperatorcalculator/src/mysqloperatorcalculator" <----------- Import the module
469+
"strconv"
470+
)
471+
472+
```
473+
Then when you need it:
474+
```go
475+
func main() {
476+
var my MO.MysqlOperatorCalculator
477+
478+
testSupportedJson(my.GetSupportedLayouts(), my) <----------------
479+
480+
testGetconfiguration(my)
481+
482+
}
483+
func testSupportedJson(supported MO.Configuration, calculator MO.MysqlOperatorCalculator) {
484+
output, err := json.MarshalIndent(&supported, "", " ")
485+
if err != nil {
486+
print(err.Error())
487+
}
488+
fmt.Println(string(output))
489+
490+
}
491+
```
492+
In the example above I get the list of all supported platform in Json format
493+
But the list as objects is already there in one single call:
494+
```go
495+
my.GetSupportedLayouts()
496+
```
497+
To get the full set of parameters we first need to build the request, then pass it and get back a map containing all the settings:
498+
```go
499+
func testGetconfiguration(moc MO.MysqlOperatorCalculator) {
500+
var b bytes.Buffer
501+
var myRequest MO.ConfigurationRequest
502+
var err error
503+
504+
myRequest.LoadType = MO.LoadType{Id: 2}
505+
myRequest.Dimension = MO.Dimension{Id: 999, Cpu: 4000, Memory: 2.5}
506+
myRequest.DBType = "group_replication" //"pxc"
507+
myRequest.Output = "human" //"human"
508+
myRequest.Connections = 70
509+
myRequest.Mysqlversion = MO.Version{8, 0, 33}
510+
511+
moc.Init(myRequest)
512+
error, responseMessage, families := moc.GetCalculate()
513+
if error != nil {
514+
print(error.Error())
515+
}
516+
517+
if responseMessage.MType > 0 {
518+
fmt.Errorf(strconv.Itoa(responseMessage.MType) + ": " + responseMessage.MName + " " + responseMessage.MText)
519+
}
520+
if len(families) > 0 {
521+
if myRequest.Output == "json" {
522+
b, err = moc.GetJSONOutput(responseMessage, myRequest, families)
523+
} else {
524+
b, err = moc.GetHumanOutput(responseMessage, myRequest, families)
525+
}
526+
if err != nil {
527+
print(err.Error())
528+
return
529+
}
530+
531+
println(b.String())
532+
533+
}
534+
535+
}
536+
```
537+
The first object we need to create is the ConfigurationRequest:
538+
```go
539+
var myRequest MO.ConfigurationRequest
540+
```
541+
then we can populate it, in this case we DO NOT set a supported environment but an open scope:
542+
```go
543+
myRequest.LoadType = MO.LoadType{Id: 2}
544+
myRequest.Dimension = MO.Dimension{Id: 999, Cpu: 4000, Memory: 2.5}
545+
myRequest.DBType = "group_replication" //"pxc"
546+
myRequest.Output = "human" //"human"
547+
myRequest.Connections = 70
548+
myRequest.Mysqlversion = MO.Version{8, 0, 33}
549+
```
550+
With DImension Id 999 we declare is going to be an open scope and after we must declare the CPU and memory.
551+
If instead we choose a fix and supported dimension, then the Dimension.Id and MySQL Version, will be enough.
552+
553+
We then need to:
554+
```go
555+
moc.Init(myRequest)
556+
error, responseMessage, families := moc.GetCalculate()
557+
```
558+
Where families will be the top container of all our settings.
559+
We can decide if to parse it as a Map or if convert it to other formats like Json, or plain text.
560+
561+
This is it, easy.
562+
459563
# Final...
460564
The toool is there and it needs testing and real evaluation, so I reccomand you to test, test, test whatever configuration you will get.
461565
Notihing is perfect, so let me know if you find things that make no sense or not workign as expected.

go.mod

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
module mysqloperatorcalculator
1+
module github.com/Tusamarco/mysqloperatorcalculator
22

3-
4-
go 1.18
3+
go 1.20
54

65
require github.com/sirupsen/logrus v1.9.0
76

8-
require golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
7+
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
8+
9+
//require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
10+
//

go.sum

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs
88
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
99
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
1010
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
1112
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
13-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1413
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1514
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
1615
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

src/example/example.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
MO "github.com/Tusamarco/mysqloperatorcalculator/src/mysqloperatorcalculator"
8+
"strconv"
9+
)
10+
11+
func main() {
12+
var my MO.MysqlOperatorCalculator
13+
14+
testSupportedJson(my.GetSupportedLayouts(), my)
15+
16+
testGetconfiguration(my)
17+
18+
}
19+
20+
//get all the supported platforms and dimensions as an object that you can transform or not:
21+
//type Configuration struct {
22+
// DBType []string `json:"dbtype"`
23+
// Dimension []Dimension `json:"dimension"`
24+
// LoadType []LoadType `json:"loadtype"`
25+
// Connections []int `json:"connections"`
26+
// Output []string `json:"output"`
27+
// Mysqlversions MySQLVersions `json:"mysqlversions"`
28+
//}
29+
30+
func testSupportedJson(supported MO.Configuration, calculator MO.MysqlOperatorCalculator) {
31+
output, err := json.MarshalIndent(&supported, "", " ")
32+
if err != nil {
33+
print(err.Error())
34+
}
35+
fmt.Println(string(output))
36+
37+
}
38+
39+
//Get the whole set of parameters plus message as the following objects hierarchy
40+
// Families->
41+
// Groups -->
42+
// Parameters
43+
// THE VALID value in parameter to be consider as CURRENT is !!Value!!!
44+
45+
func testGetconfiguration(moc MO.MysqlOperatorCalculator) {
46+
var b bytes.Buffer
47+
var myRequest MO.ConfigurationRequest
48+
var err error
49+
50+
myRequest.LoadType = MO.LoadType{Id: 2}
51+
myRequest.Dimension = MO.Dimension{Id: 999, Cpu: 4000, Memory: 2.5}
52+
myRequest.DBType = "group_replication" //"pxc"
53+
myRequest.Output = "human" //"human"
54+
myRequest.Connections = 70
55+
myRequest.Mysqlversion = MO.Version{8, 0, 33}
56+
57+
moc.Init(myRequest)
58+
error, responseMessage, families := moc.GetCalculate()
59+
if error != nil {
60+
print(error.Error())
61+
}
62+
63+
if responseMessage.MType > 0 {
64+
fmt.Errorf(strconv.Itoa(responseMessage.MType) + ": " + responseMessage.MName + " " + responseMessage.MText)
65+
}
66+
if len(families) > 0 {
67+
if myRequest.Output == "json" {
68+
b, err = moc.GetJSONOutput(responseMessage, myRequest, families)
69+
} else {
70+
b, err = moc.GetHumanOutput(responseMessage, myRequest, families)
71+
}
72+
if err != nil {
73+
print(err.Error())
74+
return
75+
}
76+
77+
println(b.String())
78+
79+
}
80+
81+
}

src/Global/help.go renamed to src/help.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*/
1717

18-
package Global
18+
package main
1919

2020
import "fmt"
2121

@@ -25,9 +25,9 @@ type HelpText struct {
2525
helpShort string
2626
}
2727

28-
//func (help *HelpText) Init() {
29-
// help.inParams = [2]string{"configfile", "configPath"}
30-
//}
28+
// func (help *HelpText) Init() {
29+
// help.inParams = [2]string{"configfile", "configPath"}
30+
// }
3131
func (help *HelpText) PrintLicense() {
3232
fmt.Println(help.GetHelpText())
3333
}

src/Objects/Configuration.go renamed to src/mysqloperatorcalculator/Configuration.go

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package Objects
1+
package mysqloperatorcalculator
22

33
import (
44
"bytes"
@@ -21,6 +21,18 @@ const ErrorexecT = "There is an error while processing. See details: %s"
2121
// Structure definitions
2222
//********************************
2323

24+
// MySQL version definition
25+
type Version struct {
26+
Major int `json:"major"`
27+
Minor int `json:"minor"`
28+
Patch int `json:"patch"`
29+
}
30+
31+
type MySQLVersions struct {
32+
Min Version `json:"min"`
33+
Max Version `json:"max"`
34+
}
35+
2436
// ResponseMessage Message is the retruned message
2537
type ResponseMessage struct {
2638
MType int `json:"type"`
@@ -30,20 +42,22 @@ type ResponseMessage struct {
3042

3143
// Configuration used to pass available configurations
3244
type Configuration struct {
33-
DBType []string `json:"dbtype"`
34-
Dimension []Dimension `json:"dimension"`
35-
LoadType []LoadType `json:"loadtype"`
36-
Connections []int `json:"connections"`
37-
Output []string `json:"output"`
45+
DBType []string `json:"dbtype"`
46+
Dimension []Dimension `json:"dimension"`
47+
LoadType []LoadType `json:"loadtype"`
48+
Connections []int `json:"connections"`
49+
Output []string `json:"output"`
50+
Mysqlversions MySQLVersions `json:"mysqlversions"`
3851
}
3952

4053
// ConfigurationRequest used to store the incoming request
4154
type ConfigurationRequest struct {
42-
DBType string `json:"dbtype"`
43-
Dimension Dimension `json:"dimension"`
44-
LoadType LoadType `json:"loadtype"`
45-
Connections int `json:"connections"`
46-
Output string `json:"output"`
55+
DBType string `json:"dbtype"`
56+
Dimension Dimension `json:"dimension"`
57+
LoadType LoadType `json:"loadtype"`
58+
Connections int `json:"connections"`
59+
Output string `json:"output"`
60+
Mysqlversion Version `json:"mysqlversion"`
4761
}
4862

4963
// Dimension used to represent the POD dimension
@@ -160,6 +174,9 @@ func (conf *Configuration) Init() {
160174
}
161175

162176
conf.Connections = []int{50, 100, 200, 500, 1000, 2000}
177+
178+
//MySQL SUpported version (testing is with hardcoded then we need to query check.percona.com
179+
conf.getMySQLVersion()
163180
}
164181

165182
func (family *Family) Init(DBTypeRequest string) map[string]Family {
@@ -406,3 +423,9 @@ func InBetween(i, min, max int) bool {
406423
return false
407424
}
408425
}
426+
427+
// TODO ***MySQL Supported version (testing is with hardcoded then we need to query check.percona.com ***
428+
func (conf *Configuration) getMySQLVersion() {
429+
conf.Mysqlversions.Max = Version{8, 1, 0}
430+
conf.Mysqlversions.Min = Version{8, 0, 32}
431+
}

0 commit comments

Comments
 (0)