...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/physical_machine_webhook.go

Documentation: github.com/chaos-mesh/chaos-mesh/api/v1alpha1

     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 v1alpha1
    17  
    18  import (
    19  	"fmt"
    20  	"net/url"
    21  	"reflect"
    22  	"strings"
    23  
    24  	"k8s.io/apimachinery/pkg/util/validation/field"
    25  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    26  )
    27  
    28  var physicalMachineLog = logf.Log.WithName("physical-machine-resource")
    29  
    30  func (in *PhysicalMachineSpec) Default(root interface{}, field *reflect.StructField) {
    31  	if in == nil {
    32  		return
    33  	}
    34  
    35  	physicalMachineLog.Info("default", "address", in.Address)
    36  	// add http prefix for address
    37  	if len(in.Address) > 0 && !strings.HasPrefix(in.Address, "http") {
    38  		in.Address = fmt.Sprintf("http://%s", in.Address)
    39  	}
    40  }
    41  
    42  func (in *PhysicalMachineSpec) Validate(root interface{}, path *field.Path) field.ErrorList {
    43  	allErrs := field.ErrorList{}
    44  
    45  	// make sure address is not empty
    46  	if len(in.Address) == 0 {
    47  		allErrs = append(allErrs,
    48  			field.Invalid(path.Child("address"), in.Address, "the address is required"))
    49  	}
    50  
    51  	if _, err := url.ParseRequestURI(in.Address); err != nil {
    52  		allErrs = append(allErrs,
    53  			field.Invalid(path.Child("address"), in.Address, fmt.Sprintf("the address is invalid, %s", err)))
    54  	}
    55  	return allErrs
    56  }
    57