...
1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
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