-
Notifications
You must be signed in to change notification settings - Fork 275
/
ipvalidator.go
52 lines (45 loc) 路 1.83 KB
/
ipvalidator.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
50
51
52
package clusters
import (
"context"
"github.com/go-logr/logr"
"github.com/pkg/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/controller"
)
// IPUniquenessValidator defines an interface for the methods to validate the control plane IP.
type IPUniquenessValidator interface {
ValidateControlPlaneIPUniqueness(cluster *anywherev1.Cluster) error
}
// IPValidator validates control plane IP.
type IPValidator struct {
ipUniquenessValidator IPUniquenessValidator
client client.Client
}
// NewIPValidator returns a new NewIPValidator.
func NewIPValidator(ipUniquenessValidator IPUniquenessValidator, client client.Client) *IPValidator {
return &IPValidator{
ipUniquenessValidator: ipUniquenessValidator,
client: client,
}
}
// ValidateControlPlaneIP only validates IP on cluster creation.
func (i *IPValidator) ValidateControlPlaneIP(ctx context.Context, log logr.Logger, spec *cluster.Spec) (controller.Result, error) {
capiCluster, err := controller.GetCAPICluster(ctx, i.client, spec.Cluster)
if err != nil {
return controller.Result{}, errors.Wrap(err, "validating control plane IP")
}
if capiCluster != nil {
// If CAPI cluster exists, the control plane IP has already been validated,
// and it's possibly already in use so no need to validate it again
log.Info("CAPI cluster already exists, skipping control plane IP validation")
return controller.Result{}, nil
}
if err := i.ipUniquenessValidator.ValidateControlPlaneIPUniqueness(spec.Cluster); err != nil {
spec.Cluster.SetFailure(anywherev1.UnavailableControlPlaneIPReason, err.Error())
log.Error(err, "Unavailable control plane IP")
return controller.ResultWithReturn(), nil
}
return controller.Result{}, nil
}