@@ -25,6 +25,7 @@ import (
25
25
26
26
"github.com/stretchr/testify/assert"
27
27
gce "google.golang.org/api/compute/v1"
28
+ "google.golang.org/protobuf/proto"
28
29
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
29
30
"k8s.io/autoscaler/cluster-autoscaler/utils/units"
30
31
)
33
34
errFetchMig = errors .New ("fetch migs error" )
34
35
errFetchMigInstances = errors .New ("fetch mig instances error" )
35
36
errFetchMigTargetSize = errors .New ("fetch mig target size error" )
37
+ errFetchMigWorkloadPolicy = errors .New ("fetch mig workload policy error" )
36
38
errFetchMigBaseName = errors .New ("fetch mig basename error" )
37
39
errFetchMigTemplateName = errors .New ("fetch mig template name error" )
38
40
errFetchMigTemplate = errors .New ("fetch mig template error" )
@@ -112,6 +114,7 @@ type mockAutoscalingGceClient struct {
112
114
fetchMigs func (string ) ([]* gce.InstanceGroupManager , error )
113
115
fetchAllInstances func (project , zone string , filter string ) ([]GceInstance , error )
114
116
fetchMigTargetSize func (GceRef ) (int64 , error )
117
+ fetchMigWorkloadPolicy func (GceRef ) (* string , error )
115
118
fetchMigBasename func (GceRef ) (string , error )
116
119
fetchMigInstances func (GceRef ) ([]GceInstance , error )
117
120
fetchMigTemplateName func (GceRef ) (InstanceTemplateName , error )
@@ -140,6 +143,10 @@ func (client *mockAutoscalingGceClient) FetchMigTargetSize(migRef GceRef) (int64
140
143
return client .fetchMigTargetSize (migRef )
141
144
}
142
145
146
+ func (client * mockAutoscalingGceClient ) FetchMigWorkloadPolicy (migRef GceRef ) (* string , error ) {
147
+ return client .fetchMigWorkloadPolicy (migRef )
148
+ }
149
+
143
150
func (client * mockAutoscalingGceClient ) FetchMigBasename (migRef GceRef ) (string , error ) {
144
151
return client .fetchMigBasename (migRef )
145
152
}
@@ -685,6 +692,7 @@ func TestRegenerateMigInstancesCache(t *testing.T) {
685
692
listManagedInstancesResultsCache : map [GceRef ]string {},
686
693
instanceTemplateNameCache : map [GceRef ]InstanceTemplateName {},
687
694
migInstancesStateCountCache : map [GceRef ]map [cloudprovider.InstanceState ]int64 {},
695
+ migWorkloadPolicyCache : map [GceRef ]* string {},
688
696
},
689
697
fetchMigInstances : fetchMigInstancesConst (mig1Instances ),
690
698
fetchMigs : fetchMigsConst ([]* gce.InstanceGroupManager {mig1Igm }),
@@ -710,6 +718,7 @@ func TestRegenerateMigInstancesCache(t *testing.T) {
710
718
listManagedInstancesResultsCache : map [GceRef ]string {},
711
719
instanceTemplateNameCache : map [GceRef ]InstanceTemplateName {},
712
720
migInstancesStateCountCache : map [GceRef ]map [cloudprovider.InstanceState ]int64 {},
721
+ migWorkloadPolicyCache : map [GceRef ]* string {},
713
722
},
714
723
fetchMigInstances : fetchMigInstancesConst (mig2Instances ),
715
724
fetchMigs : fetchMigsConst ([]* gce.InstanceGroupManager {mig2Igm }),
@@ -736,6 +745,7 @@ func TestRegenerateMigInstancesCache(t *testing.T) {
736
745
listManagedInstancesResultsCache : map [GceRef ]string {},
737
746
instanceTemplateNameCache : map [GceRef ]InstanceTemplateName {},
738
747
migInstancesStateCountCache : map [GceRef ]map [cloudprovider.InstanceState ]int64 {},
748
+ migWorkloadPolicyCache : map [GceRef ]* string {},
739
749
},
740
750
fetchMigs : fetchMigsConst ([]* gce.InstanceGroupManager {mig2Igm }),
741
751
fetchAllInstances : fetchAllInstancesInZone (map [string ][]GceInstance {"myzone2" : {instance3 , instance6 }}),
@@ -863,6 +873,90 @@ func TestGetMigTargetSize(t *testing.T) {
863
873
}
864
874
}
865
875
876
+ func TestGetMigWorkloadPolicy (t * testing.T ) {
877
+ workloadPolicy := proto .String ("wp-123" )
878
+ instanceGroupManager := & gce.InstanceGroupManager {
879
+ Zone : mig .GceRef ().Zone ,
880
+ Name : mig .GceRef ().Name ,
881
+ ResourcePolicies : & gce.InstanceGroupManagerResourcePolicies {
882
+ WorkloadPolicy : * workloadPolicy ,
883
+ },
884
+ }
885
+
886
+ testCases := []struct {
887
+ name string
888
+ cache * GceCache
889
+ fetchMigs func (string ) ([]* gce.InstanceGroupManager , error )
890
+ fetchMigWorkloadPolicy func (GceRef ) (* string , error )
891
+ expectedWorkloadPolicy * string
892
+ expectedErr error
893
+ }{
894
+ {
895
+ name : "workload policy in cache" ,
896
+ cache : & GceCache {
897
+ migs : map [GceRef ]Mig {mig .GceRef (): mig },
898
+ migWorkloadPolicyCache : map [GceRef ]* string {mig .GceRef (): workloadPolicy },
899
+ },
900
+ expectedWorkloadPolicy : workloadPolicy ,
901
+ },
902
+ {
903
+ name : "workload policy from cache fill" ,
904
+ cache : emptyCache (),
905
+ fetchMigs : fetchMigsConst ([]* gce.InstanceGroupManager {instanceGroupManager }),
906
+ expectedWorkloadPolicy : workloadPolicy ,
907
+ },
908
+ {
909
+ name : "cache fill without mig, fallback success" ,
910
+ cache : emptyCache (),
911
+ fetchMigs : fetchMigsConst ([]* gce.InstanceGroupManager {}),
912
+ fetchMigWorkloadPolicy : fetchMigWorkloadPolicyConst (workloadPolicy ),
913
+ expectedWorkloadPolicy : workloadPolicy ,
914
+ },
915
+ {
916
+ name : "cache fill failure, fallback success" ,
917
+ cache : emptyCache (),
918
+ fetchMigs : fetchMigsFail ,
919
+ fetchMigWorkloadPolicy : fetchMigWorkloadPolicyConst (workloadPolicy ),
920
+ expectedWorkloadPolicy : workloadPolicy ,
921
+ },
922
+ {
923
+ name : "cache fill without mig, fallback failure" ,
924
+ cache : emptyCache (),
925
+ fetchMigs : fetchMigsConst ([]* gce.InstanceGroupManager {}),
926
+ fetchMigWorkloadPolicy : fetchMigWorkloadPolicyFail ,
927
+ expectedErr : errFetchMigWorkloadPolicy ,
928
+ },
929
+ {
930
+ name : "cache fill failure, fallback failure" ,
931
+ cache : emptyCache (),
932
+ fetchMigs : fetchMigsFail ,
933
+ fetchMigWorkloadPolicy : fetchMigWorkloadPolicyFail ,
934
+ expectedErr : errFetchMigWorkloadPolicy ,
935
+ },
936
+ }
937
+
938
+ for _ , tc := range testCases {
939
+ t .Run (tc .name , func (t * testing.T ) {
940
+ client := & mockAutoscalingGceClient {
941
+ fetchMigs : tc .fetchMigs ,
942
+ fetchMigWorkloadPolicy : tc .fetchMigWorkloadPolicy ,
943
+ }
944
+ migLister := NewMigLister (tc .cache )
945
+ provider := NewCachingMigInfoProvider (tc .cache , migLister , client , mig .GceRef ().Project , 1 , 0 * time .Second , false )
946
+
947
+ workloadPolicy , err := provider .GetMigWorkloadPolicy (mig .GceRef ())
948
+ cachedWorkloadPolicy , found := tc .cache .GetMigWorkloadPolicy (mig .GceRef ())
949
+
950
+ assert .Equal (t , tc .expectedErr , err )
951
+ assert .Equal (t , tc .expectedErr == nil , found )
952
+ if tc .expectedErr == nil {
953
+ assert .Equal (t , tc .expectedWorkloadPolicy , workloadPolicy )
954
+ assert .Equal (t , tc .expectedWorkloadPolicy , cachedWorkloadPolicy )
955
+ }
956
+ })
957
+ }
958
+ }
959
+
866
960
func TestGetMigBasename (t * testing.T ) {
867
961
basename := "base-instance-name"
868
962
instanceGroupManager := & gce.InstanceGroupManager {
@@ -1921,6 +2015,7 @@ func emptyCache() *GceCache {
1921
2015
instancesUpdateTime : make (map [GceRef ]time.Time ),
1922
2016
migTargetSizeCache : make (map [GceRef ]int64 ),
1923
2017
migBaseNameCache : make (map [GceRef ]string ),
2018
+ migWorkloadPolicyCache : make (map [GceRef ]* string ),
1924
2019
migInstancesStateCountCache : make (map [GceRef ]map [cloudprovider.InstanceState ]int64 ),
1925
2020
listManagedInstancesResultsCache : make (map [GceRef ]string ),
1926
2021
instanceTemplateNameCache : make (map [GceRef ]InstanceTemplateName ),
@@ -1972,10 +2067,20 @@ func fetchMigTargetSizeConst(targetSize int64) func(GceRef) (int64, error) {
1972
2067
}
1973
2068
}
1974
2069
2070
+ func fetchMigWorkloadPolicyFail (_ GceRef ) (* string , error ) {
2071
+ return nil , errFetchMigWorkloadPolicy
2072
+ }
2073
+
1975
2074
func fetchMigBasenameFail (_ GceRef ) (string , error ) {
1976
2075
return "" , errFetchMigBaseName
1977
2076
}
1978
2077
2078
+ func fetchMigWorkloadPolicyConst (workloadPolicy * string ) func (GceRef ) (* string , error ) {
2079
+ return func (GceRef ) (* string , error ) {
2080
+ return workloadPolicy , nil
2081
+ }
2082
+ }
2083
+
1979
2084
func fetchMigBasenameConst (basename string ) func (GceRef ) (string , error ) {
1980
2085
return func (GceRef ) (string , error ) {
1981
2086
return basename , nil
0 commit comments