...

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

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

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