-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #215 from G-Research/submit-validation
Improving armadactl submit validation and adding --dry-run
- Loading branch information
Showing
12 changed files
with
277 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"k8s.io/apimachinery/pkg/util/yaml" | ||
) | ||
|
||
func BindJsonOrYaml(filePath string, obj interface{}) { | ||
func BindJsonOrYaml(filePath string, obj interface{}) error { | ||
reader, err := os.Open(filePath) | ||
if err != nil { | ||
log.Fatalf("Failed opening file %s due to %s", filePath, err) | ||
return fmt.Errorf("Failed opening file %s due to %s", filePath, err) | ||
} | ||
err = yaml.NewYAMLOrJSONDecoder(reader, 128).Decode(obj) | ||
if err != nil { | ||
log.Fatalf("Unmarshal: %v", err) | ||
return fmt.Errorf("Failed to parse file %s because: %v", filePath, err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package validation | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/instrumenta/kubeval/kubeval" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/G-Research/armada/internal/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 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 | ||
return kubeval.Validate(output, config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package validation | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValidateSubmitFile_WhenValidFile_ReturnValid(t *testing.T) { | ||
path := filepath.Join("testdata", "valid.yaml") | ||
|
||
ok, err := ValidateSubmitFile(path) | ||
|
||
assert.Nil(t, err) | ||
assert.True(t, ok) | ||
} | ||
|
||
func TestValidateSubmitFile_WhenEmptyFile_ReturnInvalid(t *testing.T) { | ||
path := filepath.Join("testdata", "empty.json") | ||
|
||
ok, err := ValidateSubmitFile(path) | ||
|
||
assert.NotNil(t, err) | ||
assert.False(t, ok) | ||
} | ||
|
||
func TestValidateSubmitFile_WhenFileFormatting_ReturnInvalid(t *testing.T) { | ||
path := filepath.Join("testdata", "invalid_format.yaml") | ||
|
||
ok, err := ValidateSubmitFile(path) | ||
|
||
assert.NotNil(t, err) | ||
assert.False(t, ok) | ||
} | ||
|
||
func TestValidateSubmitFile_WhenUnknownField_ReturnInvalid(t *testing.T) { | ||
path := filepath.Join("testdata", "invalid_field.yaml") | ||
|
||
ok, err := ValidateSubmitFile(path) | ||
|
||
assert.NotNil(t, err) | ||
assert.False(t, ok) | ||
} | ||
|
||
func TestValidateSubmitFile_WhenMultiJobUnknownField_ReturnInvalid(t *testing.T) { | ||
path := filepath.Join("testdata", "invalid_field_multi_job.yaml") | ||
|
||
ok, err := ValidateSubmitFile(path) | ||
|
||
assert.NotNil(t, err) | ||
assert.False(t, ok) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
Oops, something went wrong.