Skip to content

Commit

Permalink
Merge pull request #47 from lumbrjx/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
lumbrjx authored Sep 12, 2024
2 parents 8cd7318 + 4d4aef0 commit bc9d1dc
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 91 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ MAIN_BRANCH = main
all: create-cluster deploy-controller deploy-daemonset setup-prometheus port-forward-prometheus


build-cli:
cd cmd/cli && go build -o obzevMini *.go

package-chart:
cd chart && helm package .
helm repo index . --merge index.yaml
Expand Down
5 changes: 3 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
- [x] create packetManipulation k8s-controller call
- [x] use reqSimulator for latency service
- [x] add helm charts
- [ ] create a CLI only version (obzevMini)
- [ ] add kustomize
- [x] create a CLI only version (obzevMini)
- [ ] remove junk
- [ ] write docs
68 changes: 68 additions & 0 deletions cmd/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"flag"
"fmt"
"log"
"os"
)

func printHelp() {
fmt.Println(`Usage: obzev0 <command> [options]
Commands:
init Initialize the service
apply Apply the configuration
Run 'obzev0 help <command>' for more information on a specific command.`)
}
func readArgs() {
if len(os.Args) < 2 {
fmt.Println("expected 'init' or 'apply' command")
os.Exit(1)
}

switch os.Args[1] {
case "help":
printHelp()
case "init":
initCmd := flag.NewFlagSet("init", flag.ExitOnError)
dist := initCmd.String(
"dist",
"",
"Specify the distination directory (default is current directory)",
)
addr := initCmd.String(
"Addr",
"127.0.0.1:50051",
"Specify the gRPC server address (default is 127.0.0.1:50051)",
)

initCmd.Parse(os.Args[2:])

fmt.Printf("Initializing with dist=%s and Addr=%s\n", *dist, *addr)

generateYaml(*addr, *dist)

case "apply":
applyCmd := flag.NewFlagSet("apply", flag.ExitOnError)
path := applyCmd.String(
"path",
"",
"Specify the path to apply (default is current directory)",
)

applyCmd.Parse(os.Args[2:])

fmt.Printf("Applying configurations from path=%s\n", *path)
c, err := LoadConfig(*path)
if err != nil {
log.Fatalf("Error: %v", err)
}
apply(c)

default:
fmt.Println("expected 'init', 'apply' or 'help' command")
os.Exit(1)
}
}
104 changes: 104 additions & 0 deletions cmd/cli/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"context"
"fmt"
"log"
"obzev0/common/definitions"
"obzev0/common/proto/latency"
"obzev0/common/proto/packetManipulation"
"obzev0/common/proto/tcAnalyser"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

func client(addr string) *grpc.ClientConn {
conn, err := grpc.NewClient(
addr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
log.Fatalf("did not connect: %v", err)
}
return conn
}

func apply(rpcConfig definitions.Config) {
conn := client(rpcConfig.ServerAddr)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*100)
defer cancel()
defer conn.Close()

if rpcConfig.LatencySvcConfig.Enabled {

c := latency.NewLatencyServiceClient(conn)
response, err := c.StartTcpServer(
ctx,
&latency.RequestForTcp{Config: &latency.TcpConfig{
ReqDelay: int32(rpcConfig.LatencySvcConfig.ReqDelay),
ResDelay: int32(rpcConfig.LatencySvcConfig.ReqDelay),
Server: rpcConfig.LatencySvcConfig.Server,
Client: rpcConfig.LatencySvcConfig.Client,
}},
)
if err != nil {
log.Printf("Error calling StartTcpServer: %v", err)
log.Fatal("error calling StartTcpServer: %w", err)
}
fmt.Printf(
"Response from LatencyService gRPC server: %s\n",
response.Message,
)
}
if rpcConfig.TcAnalyserSvcConfig.Enabled {

client2 := tcAnalyser.NewTcAnalyserServiceClient(conn)
rsp, err := client2.StartUserSpace(
ctx,
&tcAnalyser.RequestForUserSpace{Config: &tcAnalyser.TcConfig{
Interface: rpcConfig.TcAnalyserSvcConfig.NetIFace,
}},
)
if err != nil {
log.Fatal("error calling StartUserSpace: %w", err)
}
fmt.Printf(
"Response from TcAnalyserService gRPC server: %s\n",
rsp.Message,
)
}

if rpcConfig.PacketManipulationSvcConfig.Enabled {
client3 := packetManipulation.NewPacketManipulationServiceClient(conn)
s := strToFlt(rpcConfig.PacketManipulationSvcConfig.DropRate)
r := strToFlt(rpcConfig.PacketManipulationSvcConfig.CorruptRate)

d, err := client3.StartManipulationProxy(
ctx,
&packetManipulation.RequestForManipulationProxy{
Config: &packetManipulation.PctmConfig{
Server: rpcConfig.PacketManipulationSvcConfig.Server,
Client: rpcConfig.PacketManipulationSvcConfig.Client,

DurationConfig: &packetManipulation.DurationConfig{
DurationSeconds: int32(
rpcConfig.PacketManipulationSvcConfig.DurationSeconds,
),
DropRate: s,
CorruptRate: r,
},
},
},
)
if err != nil {
log.Fatal("error calling StartManipulationProxy: %w", err)
}
fmt.Printf(
"Response from packetManipulationService gRPC server: %s\n",
d.Message,
)
}

}
55 changes: 55 additions & 0 deletions cmd/cli/generateYaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"fmt"
"obzev0/common/definitions"
"os"

"gopkg.in/yaml.v2"
)

func generateYaml(serverAddr string, path string) {
config := definitions.Config{
ServerAddr: serverAddr,
LatencySvcConfig: definitions.LatencySvcConfig{
Enabled: true,
ReqDelay: 1,
ResDelay: 1,
Server: "7070",
Client: "127.0.0.1:8080",
},
TcAnalyserSvcConfig: definitions.TcAnalyserSvcConfig{
Enabled: false,
NetIFace: "eth0",
},
PacketManipulationSvcConfig: definitions.PacketManipulationSvcConfig{
Enabled: false,
Server: "9091",
Client: "127.0.0.1:8080",
DropRate: "0.8",
CorruptRate: "0.4",
DurationSeconds: 8,
},
}

data, err := yaml.Marshal(&config)
if err != nil {
fmt.Println("Error marshalling struct to YAML:", err)
return
}

file, err := os.Create(path + "obzev0cnf.yaml")
if err != nil {
fmt.Println("Error creating YAML file:", err)
return
}
defer file.Close()

_, err = file.Write(data)
if err != nil {
fmt.Println("Error writing to YAML file:", err)
return
}

fmt.Println("YAML file created successfully.")
}
20 changes: 20 additions & 0 deletions cmd/cli/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module obzev0/cli

go 1.22.5

require (
google.golang.org/grpc v1.66.2
gopkg.in/yaml.v2 v2.4.0
obzev0/common v0.0.0-00010101000000-000000000000
)

replace obzev0/common => ../../common

require (
github.com/envoyproxy/protoc-gen-validate v1.0.4 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
20 changes: 20 additions & 0 deletions cmd/cli/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
github.com/envoyproxy/protoc-gen-validate v1.0.4 h1:gVPz/FMfvh57HdSJQyvBtF00j8JU4zdyUgIUNhlgg0A=
github.com/envoyproxy/protoc-gen-validate v1.0.4/go.mod h1:qys6tmnRsYrQqIhm2bvKZH4Blx/1gTIZ2UKVY1M+Yew=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 h1:1GBuWVLM/KMVUv1t1En5Gs+gFZCNd360GGb4sSxtrhU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
google.golang.org/grpc v1.66.2 h1:3QdXkuq3Bkh7w+ywLdLvM56cmGvQHUMZpiCzt6Rqaoo=
google.golang.org/grpc v1.66.2/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
15 changes: 15 additions & 0 deletions cmd/cli/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"fmt"
"strconv"
)

func strToFlt(str string) float32 {
f, err := strconv.ParseFloat(str, 64)
if err != nil {
fmt.Println("Error converting string to float:", err)
return 0
}
return float32(f)
}
5 changes: 5 additions & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func main() {
readArgs()
}
Binary file added cmd/cli/obzev0mini
Binary file not shown.
24 changes: 24 additions & 0 deletions cmd/cli/readYaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"
"obzev0/common/definitions"
"os"

"gopkg.in/yaml.v2"
)

func LoadConfig(path string) (definitions.Config, error) {
var config definitions.Config

yamlFile, err := os.ReadFile(path + "obzev0cnf.yaml")
if err != nil {
return config, fmt.Errorf("failed to read YAML file: %w", err)
}

if err := yaml.Unmarshal(yamlFile, &config); err != nil {
return config, fmt.Errorf("failed to unmarshal YAML: %w", err)
}

return config, nil
}
34 changes: 23 additions & 11 deletions common/definitions/yamlConfig.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package definitions

type Config struct {
Delays DelaysConfig `yaml:"delays"`
Server ServerConfig `yaml:"server"`
Client ClientConfig `yaml:"client"`
type LatencySvcConfig struct {
Enabled bool `yaml:"enabled"`
ReqDelay int `yaml:"reqDelay"`
ResDelay int `yaml:"resDelay"`
Server string `yaml:"server"`
Client string `yaml:"client"`
}

type DelaysConfig struct {
ReqDelay int32 `yaml:"reqDelay"`
ResDelay int32 `yaml:"resDelay"`
type TcAnalyserSvcConfig struct {
Enabled bool `yaml:"enabled"`
NetIFace string `yaml:"netIFace"`
}
type ServerConfig struct {
Port string `yaml:"port"`

type PacketManipulationSvcConfig struct {
Enabled bool `yaml:"enabled"`
Server string `yaml:"server"`
Client string `yaml:"client"`
DropRate string `yaml:"dropRate"`
CorruptRate string `yaml:"corruptRate"`
DurationSeconds int `yaml:"durationSeconds"`
}
type ClientConfig struct {
Port string `yaml:"port"`

type Config struct {
ServerAddr string `yaml:"serverAddr"`
LatencySvcConfig LatencySvcConfig `yaml:"latencySvcConfig"`
TcAnalyserSvcConfig TcAnalyserSvcConfig `yaml:"tcAnalyserSvcConfig"`
PacketManipulationSvcConfig PacketManipulationSvcConfig `yaml:"packetManipulationSvcConfig"`
}
Loading

0 comments on commit bc9d1dc

Please sign in to comment.