-
Notifications
You must be signed in to change notification settings - Fork 274
/
validate.go
49 lines (40 loc) 路 1.35 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package validator
import (
"github.com/pkg/errors"
"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/networkutils"
)
// IPValidator defines the struct for control plane IP validations.
type IPValidator struct {
netClient networkutils.NetClient
}
// IPValidatorOpt is the type for optional IPValidator configurations.
type IPValidatorOpt func(e *IPValidator)
// CustomNetClient passes in a custom net client to the IPValidator.
func CustomNetClient(netClient networkutils.NetClient) IPValidatorOpt {
return func(d *IPValidator) {
d.netClient = netClient
}
}
// NewIPValidator initializes a new IPValidator object.
func NewIPValidator(opts ...IPValidatorOpt) *IPValidator {
v := &IPValidator{
netClient: &networkutils.DefaultNetClient{},
}
for _, opt := range opts {
opt(v)
}
return v
}
// ValidateControlPlaneIPUniqueness checks whether or not the control plane endpoint defined
// in the cluster spec is available.
func (v *IPValidator) ValidateControlPlaneIPUniqueness(cluster *v1alpha1.Cluster) error {
if cluster.ControlPlaneIPCheckDisabled() {
return nil
}
ip := cluster.Spec.ControlPlaneConfiguration.Endpoint.Host
if networkutils.IsIPInUse(v.netClient, ip) {
return errors.Errorf("cluster controlPlaneConfiguration.Endpoint.Host <%s> is already in use, control plane IP must be unique", ip)
}
return nil
}