istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tools/bug-report/pkg/common/common.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 common contains resource names, which may vary from version to version.
    16  package common
    17  
    18  import (
    19  	"fmt"
    20  	"strings"
    21  
    22  	"istio.io/istio/pkg/log"
    23  )
    24  
    25  const (
    26  	// latestKey is an arbitrary value that represents the fallback version (master).
    27  	latestKey = "latest"
    28  
    29  	ProxyContainerName     = "istio-proxy"
    30  	DiscoveryContainerName = "discovery"
    31  	OperatorContainerName  = "istio-operator"
    32  
    33  	// namespaceAll is the default argument of across all namespaces
    34  	NamespaceAll        = ""
    35  	StrNamespaceAll     = "allNamespaces"
    36  	KubeSystemNamespace = "kube-system"
    37  )
    38  
    39  type kv struct {
    40  	k string
    41  	v string
    42  }
    43  
    44  type resourceNames struct {
    45  	discoveryLabels  []kv
    46  	istioDebugURLs   []string
    47  	proxyDebugURLs   []string
    48  	ztunnelDebugURLs []string
    49  }
    50  
    51  var versionMap = map[string]*resourceNames{
    52  	latestKey: {
    53  		discoveryLabels: []kv{
    54  			{k: "app", v: "istiod"},
    55  		},
    56  		istioDebugURLs: []string{
    57  			"debug/adsz",
    58  			"debug/authorizationz",
    59  			"debug/cachez",
    60  			"debug/clusterz",
    61  			"debug/configz",
    62  			"debug/endpointShardz",
    63  			"debug/endpointz",
    64  			"debug/inject",
    65  			"debug/instancesz",
    66  			"debug/mcsz",
    67  			"debug/mesh",
    68  			"debug/networkz",
    69  			"debug/push_status",
    70  			"debug/registryz",
    71  			"debug/resourcesz",
    72  			"debug/syncz",
    73  			"debug/telemetryz",
    74  			"metrics",
    75  		},
    76  		proxyDebugURLs: []string{
    77  			"certs",
    78  			"clusters",
    79  			"config_dump?include_eds",
    80  			"listeners",
    81  			"memory",
    82  			"server_info",
    83  			"stats/prometheus",
    84  			"runtime",
    85  		},
    86  		ztunnelDebugURLs: []string{
    87  			"config_dump",
    88  		},
    89  	},
    90  }
    91  
    92  // IstiodDebugURLs returns a list of Istiod debug URLs for the given version.
    93  func IstiodDebugURLs(clusterVersion string) []string {
    94  	return versionMap[getVersionKey(clusterVersion)].istioDebugURLs
    95  }
    96  
    97  // ProxyDebugURLs returns a list of proxy debug URLs for the given version.
    98  func ProxyDebugURLs(clusterVersion string) []string {
    99  	return versionMap[getVersionKey(clusterVersion)].proxyDebugURLs
   100  }
   101  
   102  // ZtunnelDebugURLs returns a list of ztunnel debug URLs for the given version.
   103  func ZtunnelDebugURLs(clusterVersion string) []string {
   104  	return versionMap[getVersionKey(clusterVersion)].ztunnelDebugURLs
   105  }
   106  
   107  // IsDiscoveryContainer reports whether the given container is an Istio discovery container for the given version.
   108  // Labels are the labels for the given pod.
   109  func IsDiscoveryContainer(clusterVersion, container string, labels map[string]string) bool {
   110  	if container != DiscoveryContainerName {
   111  		return false
   112  	}
   113  
   114  	for _, kv := range versionMap[getVersionKey(clusterVersion)].discoveryLabels {
   115  		if labels[kv.k] != kv.v {
   116  			return false
   117  		}
   118  	}
   119  	return true
   120  }
   121  
   122  // IsProxyContainer reports whether container is an istio proxy container.
   123  func IsProxyContainer(_, container string) bool {
   124  	return container == ProxyContainerName
   125  }
   126  
   127  // IsOperatorContainer reports whether the container is an istio-operator container.
   128  func IsOperatorContainer(_, container string) bool {
   129  	return container == OperatorContainerName
   130  }
   131  
   132  func IsCniPod(pod string) bool {
   133  	return strings.HasPrefix(pod, "istio-cni-node")
   134  }
   135  
   136  func getVersionKey(clusterVersion string) string {
   137  	if versionMap[clusterVersion] == nil {
   138  		return latestKey
   139  	}
   140  	return clusterVersion
   141  }
   142  
   143  func LogAndPrintf(format string, a ...any) {
   144  	fmt.Printf(format, a...)
   145  	log.Info(fmt.Sprintf(format, a...))
   146  }