...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/debug/iochaos/iochaos.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/debug/iochaos

     1  // Copyright 2019 Chaos Mesh 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package iochaos
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  
    20  	"github.com/pkg/errors"
    21  	v1 "k8s.io/api/core/v1"
    22  	"k8s.io/apimachinery/pkg/runtime"
    23  
    24  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    25  	cm "github.com/chaos-mesh/chaos-mesh/pkg/chaosctl/common"
    26  )
    27  
    28  // Debug get chaos debug information
    29  func Debug(ctx context.Context, chaos runtime.Object, c *cm.ClientSet, result *cm.ChaosResult) error {
    30  	ioChaos, ok := chaos.(*v1alpha1.IOChaos)
    31  	if !ok {
    32  		return fmt.Errorf("chaos is not iochaos")
    33  	}
    34  	chaosStatus := ioChaos.Status.ChaosStatus
    35  	chaosSelector := ioChaos.Spec.Selector
    36  
    37  	pods, daemons, err := cm.GetPods(ctx, ioChaos.GetName(), chaosStatus, chaosSelector, c.CtrlCli)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	for i := range pods {
    43  		podName := pods[i].Name
    44  		podResult := cm.PodResult{Name: podName}
    45  		_ = debugEachPod(ctx, pods[i], daemons[i], ioChaos, c, &podResult)
    46  		result.Pods = append(result.Pods, podResult)
    47  		// TODO: V(4) log when err != nil, wait for #1433
    48  	}
    49  	return nil
    50  }
    51  
    52  func debugEachPod(ctx context.Context, pod v1.Pod, daemon v1.Pod, chaos *v1alpha1.IOChaos, c *cm.ClientSet, result *cm.PodResult) error {
    53  	// print out debug info
    54  	cmd := "cat /proc/mounts"
    55  	out, err := cm.ExecBypass(ctx, pod, daemon, cmd, c.KubeCli)
    56  	if err != nil {
    57  		return errors.Wrapf(err, "run command '%s' failed", cmd)
    58  	}
    59  	result.Items = append(result.Items, cm.ItemResult{Name: "mount information", Value: string(out)})
    60  
    61  	pids, commands, err := cm.GetPidFromPS(ctx, pod, daemon, c.KubeCli)
    62  	if err != nil {
    63  		return errors.Wrapf(err, "get pid for pod %s/%s from ps failed", pod.GetNamespace(), pod.GetName())
    64  	}
    65  
    66  	for i := range pids {
    67  		cmd = fmt.Sprintf("ls -l /proc/%s/fd", pids[i])
    68  		out, err = cm.ExecBypass(ctx, pod, daemon, cmd, c.KubeCli)
    69  
    70  		var itemValue string
    71  		if err != nil {
    72  			itemValue = err.Error()
    73  		} else {
    74  			itemValue = string(out)
    75  		}
    76  		result.Items = append(result.Items, cm.ItemResult{Name: fmt.Sprintf("file descriptors of PID: %s, COMMAND: %s", pids[i], commands[i]), Value: itemValue})
    77  	}
    78  
    79  	return nil
    80  }
    81