sigs.k8s.io/cluster-api@v1.7.1/api/v1beta1/index/machine.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  	"k8s.io/utils/ptr"
    25  	ctrl "sigs.k8s.io/controller-runtime"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    29  )
    30  
    31  const (
    32  	// MachineNodeNameField is used by the Machine Controller to index Machines by Node name, and add a watch on Nodes.
    33  	MachineNodeNameField = "status.nodeRef.name"
    34  
    35  	// MachineProviderIDField is used to index Machines by ProviderID. It's useful to find Machines
    36  	// in a management cluster from Nodes in a workload cluster.
    37  	MachineProviderIDField = "spec.providerID"
    38  )
    39  
    40  // ByMachineNode adds the machine node name index to the
    41  // managers cache.
    42  func ByMachineNode(ctx context.Context, mgr ctrl.Manager) error {
    43  	if err := mgr.GetCache().IndexField(ctx, &clusterv1.Machine{},
    44  		MachineNodeNameField,
    45  		MachineByNodeName,
    46  	); err != nil {
    47  		return errors.Wrap(err, "error setting index field")
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  // MachineByNodeName contains the logic to index Machines by Node name.
    54  func MachineByNodeName(o client.Object) []string {
    55  	machine, ok := o.(*clusterv1.Machine)
    56  	if !ok {
    57  		panic(fmt.Sprintf("Expected a Machine but got a %T", o))
    58  	}
    59  	if machine.Status.NodeRef != nil {
    60  		return []string{machine.Status.NodeRef.Name}
    61  	}
    62  	return nil
    63  }
    64  
    65  // ByMachineProviderID adds the machine providerID index to the
    66  // managers cache.
    67  func ByMachineProviderID(ctx context.Context, mgr ctrl.Manager) error {
    68  	if err := mgr.GetCache().IndexField(ctx, &clusterv1.Machine{},
    69  		MachineProviderIDField,
    70  		machineByProviderID,
    71  	); err != nil {
    72  		return errors.Wrap(err, "error setting index field")
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  func machineByProviderID(o client.Object) []string {
    79  	machine, ok := o.(*clusterv1.Machine)
    80  	if !ok {
    81  		panic(fmt.Sprintf("Expected a Machine but got a %T", o))
    82  	}
    83  
    84  	providerID := ptr.Deref(machine.Spec.ProviderID, "")
    85  
    86  	if providerID == "" {
    87  		return nil
    88  	}
    89  
    90  	return []string{providerID}
    91  }