...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/jvm/action.go

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

     1  // Copyright 2020 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 jvm
    15  
    16  import (
    17  	"encoding/json"
    18  	"fmt"
    19  	"strconv"
    20  
    21  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    22  )
    23  
    24  const (
    25  	SUID   string = "suid"
    26  	ACTION string = "action"
    27  	TARGET string = "target"
    28  )
    29  
    30  // ToSandboxAction convertes chaos to sandbox action
    31  func ToSandboxAction(suid string, chaos *v1alpha1.JVMChaos) ([]byte, error) {
    32  	if _, ok := v1alpha1.JvmSpec[chaos.Spec.Target]; !ok {
    33  		return nil, fmt.Errorf("unknown JVM chaos target:%s",
    34  			chaos.Spec.Target)
    35  	}
    36  
    37  	if _, ok := v1alpha1.JvmSpec[chaos.Spec.Target][chaos.Spec.Action]; !ok {
    38  		return nil, fmt.Errorf("JVM target: %s does not supported action: %s",
    39  			chaos.Spec.Target, chaos.Spec.Action)
    40  	}
    41  
    42  	kv := make(map[string]string)
    43  	flags := v1alpha1.JvmSpec[chaos.Spec.Target][chaos.Spec.Action].Flags
    44  	if flags != nil {
    45  		for k, v := range chaos.Spec.Flags {
    46  			for _, rule := range flags {
    47  				if rule.Name != k {
    48  					continue
    49  				}
    50  
    51  				if rule.ParameterType != v1alpha1.BoolType {
    52  					kv[k] = v
    53  				} else {
    54  					f, err := strconv.ParseBool(v)
    55  					if err != nil {
    56  						return nil, fmt.Errorf("can not parse Spec.Flags.%s's value:%s as boolean", k, v)
    57  					}
    58  					// if f is false, should not send key-value to sandbox server.
    59  					if f {
    60  						kv[k] = v
    61  					}
    62  				}
    63  			}
    64  		}
    65  	}
    66  
    67  	matchers := v1alpha1.JvmSpec[chaos.Spec.Target][chaos.Spec.Action].Matchers
    68  	if matchers != nil {
    69  		for k, v := range chaos.Spec.Matchers {
    70  			for _, rule := range matchers {
    71  				if rule.Name != k {
    72  					continue
    73  				}
    74  
    75  				if rule.ParameterType != v1alpha1.BoolType {
    76  					kv[k] = v
    77  				} else {
    78  					f, err := strconv.ParseBool(v)
    79  					if err != nil {
    80  						return nil, fmt.Errorf("can not parse Spec.Matchers.%s's value:%s as boolean", k, v)
    81  					}
    82  					// if f is false, should not send key-value to sandbox server.
    83  					if f {
    84  						kv[k] = v
    85  					}
    86  				}
    87  			}
    88  		}
    89  	}
    90  
    91  	kv[SUID] = suid
    92  	kv[ACTION] = fmt.Sprint(chaos.Spec.Action)
    93  	kv[TARGET] = fmt.Sprint(chaos.Spec.Target)
    94  	return json.Marshal(kv)
    95  }
    96