fix: fix kvm2 numa simulate ut&lint

pull/10471/head
phantooom 2021-02-21 00:28:38 +08:00
parent badc5d4532
commit 529881e01c
5 changed files with 37 additions and 28 deletions

View File

@ -313,18 +313,7 @@ func generateClusterConfig(cmd *cobra.Command, existing *config.ClusterConfig, k
out.WarningT("--network flag is only valid with the docker/podman drivers, it will be ignored") out.WarningT("--network flag is only valid with the docker/podman drivers, it will be ignored")
} }
if viper.GetInt(kvmNUMACount) < 1 || viper.GetInt(kvmNUMACount) > 8 { checkNumaCount(k8sVersion)
exit.Message(reason.Usage, "--kvm-numa-count range is 1-8")
}
if viper.GetInt(kvmNUMACount) > 1 {
v, err := pkgutil.ParseKubernetesVersion(k8sVersion)
if err != nil {
exit.Message(reason.Usage, "invalid kubernetes version")
}
if v.LT(semver.Version{Major: 1,Minor: 18}){
exit.Message(reason.Usage, "numa node is only supported on k8s v1.18 and later")
}
}
cc = config.ClusterConfig{ cc = config.ClusterConfig{
Name: ClusterFlagValue(), Name: ClusterFlagValue(),
@ -423,6 +412,22 @@ func generateClusterConfig(cmd *cobra.Command, existing *config.ClusterConfig, k
return createNode(cc, kubeNodeName, existing) return createNode(cc, kubeNodeName, existing)
} }
func checkNumaCount(k8sVersion string) {
if viper.GetInt(kvmNUMACount) < 1 || viper.GetInt(kvmNUMACount) > 8 {
exit.Message(reason.Usage, "--kvm-numa-count range is 1-8")
}
if viper.GetInt(kvmNUMACount) > 1 {
v, err := pkgutil.ParseKubernetesVersion(k8sVersion)
if err != nil {
exit.Message(reason.Usage, "invalid kubernetes version")
}
if v.LT(semver.Version{Major: 1, Minor: 18}) {
exit.Message(reason.Usage, "numa node is only supported on k8s v1.18 and later")
}
}
}
// upgradeExistingConfig upgrades legacy configuration files // upgradeExistingConfig upgrades legacy configuration files
func upgradeExistingConfig(cc *config.ClusterConfig) { func upgradeExistingConfig(cc *config.ClusterConfig) {
if cc == nil { if cc == nil {

View File

@ -133,6 +133,7 @@ func TestMirrorCountry(t *testing.T) {
cmd := &cobra.Command{} cmd := &cobra.Command{}
viper.SetDefault(imageRepository, test.imageRepository) viper.SetDefault(imageRepository, test.imageRepository)
viper.SetDefault(imageMirrorCountry, test.mirrorCountry) viper.SetDefault(imageMirrorCountry, test.mirrorCountry)
viper.SetDefault(kvmNUMACount, 1)
config, _, err := generateClusterConfig(cmd, nil, k8sVersion, "none") config, _, err := generateClusterConfig(cmd, nil, k8sVersion, "none")
if err != nil { if err != nil {
t.Fatalf("Got unexpected error %v during config generation", err) t.Fatalf("Got unexpected error %v during config generation", err)

View File

@ -320,11 +320,11 @@ func (d *Driver) Create() (err error) {
} }
if d.NUMANodeCount > 1 { if d.NUMANodeCount > 1 {
NUMAXML, err := NumaXml(d.CPU, d.Memory, d.NUMANodeCount) numaXML, err := numaXML(d.CPU, d.Memory, d.NUMANodeCount)
if err != nil { if err != nil {
return errors.Wrap(err, "creating NUMA XML") return errors.Wrap(err, "creating NUMA XML")
} }
d.NUMANodeXML = NUMAXML d.NUMANodeXML = numaXML
} }
store := d.ResolveStorePath(".") store := d.ResolveStorePath(".")

View File

@ -24,8 +24,8 @@ import (
"text/template" "text/template"
) )
// NUMATmpl NUMA XML Template // numaTmpl NUMA XML Template
const NUMATmpl = ` const numaTmpl = `
<numa> <numa>
{{- range $idx,$val :=. }} {{- range $idx,$val :=. }}
<cell id='{{$idx}}' cpus='{{$val.CPUTopology}}' memory='{{$val.Memory}}' unit='MiB'/> <cell id='{{$idx}}' cpus='{{$val.CPUTopology}}' memory='{{$val.Memory}}' unit='MiB'/>
@ -33,16 +33,19 @@ const NUMATmpl = `
</numa> </numa>
` `
// NUMA this struct use for NUMATmpl // NUMA this struct use for numaTmpl
type NUMA struct { type NUMA struct {
CPUCount int // cpu count on numa node
Memory int CPUCount int
// memory on numa node
Memory int
// cpu sequence on numa node eg: 0,1,2,3
CPUTopology string CPUTopology string
} }
// NumaXml generate numa xml // numaXML generate numa xml
// evenly distributed cpu core & memory to each numa node // evenly distributed cpu core & memory to each numa node
func NumaXml(cpu, memory, numaCount int) (string, error) { func numaXML(cpu, memory, numaCount int) (string, error) {
if numaCount < 1 { if numaCount < 1 {
return "", fmt.Errorf("numa node count must >= 1") return "", fmt.Errorf("numa node count must >= 1")
} }
@ -81,10 +84,10 @@ func NumaXml(cpu, memory, numaCount int) (string, error) {
numaNodes[i].Memory++ numaNodes[i].Memory++
} }
tmpl := template.Must(template.New("numa").Parse(NUMATmpl)) tmpl := template.Must(template.New("numa").Parse(numaTmpl))
var NUMAXML bytes.Buffer var numaXML bytes.Buffer
if err := tmpl.Execute(&NUMAXML, numaNodes); err != nil { if err := tmpl.Execute(&numaXML, numaNodes); err != nil {
return "", fmt.Errorf("couldn't generate numa XML: %v", err) return "", fmt.Errorf("couldn't generate numa XML: %v", err)
} }
return NUMAXML.String(), nil return numaXML.String(), nil
} }

View File

@ -21,13 +21,13 @@ import (
"testing" "testing"
) )
func TestGetNUMAXml(t *testing.T) { func TestNumaXml(t *testing.T) {
_, err := NumaXml(1, 1024, 0) _, err := numaXML(1, 1024, 0)
if err == nil { if err == nil {
t.Errorf("check invalid numa count failed: %s", err) t.Errorf("check invalid numa count failed: %s", err)
} }
xml, err := NumaXml(10, 10240, 8) xml, err := numaXML(10, 10240, 8)
expXML := `<numa> expXML := `<numa>
<cell id='0' cpus='0,1' memory='1280' unit='MiB'/> <cell id='0' cpus='0,1' memory='1280' unit='MiB'/>
<cell id='1' cpus='2,3' memory='1280' unit='MiB'/> <cell id='1' cpus='2,3' memory='1280' unit='MiB'/>