-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
51 lines (44 loc) · 1.59 KB
/
validator.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
package cluster
import (
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
)
type Validator struct {
ClusterLister v3.ClusterLister
}
func (v *Validator) Validator(request *types.APIContext, schema *types.Schema, data map[string]interface{}) error {
var spec v3.ClusterSpec
if err := convert.ToObj(data, &spec); err != nil {
return httperror.WrapAPIError(err, httperror.InvalidBodyContent, "Cluster spec conversion error")
}
if err := v.validateLocalClusterAuthEndpoint(request, &spec); err != nil {
return err
}
return nil
}
func (v *Validator) validateLocalClusterAuthEndpoint(request *types.APIContext, spec *v3.ClusterSpec) error {
if !spec.LocalClusterAuthEndpoint.Enabled {
return nil
}
var isValidCluster bool
if request.ID == "" {
isValidCluster = spec.RancherKubernetesEngineConfig != nil
} else {
cluster, err := v.ClusterLister.Get("", request.ID)
if err != nil {
return err
}
isValidCluster = cluster.Status.Driver == "" ||
cluster.Status.Driver == v3.ClusterDriverRKE ||
cluster.Status.Driver == v3.ClusterDriverImported
}
if !isValidCluster {
return httperror.NewFieldAPIError(httperror.InvalidState, "LocalClusterAuthEndpoint.Enabled", "Can only enable LocalClusterAuthEndpoint with RKE")
}
if spec.LocalClusterAuthEndpoint.CACerts != "" && spec.LocalClusterAuthEndpoint.FQDN == "" {
return httperror.NewFieldAPIError(httperror.MissingRequired, "LocalClusterAuthEndpoint.FQDN", "CACerts defined but FQDN is not defined")
}
return nil
}