Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type machineController struct {
nodeInformer cache.SharedIndexInformer
managementClient dynamic.Interface
managementScaleClient scale.ScalesGetter
managementDiscoveryClient discovery.DiscoveryInterface
machineSetResource schema.GroupVersionResource
machineResource schema.GroupVersionResource
machinePoolResource schema.GroupVersionResource
Expand Down Expand Up @@ -419,7 +420,7 @@ func newMachineController(
managementInformerFactory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(managementClient, 0, namespaceToWatch(autoDiscoverySpecs), nil)

CAPIGroup := getCAPIGroup()
CAPIVersion, err := getAPIGroupPreferredVersion(managementDiscoveryClient, CAPIGroup)
CAPIVersion, err := getCAPIGroupPreferredVersion(managementDiscoveryClient, CAPIGroup)
if err != nil {
return nil, fmt.Errorf("could not find preferred version for CAPI group %q: %v", CAPIGroup, err)
}
Expand Down Expand Up @@ -523,6 +524,7 @@ func newMachineController(
nodeInformer: nodeInformer,
managementClient: managementClient,
managementScaleClient: managementScaleClient,
managementDiscoveryClient: managementDiscoveryClient,
machineSetResource: gvrMachineSet,
machinePoolResource: gvrMachinePool,
machinePoolsAvailable: machinePoolsAvailable,
Expand All @@ -548,11 +550,15 @@ func groupVersionHasResource(client discovery.DiscoveryInterface, groupVersion,
return false, nil
}

func getAPIGroupPreferredVersion(client discovery.DiscoveryInterface, APIGroup string) (string, error) {
func getCAPIGroupPreferredVersion(client discovery.DiscoveryInterface, APIGroup string) (string, error) {
if version := os.Getenv(CAPIVersionEnvVar); version != "" {
return version, nil
}

return getAPIGroupPreferredVersion(client, APIGroup)
}

func getAPIGroupPreferredVersion(client discovery.DiscoveryInterface, APIGroup string) (string, error) {
groupList, err := client.ServerGroups()
if err != nil {
return "", fmt.Errorf("failed to get ServerGroups: %v", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ func mustCreateTestController(t *testing.T, testConfigs ...*testConfig) (*machin
dynamicClientset := fakedynamic.NewSimpleDynamicClientWithCustomListKinds(
runtime.NewScheme(),
map[schema.GroupVersionResource]string{
{Group: "cluster.x-k8s.io", Version: "v1alpha3", Resource: "machinedeployments"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1alpha3", Resource: "machines"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1alpha3", Resource: "machinesets"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1alpha3", Resource: "machinepools"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1beta1", Resource: "machinedeployments"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1beta1", Resource: "machines"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1beta1", Resource: "machinesets"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1beta2", Resource: "machinedeployments"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1beta2", Resource: "machines"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1beta2", Resource: "machinesets"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1beta2", Resource: "machinepools"}: "kindList",
{Group: "custom.x-k8s.io", Version: "v1beta1", Resource: "machinepools"}: "kindList",
{Group: "custom.x-k8s.io", Version: "v1beta1", Resource: "machinedeployments"}: "kindList",
{Group: "custom.x-k8s.io", Version: "v1beta1", Resource: "machines"}: "kindList",
Expand Down Expand Up @@ -149,7 +149,7 @@ func mustCreateTestController(t *testing.T, testConfigs ...*testConfig) (*machin
},
},
{
GroupVersion: fmt.Sprintf("%s/v1alpha3", defaultCAPIGroup),
GroupVersion: fmt.Sprintf("%s/v1beta2", defaultCAPIGroup),
APIResources: []metav1.APIResource{
{
Name: resourceNameMachineDeployment,
Expand Down Expand Up @@ -189,7 +189,7 @@ func mustCreateTestController(t *testing.T, testConfigs ...*testConfig) (*machin

gvr := schema.GroupVersionResource{
Group: action.GetResource().Group,
Version: "v1alpha3",
Version: "v1beta2",
Resource: resource,
}

Expand Down Expand Up @@ -331,7 +331,7 @@ func createTestConfigs(specs ...testSpec) []*testConfig {
config.machineSet = &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": machineSetKind,
"apiVersion": "cluster.x-k8s.io/v1alpha3",
"apiVersion": "cluster.x-k8s.io/v1beta2",
"metadata": map[string]interface{}{
"name": spec.machineSetName,
"namespace": spec.namespace,
Expand Down Expand Up @@ -369,7 +369,7 @@ func createTestConfigs(specs ...testSpec) []*testConfig {
config.machineDeployment = &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": machineDeploymentKind,
"apiVersion": "cluster.x-k8s.io/v1alpha3",
"apiVersion": "cluster.x-k8s.io/v1beta2",
"metadata": map[string]interface{}{
"name": spec.machineDeploymentName,
"namespace": spec.namespace,
Expand All @@ -381,9 +381,9 @@ func createTestConfigs(specs ...testSpec) []*testConfig {
"template": map[string]interface{}{
"spec": map[string]interface{}{
"infrastructureRef": map[string]interface{}{
"apiVersion": "infrastructure.cluster.x-k8s.io/v1beta1",
"kind": machineTemplateKind,
"name": "TestMachineTemplate",
"apiGroup": "infrastructure.cluster.x-k8s.io",
"kind": machineTemplateKind,
"name": "TestMachineTemplate",
},
},
},
Expand Down Expand Up @@ -469,7 +469,7 @@ func makeLinkedNodeAndMachine(i int, namespace, clusterName string, owner metav1
machine := &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": machineKind,
"apiVersion": "cluster.x-k8s.io/v1alpha3",
"apiVersion": "cluster.x-k8s.io/v1beta2",
"metadata": map[string]interface{}{
"name": fmt.Sprintf("%s-%s-machine-%d", namespace, owner.Name, i),
"namespace": namespace,
Expand Down Expand Up @@ -1526,7 +1526,7 @@ func TestGetAPIGroupPreferredVersion(t *testing.T) {
{
description: "find version for default API group",
APIGroup: defaultCAPIGroup,
preferredVersion: "v1alpha3",
preferredVersion: "v1beta2",
envVar: "",
error: false,
},
Expand Down Expand Up @@ -1560,7 +1560,7 @@ func TestGetAPIGroupPreferredVersion(t *testing.T) {
GroupVersion: fmt.Sprintf("%s/v1beta1", customCAPIGroup),
},
{
GroupVersion: fmt.Sprintf("%s/v1alpha3", defaultCAPIGroup),
GroupVersion: fmt.Sprintf("%s/v1beta2", defaultCAPIGroup),
},
{
GroupVersion: fmt.Sprintf("%s/%s", customCAPIGroup, customVersion),
Expand All @@ -1571,7 +1571,7 @@ func TestGetAPIGroupPreferredVersion(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
t.Setenv(CAPIVersionEnvVar, tc.envVar)
version, err := getAPIGroupPreferredVersion(discoveryClient, tc.APIGroup)
version, err := getCAPIGroupPreferredVersion(discoveryClient, tc.APIGroup)
if (err != nil) != tc.error {
t.Errorf("expected to have error: %t. Had an error: %t", tc.error, err != nil)
}
Expand All @@ -1593,14 +1593,14 @@ func TestGroupVersionHasResource(t *testing.T) {
{
description: "true when it finds resource",
resourceName: resourceNameMachineDeployment,
APIGroup: fmt.Sprintf("%s/v1alpha3", defaultCAPIGroup),
APIGroup: fmt.Sprintf("%s/v1beta2", defaultCAPIGroup),
expected: true,
error: false,
},
{
description: "false when it does not find resource",
resourceName: "resourceDoesNotExist",
APIGroup: fmt.Sprintf("%s/v1alpha3", defaultCAPIGroup),
APIGroup: fmt.Sprintf("%s/v1beta2", defaultCAPIGroup),
expected: false,
error: false,
},
Expand All @@ -1617,7 +1617,7 @@ func TestGroupVersionHasResource(t *testing.T) {
Fake: &clientgotesting.Fake{
Resources: []*metav1.APIResourceList{
{
GroupVersion: fmt.Sprintf("%s/v1alpha3", defaultCAPIGroup),
GroupVersion: fmt.Sprintf("%s/v1beta2", defaultCAPIGroup),
APIResources: []metav1.APIResource{
{
Name: resourceNameMachineDeployment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,22 +321,44 @@ func (r unstructuredScalableResource) InstanceMaxPodsCapacityAnnotation() (resou
}

func (r unstructuredScalableResource) readInfrastructureReferenceResource() (*unstructured.Unstructured, error) {
obKind := r.unstructured.GetKind()
obName := r.unstructured.GetName()

infraref, found, err := unstructured.NestedStringMap(r.unstructured.Object, "spec", "template", "spec", "infrastructureRef")
if !found || err != nil {
return nil, nil
}

apiversion, ok := infraref["apiVersion"]
if !ok {
return nil, nil
var apiversion string

apiGroup, ok := infraref["apiGroup"]
if ok {
if apiversion, err = getAPIGroupPreferredVersion(r.controller.managementDiscoveryClient, apiGroup); err != nil {
klog.V(4).Infof("Unable to read preferred version from api group %s, error: %v", apiGroup, err)
return nil, err
}
apiversion = fmt.Sprintf("%s/%s", apiGroup, apiversion)
} else {
// Fall back to ObjectReference in capi v1beta1
apiversion, ok = infraref["apiVersion"]
if !ok {
info := fmt.Sprintf("Missing apiVersion from %s %s's InfrastructureReference", obKind, obName)
klog.V(4).Info(info)
return nil, errors.New(info)
}
}

kind, ok := infraref["kind"]
if !ok {
return nil, nil
info := fmt.Sprintf("Missing kind from %s %s's InfrastructureReference", obKind, obName)
klog.V(4).Info(info)
return nil, errors.New(info)
}
name, ok := infraref["name"]
if !ok {
return nil, nil
info := fmt.Sprintf("Missing name from %s %s's InfrastructureReference", obKind, obName)
klog.V(4).Info(info)
return nil, errors.New(info)
}
// kind needs to be lower case and plural
kind = fmt.Sprintf("%ss", strings.ToLower(kind))
Expand Down
Loading