Skip to content

Commit

Permalink
Merge pull request #6776 from helio/clusterapi-fix-HasInstance
Browse files Browse the repository at this point in the history
fix(clusterapi): HasInstance with namespace prefix
  • Loading branch information
k8s-ci-robot committed Jul 12, 2024
2 parents 717911f + 98f9489 commit 1997b5f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package clusterapi
import (
"fmt"
"os"
"path"
"strings"
"sync"

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package clusterapi

import (
"fmt"
"path"
"reflect"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -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
}
Expand Down
22 changes: 22 additions & 0 deletions cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 1997b5f

Please sign in to comment.