Merge pull request #2692 from nilo19/qi-generate-instance

Generate azure instance types.
This commit is contained in:
Kubernetes Prow Robot 2020-01-07 00:38:19 -08:00 committed by GitHub
commit f7843fb5d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 1683 additions and 1057 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,164 @@
// +build ignore
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"html/template"
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/azure"
"k8s.io/klog"
)
var packageTemplate = template.Must(template.New("").Parse(`/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// This file was generated by go generate; DO NOT EDIT
package azure
// InstanceType is the sepc of Azure instance
type InstanceType struct {
InstanceType string
VCPU int64
MemoryMb int64
GPU int64
}
// InstanceTypes is a map of azure resources
var InstanceTypes = map[string]*InstanceType{
{{- range .InstanceTypes }}
"{{ .InstanceType }}": {
InstanceType: "{{ .InstanceType }}",
VCPU: {{ .VCPU }},
MemoryMb: {{ .MemoryMb }},
GPU: {{ .GPU }},
},
{{- end }}
}
`))
type InstanceCapabilities struct {
Name string
Value string
}
type RawInstanceType struct {
Name string
ResourceType string
Capabilities []InstanceCapabilities
}
func getAllAzureVirtualMachineTypes() (result map[string]*azure.InstanceType, err error) {
cmd := exec.Command("az", "vm", "list-skus", "-o", "json")
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
if err = cmd.Start(); err != nil {
return nil, err
}
bytes, err := ioutil.ReadAll(stdout)
if err != nil {
return nil, err
}
if err := cmd.Wait(); err != nil {
return nil, err
}
allInstances := make([]RawInstanceType, 0)
err = json.Unmarshal(bytes, &allInstances)
if err != nil {
return nil, err
}
virtualMachines := make(map[string]*azure.InstanceType)
for _, instance := range allInstances {
if strings.EqualFold(instance.ResourceType, "virtualMachines") {
var virtualMachine azure.InstanceType
virtualMachine.InstanceType = instance.Name
for _, capability := range instance.Capabilities {
switch capability.Name {
case "vCPUs":
virtualMachine.VCPU, err = strconv.ParseInt(capability.Value, 10, 64)
if err != nil {
return nil, err
}
case "MemoryGB":
memoryMb, err := strconv.ParseFloat(capability.Value, 10)
if err != nil {
return nil, err
}
virtualMachine.MemoryMb = int64(memoryMb) * 1024
case "GPUs":
virtualMachine.GPU, err = strconv.ParseInt(capability.Value, 10, 64)
if err != nil {
return nil, err
}
}
}
virtualMachines[virtualMachine.InstanceType] = &virtualMachine
}
}
return virtualMachines, err
}
func main() {
instanceTypes, err := getAllAzureVirtualMachineTypes()
if err != nil {
klog.Fatal(err)
}
f, err := os.Create("azure_instance_types.go")
if err != nil {
klog.Fatal(err)
}
defer f.Close()
err = packageTemplate.Execute(f, struct {
InstanceTypes map[string]*azure.InstanceType
}{
InstanceTypes: instanceTypes,
})
if err != nil {
klog.Fatal(err)
}
}

View File

@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
//go:generate go run azure_instance_types/gen.go
package azure
import (

View File

@ -503,7 +503,7 @@ func (scaleSet *ScaleSet) buildNodeFromTemplate(template compute.VirtualMachineS
Capacity: apiv1.ResourceList{},
}
var vmssType *instanceType
var vmssType *InstanceType
for k := range InstanceTypes {
if strings.EqualFold(k, *template.Sku.Name) {
vmssType = InstanceTypes[k]

View File

@ -154,7 +154,8 @@ skipped_dirs = ['Godeps', 'third_party', '_gopath', '_output', '.git', 'cluster/
# list all the files contain 'DO NOT EDIT', but are not generated
skipped_ungenerated_files = ['hack/build-ui.sh', 'hack/lib/swagger.sh',
'hack/boilerplate/boilerplate.py',
'cluster-autoscaler/cloudprovider/aws/ec2_instance_types/gen.go']
'cluster-autoscaler/cloudprovider/aws/ec2_instance_types/gen.go',
'cluster-autoscaler/cloudprovider/azure/azure_instance_types/gen.go']
def normalize_files(files):
newfiles = []