/
cluster_validation.go
57 lines (54 loc) · 1.84 KB
/
cluster_validation.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
53
54
55
56
57
package v1alpha1
import (
"errors"
"fmt"
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/util/validation"
"strings"
)
func ValidateV1alphaCluster(cluster *KubeCluster) error {
if errs := apimachineryvalidation.NameIsDNS1035Label(cluster.ObjectMeta.Name, false); errs != nil {
return fmt.Errorf("TFCluster name is invalid: %v", errs)
}
if err := validateV1alphaClusterSpecs(cluster.Spec); err != nil {
return err
}
return nil
}
func validateV1alphaClusterSpecs(spec ClusterSpec) error {
if len(spec.ClusterType) == 0 {
return fmt.Errorf("KubeCluster is not valid: cluster type expected")
}
if len(spec.ClusterReplicaSpec) == 0 {
return fmt.Errorf("KubeCluster is not valid")
}
defaultContainerName := ClusterDefaultContainerName
if (len(spec.MainContainer)) != 0 {
defaultContainerName = spec.MainContainer
}
for rType, value := range spec.ClusterReplicaSpec {
if errs := validation.IsDNS1035Label(strings.ToLower(string(rType))); len(errs) != 0 {
return errors.New(strings.Join(errs, ";"))
}
if value == nil || len(value.Template.Spec.Containers) == 0 {
return fmt.Errorf("KubeCluster is not valid: containers definition expected in %v", rType)
}
// Make sure the image is defined in the container.
numNamedkubenode := 0
for _, container := range value.Template.Spec.Containers {
if container.Image == "" {
msg := fmt.Sprintf("KubeCluster is not valid: Image is undefined in the container of %v", rType)
return fmt.Errorf(msg)
}
if container.Name == defaultContainerName {
numNamedkubenode++
}
}
// Make sure there has at least one container named "tensorflow".
if numNamedkubenode == 0 {
msg := fmt.Sprintf("KubeClusterSpec is not valid: There is no container named %s in %v", ClusterDefaultContainerName, rType)
return fmt.Errorf(msg)
}
}
return nil
}