Skip to content

Commit 75eb791

Browse files
k8s-infra-cherrypick-robotwjunottjackfrancis
authored
[cluster-autoscaler-release-1.32] Process apiGroup in capi provider (#8454)
* Add process with apiGroup in capi provider * Replace capi v1alpha3 with v1beta2 in test cases * Add detailed error messages * remove unused package --------- Co-authored-by: Jun Wang <[email protected]> Co-authored-by: Jack Francis <[email protected]>
1 parent 946bbcb commit 75eb791

File tree

3 files changed

+53
-25
lines changed

3 files changed

+53
-25
lines changed

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ type machineController struct {
8181
nodeInformer cache.SharedIndexInformer
8282
managementClient dynamic.Interface
8383
managementScaleClient scale.ScalesGetter
84+
managementDiscoveryClient discovery.DiscoveryInterface
8485
machineSetResource schema.GroupVersionResource
8586
machineResource schema.GroupVersionResource
8687
machinePoolResource schema.GroupVersionResource
@@ -422,7 +423,7 @@ func newMachineController(
422423
managementInformerFactory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(managementClient, 0, namespaceToWatch(autoDiscoverySpecs), nil)
423424

424425
CAPIGroup := getCAPIGroup()
425-
CAPIVersion, err := getAPIGroupPreferredVersion(managementDiscoveryClient, CAPIGroup)
426+
CAPIVersion, err := getCAPIGroupPreferredVersion(managementDiscoveryClient, CAPIGroup)
426427
if err != nil {
427428
return nil, fmt.Errorf("could not find preferred version for CAPI group %q: %v", CAPIGroup, err)
428429
}
@@ -526,6 +527,7 @@ func newMachineController(
526527
nodeInformer: nodeInformer,
527528
managementClient: managementClient,
528529
managementScaleClient: managementScaleClient,
530+
managementDiscoveryClient: managementDiscoveryClient,
529531
machineSetResource: gvrMachineSet,
530532
machinePoolResource: gvrMachinePool,
531533
machinePoolsAvailable: machinePoolsAvailable,
@@ -551,11 +553,15 @@ func groupVersionHasResource(client discovery.DiscoveryInterface, groupVersion,
551553
return false, nil
552554
}
553555

554-
func getAPIGroupPreferredVersion(client discovery.DiscoveryInterface, APIGroup string) (string, error) {
556+
func getCAPIGroupPreferredVersion(client discovery.DiscoveryInterface, APIGroup string) (string, error) {
555557
if version := os.Getenv(CAPIVersionEnvVar); version != "" {
556558
return version, nil
557559
}
558560

561+
return getAPIGroupPreferredVersion(client, APIGroup)
562+
}
563+
564+
func getAPIGroupPreferredVersion(client discovery.DiscoveryInterface, APIGroup string) (string, error) {
559565
groupList, err := client.ServerGroups()
560566
if err != nil {
561567
return "", fmt.Errorf("failed to get ServerGroups: %v", err)

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ func mustCreateTestController(t testing.TB, testConfigs ...*testConfig) (*machin
105105
dynamicClientset := fakedynamic.NewSimpleDynamicClientWithCustomListKinds(
106106
runtime.NewScheme(),
107107
map[schema.GroupVersionResource]string{
108-
{Group: "cluster.x-k8s.io", Version: "v1alpha3", Resource: "machinedeployments"}: "kindList",
109-
{Group: "cluster.x-k8s.io", Version: "v1alpha3", Resource: "machines"}: "kindList",
110-
{Group: "cluster.x-k8s.io", Version: "v1alpha3", Resource: "machinesets"}: "kindList",
111-
{Group: "cluster.x-k8s.io", Version: "v1alpha3", Resource: "machinepools"}: "kindList",
112108
{Group: "cluster.x-k8s.io", Version: "v1beta1", Resource: "machinedeployments"}: "kindList",
113109
{Group: "cluster.x-k8s.io", Version: "v1beta1", Resource: "machines"}: "kindList",
114110
{Group: "cluster.x-k8s.io", Version: "v1beta1", Resource: "machinesets"}: "kindList",
111+
{Group: "cluster.x-k8s.io", Version: "v1beta2", Resource: "machinedeployments"}: "kindList",
112+
{Group: "cluster.x-k8s.io", Version: "v1beta2", Resource: "machines"}: "kindList",
113+
{Group: "cluster.x-k8s.io", Version: "v1beta2", Resource: "machinesets"}: "kindList",
114+
{Group: "cluster.x-k8s.io", Version: "v1beta2", Resource: "machinepools"}: "kindList",
115115
{Group: "custom.x-k8s.io", Version: "v1beta1", Resource: "machinepools"}: "kindList",
116116
{Group: "custom.x-k8s.io", Version: "v1beta1", Resource: "machinedeployments"}: "kindList",
117117
{Group: "custom.x-k8s.io", Version: "v1beta1", Resource: "machines"}: "kindList",
@@ -149,7 +149,7 @@ func mustCreateTestController(t testing.TB, testConfigs ...*testConfig) (*machin
149149
},
150150
},
151151
{
152-
GroupVersion: fmt.Sprintf("%s/v1alpha3", defaultCAPIGroup),
152+
GroupVersion: fmt.Sprintf("%s/v1beta2", defaultCAPIGroup),
153153
APIResources: []metav1.APIResource{
154154
{
155155
Name: resourceNameMachineDeployment,
@@ -189,7 +189,7 @@ func mustCreateTestController(t testing.TB, testConfigs ...*testConfig) (*machin
189189

190190
gvr := schema.GroupVersionResource{
191191
Group: action.GetResource().Group,
192-
Version: "v1alpha3",
192+
Version: "v1beta2",
193193
Resource: resource,
194194
}
195195

@@ -331,7 +331,7 @@ func createTestConfigs(specs ...testSpec) []*testConfig {
331331
config.machineSet = &unstructured.Unstructured{
332332
Object: map[string]interface{}{
333333
"kind": machineSetKind,
334-
"apiVersion": "cluster.x-k8s.io/v1alpha3",
334+
"apiVersion": "cluster.x-k8s.io/v1beta2",
335335
"metadata": map[string]interface{}{
336336
"name": spec.machineSetName,
337337
"namespace": spec.namespace,
@@ -369,7 +369,7 @@ func createTestConfigs(specs ...testSpec) []*testConfig {
369369
config.machineDeployment = &unstructured.Unstructured{
370370
Object: map[string]interface{}{
371371
"kind": machineDeploymentKind,
372-
"apiVersion": "cluster.x-k8s.io/v1alpha3",
372+
"apiVersion": "cluster.x-k8s.io/v1beta2",
373373
"metadata": map[string]interface{}{
374374
"name": spec.machineDeploymentName,
375375
"namespace": spec.namespace,
@@ -381,9 +381,9 @@ func createTestConfigs(specs ...testSpec) []*testConfig {
381381
"template": map[string]interface{}{
382382
"spec": map[string]interface{}{
383383
"infrastructureRef": map[string]interface{}{
384-
"apiVersion": "infrastructure.cluster.x-k8s.io/v1beta1",
385-
"kind": machineTemplateKind,
386-
"name": "TestMachineTemplate",
384+
"apiGroup": "infrastructure.cluster.x-k8s.io",
385+
"kind": machineTemplateKind,
386+
"name": "TestMachineTemplate",
387387
},
388388
},
389389
},
@@ -471,7 +471,7 @@ func makeLinkedNodeAndMachine(i int, namespace, clusterName string, owner metav1
471471
machine := &unstructured.Unstructured{
472472
Object: map[string]interface{}{
473473
"kind": machineKind,
474-
"apiVersion": "cluster.x-k8s.io/v1alpha3",
474+
"apiVersion": "cluster.x-k8s.io/v1beta2",
475475
"metadata": map[string]interface{}{
476476
"name": fmt.Sprintf("%s-%s-machine-%d", namespace, owner.Name, i),
477477
"namespace": namespace,
@@ -1528,7 +1528,7 @@ func TestGetAPIGroupPreferredVersion(t *testing.T) {
15281528
{
15291529
description: "find version for default API group",
15301530
APIGroup: defaultCAPIGroup,
1531-
preferredVersion: "v1alpha3",
1531+
preferredVersion: "v1beta2",
15321532
envVar: "",
15331533
error: false,
15341534
},
@@ -1562,7 +1562,7 @@ func TestGetAPIGroupPreferredVersion(t *testing.T) {
15621562
GroupVersion: fmt.Sprintf("%s/v1beta1", customCAPIGroup),
15631563
},
15641564
{
1565-
GroupVersion: fmt.Sprintf("%s/v1alpha3", defaultCAPIGroup),
1565+
GroupVersion: fmt.Sprintf("%s/v1beta2", defaultCAPIGroup),
15661566
},
15671567
{
15681568
GroupVersion: fmt.Sprintf("%s/%s", customCAPIGroup, customVersion),
@@ -1573,7 +1573,7 @@ func TestGetAPIGroupPreferredVersion(t *testing.T) {
15731573
for _, tc := range testCases {
15741574
t.Run(tc.description, func(t *testing.T) {
15751575
t.Setenv(CAPIVersionEnvVar, tc.envVar)
1576-
version, err := getAPIGroupPreferredVersion(discoveryClient, tc.APIGroup)
1576+
version, err := getCAPIGroupPreferredVersion(discoveryClient, tc.APIGroup)
15771577
if (err != nil) != tc.error {
15781578
t.Errorf("expected to have error: %t. Had an error: %t", tc.error, err != nil)
15791579
}
@@ -1595,14 +1595,14 @@ func TestGroupVersionHasResource(t *testing.T) {
15951595
{
15961596
description: "true when it finds resource",
15971597
resourceName: resourceNameMachineDeployment,
1598-
APIGroup: fmt.Sprintf("%s/v1alpha3", defaultCAPIGroup),
1598+
APIGroup: fmt.Sprintf("%s/v1beta2", defaultCAPIGroup),
15991599
expected: true,
16001600
error: false,
16011601
},
16021602
{
16031603
description: "false when it does not find resource",
16041604
resourceName: "resourceDoesNotExist",
1605-
APIGroup: fmt.Sprintf("%s/v1alpha3", defaultCAPIGroup),
1605+
APIGroup: fmt.Sprintf("%s/v1beta2", defaultCAPIGroup),
16061606
expected: false,
16071607
error: false,
16081608
},
@@ -1619,7 +1619,7 @@ func TestGroupVersionHasResource(t *testing.T) {
16191619
Fake: &clientgotesting.Fake{
16201620
Resources: []*metav1.APIResourceList{
16211621
{
1622-
GroupVersion: fmt.Sprintf("%s/v1alpha3", defaultCAPIGroup),
1622+
GroupVersion: fmt.Sprintf("%s/v1beta2", defaultCAPIGroup),
16231623
APIResources: []metav1.APIResource{
16241624
{
16251625
Name: resourceNameMachineDeployment,

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_unstructured.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,22 +322,44 @@ func (r unstructuredScalableResource) InstanceMaxPodsCapacityAnnotation() (resou
322322
}
323323

324324
func (r unstructuredScalableResource) readInfrastructureReferenceResource() (*unstructured.Unstructured, error) {
325+
obKind := r.unstructured.GetKind()
326+
obName := r.unstructured.GetName()
327+
325328
infraref, found, err := unstructured.NestedStringMap(r.unstructured.Object, "spec", "template", "spec", "infrastructureRef")
326329
if !found || err != nil {
327330
return nil, nil
328331
}
329332

330-
apiversion, ok := infraref["apiVersion"]
331-
if !ok {
332-
return nil, nil
333+
var apiversion string
334+
335+
apiGroup, ok := infraref["apiGroup"]
336+
if ok {
337+
if apiversion, err = getAPIGroupPreferredVersion(r.controller.managementDiscoveryClient, apiGroup); err != nil {
338+
klog.V(4).Infof("Unable to read preferred version from api group %s, error: %v", apiGroup, err)
339+
return nil, err
340+
}
341+
apiversion = fmt.Sprintf("%s/%s", apiGroup, apiversion)
342+
} else {
343+
// Fall back to ObjectReference in capi v1beta1
344+
apiversion, ok = infraref["apiVersion"]
345+
if !ok {
346+
info := fmt.Sprintf("Missing apiVersion from %s %s's InfrastructureReference", obKind, obName)
347+
klog.V(4).Info(info)
348+
return nil, errors.New(info)
349+
}
333350
}
351+
334352
kind, ok := infraref["kind"]
335353
if !ok {
336-
return nil, nil
354+
info := fmt.Sprintf("Missing kind from %s %s's InfrastructureReference", obKind, obName)
355+
klog.V(4).Info(info)
356+
return nil, errors.New(info)
337357
}
338358
name, ok := infraref["name"]
339359
if !ok {
340-
return nil, nil
360+
info := fmt.Sprintf("Missing name from %s %s's InfrastructureReference", obKind, obName)
361+
klog.V(4).Info(info)
362+
return nil, errors.New(info)
341363
}
342364
// kind needs to be lower case and plural
343365
kind = fmt.Sprintf("%ss", strings.ToLower(kind))

0 commit comments

Comments
 (0)