istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/util/ambient/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 ambient
    16  
    17  import (
    18  	"context"
    19  	"strings"
    20  
    21  	corev1 "k8s.io/api/core/v1"
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  
    24  	"istio.io/api/label"
    25  	"istio.io/istio/pkg/config/constants"
    26  	"istio.io/istio/pkg/kube"
    27  )
    28  
    29  func IsZtunnelPod(client kube.CLIClient, podName, podNamespace string) bool {
    30  	isZtunnel := strings.HasPrefix(podName, "ztunnel")
    31  	if client == nil {
    32  		return isZtunnel
    33  	}
    34  	pod, err := client.Kube().CoreV1().Pods(podNamespace).Get(context.Background(), podName, metav1.GetOptions{})
    35  	if err != nil {
    36  		return isZtunnel
    37  	}
    38  	if v, ok := pod.Labels["app"]; ok {
    39  		return v == "ztunnel"
    40  	}
    41  	return isZtunnel
    42  }
    43  
    44  // InAmbient returns true if a resource is in ambient data plane mode.
    45  func InAmbient(obj metav1.Object) bool {
    46  	if obj == nil {
    47  		return false
    48  	}
    49  	switch t := obj.(type) {
    50  	case *corev1.Pod:
    51  		return t.GetAnnotations()[constants.AmbientRedirection] == constants.AmbientRedirectionEnabled
    52  	case *corev1.Namespace:
    53  		if t.GetLabels()["istio-injection"] == "enabled" {
    54  			return false
    55  		}
    56  		if v, ok := t.GetLabels()[label.IoIstioRev.Name]; ok && v != "" {
    57  			return false
    58  		}
    59  		return t.GetLabels()[constants.DataplaneModeLabel] == constants.DataplaneModeAmbient
    60  	}
    61  	return false
    62  }