istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/serviceregistry/util/workloadinstances/util.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package workloadinstances
    16  
    17  import (
    18  	"strings"
    19  
    20  	"k8s.io/apimachinery/pkg/types"
    21  
    22  	"istio.io/istio/pilot/pkg/model"
    23  	"istio.io/istio/pkg/slices"
    24  )
    25  
    26  // FindInstance returns the first workload instance matching given predicate.
    27  func FindInstance(instances []*model.WorkloadInstance, predicate func(*model.WorkloadInstance) bool) *model.WorkloadInstance {
    28  	for _, instance := range instances {
    29  		if predicate(instance) {
    30  			return instance
    31  		}
    32  	}
    33  	return nil
    34  }
    35  
    36  // InstanceNameForProxy returns a name of the workload instance that
    37  // corresponds to a given proxy, if any.
    38  func InstanceNameForProxy(proxy *model.Proxy) types.NamespacedName {
    39  	parts := strings.Split(proxy.ID, ".")
    40  	if len(parts) == 2 && proxy.ConfigNamespace == parts[1] {
    41  		return types.NamespacedName{Name: parts[0], Namespace: parts[1]}
    42  	}
    43  	return types.NamespacedName{}
    44  }
    45  
    46  // GetInstanceForProxy returns a workload instance that
    47  // corresponds to a given proxy, if any.
    48  func GetInstanceForProxy(index Index, proxy *model.Proxy, proxyIP string) *model.WorkloadInstance {
    49  	if !slices.Contains(proxy.IPAddresses, proxyIP) {
    50  		return nil
    51  	}
    52  	instances := index.GetByIP(proxyIP) // list is ordered by namespace/name
    53  	if len(instances) == 0 {
    54  		return nil
    55  	}
    56  	if len(instances) == 1 { // dominant use case
    57  		// NOTE: for the sake of backwards compatibility, we don't enforce
    58  		//       instance.Namespace == proxy.ConfigNamespace
    59  		return instances[0]
    60  	}
    61  
    62  	// try to find workload instance with the same name as proxy
    63  	proxyName := InstanceNameForProxy(proxy)
    64  	if proxyName != (types.NamespacedName{}) {
    65  		instance := FindInstance(instances, func(wi *model.WorkloadInstance) bool {
    66  			return wi.Name == proxyName.Name && wi.Namespace == proxyName.Namespace
    67  		})
    68  		if instance != nil {
    69  			return instance
    70  		}
    71  	}
    72  
    73  	// try to find workload instance in the same namespace as proxy
    74  	instance := FindInstance(instances, func(wi *model.WorkloadInstance) bool {
    75  		// TODO: take auto-registration group into account once it's included into workload instance
    76  		return wi.Namespace == proxy.ConfigNamespace
    77  	})
    78  	if instance != nil {
    79  		return instance
    80  	}
    81  
    82  	// fall back to choosing one of the workload instances
    83  
    84  	// NOTE: for the sake of backwards compatibility, we don't enforce
    85  	//       instance.Namespace == proxy.ConfigNamespace
    86  	return instances[0]
    87  }