...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/netem/convert.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/netem

     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 netem
    17  
    18  import (
    19  	"strconv"
    20  	"time"
    21  
    22  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    23  	chaosdaemonpb "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
    24  )
    25  
    26  // FromDelay convert delay to netem
    27  func FromDelay(in *v1alpha1.DelaySpec) (*chaosdaemonpb.Netem, error) {
    28  	// Parse latency and jitter inputs to ensure they are valid
    29  	if _, err := time.ParseDuration(in.Latency); err != nil {
    30  		return nil, err
    31  	}
    32  	if _, err := time.ParseDuration(in.Jitter); err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	corr, err := strconv.ParseFloat(in.Correlation, 32)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	netem := &chaosdaemonpb.Netem{
    42  		Time:      in.Latency,
    43  		DelayCorr: float32(corr),
    44  		Jitter:    in.Jitter,
    45  	}
    46  
    47  	if in.Reorder != nil {
    48  		reorderPercentage, err := strconv.ParseFloat(in.Reorder.Reorder, 32)
    49  		if err != nil {
    50  			return nil, err
    51  		}
    52  
    53  		corr, err := strconv.ParseFloat(in.Reorder.Correlation, 32)
    54  		if err != nil {
    55  			return nil, err
    56  		}
    57  
    58  		netem.Reorder = float32(reorderPercentage)
    59  		netem.ReorderCorr = float32(corr)
    60  		netem.Gap = uint32(in.Reorder.Gap)
    61  	}
    62  
    63  	return netem, nil
    64  }
    65  
    66  // FromLoss convert loss to netem
    67  func FromLoss(in *v1alpha1.LossSpec) (*chaosdaemonpb.Netem, error) {
    68  	lossPercentage, err := strconv.ParseFloat(in.Loss, 32)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	corr, err := strconv.ParseFloat(in.Correlation, 32)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	return &chaosdaemonpb.Netem{
    79  		Loss:     float32(lossPercentage),
    80  		LossCorr: float32(corr),
    81  	}, nil
    82  }
    83  
    84  // FromDuplicate convert duplicate to netem
    85  func FromDuplicate(in *v1alpha1.DuplicateSpec) (*chaosdaemonpb.Netem, error) {
    86  	duplicatePercentage, err := strconv.ParseFloat(in.Duplicate, 32)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	corr, err := strconv.ParseFloat(in.Correlation, 32)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	return &chaosdaemonpb.Netem{
    97  		Duplicate:     float32(duplicatePercentage),
    98  		DuplicateCorr: float32(corr),
    99  	}, nil
   100  }
   101  
   102  // FromCorrupt convert corrupt to netem
   103  func FromCorrupt(in *v1alpha1.CorruptSpec) (*chaosdaemonpb.Netem, error) {
   104  	corruptPercentage, err := strconv.ParseFloat(in.Corrupt, 32)
   105  	if err != nil {
   106  		return nil, err
   107  	}
   108  
   109  	corr, err := strconv.ParseFloat(in.Correlation, 32)
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  
   114  	return &chaosdaemonpb.Netem{
   115  		Corrupt:     float32(corruptPercentage),
   116  		CorruptCorr: float32(corr),
   117  	}, nil
   118  }
   119  
   120  // FromRate convert RateSpec to netem
   121  func FromRate(in *v1alpha1.RateSpec) (*chaosdaemonpb.Netem, error) {
   122  	return &chaosdaemonpb.Netem{
   123  		Rate: in.Rate,
   124  	}, nil
   125  }
   126  
   127  // FromBandwidth converts BandwidthSpec to *chaosdaemonpb.Tbf
   128  // Bandwidth action use TBF under the hood.
   129  // TBF stands for Token Bucket Filter, is a classful queueing discipline available
   130  // for traffic control with the tc command.
   131  // http://man7.org/linux/man-pages/man8/tc-tbf.8.html
   132  func FromBandwidth(in *v1alpha1.BandwidthSpec) (*chaosdaemonpb.Tbf, error) {
   133  	tbf := &chaosdaemonpb.Tbf{
   134  		Rate:   in.Rate,
   135  		Limit:  in.Limit,
   136  		Buffer: in.Buffer,
   137  	}
   138  
   139  	if in.Peakrate != nil && in.Minburst != nil {
   140  		tbf.PeakRate = *in.Peakrate
   141  		tbf.MinBurst = *in.Minburst
   142  	}
   143  
   144  	return tbf, nil
   145  }
   146