@@ -20,20 +20,14 @@ import (
20
20
"fmt"
21
21
"math/rand"
22
22
"net/http"
23
- "regexp"
24
23
"strings"
25
24
"sync"
26
25
"time"
27
26
28
27
apiv1 "k8s.io/api/core/v1"
29
- "k8s.io/apimachinery/pkg/api/resource"
30
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31
28
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
32
29
"k8s.io/autoscaler/cluster-autoscaler/config/dynamic"
33
- "k8s.io/autoscaler/cluster-autoscaler/utils/gpu"
34
- cloudvolume "k8s.io/cloud-provider/volume"
35
30
klog "k8s.io/klog/v2"
36
- kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis"
37
31
schedulerframework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
38
32
"k8s.io/legacy-cloud-providers/azure/retry"
39
33
@@ -524,184 +518,14 @@ func (scaleSet *ScaleSet) Debug() string {
524
518
return fmt .Sprintf ("%s (%d:%d)" , scaleSet .Id (), scaleSet .MinSize (), scaleSet .MaxSize ())
525
519
}
526
520
527
- func buildInstanceOS (template compute.VirtualMachineScaleSet ) string {
528
- instanceOS := cloudprovider .DefaultOS
529
- if template .VirtualMachineProfile != nil && template .VirtualMachineProfile .OsProfile != nil && template .VirtualMachineProfile .OsProfile .WindowsConfiguration != nil {
530
- instanceOS = "windows"
531
- }
532
-
533
- return instanceOS
534
- }
535
-
536
- func buildGenericLabels (template compute.VirtualMachineScaleSet , nodeName string ) map [string ]string {
537
- result := make (map [string ]string )
538
-
539
- result [kubeletapis .LabelArch ] = cloudprovider .DefaultArch
540
- result [kubeletapis .LabelOS ] = buildInstanceOS (template )
541
- result [apiv1 .LabelInstanceType ] = * template .Sku .Name
542
- result [apiv1 .LabelZoneRegion ] = strings .ToLower (* template .Location )
543
-
544
- if template .Zones != nil && len (* template .Zones ) > 0 {
545
- failureDomains := make ([]string , len (* template .Zones ))
546
- for k , v := range * template .Zones {
547
- failureDomains [k ] = strings .ToLower (* template .Location ) + "-" + v
548
- }
549
-
550
- result [apiv1 .LabelZoneFailureDomain ] = strings .Join (failureDomains [:], cloudvolume .LabelMultiZoneDelimiter )
551
- } else {
552
- result [apiv1 .LabelZoneFailureDomain ] = "0"
553
- }
554
-
555
- result [apiv1 .LabelHostname ] = nodeName
556
- return result
557
- }
558
-
559
- func (scaleSet * ScaleSet ) buildNodeFromTemplate (template compute.VirtualMachineScaleSet ) (* apiv1.Node , error ) {
560
- node := apiv1.Node {}
561
- nodeName := fmt .Sprintf ("%s-asg-%d" , scaleSet .Name , rand .Int63 ())
562
-
563
- node .ObjectMeta = metav1.ObjectMeta {
564
- Name : nodeName ,
565
- SelfLink : fmt .Sprintf ("/api/v1/nodes/%s" , nodeName ),
566
- Labels : map [string ]string {},
567
- }
568
-
569
- node .Status = apiv1.NodeStatus {
570
- Capacity : apiv1.ResourceList {},
571
- }
572
-
573
- var vmssType * InstanceType
574
- for k := range InstanceTypes {
575
- if strings .EqualFold (k , * template .Sku .Name ) {
576
- vmssType = InstanceTypes [k ]
577
- break
578
- }
579
- }
580
-
581
- promoRe := regexp .MustCompile (`(?i)_promo` )
582
- if promoRe .MatchString (* template .Sku .Name ) {
583
- if vmssType == nil {
584
- // We didn't find an exact match but this is a promo type, check for matching standard
585
- klog .V (1 ).Infof ("No exact match found for %s, checking standard types" , * template .Sku .Name )
586
- skuName := promoRe .ReplaceAllString (* template .Sku .Name , "" )
587
- for k := range InstanceTypes {
588
- if strings .EqualFold (k , skuName ) {
589
- vmssType = InstanceTypes [k ]
590
- break
591
- }
592
- }
593
- }
594
- }
595
-
596
- if vmssType == nil {
597
- return nil , fmt .Errorf ("instance type %q not supported" , * template .Sku .Name )
598
- }
599
- node .Status .Capacity [apiv1 .ResourcePods ] = * resource .NewQuantity (110 , resource .DecimalSI )
600
- node .Status .Capacity [apiv1 .ResourceCPU ] = * resource .NewQuantity (vmssType .VCPU , resource .DecimalSI )
601
- node .Status .Capacity [gpu .ResourceNvidiaGPU ] = * resource .NewQuantity (vmssType .GPU , resource .DecimalSI )
602
- node .Status .Capacity [apiv1 .ResourceMemory ] = * resource .NewQuantity (vmssType .MemoryMb * 1024 * 1024 , resource .DecimalSI )
603
-
604
- resourcesFromTags := extractAllocatableResourcesFromScaleSet (template .Tags )
605
- for resourceName , val := range resourcesFromTags {
606
- node .Status .Capacity [apiv1 .ResourceName (resourceName )] = * val
607
- }
608
-
609
- // TODO: set real allocatable.
610
- node .Status .Allocatable = node .Status .Capacity
611
-
612
- // NodeLabels
613
- if template .Tags != nil {
614
- for k , v := range template .Tags {
615
- if v != nil {
616
- node .Labels [k ] = * v
617
- } else {
618
- node .Labels [k ] = ""
619
- }
620
-
621
- }
622
- }
623
-
624
- // GenericLabels
625
- node .Labels = cloudprovider .JoinStringMaps (node .Labels , buildGenericLabels (template , nodeName ))
626
- // Labels from the Scale Set's Tags
627
- node .Labels = cloudprovider .JoinStringMaps (node .Labels , extractLabelsFromScaleSet (template .Tags ))
628
-
629
- // Taints from the Scale Set's Tags
630
- node .Spec .Taints = extractTaintsFromScaleSet (template .Tags )
631
-
632
- node .Status .Conditions = cloudprovider .BuildReadyConditions ()
633
- return & node , nil
634
- }
635
-
636
- func extractLabelsFromScaleSet (tags map [string ]* string ) map [string ]string {
637
- result := make (map [string ]string )
638
-
639
- for tagName , tagValue := range tags {
640
- splits := strings .Split (tagName , nodeLabelTagName )
641
- if len (splits ) > 1 {
642
- label := strings .Replace (splits [1 ], "_" , "/" , - 1 )
643
- if label != "" {
644
- result [label ] = * tagValue
645
- }
646
- }
647
- }
648
-
649
- return result
650
- }
651
-
652
- func extractTaintsFromScaleSet (tags map [string ]* string ) []apiv1.Taint {
653
- taints := make ([]apiv1.Taint , 0 )
654
-
655
- for tagName , tagValue := range tags {
656
- // The tag value must be in the format <tag>:NoSchedule
657
- r , _ := regexp .Compile ("(.*):(?:NoSchedule|NoExecute|PreferNoSchedule)" )
658
-
659
- if r .MatchString (* tagValue ) {
660
- splits := strings .Split (tagName , nodeTaintTagName )
661
- if len (splits ) > 1 {
662
- values := strings .SplitN (* tagValue , ":" , 2 )
663
- if len (values ) > 1 {
664
- taintKey := strings .Replace (splits [1 ], "_" , "/" , - 1 )
665
- taints = append (taints , apiv1.Taint {
666
- Key : taintKey ,
667
- Value : values [0 ],
668
- Effect : apiv1 .TaintEffect (values [1 ]),
669
- })
670
- }
671
- }
672
- }
673
- }
674
-
675
- return taints
676
- }
677
-
678
- func extractAllocatableResourcesFromScaleSet (tags map [string ]* string ) map [string ]* resource.Quantity {
679
- resources := make (map [string ]* resource.Quantity )
680
-
681
- for tagName , tagValue := range tags {
682
- resourceName := strings .Split (tagName , nodeResourcesTagName )
683
- if len (resourceName ) < 2 || resourceName [1 ] == "" {
684
- continue
685
- }
686
-
687
- quantity , err := resource .ParseQuantity (* tagValue )
688
- if err != nil {
689
- continue
690
- }
691
- resources [resourceName [1 ]] = & quantity
692
- }
693
-
694
- return resources
695
- }
696
-
697
521
// TemplateNodeInfo returns a node template for this scale set.
698
522
func (scaleSet * ScaleSet ) TemplateNodeInfo () (* schedulerframework.NodeInfo , error ) {
699
523
template , rerr := scaleSet .getVMSSInfo ()
700
524
if rerr != nil {
701
525
return nil , rerr .Error ()
702
526
}
703
527
704
- node , err := scaleSet . buildNodeFromTemplate (template )
528
+ node , err := buildNodeFromTemplate (scaleSet . Name , template )
705
529
if err != nil {
706
530
return nil , err
707
531
}
0 commit comments