...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/dashboard/apivalidator/scope_validator.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/dashboard/apivalidator

     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 apivalidator
    17  
    18  import (
    19  	"strconv"
    20  
    21  	"github.com/go-playground/validator/v10"
    22  	corev1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/util/validation"
    25  )
    26  
    27  // NamespaceSelectorsValid can be used to check whether namespace selectors is valid.
    28  func NamespaceSelectorsValid(fl validator.FieldLevel) bool {
    29  	ns, ok := fl.Field().Interface().([]string)
    30  	if !ok {
    31  		return false
    32  	}
    33  
    34  	for _, n := range ns {
    35  		if len(n) == 0 || len(n) > 63 {
    36  			return false
    37  		}
    38  
    39  		if !checkName(n) {
    40  			return false
    41  		}
    42  	}
    43  
    44  	return true
    45  }
    46  
    47  // MapSelectorsValid can be used to check whether map selectors is valid.
    48  func MapSelectorsValid(fl validator.FieldLevel) bool {
    49  	if fl.Field().IsNil() {
    50  		return true
    51  	}
    52  
    53  	ms, ok := fl.Field().Interface().(map[string]string)
    54  	if !ok {
    55  		return false
    56  	}
    57  
    58  	for k := range ms {
    59  		if len(validation.IsQualifiedName(k)) != 0 {
    60  			return false
    61  		}
    62  	}
    63  
    64  	return true
    65  }
    66  
    67  // RequirementSelectorsValid can be used to check whether label requirement selectors is valid.
    68  func RequirementSelectorsValid(fl validator.FieldLevel) bool {
    69  	if fl.Field().IsNil() {
    70  		return true
    71  	}
    72  
    73  	rs, ok := fl.Field().Interface().([]metav1.LabelSelectorRequirement)
    74  	if !ok {
    75  		return false
    76  	}
    77  
    78  	for _, r := range rs {
    79  		if len(validation.IsQualifiedName(r.Key)) != 0 {
    80  			return false
    81  		}
    82  
    83  		switch r.Operator {
    84  		case metav1.LabelSelectorOpIn, metav1.LabelSelectorOpNotIn:
    85  			if len(r.Values) == 0 {
    86  				return false
    87  			}
    88  		case metav1.LabelSelectorOpExists, metav1.LabelSelectorOpDoesNotExist:
    89  			if len(r.Values) > 0 {
    90  				return false
    91  			}
    92  		default:
    93  			// unsupport operator
    94  			return false
    95  		}
    96  	}
    97  
    98  	return true
    99  }
   100  
   101  // PhaseSelectorsValid can be used to check whether phase selectors is valid.
   102  func PhaseSelectorsValid(fl validator.FieldLevel) bool {
   103  	ph, ok := fl.Field().Interface().([]string)
   104  	if !ok {
   105  		return false
   106  	}
   107  
   108  	for _, phase := range ph {
   109  		if !checkPhase(phase) {
   110  			return false
   111  		}
   112  	}
   113  
   114  	return true
   115  }
   116  
   117  // ValueValid can be used to check whether the mode value is valid.
   118  func ValueValid(fl validator.FieldLevel) bool {
   119  	val := fl.Field().String()
   120  	if val == "" {
   121  		return true
   122  	}
   123  
   124  	f, err := strconv.ParseFloat(val, 64)
   125  	if err != nil {
   126  		return false
   127  	}
   128  
   129  	if f < 0 {
   130  		return false
   131  	}
   132  
   133  	return true
   134  }
   135  
   136  func checkPhase(ph string) bool {
   137  	phases := []corev1.PodPhase{
   138  		corev1.PodRunning,
   139  		corev1.PodFailed,
   140  		corev1.PodPending,
   141  		corev1.PodSucceeded,
   142  		corev1.PodUnknown,
   143  		corev1.PodPending,
   144  	}
   145  
   146  	for _, phase := range phases {
   147  		if string(phase) == ph {
   148  			return true
   149  		}
   150  	}
   151  
   152  	return false
   153  }
   154  
   155  // PodsValid can be used to check whether the pod name is valid.
   156  func PodsValid(fl validator.FieldLevel) bool {
   157  	if fl.Field().IsNil() {
   158  		return true
   159  	}
   160  
   161  	pods, ok := fl.Field().Interface().(map[string][]string)
   162  	if !ok {
   163  		return false
   164  	}
   165  
   166  	for ns, ps := range pods {
   167  		if !checkName(ns) {
   168  			return false
   169  		}
   170  
   171  		for _, p := range ps {
   172  			if !checkName(p) {
   173  				return false
   174  			}
   175  		}
   176  	}
   177  
   178  	return true
   179  }
   180  
   181  // PhysicalMachineValid can be used to check whether the physicalMachine name is valid.
   182  func PhysicalMachineValid(fl validator.FieldLevel) bool {
   183  	if fl.Field().IsNil() {
   184  		return true
   185  	}
   186  
   187  	physicalMachines, ok := fl.Field().Interface().(map[string][]string)
   188  	if !ok {
   189  		return false
   190  	}
   191  
   192  	for ns, ps := range physicalMachines {
   193  		if !checkName(ns) {
   194  			return false
   195  		}
   196  
   197  		for _, p := range ps {
   198  			if !checkName(p) {
   199  				return false
   200  			}
   201  		}
   202  	}
   203  
   204  	return true
   205  }
   206