sigs.k8s.io/cluster-api@v1.7.1/api/v1beta1/index/machinepool.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package index
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/pkg/errors"
    24  	ctrl "sigs.k8s.io/controller-runtime"
    25  	"sigs.k8s.io/controller-runtime/pkg/client"
    26  
    27  	expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
    28  )
    29  
    30  const (
    31  	// MachinePoolNodeNameField is used by the MachinePool Controller to index MachinePools by Node name, and add a watch on Nodes.
    32  	MachinePoolNodeNameField = "status.nodeRefs.name"
    33  
    34  	// MachinePoolProviderIDField is used to index MachinePools by ProviderID. It's useful to find MachinePools
    35  	// in a management cluster from Nodes in a workload cluster.
    36  	MachinePoolProviderIDField = "spec.providerIDList"
    37  )
    38  
    39  // ByMachinePoolNode adds the machinepool node name index to the
    40  // managers cache.
    41  func ByMachinePoolNode(ctx context.Context, mgr ctrl.Manager) error {
    42  	if err := mgr.GetCache().IndexField(ctx, &expv1.MachinePool{},
    43  		MachinePoolNodeNameField,
    44  		MachinePoolByNodeName,
    45  	); err != nil {
    46  		return errors.Wrap(err, "error setting index field")
    47  	}
    48  
    49  	return nil
    50  }
    51  
    52  // MachinePoolByNodeName contains the logic to index MachinePools by Node name.
    53  func MachinePoolByNodeName(o client.Object) []string {
    54  	machinepool, ok := o.(*expv1.MachinePool)
    55  	if !ok {
    56  		panic(fmt.Sprintf("Expected a MachinePool but got a %T", o))
    57  	}
    58  
    59  	if len(machinepool.Status.NodeRefs) == 0 {
    60  		return nil
    61  	}
    62  
    63  	nodeNames := make([]string, 0, len(machinepool.Status.NodeRefs))
    64  	for _, ref := range machinepool.Status.NodeRefs {
    65  		nodeNames = append(nodeNames, ref.Name)
    66  	}
    67  	return nodeNames
    68  }
    69  
    70  // ByMachinePoolProviderID adds the machinepool providerID index to the
    71  // managers cache.
    72  func ByMachinePoolProviderID(ctx context.Context, mgr ctrl.Manager) error {
    73  	if err := mgr.GetCache().IndexField(ctx, &expv1.MachinePool{},
    74  		MachinePoolProviderIDField,
    75  		machinePoolByProviderID,
    76  	); err != nil {
    77  		return errors.Wrap(err, "error setting index field")
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  func machinePoolByProviderID(o client.Object) []string {
    84  	machinepool, ok := o.(*expv1.MachinePool)
    85  	if !ok {
    86  		panic(fmt.Sprintf("Expected a MachinePool but got a %T", o))
    87  	}
    88  
    89  	if len(machinepool.Spec.ProviderIDList) == 0 {
    90  		return nil
    91  	}
    92  
    93  	providerIDs := make([]string, 0, len(machinepool.Spec.ProviderIDList))
    94  	for _, id := range machinepool.Spec.ProviderIDList {
    95  		if id == "" {
    96  			// Valid providerID not found, skipping.
    97  			continue
    98  		}
    99  		providerIDs = append(providerIDs, id)
   100  	}
   101  
   102  	return providerIDs
   103  }