Skip to content

Commit

Permalink
kev-409 Extend json validation for probes
Browse files Browse the repository at this point in the history
  • Loading branch information
mangas committed Mar 18, 2021
1 parent c9c324f commit c2cc411
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
76 changes: 76 additions & 0 deletions pkg/kev/config/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,82 @@ var ServicesSchema = map[string]interface{}{
LabelWorkloadReplicas,
LabelWorkloadLivenessProbeType,
},
"allOf": []map[string]interface{}{
// Liveness probe
{
"oneOf": []map[string]interface{}{
{
"properties": map[string]interface{}{
LabelWorkloadLivenessProbeType: map[string]interface{}{
"const": "http",
},
},
"required": []string{LabelWorkloadLivenessProbeHTTPPath, LabelWorkloadLivenessProbeHTTPPort},
},
{
"properties": map[string]interface{}{
LabelWorkloadLivenessProbeType: map[string]interface{}{
"const": "tcp",
},
},
"required": []string{LabelWorkloadLivenessProbeTCPPort},
},
{
"properties": map[string]interface{}{
LabelWorkloadLivenessProbeType: map[string]interface{}{
"const": "command",
},
},
"required": []string{LabelWorkloadLivenessProbeCommand},
},
// This acts as a catch all when the required properties are not provided, should come in the end
// otherwise instead of matching that "command" type needs a properties the error says the value is not "none".
{
"properties": map[string]interface{}{
LabelWorkloadLivenessProbeType: map[string]interface{}{
"const": "none",
},
},
},
},
},
// Readiness probe
{
"anyOf": []map[string]interface{}{
{
"properties": map[string]interface{}{
LabelWorkloadReadinessProbeType: map[string]interface{}{
"const": "http",
},
},
"required": []string{LabelWorkloadReadinessProbeHTTPPath, LabelWorkloadReadinessProbeHTTPPort},
},
{
"properties": map[string]interface{}{
LabelWorkloadReadinessProbeType: map[string]interface{}{
"const": "tcp",
},
},
"required": []string{LabelWorkloadReadinessProbeTCPPort},
},
{
"properties": map[string]interface{}{
LabelWorkloadReadinessProbeType: map[string]interface{}{
"const": "command",
},
},
"required": []string{LabelWorkloadReadinessProbeCommand},
},
{
"properties": map[string]interface{}{
LabelWorkloadReadinessProbeType: map[string]interface{}{
"const": "none",
},
},
},
},
},
},
"additionalProperties": false,
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/kev/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ func (sc ServiceConfig) validate() error {
}

if !result.Valid() {
// Prioritise clear error messages.
for _, e := range result.Errors() {
if e.Type() == "required" {
return errors.New(e.Description())
}
}

for _, e := range result.Errors() {
// These errors are very cryptic and hurt usability.
if e.Type() == "number_one_of" || e.Type() == "number_any_of" || e.Type() == "number_all_of" {
continue
}

return errors.New(e.Description())
}

// If we don't find anything useful just go with whatever is available.
return errors.New(result.Errors()[0].Description())
}
return nil
Expand Down
8 changes: 5 additions & 3 deletions pkg/kev/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ var _ = Describe("ServiceConfig", func() {
Expect(err).Should(MatchError(ContainSubstring(config.LabelWorkloadLivenessProbeType)))

err = ServiceConfig{Labels: composego.Labels{
config.LabelWorkloadLivenessProbeType: kubernetes.ProbeTypeCommand.String(),
config.LabelWorkloadLivenessProbeType: kubernetes.ProbeTypeCommand.String(),
config.LabelWorkloadLivenessProbeCommand: "echo i'm a useless probe",
}}.validate()
Expect(err).Should(MatchError(ContainSubstring(config.LabelWorkloadReplicas)))

Expand All @@ -48,8 +49,9 @@ var _ = Describe("ServiceConfig", func() {

It("success if the necessary labels are present", func() {
err := ServiceConfig{Labels: composego.Labels{
config.LabelWorkloadLivenessProbeType: kubernetes.ProbeTypeCommand.String(),
config.LabelWorkloadReplicas: "1",
config.LabelWorkloadLivenessProbeType: kubernetes.ProbeTypeCommand.String(),
config.LabelWorkloadLivenessProbeCommand: "echo i'm a useless probe",
config.LabelWorkloadReplicas: "1",
}}.validate()
Expect(err).NotTo(HaveOccurred())
})
Expand Down

0 comments on commit c2cc411

Please sign in to comment.