Skip to content

Commit 1997b5f

Browse files
authored
Merge pull request #6776 from helio/clusterapi-fix-HasInstance
fix(clusterapi): HasInstance with namespace prefix
2 parents 717911f + 98f9489 commit 1997b5f

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package clusterapi
1919
import (
2020
"fmt"
2121
"os"
22+
"path"
2223
"strings"
2324
"sync"
2425

@@ -144,6 +145,7 @@ func indexNodeByProviderID(obj interface{}) ([]string, error) {
144145
return []string{}, nil
145146
}
146147

148+
// findMachine looks up a machine by the ID which is stored in the index as <namespace>/<name>.
147149
func (c *machineController) findMachine(id string) (*unstructured.Unstructured, error) {
148150
return c.findResourceByKey(c.machineInformer.Informer().GetStore(), id)
149151
}
@@ -333,7 +335,8 @@ func (c *machineController) findMachineByProviderID(providerID normalizedProvide
333335
}
334336

335337
machineID := node.Annotations[machineAnnotationKey]
336-
return c.findMachine(machineID)
338+
ns := node.Annotations[clusterNamespaceAnnotationKey]
339+
return c.findMachine(path.Join(ns, machineID))
337340
}
338341

339342
func isPendingMachineProviderID(providerID normalizedProviderID) bool {

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,9 @@ func makeLinkedNodeAndMachine(i int, namespace, clusterName string, owner metav1
458458
ObjectMeta: metav1.ObjectMeta{
459459
Name: fmt.Sprintf("%s-%s-node-%d", namespace, owner.Name, i),
460460
Annotations: map[string]string{
461-
machineAnnotationKey: fmt.Sprintf("%s/%s-%s-machine-%d", namespace, namespace, owner.Name, i),
461+
clusterNameAnnotationKey: clusterName,
462+
clusterNamespaceAnnotationKey: namespace,
463+
machineAnnotationKey: fmt.Sprintf("%s-%s-machine-%d", namespace, owner.Name, i),
462464
},
463465
},
464466
Spec: corev1.NodeSpec{

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_provider.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package clusterapi
1818

1919
import (
2020
"fmt"
21+
"path"
2122
"reflect"
2223

2324
corev1 "k8s.io/api/core/v1"
@@ -81,8 +82,9 @@ func (p *provider) NodeGroupForNode(node *corev1.Node) (cloudprovider.NodeGroup,
8182
// HasInstance returns whether a given node has a corresponding instance in this cloud provider
8283
func (p *provider) HasInstance(node *corev1.Node) (bool, error) {
8384
machineID := node.Annotations[machineAnnotationKey]
85+
ns := node.Annotations[clusterNamespaceAnnotationKey]
8486

85-
machine, err := p.controller.findMachine(machineID)
87+
machine, err := p.controller.findMachine(path.Join(ns, machineID))
8688
if machine != nil {
8789
return true, nil
8890
}

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ var (
9090
// by the CAPI_GROUP env variable, it is initialized here.
9191
machineAnnotationKey = getMachineAnnotationKey()
9292

93+
// clusterNameAnnotationKey is the annotation used by cluster-api for annotating nodes
94+
// with their cluster name.
95+
clusterNameAnnotationKey = getClusterNameAnnotationKey()
96+
97+
// clusterNamespaceAnnotationKey is the annotation used by cluster-api for annotating nodes
98+
// with their cluster namespace.
99+
clusterNamespaceAnnotationKey = getClusterNamespaceAnnotationKey()
100+
93101
// nodeGroupMinSizeAnnotationKey and nodeGroupMaxSizeAnnotationKey are the keys
94102
// used in MachineSet and MachineDeployment annotations to specify the limits
95103
// for the node group. Because the keys can be affected by the CAPI_GROUP env
@@ -332,6 +340,20 @@ func getMachineAnnotationKey() string {
332340
return key
333341
}
334342

343+
// getClusterNameAnnotationKey returns the key that is used by cluster-api for annotating nodes
344+
// with their cluster name.
345+
func getClusterNameAnnotationKey() string {
346+
key := fmt.Sprintf("%s/cluster-name", getCAPIGroup())
347+
return key
348+
}
349+
350+
// getClusterNamespaceAnnotationKey returns the key that is used by cluster-api for annotating nodes
351+
// with their cluster namespace.
352+
func getClusterNamespaceAnnotationKey() string {
353+
key := fmt.Sprintf("%s/cluster-namespace", getCAPIGroup())
354+
return key
355+
}
356+
335357
// getClusterNameLabel returns the key that is used by cluster-api for labeling
336358
// which cluster an object belongs to. This function is needed because the user can change
337359
// the default group name by using the CAPI_GROUP environment variable.

0 commit comments

Comments
 (0)