Merge pull request #6776 from helio/clusterapi-fix-HasInstance

fix(clusterapi): HasInstance with namespace prefix
This commit is contained in:
Kubernetes Prow Robot 2024-07-12 10:25:22 -07:00 committed by GitHub
commit 1997b5fc10
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 32 additions and 3 deletions

View File

@ -19,6 +19,7 @@ package clusterapi
import (
"fmt"
"os"
"path"
"strings"
"sync"
@ -144,6 +145,7 @@ func indexNodeByProviderID(obj interface{}) ([]string, error) {
return []string{}, nil
}
// findMachine looks up a machine by the ID which is stored in the index as <namespace>/<name>.
func (c *machineController) findMachine(id string) (*unstructured.Unstructured, error) {
return c.findResourceByKey(c.machineInformer.Informer().GetStore(), id)
}
@ -333,7 +335,8 @@ func (c *machineController) findMachineByProviderID(providerID normalizedProvide
}
machineID := node.Annotations[machineAnnotationKey]
return c.findMachine(machineID)
ns := node.Annotations[clusterNamespaceAnnotationKey]
return c.findMachine(path.Join(ns, machineID))
}
func isPendingMachineProviderID(providerID normalizedProviderID) bool {

View File

@ -458,7 +458,9 @@ func makeLinkedNodeAndMachine(i int, namespace, clusterName string, owner metav1
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-%s-node-%d", namespace, owner.Name, i),
Annotations: map[string]string{
machineAnnotationKey: fmt.Sprintf("%s/%s-%s-machine-%d", namespace, namespace, owner.Name, i),
clusterNameAnnotationKey: clusterName,
clusterNamespaceAnnotationKey: namespace,
machineAnnotationKey: fmt.Sprintf("%s-%s-machine-%d", namespace, owner.Name, i),
},
},
Spec: corev1.NodeSpec{

View File

@ -18,6 +18,7 @@ package clusterapi
import (
"fmt"
"path"
"reflect"
corev1 "k8s.io/api/core/v1"
@ -81,8 +82,9 @@ func (p *provider) NodeGroupForNode(node *corev1.Node) (cloudprovider.NodeGroup,
// HasInstance returns whether a given node has a corresponding instance in this cloud provider
func (p *provider) HasInstance(node *corev1.Node) (bool, error) {
machineID := node.Annotations[machineAnnotationKey]
ns := node.Annotations[clusterNamespaceAnnotationKey]
machine, err := p.controller.findMachine(machineID)
machine, err := p.controller.findMachine(path.Join(ns, machineID))
if machine != nil {
return true, nil
}

View File

@ -90,6 +90,14 @@ var (
// by the CAPI_GROUP env variable, it is initialized here.
machineAnnotationKey = getMachineAnnotationKey()
// clusterNameAnnotationKey is the annotation used by cluster-api for annotating nodes
// with their cluster name.
clusterNameAnnotationKey = getClusterNameAnnotationKey()
// clusterNamespaceAnnotationKey is the annotation used by cluster-api for annotating nodes
// with their cluster namespace.
clusterNamespaceAnnotationKey = getClusterNamespaceAnnotationKey()
// nodeGroupMinSizeAnnotationKey and nodeGroupMaxSizeAnnotationKey are the keys
// used in MachineSet and MachineDeployment annotations to specify the limits
// for the node group. Because the keys can be affected by the CAPI_GROUP env
@ -332,6 +340,20 @@ func getMachineAnnotationKey() string {
return key
}
// getClusterNameAnnotationKey returns the key that is used by cluster-api for annotating nodes
// with their cluster name.
func getClusterNameAnnotationKey() string {
key := fmt.Sprintf("%s/cluster-name", getCAPIGroup())
return key
}
// getClusterNamespaceAnnotationKey returns the key that is used by cluster-api for annotating nodes
// with their cluster namespace.
func getClusterNamespaceAnnotationKey() string {
key := fmt.Sprintf("%s/cluster-namespace", getCAPIGroup())
return key
}
// getClusterNameLabel returns the key that is used by cluster-api for labeling
// which cluster an object belongs to. This function is needed because the user can change
// the default group name by using the CAPI_GROUP environment variable.