/
submit_file.go
76 lines (61 loc) · 1.72 KB
/
submit_file.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package validation
import (
"encoding/json"
"errors"
"fmt"
"github.com/instrumenta/kubeval/kubeval"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/G-Research/armada/pkg/client/util"
)
type rawJobSubmitFile struct {
Jobs []*rawJobRequest
}
type rawJobRequest struct {
PodSpec *json.RawMessage `json:"PodSpec,omitempty"`
}
type rawKubernetesType struct {
metav1.TypeMeta `json:",inline"`
Spec *json.RawMessage `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
}
func rawPod(spec *json.RawMessage) *rawKubernetesType {
return &rawKubernetesType{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
Spec: spec,
}
}
func ValidateSubmitFile(filePath string) (bool, error) {
submitFile := &rawJobSubmitFile{}
err := util.BindJsonOrYaml(filePath, submitFile)
if err != nil {
return false, err
}
if len(submitFile.Jobs) <= 0 {
return false, errors.New("Warning: You have provided no jobs to submit.")
}
for i, job := range submitFile.Jobs {
rawPod := rawPod(job.PodSpec)
result, err := validate(rawPod)
if !result[0].ValidatedAgainstSchema {
log.Warn("Warning: Unable to validate jobs. You must be offline as pod schemas cannot be retrieved to perform validation")
break
}
if err != nil {
return false, err
}
if len(result[0].Errors) > 0 {
return false, fmt.Errorf("Validation error in job[%d]: %s", i, result[0].Errors[0].Description())
}
}
return true, nil
}
func validate(value *rawKubernetesType) ([]kubeval.ValidationResult, error) {
output, _ := json.Marshal(value)
config := kubeval.NewDefaultConfig()
config.Strict = true
config.IgnoreMissingSchemas = true
return kubeval.Validate(output, config)
}