Skip to content

Commit

Permalink
Add a better check for ztunnel pod guess (istio#44292)
Browse files Browse the repository at this point in the history
* add a better check for ztunnel guess

* rebise based on comments
  • Loading branch information
hanxiaop authored Apr 14, 2023
1 parent bc2ce84 commit 4721395
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
17 changes: 11 additions & 6 deletions istioctl/cmd/proxyconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ var (
reset = false
)

var isZtunnelPod = func(podName, podNamespace string) bool {
client, _ := kubeClient(kubeconfig, configContext)
return ambientutil.IsZtunnelPod(client, podName, podNamespace)
}

func ztunnelLogLevel(level string) string {
switch level {
case "warning":
Expand Down Expand Up @@ -497,7 +502,7 @@ func allConfigCmd() *cobra.Command {
return err
}

if ambientutil.IsZtunnelPod(podName) {
if isZtunnelPod(podName, podNamespace) {
dump, err = extractZtunnelConfigDump(podName, podNamespace)
} else {
dump, err = extractConfigDump(podName, podNamespace, false)
Expand Down Expand Up @@ -526,7 +531,7 @@ func allConfigCmd() *cobra.Command {
return err
}

if ambientutil.IsZtunnelPod(podName) {
if isZtunnelPod(podName, podNamespace) {
w, err := setupZtunnelConfigDumpWriter(podName, podNamespace, c.OutOrStdout())
if err != nil {
return err
Expand Down Expand Up @@ -636,7 +641,7 @@ func workloadConfigCmd() *cobra.Command {
if podName, podNamespace, err = getComponentPodName(args[0]); err != nil {
return err
}
if !ambientutil.IsZtunnelPod(podName) {
if !isZtunnelPod(podName, podNamespace) {
return fmt.Errorf("workloads command is only supported by ztunnel proxies: %v", podName)
}
configWriter, err = setupZtunnelConfigDumpWriter(podName, podNamespace, c.OutOrStdout())
Expand Down Expand Up @@ -878,7 +883,7 @@ func logCmd() *cobra.Command {
if err != nil {
return err
}
if ambientutil.IsZtunnelPod(pod) {
if isZtunnelPod(pod, podNamespace) {
loggerNames[name] = Ztunnel
} else {
loggerNames[name] = Envoy
Expand Down Expand Up @@ -932,7 +937,7 @@ func logCmd() *cobra.Command {
var resp string
var errs *multierror.Error
for _, podName := range podNames {
if ambientutil.IsZtunnelPod(podName) {
if isZtunnelPod(podName, podNamespace) {
q := "level=" + ztunnelLogLevel(loggerLevelString)
if reset {
q += "&reset"
Expand Down Expand Up @@ -1311,7 +1316,7 @@ func secretConfigCmd() *cobra.Command {
if podName, podNamespace, err = getPodName(args[0]); err != nil {
return err
}
if ambientutil.IsZtunnelPod(podName) {
if isZtunnelPod(podName, podNamespace) {
newWriter, err = setupZtunnelConfigDumpWriter(podName, podNamespace, c.OutOrStdout())
} else {
newWriter, err = setupPodConfigdumpWriter(podName, podNamespace, false, c.OutOrStdout())
Expand Down
3 changes: 3 additions & 0 deletions istioctl/cmd/proxyconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func TestProxyConfig(t *testing.T) {
"httpbin-794b576b6c-qx6pf": []byte("{}"),
"ztunnel-9v7nw": []byte("current log level is debug"),
}
isZtunnelPod = func(podName, _ string) bool {
return strings.HasPrefix(podName, "ztunnel")
}
cases := []execTestCase{
{
args: strings.Split("proxy-config", " "),
Expand Down
24 changes: 21 additions & 3 deletions istioctl/pkg/util/ambient/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,26 @@

package ambient

import "strings"
import (
"context"
"strings"

func IsZtunnelPod(podName string) bool {
return strings.HasPrefix(podName, "ztunnel")
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"istio.io/istio/pkg/kube"
)

func IsZtunnelPod(client kube.Client, podName, podNamespace string) bool {
isZtunnel := strings.HasPrefix(podName, "ztunnel")
if client == nil {
return isZtunnel
}
pod, err := client.Kube().CoreV1().Pods(podNamespace).Get(context.Background(), podName, metav1.GetOptions{})
if err != nil {
return isZtunnel
}
if v, ok := pod.Labels["app"]; ok {
return v == "ztunnel"
}
return isZtunnel
}
26 changes: 16 additions & 10 deletions tools/bug-report/pkg/bugreport/bugreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ func gatherInfo(runner *kubectlcmd.Runner, config *config.BugReportConfig, resou
cmdTimer := time.NewTimer(time.Duration(config.CommandTimeout))
beginTime := time.Now()

client, err := kube.NewCLIClient(kube.BuildClientCmd(config.KubeConfigPath, config.Context), "")
if err != nil {
appendGlobalErr(err)
}

clusterDir := archive.ClusterInfoPath(tempDir)

params := &content.Params{
Expand Down Expand Up @@ -306,16 +311,17 @@ func gatherInfo(runner *kubectlcmd.Runner, config *config.BugReportConfig, resou
cp := params.SetNamespace(namespace).SetPod(pod).SetContainer(container)
proxyDir := archive.ProxyOutputPath(tempDir, namespace, pod)
switch {
case common.IsProxyContainer(params.ClusterVersion, container) && !ambient.IsZtunnelPod(pod):
getFromCluster(content.GetCoredumps, cp, filepath.Join(proxyDir, "cores"), &mandatoryWg)
getFromCluster(content.GetNetstat, cp, proxyDir, &mandatoryWg)
getFromCluster(content.GetProxyInfo, cp, archive.ProxyOutputPath(tempDir, namespace, pod), &optionalWg)
getProxyLogs(runner, config, resources, p, namespace, pod, container, &optionalWg)

case ambient.IsZtunnelPod(pod):
getFromCluster(content.GetNetstat, cp, proxyDir, &mandatoryWg)
getFromCluster(content.GetZtunnelInfo, cp, archive.ProxyOutputPath(tempDir, namespace, pod), &optionalWg)
getProxyLogs(runner, config, resources, p, namespace, pod, container, &optionalWg)
case common.IsProxyContainer(params.ClusterVersion, container):
if !ambient.IsZtunnelPod(client, pod, namespace) {
getFromCluster(content.GetCoredumps, cp, filepath.Join(proxyDir, "cores"), &mandatoryWg)
getFromCluster(content.GetNetstat, cp, proxyDir, &mandatoryWg)
getFromCluster(content.GetProxyInfo, cp, archive.ProxyOutputPath(tempDir, namespace, pod), &optionalWg)
getProxyLogs(runner, config, resources, p, namespace, pod, container, &optionalWg)
} else {
getFromCluster(content.GetNetstat, cp, proxyDir, &mandatoryWg)
getFromCluster(content.GetZtunnelInfo, cp, archive.ProxyOutputPath(tempDir, namespace, pod), &optionalWg)
getProxyLogs(runner, config, resources, p, namespace, pod, container, &optionalWg)
}
case resources.IsDiscoveryContainer(params.ClusterVersion, namespace, pod, container):
getFromCluster(content.GetIstiodInfo, cp, archive.IstiodPath(tempDir, namespace, pod), &mandatoryWg)
getIstiodLogs(runner, config, resources, namespace, pod, &mandatoryWg)
Expand Down

0 comments on commit 4721395

Please sign in to comment.