...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/ctrl/server/net.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/ctrl/server

     1  // Copyright 2021 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  // 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  
    16  package server
    17  
    18  import (
    19  	"context"
    20  	"strings"
    21  
    22  	"github.com/pingcap/errors"
    23  	v1 "k8s.io/api/core/v1"
    24  
    25  	"github.com/chaos-mesh/chaos-mesh/pkg/bpm"
    26  )
    27  
    28  // GetIpset returns result of ipset list
    29  func (r *Resolver) GetIpset(ctx context.Context, obj *v1.Pod) (string, error) {
    30  	cmd := "ipset list"
    31  	return r.ExecBypass(ctx, obj, cmd, bpm.PidNS, bpm.NetNS)
    32  }
    33  
    34  // GetIpset returns result of tc qdisc list
    35  func (r *Resolver) GetTcQdisc(ctx context.Context, obj *v1.Pod) ([]string, error) {
    36  	cmd := "tc qdisc list"
    37  	rules, err := r.ExecBypass(ctx, obj, cmd, bpm.PidNS, bpm.NetNS)
    38  	if err != nil {
    39  		return nil, errors.Wrapf(err, "exec `%s`", cmd)
    40  	}
    41  	return strings.Split(rules, "\n"), nil
    42  }
    43  
    44  // GetIptables returns result of iptables --list
    45  func (r *Resolver) GetIptables(ctx context.Context, obj *v1.Pod) ([]string, error) {
    46  	cmd := "iptables --list"
    47  	rules, err := r.ExecBypass(ctx, obj, cmd, bpm.PidNS, bpm.NetNS)
    48  	if err != nil {
    49  		return nil, errors.Wrapf(err, "exec `%s`", cmd)
    50  	}
    51  	return strings.Split(rules, "\n"), nil
    52  }
    53  
    54  // cleanTcs returns actually cleaned devices
    55  func (r *Resolver) cleanTcs(ctx context.Context, obj *v1.Pod, devices []string) ([]string, error) {
    56  	var cleaned []string
    57  	for _, device := range devices {
    58  		cmd := "tc qdisc del dev " + device + " root"
    59  		_, err := r.ExecBypass(ctx, obj, cmd, bpm.PidNS, bpm.NetNS)
    60  		if err != nil {
    61  			return cleaned, errors.Wrapf(err, "exec `%s`", cmd)
    62  		}
    63  		cleaned = append(cleaned, device)
    64  	}
    65  	return cleaned, nil
    66  }
    67  
    68  // cleanIptables returns actually cleaned chains
    69  func (r *Resolver) cleanIptables(ctx context.Context, obj *v1.Pod, chains []string) ([]string, error) {
    70  	var cleaned []string
    71  	for _, chain := range chains {
    72  		cmd := "iptables -F " + chain
    73  		_, err := r.ExecBypass(ctx, obj, cmd, bpm.PidNS, bpm.NetNS)
    74  		if err != nil {
    75  			return cleaned, errors.Wrapf(err, "exec `%s`", cmd)
    76  		}
    77  		cleaned = append(cleaned, chain)
    78  	}
    79  	return cleaned, nil
    80  }
    81