-
Notifications
You must be signed in to change notification settings - Fork 17
/
config.go
137 lines (126 loc) · 3.41 KB
/
config.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
package config
import (
"fmt"
"os"
"regexp"
apiv1 "k8s.io/api/core/v1"
log "github.com/cloud-bulldozer/k8s-netperf/pkg/logging"
"github.com/cloud-bulldozer/k8s-netperf/pkg/metrics"
"gopkg.in/yaml.v3"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
// Config describes the netperf tests
type Config struct {
Parallelism int `default:"1" yaml:"parallelism,omitempty"`
Duration int `yaml:"duration,omitempty"`
Profile string `yaml:"profile,omitempty"`
Samples int `yaml:"samples,omitempty"`
MessageSize int `yaml:"messagesize,omitempty"`
Service bool `default:"false" yaml:"service,omitempty"`
Metric string
AcrossAZ bool
}
// PerfScenarios describes the different scenarios
type PerfScenarios struct {
NodeLocal bool
AcrossAZ bool
HostNetwork bool
Configs []Config
ServerNodeInfo metrics.NodeInfo
ClientNodeInfo metrics.NodeInfo
Client apiv1.PodList
Server apiv1.PodList
ClientAcross apiv1.PodList
ClientHost apiv1.PodList
ServerHost apiv1.PodList
NetperfService *apiv1.Service
IperfService *apiv1.Service
UperfService *apiv1.Service
RestConfig rest.Config
ClientSet *kubernetes.Clientset
}
// Tests we will support in k8s-netperf
const validTests = "tcp_stream|udp_stream|tcp_rr|udp_rr|tcp_crr|udp_crr|sctp_stream|sctp_rr|sctp_crr"
func validConfig(cfg Config) (bool, error) {
preEval := regexp.MustCompile("(?i)" + validTests)
p := preEval.MatchString(cfg.Profile)
if !p {
return false, fmt.Errorf("unknown netperf profile")
}
if cfg.Duration < 1 {
return false, fmt.Errorf("duration must be > 0")
}
if cfg.Samples < 1 {
return false, fmt.Errorf("samples must be > 0")
}
if cfg.MessageSize < 1 {
return false, fmt.Errorf("messagesize must be > 0")
}
if cfg.Parallelism < 1 {
return false, fmt.Errorf("parallelism must be > 0")
}
return true, nil
}
// ParseConf will read in the netperf configuration file which
// describes which tests to run
// Returns Config struct
func ParseConf(fn string) ([]Config, error) {
log.Infof("📒 Reading %s file. ", fn)
buf, err := os.ReadFile(fn)
if err != nil {
return nil, err
}
c := make(map[string]Config)
err = yaml.Unmarshal(buf, &c)
if err != nil {
return nil, fmt.Errorf("in file %q: %v", fn, err)
}
// Ignore the key
// Pull out the specific tests
var tests []Config
for _, value := range c {
ok, err := validConfig(value)
if !ok {
return nil, err
}
tests = append(tests, value)
}
return tests, nil
}
// ParseV2Conf will read in the netperf configuration file which
// describes which tests to run
// Returns Config struct
func ParseV2Conf(fn string) ([]Config, error) {
log.Infof("📒 Reading %s file - using ConfigV2 Method. ", fn)
buf, err := os.ReadFile(fn)
if err != nil {
return nil, err
}
c := make(map[string][]Config)
// New YAML structure :
// tests :
// - Test_name :
// profile: <xyz> ...
err = yaml.Unmarshal(buf, &c)
if err != nil {
return nil, fmt.Errorf("in file %q: %v", fn, err)
}
// Ignore the key
// Pull out the specific tests
var tests []Config
for _, cf := range c {
for _, cfg := range cf {
ok, err := validConfig(cfg)
if !ok {
return nil, err
}
tests = append(tests, cfg)
}
}
return tests, nil
}
// Show Display the netperf config
func Show(c Config, driver string) {
log.Infof("🗒️ Running %s %s (service %t) for %ds ", driver, c.Profile, c.Service, c.Duration)
}